Files
test-repo/tests/unit/syntax-transform.js
T
renovate[bot]andfisker 3e4b452b4a chore(deps): update babel to v7.26.2 (#16811)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: fisker <lionkay@gmail.com>
2024-10-31 18:24:42 +08:00

95 lines
2.8 KiB
JavaScript

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)"`);
});