Skip to content
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

Fix: correctly transform this.#m?.(...arguments) #12350

Merged
merged 3 commits into from Nov 16, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/babel-helper-optimise-call-expression/package.json
Expand Up @@ -14,5 +14,9 @@
"main": "lib/index.js",
"dependencies": {
"@babel/types": "workspace:^7.10.4"
},
"devDependencies": {
"@babel/generator": "workspace:*",
"@babel/parser": "workspace:*"
}
}
30 changes: 28 additions & 2 deletions packages/babel-helper-optimise-call-expression/src/index.js
@@ -1,24 +1,50 @@
import * as t from "@babel/types";

export default function (callee, thisNode, args, optional) {
/**
* A helper function that generates a new call expression with given thisNode.
It will also optimize `(...arguments)` to `.apply(arguments)`
*
* @export
* @param {Node} callee The callee of call expression
* @param {Node} thisNode The desired this of call expression
* @param {Node[]} args The arguments of call expression
* @param {boolean} optional Whether the call expression is optional
* @returns {CallExpression | OptionalCallExpression} The generated new call expression
*/
export default function (
callee: Node,
thisNode: Node,
args: Node[],
optional: boolean,
): CallExpression | OptionalCallExpression {
if (
args.length === 1 &&
t.isSpreadElement(args[0]) &&
t.isIdentifier(args[0].argument, { name: "arguments" })
) {
// eg. super(...arguments);
// a.b?.(...arguments);
if (optional) {
return t.optionalCallExpression(
t.optionalMemberExpression(callee, t.identifier("apply"), false, true),
[thisNode, args[0].argument],
false,
);
}
// a.b(...arguments);
return t.callExpression(t.memberExpression(callee, t.identifier("apply")), [
thisNode,
args[0].argument,
]);
} else {
// a.b?.(arg1, arg2)
if (optional) {
return t.optionalCallExpression(
t.optionalMemberExpression(callee, t.identifier("call"), false, true),
[thisNode, ...args],
false,
);
}
// a.b(arg1, arg2)
return t.callExpression(t.memberExpression(callee, t.identifier("call")), [
thisNode,
...args,
Expand Down
103 changes: 103 additions & 0 deletions packages/babel-helper-optimise-call-expression/test/index.js
@@ -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)"`,
JLHwung marked this conversation as resolved.
Show resolved Hide resolved
);
});
});
2 changes: 2 additions & 0 deletions yarn.lock
Expand Up @@ -610,6 +610,8 @@ __metadata:
version: 0.0.0-use.local
resolution: "@babel/helper-optimise-call-expression@workspace:packages/babel-helper-optimise-call-expression"
dependencies:
"@babel/generator": "workspace:*"
"@babel/parser": "workspace:*"
"@babel/types": "workspace:^7.10.4"
languageName: unknown
linkType: soft
Expand Down