Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: correctly transform
this.#m?.(...arguments)
(#12350)
* add tests on @babel/helper-optimise-call-expression * fix: correctly optimise `a.b?.(...arguments)` * add integration test with properties transform
- Loading branch information
Showing
12 changed files
with
277 additions
and
2 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
30 changes: 28 additions & 2 deletions
30
packages/babel-helper-optimise-call-expression/src/index.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
103 changes: 103 additions & 0 deletions
103
packages/babel-helper-optimise-call-expression/test/index.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,103 @@ | ||
import { parse } from "@babel/parser"; | ||
import generator from "@babel/generator"; | ||
import * as t from "@babel/types"; | ||
import optimizeCallExpression from ".."; | ||
|
||
function transformInput(input, thisIdentifier) { | ||
const ast = parse(input); | ||
const callExpression = ast.program.body[0].expression; | ||
return generator( | ||
optimizeCallExpression( | ||
callExpression.callee, | ||
thisIdentifier | ||
? t.identifier(thisIdentifier) | ||
: callExpression.callee.object, | ||
callExpression.arguments, | ||
callExpression.type === "OptionalCallExpression", | ||
), | ||
).code; | ||
} | ||
|
||
describe("@babel/helper-optimise-call-expression", () => { | ||
test("optimizeCallExpression should work when thisNode is implied from callee", () => { | ||
expect(transformInput("a.b(...arguments)")).toMatchInlineSnapshot( | ||
`"a.b.apply(a, arguments)"`, | ||
); | ||
expect(transformInput("a[b](...arguments)")).toMatchInlineSnapshot( | ||
`"a[b].apply(a, arguments)"`, | ||
); | ||
expect(transformInput("a.b?.(...arguments)")).toMatchInlineSnapshot( | ||
`"a.b?.apply(a, arguments)"`, | ||
); | ||
expect(transformInput("a[b]?.(...arguments)")).toMatchInlineSnapshot( | ||
`"a[b]?.apply(a, arguments)"`, | ||
); | ||
|
||
expect(transformInput("a.b(...args)")).toMatchInlineSnapshot( | ||
`"a.b.call(a, ...args)"`, | ||
); | ||
expect(transformInput("a[b](...args)")).toMatchInlineSnapshot( | ||
`"a[b].call(a, ...args)"`, | ||
); | ||
expect(transformInput("a.b?.(...args)")).toMatchInlineSnapshot( | ||
`"a.b?.call(a, ...args)"`, | ||
); | ||
expect(transformInput("a[b]?.(...args)")).toMatchInlineSnapshot( | ||
`"a[b]?.call(a, ...args)"`, | ||
); | ||
|
||
expect(transformInput("a.b(arg1, arg2)")).toMatchInlineSnapshot( | ||
`"a.b.call(a, arg1, arg2)"`, | ||
); | ||
expect(transformInput("a[b](arg1, arg2)")).toMatchInlineSnapshot( | ||
`"a[b].call(a, arg1, arg2)"`, | ||
); | ||
expect(transformInput("a.b?.(arg1, arg2)")).toMatchInlineSnapshot( | ||
`"a.b?.call(a, arg1, arg2)"`, | ||
); | ||
expect(transformInput("a[b]?.(arg1, arg2)")).toMatchInlineSnapshot( | ||
`"a[b]?.call(a, arg1, arg2)"`, | ||
); | ||
}); | ||
|
||
test("optimizeCallExpression should work when thisNode is provided", () => { | ||
expect(transformInput("a.b(...arguments)", "c")).toMatchInlineSnapshot( | ||
`"a.b.apply(c, arguments)"`, | ||
); | ||
expect(transformInput("a[b](...arguments)", "c")).toMatchInlineSnapshot( | ||
`"a[b].apply(c, arguments)"`, | ||
); | ||
expect(transformInput("a.b?.(...arguments)", "c")).toMatchInlineSnapshot( | ||
`"a.b?.apply(c, arguments)"`, | ||
); | ||
expect(transformInput("a[b]?.(...arguments)", "c")).toMatchInlineSnapshot( | ||
`"a[b]?.apply(c, arguments)"`, | ||
); | ||
|
||
expect(transformInput("a.b(...args)", "c")).toMatchInlineSnapshot( | ||
`"a.b.call(c, ...args)"`, | ||
); | ||
expect(transformInput("a[b](...args)", "c")).toMatchInlineSnapshot( | ||
`"a[b].call(c, ...args)"`, | ||
); | ||
expect(transformInput("a.b?.(...args)", "c")).toMatchInlineSnapshot( | ||
`"a.b?.call(c, ...args)"`, | ||
); | ||
expect(transformInput("a[b]?.(...args)", "c")).toMatchInlineSnapshot( | ||
`"a[b]?.call(c, ...args)"`, | ||
); | ||
|
||
expect(transformInput("a.b(arg1, arg2)", "c")).toMatchInlineSnapshot( | ||
`"a.b.call(c, arg1, arg2)"`, | ||
); | ||
expect(transformInput("a[b](arg1, arg2)", "c")).toMatchInlineSnapshot( | ||
`"a[b].call(c, arg1, arg2)"`, | ||
); | ||
expect(transformInput("a.b?.(arg1, arg2)", "c")).toMatchInlineSnapshot( | ||
`"a.b?.call(c, arg1, arg2)"`, | ||
); | ||
expect(transformInput("a[b]?.(arg1, arg2)", "c")).toMatchInlineSnapshot( | ||
`"a[b]?.call(c, arg1, arg2)"`, | ||
); | ||
}); | ||
}); |
19 changes: 19 additions & 0 deletions
19
.../test/fixtures/private-loose/optional-chain-member-optional-call-spread-arguments/exec.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 @@ | ||
class Foo { | ||
#m; | ||
init() { | ||
this.#m = (...args) => args; | ||
} | ||
static test() { | ||
const f = new Foo(); | ||
f.init(); | ||
return f.#m?.(...arguments); | ||
} | ||
|
||
static testNull() { | ||
const f = new Foo(); | ||
return f.#m?.(...arguments); | ||
} | ||
} | ||
|
||
expect(Foo.test(1, 2)).toEqual([1, 2]); | ||
expect(Foo.testNull(1, 2)).toBe(undefined); |
16 changes: 16 additions & 0 deletions
16
...test/fixtures/private-loose/optional-chain-member-optional-call-spread-arguments/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,16 @@ | ||
class Foo { | ||
#m; | ||
init() { | ||
this.#m = (...args) => args; | ||
} | ||
static test() { | ||
const f = new Foo(); | ||
f.init(); | ||
return f.#m?.(...arguments); | ||
} | ||
|
||
static testNull() { | ||
const f = new Foo(); | ||
return f.#m?.(...arguments); | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
.../fixtures/private-loose/optional-chain-member-optional-call-spread-arguments/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,4 @@ | ||
{ | ||
"plugins": [["proposal-class-properties", { "loose": true }]], | ||
"minNodeVersion": "14.0.0" | ||
} |
32 changes: 32 additions & 0 deletions
32
...est/fixtures/private-loose/optional-chain-member-optional-call-spread-arguments/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,32 @@ | ||
function _classPrivateFieldLooseBase(receiver, privateKey) { if (!Object.prototype.hasOwnProperty.call(receiver, privateKey)) { throw new TypeError("attempted to use private field on non-instance"); } return receiver; } | ||
|
||
var id = 0; | ||
|
||
function _classPrivateFieldLooseKey(name) { return "__private_" + id++ + "_" + name; } | ||
|
||
var _m = _classPrivateFieldLooseKey("m"); | ||
|
||
class Foo { | ||
constructor() { | ||
Object.defineProperty(this, _m, { | ||
writable: true, | ||
value: void 0 | ||
}); | ||
} | ||
|
||
init() { | ||
_classPrivateFieldLooseBase(this, _m)[_m] = (...args) => args; | ||
} | ||
|
||
static test() { | ||
const f = new Foo(); | ||
f.init(); | ||
return _classPrivateFieldLooseBase(f, _m)[_m]?.(...arguments); | ||
} | ||
|
||
static testNull() { | ||
const f = new Foo(); | ||
return _classPrivateFieldLooseBase(f, _m)[_m]?.(...arguments); | ||
} | ||
|
||
} |
19 changes: 19 additions & 0 deletions
19
...erties/test/fixtures/private/optional-chain-member-optional-call-spread-arguments/exec.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 @@ | ||
class Foo { | ||
#m; | ||
init() { | ||
this.#m = (...args) => args; | ||
} | ||
static test() { | ||
const f = new Foo(); | ||
f.init(); | ||
return f.#m?.(...arguments); | ||
} | ||
|
||
static testNull() { | ||
const f = new Foo(); | ||
return f.#m?.(...arguments); | ||
} | ||
} | ||
|
||
expect(Foo.test(1, 2)).toEqual([1, 2]); | ||
expect(Foo.testNull(1, 2)).toBe(undefined); |
16 changes: 16 additions & 0 deletions
16
...rties/test/fixtures/private/optional-chain-member-optional-call-spread-arguments/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,16 @@ | ||
class Foo { | ||
#m; | ||
init() { | ||
this.#m = (...args) => args; | ||
} | ||
static test() { | ||
const f = new Foo(); | ||
f.init(); | ||
return f.#m?.(...arguments); | ||
} | ||
|
||
static testNull() { | ||
const f = new Foo(); | ||
return f.#m?.(...arguments); | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
...s/test/fixtures/private/optional-chain-member-optional-call-spread-arguments/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,4 @@ | ||
{ | ||
"plugins": ["proposal-class-properties"], | ||
"minNodeVersion": "14.0.0" | ||
} |
30 changes: 30 additions & 0 deletions
30
...ties/test/fixtures/private/optional-chain-member-optional-call-spread-arguments/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,30 @@ | ||
function _classPrivateFieldGet(receiver, privateMap) { var descriptor = privateMap.get(receiver); if (!descriptor) { throw new TypeError("attempted to get private field on non-instance"); } if (descriptor.get) { return descriptor.get.call(receiver); } return descriptor.value; } | ||
|
||
function _classPrivateFieldSet(receiver, privateMap, value) { var descriptor = privateMap.get(receiver); if (!descriptor) { throw new TypeError("attempted to set private field on non-instance"); } if (descriptor.set) { descriptor.set.call(receiver, value); } else { if (!descriptor.writable) { throw new TypeError("attempted to set read only private field"); } descriptor.value = value; } return value; } | ||
|
||
var _m = new WeakMap(); | ||
|
||
class Foo { | ||
constructor() { | ||
_m.set(this, { | ||
writable: true, | ||
value: void 0 | ||
}); | ||
} | ||
|
||
init() { | ||
_classPrivateFieldSet(this, _m, (...args) => args); | ||
} | ||
|
||
static test() { | ||
const f = new Foo(); | ||
f.init(); | ||
return _classPrivateFieldGet(f, _m)?.apply(f, arguments); | ||
} | ||
|
||
static testNull() { | ||
const f = new Foo(); | ||
return _classPrivateFieldGet(f, _m)?.apply(f, arguments); | ||
} | ||
|
||
} |
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