Support optional chaining in .at() transform (#13528)
This commit is contained in:
1 parent
6d4d011252
commit
bb2104dd7c
3 files changed
+83
-9
No files matched your search
@@ -0,0 +1,51 @@
|
||||
/* eslint-disable quotes -- `toMatchInlineSnapshot` uses template literal */
|
||||
import url from "node:url";
|
||||
import path from "node:path";
|
||||
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(false, foo, -1);"
|
||||
`);
|
||||
|
||||
expect(transform("foo?.at(-1)")).toMatchInlineSnapshot(`
|
||||
"import __at from "<SHIMS>/at.js";
|
||||
|
||||
__at(true, foo, -1);"
|
||||
`);
|
||||
|
||||
expect(transform("foo?.bar.baz.at(-1)")).toMatchInlineSnapshot(`
|
||||
"import __at from "<SHIMS>/at.js";
|
||||
|
||||
__at(true, foo?.bar.baz, -1);"
|
||||
`);
|
||||
|
||||
expect(transform("foo.at(-1)?.bar")).toMatchInlineSnapshot(`
|
||||
"import __at from "<SHIMS>/at.js";
|
||||
|
||||
__at(false, foo, -1)?.bar;"
|
||||
`);
|
||||
|
||||
// Don't support optional call
|
||||
expect(transform("foo.at?.(-1)")).toMatchInlineSnapshot(`"foo.at?.(-1)"`);
|
||||
});
|
||||
Reference in new issue
Block a user