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
Inline hoisted, post-return declarations properly #391
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
81cd100
Inline hoisted, post-return declarations properly. Fixes #192
kangax 3404d3d
Add another test
kangax 5b735d1
Change algorithm to check references
kangax 9d49be4
undefined --> void 0
kangax eddd59f
Fix comment typo
kangax 80dd806
Deopt if reference is in different scope
kangax ffefb6d
Fix travis
kangax File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
|
@@ -3,6 +3,24 @@ | |
const some = require("lodash.some"); | ||
const { markEvalScopes, isMarked: isEvalScopesMarked, hasEval } = require("babel-helper-mark-eval-scopes"); | ||
|
||
function prevSiblings(path) { | ||
const parentPath = path.parentPath; | ||
const siblings = []; | ||
|
||
let key = parentPath.key; | ||
|
||
while ((path = parentPath.getSibling(--key)).type) { | ||
siblings.push(path); | ||
} | ||
return siblings; | ||
} | ||
|
||
function forEachAncestor(path, callback) { | ||
while (path = path.parentPath) { | ||
callback(path); | ||
} | ||
} | ||
|
||
module.exports = ({ types: t, traverse }) => { | ||
const removeOrVoid = require("babel-helper-remove-or-void")(t); | ||
const shouldRevisit = Symbol("shouldRevisit"); | ||
|
@@ -238,31 +256,57 @@ module.exports = ({ types: t, traverse }) => { | |
} | ||
|
||
if (binding.references === 1 && binding.kind !== "param" && binding.kind !== "module" && binding.constant) { | ||
|
||
let replacement = binding.path.node; | ||
let replacementPath = binding.path; | ||
let isReferencedBefore = false; | ||
|
||
if (binding.referencePaths.length > 1) { | ||
throw new Error("Expected only one reference"); | ||
} | ||
const refPath = binding.referencePaths[0]; | ||
|
||
if (t.isVariableDeclarator(replacement)) { | ||
replacement = replacement.init; | ||
|
||
const _prevSiblings = prevSiblings(replacementPath); | ||
|
||
// traverse ancestors of a reference checking if it's before declaration | ||
forEachAncestor(refPath, (ancestor) => { | ||
if (_prevSiblings.indexOf(ancestor) > -1) { | ||
isReferencedBefore = true; | ||
} | ||
}); | ||
|
||
// deopt if reference is in different scope than binding | ||
// since we don't know if it's sync or async execition | ||
// (i.e. whether value has been assigned to a reference or not) | ||
if (isReferencedBefore && refPath.scope !== binding.scope) { | ||
continue; | ||
} | ||
|
||
// simulate hoisting by replacing value | ||
// with undefined if declaration is after reference | ||
replacement = isReferencedBefore | ||
? t.unaryExpression("void", t.numericLiteral(0), true) | ||
: replacement.init; | ||
|
||
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. a few lines below this, |
||
// Bail out for ArrayPattern and ObjectPattern | ||
// TODO: maybe a more intelligent approach instead of simply bailing out | ||
if (!replacementPath.get("id").isIdentifier()) { | ||
continue; | ||
} | ||
replacementPath = replacementPath.get("init"); | ||
} | ||
|
||
if (!replacement) { | ||
continue; | ||
} | ||
|
||
if (!scope.isPure(replacement, true)) { | ||
if (!scope.isPure(replacement, true) && !isReferencedBefore) { | ||
continue; | ||
} | ||
|
||
if (binding.referencePaths.length > 1) { | ||
throw new Error("Expected only one reference"); | ||
} | ||
|
||
let bail = false; | ||
const refPath = binding.referencePaths[0]; | ||
|
||
if (replacementPath.isIdentifier()) { | ||
bail = refPath.scope.getBinding(replacement.name) !== scope.getBinding(replacement.name); | ||
|
@@ -358,7 +402,7 @@ module.exports = ({ types: t, traverse }) => { | |
return; | ||
} | ||
|
||
// Not last in it's block? (See BlockStatement visitor) | ||
// Not last in its block? (See BlockStatement visitor) | ||
if (path.container.length - 1 !== path.key && | ||
!canExistAfterCompletion(path.getSibling(path.key + 1)) && | ||
path.parentPath.isBlockStatement() | ||
|
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.
This is not ideal, of course. But we could revisit 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.
It's actually correct. This is not removed because it's a global.
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.
Created #392