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
Implement @babel/plugin-bugfix-safari-id-destructuring-collision-in-function-expression #13842
Merged
JLHwung
merged 16 commits into
babel:main
from
JLHwung:bugfix-safari-id-destructuring-collision-in-function-expression
Oct 20, 2021
Merged
Changes from 14 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
4368822
fix: register function expression id after params
JLHwung 51c00f2
implement bugfix plugin
JLHwung d78f3fd
add more testcases
JLHwung 0e31517
fix: do not skip pattern binding referencing id
JLHwung 6d8e8c1
update compat-table
JLHwung b53a29a
add bugfix plugin to preset-env
JLHwung 5632a4d
update Babel 8 test fixtures
JLHwung e354db3
Update packages/babel-plugin-bugfix-safari-id-destructuring-collision…
JLHwung 4e90f11
chore: bundle bugfix plugin
JLHwung 2aa123e
address review comments
JLHwung 6d7bcd9
add runtime version check
JLHwung 40748d2
update compat table
JLHwung faa978d
fix syntax error
JLHwung f8a2830
update test fixtures
JLHwung 28ce449
revert bugfixes targets update
JLHwung c372660
update Babel 8 test fixtures
JLHwung 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
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
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
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
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
3 changes: 3 additions & 0 deletions
3
...s/babel-plugin-bugfix-safari-id-destructuring-collision-in-function-expression/.npmignore
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 @@ | ||
src | ||
test | ||
*.log |
19 changes: 19 additions & 0 deletions
19
...lugin-bugfix-safari-id-destructuring-collision-in-function-expression/README.md
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,19 @@ | ||
# @babel/plugin-bugfix-safari-id-destructuring-collision-in-function-expression | ||
|
||
> Rename destructuring parameter to workaround a [Safari bug](https://bugs.webkit.org/show_bug.cgi?id=220517). | ||
|
||
See our website [@babel/plugin-bugfix-safari-id-destructuring-collision-in-function-expression](https://babeljs.io/docs/en/babel-plugin-bugfix-safari-id-destructuring-collision-in-function-expression) for more information. | ||
|
||
## Install | ||
|
||
Using npm: | ||
|
||
```sh | ||
npm install --save-dev @babel/plugin-bugfix-safari-id-destructuring-collision-in-function-expression | ||
``` | ||
|
||
or using yarn: | ||
|
||
```sh | ||
yarn add @babel/plugin-bugfix-safari-id-destructuring-collision-in-function-expression --dev | ||
``` |
40 changes: 40 additions & 0 deletions
40
...babel-plugin-bugfix-safari-id-destructuring-collision-in-function-expression/package.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,40 @@ | ||
{ | ||
"name": "@babel/plugin-bugfix-safari-id-destructuring-collision-in-function-expression", | ||
"version": "0.0.0", | ||
"description": "Rename destructuring parameter to workaround https://bugs.webkit.org/show_bug.cgi?id=220517", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/babel/babel.git", | ||
"directory": "packages/babel-plugin-bugfix-safari-id-destructuring-collision-in-function-expression" | ||
}, | ||
"homepage": "https://babel.dev/docs/en/next/babel-plugin-bugfix-safari-id-destructuring-collision-in-function-expression", | ||
"license": "MIT", | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"main": "./lib/index.js", | ||
"exports": { | ||
".": [ | ||
"./lib/index.js" | ||
] | ||
}, | ||
"keywords": [ | ||
"babel-plugin", | ||
"bugfix" | ||
], | ||
"dependencies": { | ||
"@babel/helper-plugin-utils": "workspace:^7.14.5" | ||
}, | ||
"peerDependencies": { | ||
"@babel/core": "^7.0.0" | ||
}, | ||
"devDependencies": { | ||
"@babel/core": "workspace:*", | ||
"@babel/helper-plugin-test-runner": "workspace:*", | ||
"@babel/traverse": "workspace:*" | ||
}, | ||
"engines": { | ||
"node": ">=6.9.0" | ||
}, | ||
"author": "The Babel Team (https://babel.dev/team)" | ||
} |
25 changes: 25 additions & 0 deletions
25
...babel-plugin-bugfix-safari-id-destructuring-collision-in-function-expression/src/index.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,25 @@ | ||
import { declare } from "@babel/helper-plugin-utils"; | ||
import type { PluginPass } from "@babel/core"; | ||
import type { Visitor } from "@babel/traverse"; | ||
import { shouldTransform } from "./util"; | ||
|
||
export default declare(api => { | ||
api.assertVersion("^7.15.0"); | ||
|
||
return { | ||
name: "plugin-bugfix-safari-id-destructuring-collision-in-function-expression", | ||
|
||
visitor: { | ||
FunctionExpression(path) { | ||
const name = shouldTransform(path); | ||
if (name) { | ||
// Now we have (function a([a]) {}) | ||
const { scope } = path; | ||
// invariant: path.node.id is always an Identifier here | ||
const newParamName = scope.generateUid(name); | ||
scope.rename(name, newParamName); | ||
} | ||
}, | ||
} as Visitor<PluginPass>, | ||
}; | ||
}); |
41 changes: 41 additions & 0 deletions
41
.../babel-plugin-bugfix-safari-id-destructuring-collision-in-function-expression/src/util.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,41 @@ | ||
import type { FunctionExpression } from "@babel/types"; | ||
import type { NodePath } from "@babel/traverse"; | ||
|
||
/** | ||
* Check whether a function expression can be affected by | ||
* https://bugs.webkit.org/show_bug.cgi?id=220517 | ||
* @param path The function expression NodePath | ||
* @returns the name of function id if it should be transformed, otherwise returns false | ||
*/ | ||
export function shouldTransform( | ||
path: NodePath<FunctionExpression>, | ||
): string | false { | ||
const { node } = path; | ||
const functionId = node.id; | ||
if (!functionId) return false; | ||
|
||
const name = functionId.name; | ||
// On collision, `getOwnBinding` returns the param binding | ||
// with the id binding be registered as constant violation | ||
const paramNameBinding = path.scope.getOwnBinding(name); | ||
const constantViolations = paramNameBinding.constantViolations; | ||
if (constantViolations.length === 0) { | ||
// the function scope has no such collided bindings | ||
return false; | ||
} | ||
const firstViolation = constantViolations[0]; | ||
|
||
if (firstViolation.node !== node) { | ||
// the violation does not happen in id | ||
// e.g. (function a() { var a; }) | ||
return false; | ||
} | ||
|
||
if (paramNameBinding.identifier === paramNameBinding.path.node) { | ||
// the param binding is a simple parameter | ||
// e.g. (function a(a) {}) | ||
return false; | ||
} | ||
|
||
return name; | ||
} |
1 change: 1 addition & 0 deletions
1
...tructuring-collision-in-function-expression/test/fixtures/basic/avoid-collision/input.mjs
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 @@ | ||
(function a([a, _a]) { a + _a }) |
3 changes: 3 additions & 0 deletions
3
...ructuring-collision-in-function-expression/test/fixtures/basic/avoid-collision/output.mjs
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 @@ | ||
(function a([_a2, _a]) { | ||
_a2 + _a; | ||
}); |
3 changes: 3 additions & 0 deletions
3
...ari-id-destructuring-collision-in-function-expression/test/fixtures/basic/basic/input.mjs
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 @@ | ||
(function a([a]) { a }); | ||
(function a({ ...a }) { a }); | ||
(function a({ a }) { a }); |
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.
The script is updated to support multiple
replaces
config. e.g.bugfix/transform-safari-id-destructuring-collision-in-function-expression
replaces bothtransform-parameters
andproposal-object-rest-spread
.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 can be reverted, since we don't need it anymore.