Skip to content

Commit

Permalink
implement bugfix plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed Oct 13, 2021
1 parent feef5e0 commit 2515eea
Show file tree
Hide file tree
Showing 16 changed files with 220 additions and 18 deletions.
18 changes: 9 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -186,15 +186,15 @@ prepublish:
IS_PUBLISH=true $(MAKE) test

new-version-checklist:
# @echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
# @echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
# @echo "!!!!!! !!!!!!"
# @echo "!!!!!! Write any message that should !!!!!!"
# @echo "!!!!!! block the release here !!!!!!"
# @echo "!!!!!! !!!!!!"
# @echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
# @echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
# @exit 1
@echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
@echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
@echo "!!!!!! !!!!!!"
@echo "!!!!!! Set packages/babel-plugin-bugfix-webkit-id-destructuring-collision-in-function-expression/package.json"
@echo "!!!!!! @babel/core peerDependencies to latest published version"
@echo "!!!!!! !!!!!!"
@echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
@echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
@exit 1

new-version:
$(MAKE) new-version-checklist
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,7 @@ function getPath(input, parserOpts = {}) {
let targetPath;
traverse(
parseSync(input, {
parserOpts: {
plugins: [
"classPrivateMethods",
"classPrivateProperties",
"classProperties",
...(parserOpts.plugins || []),
],
...parserOpts,
},
parserOpts,
filename: "example.js",
configFile: false,
}),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
src
test
*.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# @babel/plugin-bugfix-webkit-id-destructuring-collision-in-function-expression

> Transform optional chaining operators to workaround a [v8 bug](https://crbug.com/v8/11558).
See our website [@babel/plugin-bugfix-webkit-id-destructuring-collision-in-function-expression](https://babeljs.io/docs/en/babel-plugin-bugfix-webkit-id-destructuring-collision-in-function-expression) for more information.

## Install

Using npm:

```sh
npm install --save-dev @babel/plugin-bugfix-webkit-id-destructuring-collision-in-function-expression
```

or using yarn:

```sh
yarn add @babel/plugin-bugfix-webkit-id-destructuring-collision-in-function-expression --dev
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"name": "@babel/plugin-bugfix-webkit-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-webkit-id-destructuring-collision-in-function-expression"
},
"homepage": "https://babel.dev/docs/en/next/babel-plugin-bugfix-webkit-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.15.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)"
}
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);

return {
name: "plugin-bugfix-webkit-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>,
};
});
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;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(function a([a, _a]) { a + _a })
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(function a([_a2, _a]) {
_a2 + _a;
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(function a([a]) { a })
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(function a([_a]) {
_a;
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"plugins": ["bugfix-webkit-id-destructuring-collision-in-function-expression"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import runner from "@babel/helper-plugin-test-runner";

runner(import.meta.url);
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { parseSync, traverse } from "@babel/core";
import { shouldTransform } from "../src/util.ts";

function getPath(input, parserOpts = {}) {
let targetPath;
traverse(
parseSync(input, {
parserOpts,
filename: "example.js",
configFile: false,
}),
{
FunctionExpression(path) {
targetPath = path;
path.stop();
},
},
);
return targetPath;
}

describe("shouldTransform", () => {
const positiveCases = [
"(function a([a]) {})",
"({ b: function a([a]) {} })",
"(function a({a}) {})",
"(function a(...a) {})",
"(function a([a = 1]) {})",
];

const negativeCases = [
"(function () {})",
"(function a() {})",
"(function a(a) {})",
"(function a() { var a })",
"(function a(x = a) {})",
"(function a() { var { a } = {}; })",
];

describe("default parser options", () => {
test.each(positiveCases)("shouldTransform(%p) should return 'a'", input => {
expect(shouldTransform(getPath(input))).toBe("a");
});
test.each(negativeCases)(
"shouldTransform(in %p) should return false",
input => {
expect(shouldTransform(getPath(input))).toBe(false);
},
);
});
});
4 changes: 4 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"./packages/babel-helpers/src/**/*.ts",
"./packages/babel-highlight/src/**/*.ts",
"./packages/babel-plugin-bugfix-v8-spread-parameters-in-optional-chaining/src/**/*.ts",
"./packages/babel-plugin-bugfix-webkit-id-destructuring-collision-in-function-expression/src/**/*.ts",
"./packages/babel-plugin-proposal-async-do-expressions/src/**/*.ts",
"./packages/babel-plugin-syntax-async-do-expressions/src/**/*.ts",
"./packages/babel-plugin-transform-block-scoping/src/**/*.ts",
Expand Down Expand Up @@ -122,6 +123,9 @@
"@babel/plugin-bugfix-v8-spread-parameters-in-optional-chaining": [
"./packages/babel-plugin-bugfix-v8-spread-parameters-in-optional-chaining/src"
],
"@babel/plugin-bugfix-webkit-id-destructuring-collision-in-function-expression": [
"./packages/babel-plugin-bugfix-webkit-id-destructuring-collision-in-function-expression/src"
],
"@babel/plugin-proposal-async-do-expressions": [
"./packages/babel-plugin-proposal-async-do-expressions/src"
],
Expand Down
13 changes: 13 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1057,6 +1057,19 @@ __metadata:
languageName: unknown
linkType: soft

"@babel/plugin-bugfix-webkit-id-destructuring-collision-in-function-expression@workspace:packages/babel-plugin-bugfix-webkit-id-destructuring-collision-in-function-expression":
version: 0.0.0-use.local
resolution: "@babel/plugin-bugfix-webkit-id-destructuring-collision-in-function-expression@workspace:packages/babel-plugin-bugfix-webkit-id-destructuring-collision-in-function-expression"
dependencies:
"@babel/core": "workspace:*"
"@babel/helper-plugin-test-runner": "workspace:*"
"@babel/helper-plugin-utils": "workspace:^7.14.5"
"@babel/traverse": "workspace:*"
peerDependencies:
"@babel/core": ^7.15.0
languageName: unknown
linkType: soft

"@babel/plugin-codemod-object-assign-to-object-spread@workspace:codemods/babel-plugin-codemod-object-assign-to-object-spread":
version: 0.0.0-use.local
resolution: "@babel/plugin-codemod-object-assign-to-object-spread@workspace:codemods/babel-plugin-codemod-object-assign-to-object-spread"
Expand Down

0 comments on commit 2515eea

Please sign in to comment.