-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: improve variable declarator removal (#16587)
* fix: fixed issue16583 + test * modified getBinding to getOwnBinding (same scope) * reverted babel-plugin-proposal-do-expressions back * refactor: moved fix from scope/index.ts to path/removal.ts * add fail test * fix * review * Update packages/babel-traverse/test/fixtures/scope/remove-nested-let-path/plugin.js Co-authored-by: Nicolò Ribaudo <hello@nicr.dev> * review * Update packages/babel-traverse/src/path/lib/removal-hooks.ts --------- Co-authored-by: Babel Bot <30521560+liuxingbaoyu@users.noreply.github.com> Co-authored-by: Nicolò Ribaudo <hello@nicr.dev>
- Loading branch information
1 parent
30b4473
commit 801d3cb
Showing
9 changed files
with
49 additions
and
3 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
4 changes: 4 additions & 0 deletions
4
packages/babel-traverse/test/fixtures/scope/remove-nested-let-path/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,4 @@ | ||
{ | ||
let a = 33; | ||
} | ||
let a = 42; |
3 changes: 3 additions & 0 deletions
3
packages/babel-traverse/test/fixtures/scope/remove-nested-let-path/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": ["./plugin"] | ||
} |
4 changes: 4 additions & 0 deletions
4
packages/babel-traverse/test/fixtures/scope/remove-nested-let-path/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,4 @@ | ||
{} | ||
let a = 42; | ||
/*// a is defined when visiting a=33? true*/ | ||
/*// a is defined when visiting a=42? true*/ |
17 changes: 17 additions & 0 deletions
17
packages/babel-traverse/test/fixtures/scope/remove-nested-let-path/plugin.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 @@ | ||
module.exports = function ({ types }) { | ||
return { | ||
visitor: { | ||
VariableDeclarator(path) { | ||
const { value } = path.node.init; | ||
|
||
if (value === 33) path.remove(); | ||
|
||
types.addComment( | ||
path.scope.getProgramParent().path.node, | ||
"trailing", | ||
`// a is defined when visiting a=${value}? ${path.scope.hasBinding("a")}` | ||
); | ||
}, | ||
}, | ||
}; | ||
}; |
3 changes: 3 additions & 0 deletions
3
packages/babel-traverse/test/fixtures/scope/remove-var-in-block/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,3 @@ | ||
{ | ||
var a = 1; | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/babel-traverse/test/fixtures/scope/remove-var-in-block/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": ["./plugin"] | ||
} |
1 change: 1 addition & 0 deletions
1
packages/babel-traverse/test/fixtures/scope/remove-var-in-block/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 @@ | ||
{} |
10 changes: 10 additions & 0 deletions
10
packages/babel-traverse/test/fixtures/scope/remove-var-in-block/plugin.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,10 @@ | ||
module.exports = function () { | ||
return { | ||
visitor: { | ||
VariableDeclarator(path) { | ||
path.remove(); | ||
expect(path.scope.getBinding("a")).toBeUndefined(); | ||
}, | ||
}, | ||
}; | ||
}; |