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
Rewrite transform-block-scoping
plugin
#15200
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
6fe04bc
Add test for `const` with `tdz`
nicolo-ribaudo 0dd69d8
Rewrite block-scoping plugin
nicolo-ribaudo 774ed52
Update block-scoping fixtures
nicolo-ribaudo 430a493
Update other fixtures
nicolo-ribaudo f6660ad
Fix `let` shadowing variable read in a sibling scope
nicolo-ribaudo c4d4053
Update fixtures
nicolo-ribaudo 1689005
Update Babel 8 fixtures
nicolo-ribaudo 74100f6
Add minNodeVersion
nicolo-ribaudo 54fb8bb
Fix block scoped vars shadowing globals
nicolo-ribaudo e61b1cf
Review by JLHwung
nicolo-ribaudo cfb165f
minNodeVersion to class fields test
nicolo-ribaudo 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
127 changes: 127 additions & 0 deletions
127
packages/babel-plugin-transform-block-scoping/src/annex-B_3_3.ts
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 |
---|---|---|
@@ -0,0 +1,127 @@ | ||
import { types as t } from "@babel/core"; | ||
import type { NodePath, Visitor, Scope } from "@babel/traverse"; | ||
|
||
// Whenever a function declaration in a nested block scope | ||
// doesn't conflict with a block-scoped binding from an outer | ||
// scope, we transform it to a variable declaration. | ||
// | ||
// This implements the Annex B.3.3 behavior. | ||
// | ||
// TODO(Babel 8): Figure out how this should interact with | ||
// the transform-block-scoped functions plugin (it feels | ||
// wrong to handle this transform here), and what we want | ||
// to do with Anned B behavior in general. | ||
|
||
// To avoid confusing block-scoped variables transformed to | ||
// var with original vars, this transformation happens in two | ||
// different places: | ||
// 1. For functions that "conflict" with var-variables, in | ||
// the VariableDeclaration visitor. | ||
// 2. For functions that don't conflict with any variable, | ||
// in the FunctionDeclaration visitor. | ||
|
||
export const annexB33FunctionsVisitor: Visitor = { | ||
VariableDeclaration(path) { | ||
if (isStrict(path)) return; | ||
if (path.node.kind !== "var") return; | ||
|
||
const varScope = | ||
path.scope.getFunctionParent() || path.scope.getProgramParent(); | ||
// eslint-disable-next-line @typescript-eslint/no-use-before-define | ||
varScope.path.traverse(functionsToVarVisitor, { | ||
names: Object.keys(path.getBindingIdentifiers()), | ||
}); | ||
}, | ||
|
||
// NOTE: These two visitors target the same nodes as the | ||
// block-scoped-functions plugin | ||
|
||
BlockStatement(path) { | ||
if (isStrict(path)) return; | ||
if (t.isFunction(path.parent, { body: path.node })) return; | ||
transformStatementList(path.get("body")); | ||
}, | ||
|
||
SwitchCase(path) { | ||
if (isStrict(path)) return; | ||
transformStatementList(path.get("consequent")); | ||
}, | ||
}; | ||
|
||
function transformStatementList(paths: NodePath<t.Statement>[]) { | ||
outer: for (const path of paths) { | ||
if (!path.isFunctionDeclaration()) continue; | ||
// Annex B.3.3 only applies to plain functions. | ||
if (path.node.async || path.node.generator) return; | ||
|
||
const { scope } = path.parentPath; | ||
if (isVarScope(scope)) return; | ||
|
||
const { name } = path.node.id; | ||
let currScope = scope; | ||
do { | ||
if (currScope.parent.hasOwnBinding(name)) continue outer; | ||
currScope = currScope.parent; | ||
} while (!isVarScope(currScope)); | ||
|
||
maybeTransformBlockScopedFunction(path); | ||
} | ||
} | ||
|
||
function maybeTransformBlockScopedFunction( | ||
path: NodePath<t.FunctionDeclaration>, | ||
) { | ||
const { | ||
node, | ||
parentPath: { scope }, | ||
} = path; | ||
|
||
const { id } = node; | ||
scope.removeOwnBinding(id.name); | ||
node.id = null; | ||
|
||
const varNode = t.variableDeclaration("var", [ | ||
t.variableDeclarator(id, t.toExpression(node)), | ||
]); | ||
// @ts-expect-error undocumented property | ||
varNode._blockHoist = 2; | ||
|
||
const [varPath] = path.replaceWith(varNode); | ||
scope.registerDeclaration(varPath); | ||
} | ||
|
||
const functionsToVarVisitor: Visitor<{ names: string[] }> = { | ||
Scope(path, { names }) { | ||
for (const name of names) { | ||
const binding = path.scope.getOwnBinding(name); | ||
if (binding && binding.kind === "hoisted") { | ||
maybeTransformBlockScopedFunction( | ||
binding.path as NodePath<t.FunctionDeclaration>, | ||
); | ||
} | ||
} | ||
}, | ||
"Expression|Declaration"(path) { | ||
path.skip(); | ||
}, | ||
}; | ||
|
||
export function isVarScope(scope: Scope) { | ||
return scope.path.isFunctionParent() || scope.path.isProgram(); | ||
} | ||
|
||
function isStrict(path: NodePath) { | ||
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. Should |
||
return !!path.find(({ node }) => { | ||
if (t.isProgram(node)) { | ||
JLHwung marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (node.sourceType === "module") return true; | ||
} else if (t.isClass(node)) { | ||
return true; | ||
} else if (!t.isBlockStatement(node)) { | ||
return false; | ||
} | ||
|
||
return node.directives?.some( | ||
directive => directive.value.value === "use strict", | ||
); | ||
}); | ||
} |
Oops, something went wrong.
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.
We now only insert the
{...}
if the block-scoping plugins needs to inject new nodes, so there are a lot of changes like this one where we don't inject the block.