forked from OKEAMAH/prettier
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsyntax-transform.js
More file actions
95 lines (73 loc) · 2.87 KB
/
syntax-transform.js
File metadata and controls
95 lines (73 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import path from "node:path";
import url from "node:url";
import transformCode from "../../scripts/build/transform/index.js";
const file = url.fileURLToPath(
new URL("../../src/__dummy.js", import.meta.url),
);
const shimsDirectory = url.fileURLToPath(
new URL("../../scripts/build/shims", import.meta.url),
);
const transform = (code) =>
transformCode(code, file).replaceAll(
JSON.stringify(shimsDirectory + path.sep).slice(1, -1),
"<SHIMS>/",
);
test("Object.hasOwn", () => {
expect(transform("Object.hasOwn(foo, bar)")).toMatchInlineSnapshot(
`"Object.prototype.hasOwnProperty.call(foo, bar);"`,
);
});
test(".at", () => {
expect(transform("foo.at(-1)")).toMatchInlineSnapshot(`
"import __at from "<SHIMS>/at.js";
__at( /* isOptionalObject */false, foo, -1);"
`);
expect(transform("foo?.at(-1)")).toMatchInlineSnapshot(`
"import __at from "<SHIMS>/at.js";
__at( /* isOptionalObject */true, foo, -1);"
`);
expect(transform("foo?.bar.baz.at(-1)")).toMatchInlineSnapshot(`
"import __at from "<SHIMS>/at.js";
__at( /* isOptionalObject */true, foo?.bar.baz, -1);"
`);
expect(transform("foo.at(-1)?.bar")).toMatchInlineSnapshot(`
"import __at from "<SHIMS>/at.js";
__at( /* isOptionalObject */false, foo, -1)?.bar;"
`);
// Don't support optional call
expect(transform("foo.at?.(-1)")).toMatchInlineSnapshot(`"foo.at?.(-1)"`);
});
test("String#replaceAll", () => {
expect(transform("foo.replaceAll('a', 'b')")).toMatchInlineSnapshot(`
"import __stringReplaceAll from "<SHIMS>/string-replace-all.js";
__stringReplaceAll( /* isOptionalObject */false, foo, 'a', 'b');"
`);
});
test("Array#findLast", () => {
expect(transform("foo.findLast(callback)")).toMatchInlineSnapshot(`
"import __arrayFindLast from "<SHIMS>/array-find-last.js";
__arrayFindLast( /* isOptionalObject */false, foo, callback);"
`);
expect(transform("foo?.findLast(callback)")).toMatchInlineSnapshot(`
"import __arrayFindLast from "<SHIMS>/array-find-last.js";
__arrayFindLast( /* isOptionalObject */true, foo, callback);"
`);
// Don't support
expect(
transform("foo.findLast(callback, thisArgument)"),
).toMatchInlineSnapshot(`"foo.findLast(callback, thisArgument)"`);
});
test("Array#findLastIndex", () => {
expect(transform("foo.findLastIndex(callback)")).toMatchInlineSnapshot(`
"import __arrayFindLastIndex from "<SHIMS>/array-find-last-index.js";
__arrayFindLastIndex( /* isOptionalObject */false, foo, callback);"
`);
expect(transform("foo?.findLastIndex(callback)")).toMatchInlineSnapshot(`
"import __arrayFindLastIndex from "<SHIMS>/array-find-last-index.js";
__arrayFindLastIndex( /* isOptionalObject */true, foo, callback);"
`);
// Don't support
expect(
transform("foo.findLastIndex(callback, thisArgument)"),
).toMatchInlineSnapshot(`"foo.findLastIndex(callback, thisArgument)"`);
});