Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Do not remove let bindings even they are wrapped in closure (#10343)
* fix: should not remove let binding even it is wrapped in closure Fixes #10339 * fix: remove bindings defined in blockScope when wrapped in closure * Move test assertions to the top level to ensure that they run
- Loading branch information
1 parent
b0acfb2
commit 563874c
Showing
5 changed files
with
76 additions
and
5 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
38 changes: 38 additions & 0 deletions
38
packages/babel-plugin-transform-block-scoping/test/fixtures/general/issue-10339/exec.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,38 @@ | ||
const code = multiline([ | ||
"for (const {foo, ...bar} of { bar: [] }) {", | ||
"() => foo;", | ||
"const [qux] = bar;", | ||
"try {} catch (e) {", | ||
"let quux = qux;", | ||
"}", | ||
"}" | ||
]); | ||
|
||
let programPath; | ||
let forOfPath; | ||
let functionPath; | ||
|
||
transform(code, { | ||
configFile: false, | ||
plugins: [ | ||
"../../../../lib", | ||
{ | ||
post({ path }) { | ||
programPath = path; | ||
path.traverse({ | ||
ForOfStatement(path) { forOfPath = path }, | ||
FunctionExpression(path) { functionPath = path } | ||
}); | ||
} | ||
} | ||
] | ||
}); | ||
|
||
expect(Object.keys(programPath.scope.bindings)).toEqual(["foo", "bar"]); | ||
|
||
// for declarations should be transformed to for bindings | ||
expect(forOfPath.scope.bindings).toEqual({}); | ||
// The body should be wrapped into closure | ||
expect(forOfPath.get("body").scope.bindings).toEqual({}); | ||
|
||
expect(Object.keys(functionPath.scope.bindings)).toEqual(["foo", "bar", "qux", "quux"]); |
7 changes: 7 additions & 0 deletions
7
packages/babel-plugin-transform-block-scoping/test/fixtures/general/issue-10339/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,7 @@ | ||
for (const {foo, ...bar} of {}) { | ||
() => foo; | ||
const [qux] = bar; | ||
try {} catch (e) { | ||
const quux = qux; | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/babel-plugin-transform-block-scoping/test/fixtures/general/issue-10339/options.json
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,3 @@ | ||
{ | ||
"plugins": ["transform-block-scoping", ["proposal-object-rest-spread", { "loose": true }]] | ||
} |
20 changes: 20 additions & 0 deletions
20
packages/babel-plugin-transform-block-scoping/test/fixtures/general/issue-10339/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,20 @@ | ||
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } | ||
|
||
var _loop = function (foo, bar) { | ||
() => foo; | ||
|
||
var [qux] = bar; | ||
|
||
try {} catch (e) { | ||
var quux = qux; | ||
} | ||
}; | ||
|
||
for (var _ref of {}) { | ||
var { | ||
foo | ||
} = _ref, | ||
bar = _objectWithoutPropertiesLoose(_ref, ["foo"]); | ||
|
||
_loop(foo, bar); | ||
} |