2022-09-26 22:54:17 +08:00
|
|
|
import path from "node:path";
|
2023-12-17 19:34:12 +00:00
|
|
|
import url from "node:url";
|
2022-09-26 22:54:17 +08:00
|
|
|
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(
|
2024-10-31 00:05:28 +08:00
|
|
|
`"Object.prototype.hasOwnProperty.call(foo,bar)"`,
|
2022-09-26 22:54:17 +08:00
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test(".at", () => {
|
|
|
|
|
expect(transform("foo.at(-1)")).toMatchInlineSnapshot(`
|
2025-10-28 02:51:50 +08:00
|
|
|
"import __at from "<SHIMS>/method-at.js";
|
2022-09-26 22:54:17 +08:00
|
|
|
|
2025-10-28 02:51:50 +08:00
|
|
|
__at (/* OPTIONAL_OBJECT: false */0,foo,-1)"
|
2022-09-26 22:54:17 +08:00
|
|
|
`);
|
|
|
|
|
|
|
|
|
|
expect(transform("foo?.at(-1)")).toMatchInlineSnapshot(`
|
2025-10-28 02:51:50 +08:00
|
|
|
"import __at from "<SHIMS>/method-at.js";
|
2022-09-26 22:54:17 +08:00
|
|
|
|
2025-10-28 02:51:50 +08:00
|
|
|
__at (/* OPTIONAL_OBJECT: true */1,foo,-1)"
|
2022-09-26 22:54:17 +08:00
|
|
|
`);
|
|
|
|
|
|
|
|
|
|
expect(transform("foo?.bar.baz.at(-1)")).toMatchInlineSnapshot(`
|
2025-10-28 02:51:50 +08:00
|
|
|
"import __at from "<SHIMS>/method-at.js";
|
2022-09-26 22:54:17 +08:00
|
|
|
|
2025-10-28 02:51:50 +08:00
|
|
|
__at (/* OPTIONAL_OBJECT: true */1,foo?.bar.baz,-1)"
|
2022-09-26 22:54:17 +08:00
|
|
|
`);
|
|
|
|
|
|
|
|
|
|
expect(transform("foo.at(-1)?.bar")).toMatchInlineSnapshot(`
|
2025-10-28 02:51:50 +08:00
|
|
|
"import __at from "<SHIMS>/method-at.js";
|
2022-09-26 22:54:17 +08:00
|
|
|
|
2025-10-28 02:51:50 +08:00
|
|
|
__at (/* OPTIONAL_OBJECT: false */0,foo,-1)?.bar"
|
2022-09-26 22:54:17 +08:00
|
|
|
`);
|
|
|
|
|
|
2025-06-05 17:23:49 +08:00
|
|
|
// Optional call not supported
|
2022-09-26 22:54:17 +08:00
|
|
|
expect(transform("foo.at?.(-1)")).toMatchInlineSnapshot(`"foo.at?.(-1)"`);
|
|
|
|
|
});
|
2022-11-06 19:55:24 +08:00
|
|
|
|
2023-12-28 22:44:13 +08:00
|
|
|
test("String#replaceAll", () => {
|
2022-11-06 19:55:24 +08:00
|
|
|
expect(transform("foo.replaceAll('a', 'b')")).toMatchInlineSnapshot(`
|
2025-10-28 02:51:50 +08:00
|
|
|
"import __replaceAll from "<SHIMS>/method-replace-all.js";
|
2022-11-06 19:55:24 +08:00
|
|
|
|
2025-10-28 02:51:50 +08:00
|
|
|
__replaceAll (/* OPTIONAL_OBJECT: false */0,foo,'a','b')"
|
2022-11-06 19:55:24 +08:00
|
|
|
`);
|
|
|
|
|
});
|
2023-12-28 22:44:13 +08:00
|
|
|
|
|
|
|
|
test("Array#findLast", () => {
|
|
|
|
|
expect(transform("foo.findLast(callback)")).toMatchInlineSnapshot(`
|
2025-10-28 02:51:50 +08:00
|
|
|
"import __findLast from "<SHIMS>/method-find-last.js";
|
2023-12-28 22:44:13 +08:00
|
|
|
|
2025-10-28 02:51:50 +08:00
|
|
|
__findLast (/* OPTIONAL_OBJECT: false */0,foo,callback)"
|
2023-12-28 22:44:13 +08:00
|
|
|
`);
|
|
|
|
|
expect(transform("foo?.findLast(callback)")).toMatchInlineSnapshot(`
|
2025-10-28 02:51:50 +08:00
|
|
|
"import __findLast from "<SHIMS>/method-find-last.js";
|
2023-12-28 22:44:13 +08:00
|
|
|
|
2025-10-28 02:51:50 +08:00
|
|
|
__findLast (/* OPTIONAL_OBJECT: true */1,foo,callback)"
|
2023-12-28 22:44:13 +08:00
|
|
|
`);
|
|
|
|
|
|
2025-06-05 17:23:49 +08:00
|
|
|
// Not supported
|
2023-12-28 22:44:13 +08:00
|
|
|
expect(
|
|
|
|
|
transform("foo.findLast(callback, thisArgument)"),
|
|
|
|
|
).toMatchInlineSnapshot(`"foo.findLast(callback, thisArgument)"`);
|
|
|
|
|
});
|
2024-06-18 07:00:16 +08:00
|
|
|
|
|
|
|
|
test("Array#findLastIndex", () => {
|
|
|
|
|
expect(transform("foo.findLastIndex(callback)")).toMatchInlineSnapshot(`
|
2025-10-28 02:51:50 +08:00
|
|
|
"import __findLastIndex from "<SHIMS>/method-find-last-index.js";
|
2024-06-18 07:00:16 +08:00
|
|
|
|
2025-10-28 02:51:50 +08:00
|
|
|
__findLastIndex (/* OPTIONAL_OBJECT: false */0,foo,callback)"
|
2024-06-18 07:00:16 +08:00
|
|
|
`);
|
|
|
|
|
expect(transform("foo?.findLastIndex(callback)")).toMatchInlineSnapshot(`
|
2025-10-28 02:51:50 +08:00
|
|
|
"import __findLastIndex from "<SHIMS>/method-find-last-index.js";
|
2024-06-18 07:00:16 +08:00
|
|
|
|
2025-10-28 02:51:50 +08:00
|
|
|
__findLastIndex (/* OPTIONAL_OBJECT: true */1,foo,callback)"
|
2024-06-18 07:00:16 +08:00
|
|
|
`);
|
|
|
|
|
|
2025-06-05 17:23:49 +08:00
|
|
|
// Not supported
|
2024-06-18 07:00:16 +08:00
|
|
|
expect(
|
|
|
|
|
transform("foo.findLastIndex(callback, thisArgument)"),
|
|
|
|
|
).toMatchInlineSnapshot(`"foo.findLastIndex(callback, thisArgument)"`);
|
|
|
|
|
});
|
2025-06-05 17:23:49 +08:00
|
|
|
|
|
|
|
|
test("Array#toReversed", () => {
|
|
|
|
|
expect(transform("foo.toReversed()")).toMatchInlineSnapshot(`
|
2025-10-28 02:51:50 +08:00
|
|
|
"import __toReversed from "<SHIMS>/method-to-reversed.js";
|
2025-06-05 17:23:49 +08:00
|
|
|
|
2025-10-28 02:51:50 +08:00
|
|
|
__toReversed (/* OPTIONAL_OBJECT: false */0,foo)"
|
2025-06-05 17:23:49 +08:00
|
|
|
`);
|
|
|
|
|
expect(transform("foo?.toReversed()")).toMatchInlineSnapshot(`
|
2025-10-28 02:51:50 +08:00
|
|
|
"import __toReversed from "<SHIMS>/method-to-reversed.js";
|
2025-06-05 17:23:49 +08:00
|
|
|
|
2025-10-28 02:51:50 +08:00
|
|
|
__toReversed (/* OPTIONAL_OBJECT: true */1,foo)"
|
2025-06-05 17:23:49 +08:00
|
|
|
`);
|
|
|
|
|
|
|
|
|
|
expect(transform("foo.toReversed(extraArgument)")).toMatchInlineSnapshot(
|
|
|
|
|
`"foo.toReversed(extraArgument)"`,
|
|
|
|
|
);
|
|
|
|
|
});
|
2025-10-27 23:24:02 +08:00
|
|
|
|
|
|
|
|
test("String.raw", () => {
|
|
|
|
|
expect(transform("String.raw`\\\\\\uINVALID`")).toMatchInlineSnapshot(
|
|
|
|
|
String.raw`""\\\\\\uINVALID""`,
|
|
|
|
|
);
|
|
|
|
|
expect(transform("String.raw`\\uINVALID${'world'}`")).toMatchInlineSnapshot(
|
|
|
|
|
`" \`\\\\uINVALID\${'world'}\`"`,
|
|
|
|
|
);
|
|
|
|
|
});
|