-
-
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.
Add a bugfix plugin for https://crbug.com/v8/12421 (#14295)
- Loading branch information
1 parent
7a35330
commit 46ee461
Showing
56 changed files
with
933 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
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
packages/babel-plugin-bugfix-v8-static-class-fields-redefine-readonly/.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
packages/babel-plugin-bugfix-v8-static-class-fields-redefine-readonly/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-v8-static-class-fields-redefine-readonly | ||
|
||
> Transform static class fields assignments that are affected by https://crbug.com/v8/12421 | ||
See our website [@babel/plugin-bugfix-v8-static-class-fields-redefine-readonly](https://babeljs.io/docs/babel-plugin-bugfix-v8-static-class-fields-redefine-readonly) for more information. | ||
|
||
## Install | ||
|
||
Using npm: | ||
|
||
```sh | ||
npm install --save-dev @babel/plugin-bugfix-v8-static-class-fields-redefine-readonly | ||
``` | ||
|
||
or using yarn: | ||
|
||
```sh | ||
yarn add @babel/plugin-bugfix-v8-static-class-fields-redefine-readonly --dev | ||
``` |
57 changes: 57 additions & 0 deletions
57
packages/babel-plugin-bugfix-v8-static-class-fields-redefine-readonly/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,57 @@ | ||
{ | ||
"name": "@babel/plugin-bugfix-v8-static-class-fields-redefine-readonly", | ||
"version": "7.16.7", | ||
"description": "Transform static class fields assignments that are affected by https://crbug.com/v8/12421", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/babel/babel.git", | ||
"directory": "packages/babel-plugin-bugfix-v8-static-class-fields-redefine-readonly" | ||
}, | ||
"homepage": "https://babel.dev/docs/en/next/babel-plugin-bugfix-v8-static-class-fields-redefine-readonly", | ||
"license": "MIT", | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"main": "./lib/index.js", | ||
"exports": { | ||
".": "./lib/index.js", | ||
"./package.json": "./package.json" | ||
}, | ||
"keywords": [ | ||
"babel-plugin", | ||
"bugfix" | ||
], | ||
"dependencies": { | ||
"@babel/helper-environment-visitor": "workspace:^", | ||
"@babel/helper-plugin-utils": "workspace:^" | ||
}, | ||
"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)", | ||
"conditions": { | ||
"USE_ESM": [ | ||
{ | ||
"type": "module" | ||
}, | ||
null | ||
], | ||
"BABEL_8_BREAKING": [ | ||
{ | ||
"engines": { | ||
"node": "^16.20.0 || ^18.16.0 || >=20.0.0" | ||
} | ||
}, | ||
{} | ||
] | ||
}, | ||
"type": "commonjs" | ||
} |
70 changes: 70 additions & 0 deletions
70
packages/babel-plugin-bugfix-v8-static-class-fields-redefine-readonly/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,70 @@ | ||
import type { NodePath, Scope } from "@babel/traverse"; | ||
import { types as t, type PluginPass, type File } from "@babel/core"; | ||
import { declare } from "@babel/helper-plugin-utils"; | ||
|
||
import { | ||
getPotentiallyBuggyFieldsIndexes, | ||
getNameOrLengthStaticFieldsIndexes, | ||
toRanges, | ||
} from "./util.ts"; | ||
|
||
function buildFieldsReplacement( | ||
fields: t.ClassProperty[], | ||
scope: Scope, | ||
file: File, | ||
) { | ||
return t.staticBlock( | ||
fields.map(field => { | ||
const key = | ||
field.computed || !t.isIdentifier(field.key) | ||
? field.key | ||
: t.stringLiteral(field.key.name); | ||
|
||
return t.expressionStatement( | ||
t.callExpression(file.addHelper("defineProperty"), [ | ||
t.thisExpression(), | ||
key, | ||
field.value || scope.buildUndefinedNode(), | ||
]), | ||
); | ||
}), | ||
); | ||
} | ||
|
||
export default declare(api => { | ||
api.assertVersion(7); | ||
|
||
const setPublicClassFields = api.assumption("setPublicClassFields"); | ||
|
||
return { | ||
name: "bugfix-v8-static-class-fields-redefine-readonly", | ||
|
||
visitor: { | ||
Class(this: PluginPass, path: NodePath<t.Class>) { | ||
const ranges = toRanges( | ||
setPublicClassFields | ||
? getNameOrLengthStaticFieldsIndexes(path) | ||
: getPotentiallyBuggyFieldsIndexes(path), | ||
); | ||
|
||
for (let i = ranges.length - 1; i >= 0; i--) { | ||
const [start, end] = ranges[i]; | ||
|
||
const startPath = path.get("body.body")[start]; | ||
|
||
startPath.replaceWith( | ||
buildFieldsReplacement( | ||
path.node.body.body.slice(start, end) as t.ClassProperty[], | ||
path.scope, | ||
this.file, | ||
), | ||
); | ||
|
||
for (let j = end - 1; j > start; j--) { | ||
path.get("body.body")[j].remove(); | ||
} | ||
} | ||
}, | ||
}, | ||
}; | ||
}); |
Oops, something went wrong.