Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Inject IIFE when variables shadow binding in rest param (#14736)
- Loading branch information
1 parent
d307173
commit f2347e4
Showing
9 changed files
with
231 additions
and
94 deletions.
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
89 changes: 89 additions & 0 deletions
89
packages/babel-plugin-transform-parameters/src/shadow-utils.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,89 @@ | ||
import { types as t } from "@babel/core"; | ||
import type { NodePath, Scope, Visitor } from "@babel/traverse"; | ||
|
||
type State = { | ||
needsOuterBinding: boolean; | ||
scope: Scope; | ||
}; | ||
|
||
export const iifeVisitor: Visitor<State> = { | ||
"ReferencedIdentifier|BindingIdentifier"( | ||
path: NodePath<t.Identifier>, | ||
state, | ||
) { | ||
const { scope, node } = path; | ||
const { name } = node; | ||
|
||
if ( | ||
name === "eval" || | ||
(scope.getBinding(name) === state.scope.parent.getBinding(name) && | ||
state.scope.hasOwnBinding(name)) | ||
) { | ||
state.needsOuterBinding = true; | ||
path.stop(); | ||
} | ||
}, | ||
// type annotations don't use or introduce "real" bindings | ||
"TypeAnnotation|TSTypeAnnotation|TypeParameterDeclaration|TSTypeParameterDeclaration": | ||
(path: NodePath) => path.skip(), | ||
}; | ||
|
||
export function collectShadowedParamsNames( | ||
param: NodePath<t.Function["params"][number]>, | ||
functionScope: Scope, | ||
shadowedParams: Set<string>, | ||
) { | ||
for (const name of Object.keys(param.getBindingIdentifiers())) { | ||
const constantViolations = functionScope.bindings[name]?.constantViolations; | ||
if (constantViolations) { | ||
for (const redeclarator of constantViolations) { | ||
const node = redeclarator.node; | ||
// If a constant violation is a var or a function declaration, | ||
// we first check to see if it's a var without an init. | ||
// If so, we remove that declarator. | ||
// Otherwise, we have to wrap it in an IIFE. | ||
switch (node.type) { | ||
case "VariableDeclarator": { | ||
if (node.init === null) { | ||
const declaration = redeclarator.parentPath; | ||
// The following uninitialized var declarators should not be removed | ||
// for (var x in {}) | ||
// for (var x;;) | ||
if ( | ||
!declaration.parentPath.isFor() || | ||
declaration.parentPath.get("body") === declaration | ||
) { | ||
redeclarator.remove(); | ||
break; | ||
} | ||
} | ||
|
||
shadowedParams.add(name); | ||
break; | ||
} | ||
case "FunctionDeclaration": | ||
shadowedParams.add(name); | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
export function buildScopeIIFE( | ||
shadowedParams: Set<string>, | ||
body: t.BlockStatement, | ||
) { | ||
const args = []; | ||
const params = []; | ||
|
||
for (const name of shadowedParams) { | ||
// We create them twice; the other option is to use t.cloneNode | ||
args.push(t.identifier(name)); | ||
params.push(t.identifier(name)); | ||
} | ||
|
||
return t.returnStatement( | ||
t.callExpression(t.arrowFunctionExpression(params, body), args), | ||
); | ||
} |
9 changes: 9 additions & 0 deletions
9
...form-parameters/test/fixtures/parameters/var-same-name-as-nested-in-rest-param-2/input.js
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,9 @@ | ||
function f(...{ length: x = 0, y = x }) { | ||
var x; | ||
return x; | ||
} | ||
|
||
function g(...{ length: x = 0, y = x }) { | ||
var x = 1; | ||
return x; | ||
} |
26 changes: 26 additions & 0 deletions
26
...orm-parameters/test/fixtures/parameters/var-same-name-as-nested-in-rest-param-2/output.js
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,26 @@ | ||
function f() { | ||
for (var _len = arguments.length, _ref = new Array(_len), _key = 0; _key < _len; _key++) { | ||
_ref[_key] = arguments[_key]; | ||
} | ||
|
||
var _ref$length = _ref.length, | ||
x = _ref$length === void 0 ? 0 : _ref$length, | ||
_ref$y = _ref.y, | ||
y = _ref$y === void 0 ? x : _ref$y; | ||
return x; | ||
} | ||
|
||
function g() { | ||
for (var _len2 = arguments.length, _ref2 = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | ||
_ref2[_key2] = arguments[_key2]; | ||
} | ||
|
||
var _ref2$length = _ref2.length, | ||
x = _ref2$length === void 0 ? 0 : _ref2$length, | ||
_ref2$y = _ref2.y, | ||
y = _ref2$y === void 0 ? x : _ref2$y; | ||
return function (x) { | ||
var x = 1; | ||
return x; | ||
}(x); | ||
} |
9 changes: 9 additions & 0 deletions
9
...nsform-parameters/test/fixtures/parameters/var-same-name-as-nested-in-rest-param/input.js
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,9 @@ | ||
function f(...[x, y = x]) { | ||
var x; | ||
return x; | ||
} | ||
|
||
function g(...[x, y = x]) { | ||
var x = 1; | ||
return x; | ||
} |
24 changes: 24 additions & 0 deletions
24
...sform-parameters/test/fixtures/parameters/var-same-name-as-nested-in-rest-param/output.js
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,24 @@ | ||
function f() { | ||
for (var _len = arguments.length, _ref = new Array(_len), _key = 0; _key < _len; _key++) { | ||
_ref[_key] = arguments[_key]; | ||
} | ||
|
||
var x = _ref[0], | ||
_ref$ = _ref[1], | ||
y = _ref$ === void 0 ? x : _ref$; | ||
return x; | ||
} | ||
|
||
function g() { | ||
for (var _len2 = arguments.length, _ref2 = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | ||
_ref2[_key2] = arguments[_key2]; | ||
} | ||
|
||
var x = _ref2[0], | ||
_ref2$ = _ref2[1], | ||
y = _ref2$ === void 0 ? x : _ref2$; | ||
return function (x) { | ||
var x = 1; | ||
return x; | ||
}(x); | ||
} |
9 changes: 9 additions & 0 deletions
9
...plugin-transform-parameters/test/fixtures/parameters/var-same-name-as-rest-param/input.js
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,9 @@ | ||
function f(...x) { | ||
var x; | ||
return x; | ||
} | ||
|
||
function g(...x) { | ||
var x = 1; | ||
return x; | ||
} |
17 changes: 17 additions & 0 deletions
17
...lugin-transform-parameters/test/fixtures/parameters/var-same-name-as-rest-param/output.js
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,17 @@ | ||
function f() { | ||
for (var _len = arguments.length, x = new Array(_len), _key = 0; _key < _len; _key++) { | ||
x[_key] = arguments[_key]; | ||
} | ||
|
||
var x; | ||
return x; | ||
} | ||
|
||
function g() { | ||
for (var _len2 = arguments.length, x = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | ||
x[_key2] = arguments[_key2]; | ||
} | ||
|
||
var x = 1; | ||
return x; | ||
} |