| 1 | import { parse } from "@babel/parser"; |
| 2 | import * as t from "@babel/types"; |
| 3 | |
| 4 | import _generator from "@babel/generator"; |
| 5 | import _optimizeCallExpression from "../lib/index.js"; |
| 6 | const generator = _generator.default || _generator; |
| 7 | const optimizeCallExpression = |
| 8 | _optimizeCallExpression.default || _optimizeCallExpression; |
| 9 | |
| 10 | function transformInput(input, thisIdentifier) { |
| 11 | const ast = parse(input); |
| 12 | const callExpression = ast.program.body[0].expression; |
| 13 | return generator( |
| 14 | optimizeCallExpression( |
| 15 | callExpression.callee, |
| 16 | thisIdentifier |
| 17 | ? t.identifier(thisIdentifier) |
| 18 | : callExpression.callee.object, |
| 19 | callExpression.arguments, |
| 20 | callExpression.type === "OptionalCallExpression", |
| 21 | ), |
| 22 | ).code; |
| 23 | } |
| 24 | |
| 25 | describe("@babel/helper-optimise-call-expression", () => { |
| 26 | test("optimizeCallExpression should work when thisNode is implied from callee", () => { |
| 27 | expect(transformInput("a.b(...arguments)")).toMatchInlineSnapshot( |
| 28 | `"a.b.apply(a, arguments)"`, |
| 29 | ); |
| 30 | expect(transformInput("a[b](...arguments)")).toMatchInlineSnapshot( |
| 31 | `"a[b].apply(a, arguments)"`, |
| 32 | ); |
| 33 | expect(transformInput("a.b?.(...arguments)")).toMatchInlineSnapshot( |
| 34 | `"a.b?.apply(a, arguments)"`, |
| 35 | ); |
| 36 | expect(transformInput("a[b]?.(...arguments)")).toMatchInlineSnapshot( |
| 37 | `"a[b]?.apply(a, arguments)"`, |
| 38 | ); |
| 39 | |
| 40 | expect(transformInput("a.b(...args)")).toMatchInlineSnapshot( |
| 41 | `"a.b.call(a, ...args)"`, |
| 42 | ); |
| 43 | expect(transformInput("a[b](...args)")).toMatchInlineSnapshot( |
| 44 | `"a[b].call(a, ...args)"`, |
| 45 | ); |
| 46 | expect(transformInput("a.b?.(...args)")).toMatchInlineSnapshot( |
| 47 | `"a.b?.call(a, ...args)"`, |
| 48 | ); |
| 49 | expect(transformInput("a[b]?.(...args)")).toMatchInlineSnapshot( |
| 50 | `"a[b]?.call(a, ...args)"`, |
| 51 | ); |
| 52 | |
| 53 | expect(transformInput("a.b(arg1, arg2)")).toMatchInlineSnapshot( |
| 54 | `"a.b.call(a, arg1, arg2)"`, |
| 55 | ); |
| 56 | expect(transformInput("a[b](arg1, arg2)")).toMatchInlineSnapshot( |
| 57 | `"a[b].call(a, arg1, arg2)"`, |
| 58 | ); |
| 59 | expect(transformInput("a.b?.(arg1, arg2)")).toMatchInlineSnapshot( |
| 60 | `"a.b?.call(a, arg1, arg2)"`, |
| 61 | ); |
| 62 | expect(transformInput("a[b]?.(arg1, arg2)")).toMatchInlineSnapshot( |
| 63 | `"a[b]?.call(a, arg1, arg2)"`, |
| 64 | ); |
| 65 | }); |
| 66 | |
| 67 | test("optimizeCallExpression should work when thisNode is provided", () => { |
| 68 | expect(transformInput("a.b(...arguments)", "c")).toMatchInlineSnapshot( |
| 69 | `"a.b.apply(c, arguments)"`, |
| 70 | ); |
| 71 | expect(transformInput("a[b](...arguments)", "c")).toMatchInlineSnapshot( |
| 72 | `"a[b].apply(c, arguments)"`, |
| 73 | ); |
| 74 | expect(transformInput("a.b?.(...arguments)", "c")).toMatchInlineSnapshot( |
| 75 | `"a.b?.apply(c, arguments)"`, |
| 76 | ); |
| 77 | expect(transformInput("a[b]?.(...arguments)", "c")).toMatchInlineSnapshot( |
| 78 | `"a[b]?.apply(c, arguments)"`, |
| 79 | ); |
| 80 | |
| 81 | expect(transformInput("a.b(...args)", "c")).toMatchInlineSnapshot( |
| 82 | `"a.b.call(c, ...args)"`, |
| 83 | ); |
| 84 | expect(transformInput("a[b](...args)", "c")).toMatchInlineSnapshot( |
| 85 | `"a[b].call(c, ...args)"`, |
| 86 | ); |
| 87 | expect(transformInput("a.b?.(...args)", "c")).toMatchInlineSnapshot( |
| 88 | `"a.b?.call(c, ...args)"`, |
| 89 | ); |
| 90 | expect(transformInput("a[b]?.(...args)", "c")).toMatchInlineSnapshot( |
| 91 | `"a[b]?.call(c, ...args)"`, |
| 92 | ); |
| 93 | |
| 94 | expect(transformInput("a.b(arg1, arg2)", "c")).toMatchInlineSnapshot( |
| 95 | `"a.b.call(c, arg1, arg2)"`, |
| 96 | ); |
| 97 | expect(transformInput("a[b](arg1, arg2)", "c")).toMatchInlineSnapshot( |
| 98 | `"a[b].call(c, arg1, arg2)"`, |
| 99 | ); |
| 100 | expect(transformInput("a.b?.(arg1, arg2)", "c")).toMatchInlineSnapshot( |
| 101 | `"a.b?.call(c, arg1, arg2)"`, |
| 102 | ); |
| 103 | expect(transformInput("a[b]?.(arg1, arg2)", "c")).toMatchInlineSnapshot( |
| 104 | `"a[b]?.call(c, arg1, arg2)"`, |
| 105 | ); |
| 106 | }); |
| 107 | }); |