| 1 | import { parse } from "@babel/parser"; |
| 2 | |
| 3 | import _traverse from "../../lib/index.js"; |
| 4 | const traverse = _traverse.default || _traverse; |
| 5 | |
| 6 | function getPath(code) { |
| 7 | const ast = parse(code, { |
| 8 | plugins: [ |
| 9 | ["decorators", { version: "2021-12", decoratorsBeforeExport: true }], |
| 10 | ["recordAndTuple", { syntaxType: "hash" }], |
| 11 | "decoratorAutoAccessors", |
| 12 | ["pipelineOperator", { proposal: "hack", topicToken: "%" }], |
| 13 | ], |
| 14 | }); |
| 15 | let path; |
| 16 | traverse(ast, { |
| 17 | Program: function (_path) { |
| 18 | path = _path; |
| 19 | _path.stop(); |
| 20 | }, |
| 21 | }); |
| 22 | return path; |
| 23 | } |
| 24 | |
| 25 | describe("isPure() returns true", () => { |
| 26 | it.each([ |
| 27 | "class C { [0]() {} }", |
| 28 | "class C extends class {} {}", |
| 29 | "class C { static accessor x = 1; accessor y = f() }", |
| 30 | "class C { #x = f(); static #y }", |
| 31 | "class C { static target = new.target }", |
| 32 | "class X { get foo() { return 1 } set foo(v) {} }", |
| 33 | "class C { static #p = #p in C }", |
| 34 | ])(`NodePath(%p).get("body.0").isPure() should be true`, input => { |
| 35 | const path = getPath(input).get("body.0"); |
| 36 | expect(path.node).toBeTruthy(); |
| 37 | expect(path.isPure()).toBe(true); |
| 38 | }); |
| 39 | |
| 40 | it.each([ |
| 41 | "({ x: 1, foo() { return 1 } })", |
| 42 | "String.raw`foo`", |
| 43 | `"a" + "b"`, |
| 44 | `[function () {}]`, |
| 45 | `#{ 0: 0, 1n: 1, two: "two"}`, |
| 46 | `#[0, 1n, "2", \`3\`]`, |
| 47 | `[,]`, |
| 48 | `-1 || void 0`, |
| 49 | `null ?? (true && false)`, |
| 50 | `this`, |
| 51 | ])(`NodePath(%p).get("body.0.expression").isPure() should be true`, input => { |
| 52 | const path = getPath(input).get("body.0.expression"); |
| 53 | expect(path.node).toBeTruthy(); |
| 54 | expect(path.isPure()).toBe(true); |
| 55 | }); |
| 56 | |
| 57 | it.each(["let a = 1; `${a}`", `let a = 1; a |> % + %`])( |
| 58 | `NodePath(%p).get("body.1.expression").isPure() should be true`, |
| 59 | input => { |
| 60 | const path = getPath(input).get("body.1.expression"); |
| 61 | expect(path.node).toBeTruthy(); |
| 62 | expect(path.isPure()).toBe(true); |
| 63 | }, |
| 64 | ); |
| 65 | }); |
| 66 | |
| 67 | describe("isPure() returns false", () => { |
| 68 | it.each([ |
| 69 | "@dec() class X {}", |
| 70 | "@dec class C {}; function dec () {}", |
| 71 | "class C { @dec foo() {} }", |
| 72 | "class C { @dec foo }", |
| 73 | "class C { @dec accessor foo = 1 }", |
| 74 | "class C { static {} }", |
| 75 | "class C extends class { [f()] } {}", |
| 76 | ])(`NodePath(%p).get("body.0").isPure() should be false`, input => { |
| 77 | const path = getPath(input).get("body.0"); |
| 78 | expect(path.node).toBeTruthy(); |
| 79 | expect(path.isPure()).toBe(false); |
| 80 | }); |
| 81 | |
| 82 | it.each(["`${a}`", "tagged`foo`"])( |
| 83 | `NodePath(%p).get("body.0.expression").isPure() should be false`, |
| 84 | input => { |
| 85 | const path = getPath(input).get("body.0.expression"); |
| 86 | expect(path.node).toBeTruthy(); |
| 87 | expect(path.isPure()).toBe(false); |
| 88 | }, |
| 89 | ); |
| 90 | |
| 91 | it.each(["let a = 1; `${a++}`"])( |
| 92 | `NodePath(%p).get("body.1.expression").isPure() should be false`, |
| 93 | input => { |
| 94 | const path = getPath(input).get("body.1.expression"); |
| 95 | expect(path.node).toBeTruthy(); |
| 96 | expect(path.isPure()).toBe(false); |
| 97 | }, |
| 98 | ); |
| 99 | }); |
| 100 | |
| 101 | describe("isPure(constantsOnly: true) returns false", () => { |
| 102 | it.each(["x", "1 + x", "({ [x]: 0 })", "(class { static x = x })"])( |
| 103 | `NodePath(%p).get("body.0.expression").isPure(/* constantsOnly */true) should be false`, |
| 104 | input => { |
| 105 | const path = getPath(input).get("body.0.expression"); |
| 106 | expect(path.node).toBeTruthy(); |
| 107 | expect(path.isPure(true)).toBe(false); |
| 108 | }, |
| 109 | ); |
| 110 | }); |