Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement @babel/plugin-bugfix-v8-spread-parameters-in-optional-chain…
…ing (#13009)
- Loading branch information
Showing
44 changed files
with
807 additions
and
227 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
3 changes: 3 additions & 0 deletions
3
packages/babel-plugin-bugfix-v8-spread-parameters-in-optional-chaining/.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-spread-parameters-in-optional-chaining/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-spread-parameters-in-optional-chaining | ||
|
||
> Transform optional chaining operators to workaround a [v8 bug](https://crbug.com/v8/11558). | ||
See our website [@babel/plugin-bugfix-v8-spread-parameters-in-optional-chaining](https://babeljs.io/docs/en/babel-plugin-bugfix-v8-spread-parameters-in-optional-chaining) for more information. | ||
|
||
## Install | ||
|
||
Using npm: | ||
|
||
```sh | ||
npm install --save-dev @babel/plugin-bugfix-v8-spread-parameters-in-optional-chaining | ||
``` | ||
|
||
or using yarn: | ||
|
||
```sh | ||
yarn add @babel/plugin-bugfix-v8-spread-parameters-in-optional-chaining --dev | ||
``` |
38 changes: 38 additions & 0 deletions
38
packages/babel-plugin-bugfix-v8-spread-parameters-in-optional-chaining/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,38 @@ | ||
{ | ||
"name": "@babel/plugin-bugfix-v8-spread-parameters-in-optional-chaining", | ||
"version": "7.13.8", | ||
"description": "Transform optional chaining operators to workaround https://crbug.com/v8/11558", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/babel/babel.git", | ||
"directory": "packages/babel-plugin-bugfix-v8-spread-parameters-in-optional-chaining" | ||
}, | ||
"homepage": "https://babel.dev/docs/en/next/babel-plugin-bugfix-v8-spread-parameters-in-optional-chaining", | ||
"license": "MIT", | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"main": "lib/index.js", | ||
"exports": { | ||
".": [ | ||
"./lib/index.js" | ||
] | ||
}, | ||
"keywords": [ | ||
"babel-plugin", | ||
"bugfix" | ||
], | ||
"dependencies": { | ||
"@babel/helper-plugin-utils": "workspace:^7.13.0", | ||
"@babel/helper-skip-transparent-expression-wrappers": "workspace:^7.12.1", | ||
"@babel/plugin-proposal-optional-chaining": "workspace:^7.13.8" | ||
}, | ||
"peerDependencies": { | ||
"@babel/core": "^7.13.0" | ||
}, | ||
"devDependencies": { | ||
"@babel/core": "workspace:*", | ||
"@babel/helper-plugin-test-runner": "workspace:*", | ||
"@babel/traverse": "workspace:*" | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
packages/babel-plugin-bugfix-v8-spread-parameters-in-optional-chaining/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,22 @@ | ||
import { declare } from "@babel/helper-plugin-utils"; | ||
import { transform } from "@babel/plugin-proposal-optional-chaining"; | ||
import { shouldTransform } from "./util"; | ||
|
||
export default declare(api => { | ||
api.assertVersion(7); | ||
|
||
const noDocumentAll = api.assumption("noDocumentAll"); | ||
const pureGetters = api.assumption("pureGetters"); | ||
|
||
return { | ||
name: "bugfix-v8-spread-parameters-in-optional-chaining", | ||
|
||
visitor: { | ||
"OptionalCallExpression|OptionalMemberExpression"(path) { | ||
if (shouldTransform(path)) { | ||
transform(path, { noDocumentAll, pureGetters }); | ||
} | ||
}, | ||
}, | ||
}; | ||
}); |
59 changes: 59 additions & 0 deletions
59
packages/babel-plugin-bugfix-v8-spread-parameters-in-optional-chaining/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,59 @@ | ||
import { skipTransparentExprWrappers } from "@babel/helper-skip-transparent-expression-wrappers"; | ||
import type { NodePath } from "@babel/traverse"; | ||
import { types as t } from "@babel/core"; | ||
// https://crbug.com/v8/11558 | ||
|
||
// check if there is a spread element followed by another argument. | ||
// (...[], 0) or (...[], ...[]) | ||
|
||
function matchAffectedArguments(argumentNodes) { | ||
const spreadIndex = argumentNodes.findIndex(node => t.isSpreadElement(node)); | ||
return spreadIndex >= 0 && spreadIndex !== argumentNodes.length - 1; | ||
} | ||
|
||
/** | ||
* Check whether the optional chain is affected by https://crbug.com/v8/11558. | ||
* This routine MUST not manipulate NodePath | ||
* | ||
* @export | ||
* @param {(NodePath<t.OptionalMemberExpression | t.OptionalCallExpression>)} path | ||
* @returns {boolean} | ||
*/ | ||
export function shouldTransform( | ||
path: NodePath<t.OptionalMemberExpression | t.OptionalCallExpression>, | ||
): boolean { | ||
let optionalPath = path; | ||
const chains = []; | ||
while ( | ||
optionalPath.isOptionalMemberExpression() || | ||
optionalPath.isOptionalCallExpression() | ||
) { | ||
const { node } = optionalPath; | ||
chains.push(node); | ||
|
||
if (optionalPath.isOptionalMemberExpression()) { | ||
optionalPath = skipTransparentExprWrappers(optionalPath.get("object")); | ||
} else if (optionalPath.isOptionalCallExpression()) { | ||
optionalPath = skipTransparentExprWrappers(optionalPath.get("callee")); | ||
} | ||
} | ||
for (let i = 0; i < chains.length; i++) { | ||
const node = chains[i]; | ||
if ( | ||
t.isOptionalCallExpression(node) && | ||
matchAffectedArguments(node.arguments) | ||
) { | ||
// f?.(...[], 0) | ||
if (node.optional) { | ||
return true; | ||
} | ||
// o?.m(...[], 0) | ||
// when node.optional is false, chains[i + 1] is always well defined | ||
const callee = chains[i + 1]; | ||
if (t.isOptionalMemberExpression(callee, { optional: true })) { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} |
7 changes: 7 additions & 0 deletions
7
...ad-parameters-in-optional-chaining/test/fixtures/assumption-noDocumentAll/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,7 @@ | ||
fn?.(...b, 1); | ||
|
||
a?.b(...c, 1); | ||
|
||
a?.b?.(...c, 1); | ||
|
||
a.b?.(...c, 1); |
6 changes: 6 additions & 0 deletions
6
...d-parameters-in-optional-chaining/test/fixtures/assumption-noDocumentAll/basic/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,6 @@ | ||
var _fn, _a, _a2, _a2$b, _a$b, _a3; | ||
|
||
(_fn = fn) === null || _fn === void 0 ? void 0 : _fn(...b, 1); | ||
(_a = a) === null || _a === void 0 ? void 0 : _a.b(...c, 1); | ||
(_a2 = a) === null || _a2 === void 0 ? void 0 : (_a2$b = _a2.b) === null || _a2$b === void 0 ? void 0 : _a2$b.call(_a2, ...c, 1); | ||
(_a$b = (_a3 = a).b) === null || _a$b === void 0 ? void 0 : _a$b.call(_a3, ...c, 1); |
19 changes: 19 additions & 0 deletions
19
...rameters-in-optional-chaining/test/fixtures/assumption-pureGetters/function-call/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,19 @@ | ||
foo?.(...[], 1); | ||
|
||
foo?.bar(...[], 1) | ||
|
||
foo.bar?.(foo.bar, ...[], 1) | ||
|
||
foo?.bar?.(foo.bar, ...[], 1) | ||
|
||
foo?.(...[], 1).bar | ||
|
||
foo?.(...[], 1)?.bar | ||
|
||
foo.bar?.(...[], 1).baz | ||
|
||
foo.bar?.(...[], 1)?.baz | ||
|
||
foo?.bar?.(...[], 1).baz | ||
|
||
foo?.bar?.(...[], 1)?.baz |
12 changes: 12 additions & 0 deletions
12
...ameters-in-optional-chaining/test/fixtures/assumption-pureGetters/function-call/output.js
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
5 changes: 5 additions & 0 deletions
5
...rs-in-optional-chaining/test/fixtures/basic/basic-integration-optional-chaining/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,5 @@ | ||
fn?.(...b, 1); | ||
|
||
a?.b(...c, 1); | ||
|
||
a?.b?.(...c, 1); |
5 changes: 5 additions & 0 deletions
5
...in-optional-chaining/test/fixtures/basic/basic-integration-optional-chaining/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,5 @@ | ||
{ | ||
"plugins": [ | ||
"proposal-optional-chaining", "bugfix-v8-spread-parameters-in-optional-chaining" | ||
] | ||
} |
5 changes: 5 additions & 0 deletions
5
...s-in-optional-chaining/test/fixtures/basic/basic-integration-optional-chaining/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,5 @@ | ||
var _fn, _a, _a2, _a2$b; | ||
|
||
(_fn = fn) === null || _fn === void 0 ? void 0 : _fn(...b, 1); | ||
(_a = a) === null || _a === void 0 ? void 0 : _a.b(...c, 1); | ||
(_a2 = a) === null || _a2 === void 0 ? void 0 : (_a2$b = _a2.b) === null || _a2$b === void 0 ? void 0 : _a2$b.call(_a2, ...c, 1); |
7 changes: 7 additions & 0 deletions
7
...ugin-bugfix-v8-spread-parameters-in-optional-chaining/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,7 @@ | ||
fn?.(...b, 1); | ||
|
||
a?.b(...c, 1); | ||
|
||
a?.b?.(...c, 1); | ||
|
||
a.b?.(...c, 1); |
6 changes: 6 additions & 0 deletions
6
...gin-bugfix-v8-spread-parameters-in-optional-chaining/test/fixtures/basic/basic/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,6 @@ | ||
var _fn, _a, _a2, _a2$b, _a$b, _a3; | ||
|
||
(_fn = fn) === null || _fn === void 0 ? void 0 : _fn(...b, 1); | ||
(_a = a) === null || _a === void 0 ? void 0 : _a.b(...c, 1); | ||
(_a2 = a) === null || _a2 === void 0 ? void 0 : (_a2$b = _a2.b) === null || _a2$b === void 0 ? void 0 : _a2$b.call(_a2, ...c, 1); | ||
(_a$b = (_a3 = a).b) === null || _a$b === void 0 ? void 0 : _a$b.call(_a3, ...c, 1); |
13 changes: 13 additions & 0 deletions
13
...ptional-chaining/test/fixtures/basic/class-private-integration-optional-chaining/exec.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,13 @@ | ||
class C { | ||
#m; | ||
constructor() { | ||
const o = null; | ||
const n = this; | ||
const p = o?.#m(...c, 1); | ||
const q = n?.#m?.(...c, 1); | ||
expect(p).toBe(undefined); | ||
expect(q).toBe(undefined); | ||
} | ||
} | ||
|
||
new C(); |
9 changes: 9 additions & 0 deletions
9
...tional-chaining/test/fixtures/basic/class-private-integration-optional-chaining/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,9 @@ | ||
class C { | ||
#m; | ||
constructor() { | ||
const o = null; | ||
const n = this; | ||
const p = o?.#m(...c, 1); | ||
const q = n?.#m?.(...c, 1); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
...nal-chaining/test/fixtures/basic/class-private-integration-optional-chaining/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,5 @@ | ||
{ | ||
"plugins": [ | ||
"proposal-class-properties", "proposal-private-methods", "proposal-optional-chaining", "bugfix-v8-spread-parameters-in-optional-chaining" | ||
] | ||
} |
18 changes: 18 additions & 0 deletions
18
...ional-chaining/test/fixtures/basic/class-private-integration-optional-chaining/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,18 @@ | ||
var _m = new WeakMap(); | ||
|
||
class C { | ||
constructor() { | ||
var _babelHelpers$classPr; | ||
|
||
_m.set(this, { | ||
writable: true, | ||
value: void 0 | ||
}); | ||
|
||
const o = null; | ||
const n = this; | ||
const p = o === null || o === void 0 ? void 0 : babelHelpers.classPrivateFieldGet(o, _m).call(o, ...c, 1); | ||
const q = n === null || n === void 0 ? void 0 : (_babelHelpers$classPr = babelHelpers.classPrivateFieldGet(n, _m)) === null || _babelHelpers$classPr === void 0 ? void 0 : _babelHelpers$classPr.call(n, ...c, 1); | ||
} | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
...ad-parameters-in-optional-chaining/test/fixtures/basic/class-private-integration/exec.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,13 @@ | ||
class C { | ||
#m; | ||
constructor() { | ||
const o = null; | ||
const n = this; | ||
const p = o?.#m(...c, 1); | ||
const q = n?.#m?.(...c, 1); | ||
expect(p).toBe(undefined); | ||
expect(q).toBe(undefined); | ||
} | ||
} | ||
|
||
new C(); |
9 changes: 9 additions & 0 deletions
9
...d-parameters-in-optional-chaining/test/fixtures/basic/class-private-integration/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,9 @@ | ||
class C { | ||
#m; | ||
constructor() { | ||
const o = null; | ||
const n = this; | ||
const p = o?.#m(...c, 1); | ||
const q = n?.#m?.(...c, 1); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
...arameters-in-optional-chaining/test/fixtures/basic/class-private-integration/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,5 @@ | ||
{ | ||
"plugins": [ | ||
"proposal-class-properties", "proposal-private-methods", "bugfix-v8-spread-parameters-in-optional-chaining" | ||
] | ||
} |
18 changes: 18 additions & 0 deletions
18
...-parameters-in-optional-chaining/test/fixtures/basic/class-private-integration/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,18 @@ | ||
var _m = new WeakMap(); | ||
|
||
class C { | ||
constructor() { | ||
var _babelHelpers$classPr; | ||
|
||
_m.set(this, { | ||
writable: true, | ||
value: void 0 | ||
}); | ||
|
||
const o = null; | ||
const n = this; | ||
const p = o === null || o === void 0 ? void 0 : babelHelpers.classPrivateFieldGet(o, _m).call(o, ...c, 1); | ||
const q = n === null || n === void 0 ? void 0 : (_babelHelpers$classPr = babelHelpers.classPrivateFieldGet(n, _m)) === null || _babelHelpers$classPr === void 0 ? void 0 : _babelHelpers$classPr.call(n, ...c, 1); | ||
} | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
...gfix-v8-spread-parameters-in-optional-chaining/test/fixtures/basic/class-private/exec.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,13 @@ | ||
class C { | ||
#m; | ||
constructor() { | ||
const o = null; | ||
const n = this; | ||
const p = o?.#m(...c, 1); | ||
const q = n?.#m?.(...c, 1); | ||
expect(p).toBe(undefined); | ||
expect(q).toBe(undefined); | ||
} | ||
} | ||
|
||
new C; |
9 changes: 9 additions & 0 deletions
9
...fix-v8-spread-parameters-in-optional-chaining/test/fixtures/basic/class-private/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,9 @@ | ||
class C { | ||
#m; | ||
constructor() { | ||
const o = null; | ||
const n = this; | ||
const p = o?.#m(...c, 1); | ||
const q = n?.#m?.(...c, 1); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...-v8-spread-parameters-in-optional-chaining/test/fixtures/basic/class-private/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,10 @@ | ||
{ | ||
"minNodeVersion": "14.0.0", | ||
"parserOpts": { | ||
"plugins": [ | ||
"classPrivateMethods", | ||
"classPrivateProperties", | ||
"classProperties" | ||
] | ||
} | ||
} |
Oops, something went wrong.