-
Notifications
You must be signed in to change notification settings - Fork 309
Destructuring requires #298
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,144 +9,82 @@ | |
|
|
||
| /** | ||
| * This transform inlines top-level require(...) aliases with to enable lazy | ||
| * loading of dependencies. | ||
| * loading of dependencies. It is able to inline both single references and | ||
| * child property references. | ||
| * | ||
| * Continuing with the example above, this replaces all references to `Foo` in | ||
| * the module to `require('ModuleFoo')`. | ||
| * For instance: | ||
| * var Foo = require('foo'); | ||
| * f(Foo); | ||
| * | ||
| * Will be transformed into: | ||
| * f(require('foo')); | ||
| * | ||
| * When the assigment expression has a property access, it will be inlined too, | ||
| * keeping the property. For instance: | ||
| * var Bar = require('foo').bar; | ||
| * g(Bar); | ||
| * | ||
| * Will be transformed into: | ||
| * g(require('foo').bar); | ||
| */ | ||
| module.exports = function fbjsInlineRequiresTransform(babel) { | ||
| var t = babel.types; | ||
|
|
||
| function buildRequireCall(name, inlineRequiredDependencyMap) { | ||
| var call = t.callExpression( | ||
| t.identifier('require'), | ||
| [t.stringLiteral(inlineRequiredDependencyMap.get(name))] | ||
| ); | ||
| call.new = true; | ||
| return call; | ||
| } | ||
|
|
||
| function inlineRequire(path, inlineRequiredDependencyMap, identifierToPathsMap) { | ||
| var node = path.node; | ||
|
|
||
| if (path.isReferenced()) { | ||
| path.replaceWith(buildRequireCall(node.name, inlineRequiredDependencyMap)); | ||
| var paths = identifierToPathsMap.get(node.name); | ||
| if (paths) { | ||
| paths.delete(path); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| module.exports = function fbjsInlineRequiresTransform() { | ||
| return { | ||
| visitor: { | ||
| Program(_, state) { | ||
| /** | ||
| * Map of require(...) aliases to module names. | ||
| * | ||
| * `Foo` is an alias for `require('ModuleFoo')` in the following example: | ||
| * var Foo = require('ModuleFoo'); | ||
| */ | ||
| state.inlineRequiredDependencyMap = new Map(); | ||
|
|
||
| /** | ||
| * Map of variable names that have not yet been inlined. | ||
| * We track them in case we later remove their require()s, | ||
| * In which case we have to come back and update them. | ||
| */ | ||
| state.identifierToPathsMap = new Map(); | ||
| }, | ||
| CallExpression: function(path) { | ||
| var declaratorPath = requireAlias(path) || requireMemberAlias(path); | ||
| var declarator = declaratorPath && declaratorPath.node; | ||
|
|
||
| /** | ||
| * Collect top-level require(...) aliases. | ||
| */ | ||
| CallExpression: function(path, state) { | ||
| var node = path.node; | ||
| if (declarator) { | ||
| declaratorPath.scope.crawl(); | ||
|
|
||
| if (isTopLevelRequireAlias(path)) { | ||
| var varName = path.parent.id.name; | ||
| var moduleName = node.arguments[0].value; | ||
| var inlineRequiredDependencyMap = state.inlineRequiredDependencyMap; | ||
| var identifierToPathsMap = state.identifierToPathsMap; | ||
| var init = declarator.init; | ||
| var name = declarator.id && declarator.id.name; | ||
| var binding = declaratorPath.scope.getBinding(name); | ||
| var constantViolations = binding.constantViolations; | ||
|
|
||
| inlineRequiredDependencyMap.set(varName, moduleName); | ||
| if (constantViolations.length) { | ||
| var line = constantViolations[0].node.loc.start.line; | ||
|
|
||
| // If we removed require() statements for variables we've already seen, | ||
| // We need to do a second pass on this program to replace them with require(). | ||
| var maybePaths = identifierToPathsMap.get(varName); | ||
| if (maybePaths) { | ||
| maybePaths.forEach(path => | ||
| inlineRequire(path, inlineRequiredDependencyMap, identifierToPathsMap)); | ||
| maybePaths.delete(varName); | ||
| throw new ReferenceError( | ||
| 'Cannot re-assign a require; name: ' + name + ', line: ' + line | ||
| ); | ||
| } | ||
|
|
||
| // Remove the declaration. | ||
| path.parentPath.parentPath.remove(); | ||
| // And the associated binding in the scope. | ||
| path.scope.removeBinding(varName); | ||
| binding.referencePaths.forEach(ref => ref.replaceWith(init)); | ||
| declaratorPath.remove(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. … and not here. |
||
| } | ||
| }, | ||
|
|
||
| /** | ||
| * Inline require(...) aliases. | ||
| */ | ||
| Identifier: function(path, state) { | ||
| var node = path.node; | ||
| var parent = path.parent; | ||
| var scope = path.scope; | ||
| var identifierToPathsMap = state.identifierToPathsMap; | ||
|
|
||
| if (!shouldInlineRequire(node, scope, state.inlineRequiredDependencyMap)) { | ||
| // Monitor this name in case we later remove its require(). | ||
| // This won't happen often but if it does we need to come back and update here. | ||
| var paths = identifierToPathsMap.get(node.name); | ||
| if (paths) { | ||
| paths.add(path); | ||
| } else { | ||
| identifierToPathsMap.set(node.name, new Set([path])); | ||
| } | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if ( | ||
| parent.type === 'AssignmentExpression' && | ||
| path.isBindingIdentifier() && | ||
| !scope.bindingIdentifierEquals(node.name, node) | ||
| ) { | ||
| throw new Error( | ||
| 'Cannot assign to a require(...) alias, ' + node.name + | ||
| '. Line: ' + node.loc.start.line + '.' | ||
| ); | ||
| } | ||
|
|
||
| inlineRequire(path, state.inlineRequiredDependencyMap, identifierToPathsMap); | ||
| }, | ||
| }, | ||
| }; | ||
| }; | ||
|
|
||
| function isTopLevelRequireAlias(path) { | ||
| return ( | ||
| function requireAlias(path) { | ||
| const isValid = ( | ||
| isRequireCall(path.node) && | ||
| path.parent.type === 'VariableDeclarator' && | ||
| path.parent.id.type === 'Identifier' && | ||
| path.parentPath.parent.type === 'VariableDeclaration' && | ||
| path.parentPath.parent.declarations.length === 1 && | ||
| path.parentPath.parentPath.parent.type === 'Program' | ||
| ); | ||
|
|
||
| return isValid ? path.parentPath : null; | ||
| } | ||
|
|
||
| function shouldInlineRequire(node, scope, inlineRequiredDependencyMap) { | ||
| return ( | ||
| inlineRequiredDependencyMap.has(node.name) && | ||
| !scope.hasBinding(node.name, true /* noGlobals */) | ||
| function requireMemberAlias(path) { | ||
| const isValid = ( | ||
| isRequireCall(path.node) && | ||
| path.parent.type === 'MemberExpression' && | ||
| path.parentPath.parent.type === 'VariableDeclarator' && | ||
| path.parentPath.parent.id.type === 'Identifier' && | ||
| path.parentPath.parentPath.parent.type === 'VariableDeclaration' && | ||
| path.parentPath.parentPath.parentPath.parent.type === 'Program' | ||
| ); | ||
|
|
||
| return isValid ? path.parentPath.parentPath : null; | ||
| } | ||
|
|
||
| function isRequireCall(node) { | ||
| return ( | ||
| !node.new && | ||
| node.type === 'CallExpression' && | ||
| node.callee.type === 'Identifier' && | ||
| node.callee.name === 'require' && | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The only thing that I don’t understand is why you have to crawl here, and not …
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to crawl because another plugin might have changed the scope already; for instance, the
import/exportBabel plugin. We need to have accurate variable binding, counting and referencing for the plugin to work.