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), "/", ); 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 "/at.js"; __at (/* isOptionalObject */false,foo,-1)" `); expect(transform("foo?.at(-1)")).toMatchInlineSnapshot(` "import __at from "/at.js"; __at (/* isOptionalObject */true,foo,-1)" `); expect(transform("foo?.bar.baz.at(-1)")).toMatchInlineSnapshot(` "import __at from "/at.js"; __at (/* isOptionalObject */true,foo?.bar.baz,-1)" `); expect(transform("foo.at(-1)?.bar")).toMatchInlineSnapshot(` "import __at from "/at.js"; __at (/* isOptionalObject */false,foo,-1)?.bar" `); // Optional call not supported expect(transform("foo.at?.(-1)")).toMatchInlineSnapshot(`"foo.at?.(-1)"`); }); test("String#replaceAll", () => { expect(transform("foo.replaceAll('a', 'b')")).toMatchInlineSnapshot(` "import __stringReplaceAll from "/string-replace-all.js"; __stringReplaceAll(/* isOptionalObject */false,foo,'a','b')" `); }); test("Array#findLast", () => { expect(transform("foo.findLast(callback)")).toMatchInlineSnapshot(` "import __arrayFindLast from "/array-find-last.js"; __arrayFindLast(/* isOptionalObject */false,foo,callback)" `); expect(transform("foo?.findLast(callback)")).toMatchInlineSnapshot(` "import __arrayFindLast from "/array-find-last.js"; __arrayFindLast(/* isOptionalObject */true,foo,callback)" `); // Not supported expect( transform("foo.findLast(callback, thisArgument)"), ).toMatchInlineSnapshot(`"foo.findLast(callback, thisArgument)"`); }); test("Array#findLastIndex", () => { expect(transform("foo.findLastIndex(callback)")).toMatchInlineSnapshot(` "import __arrayFindLastIndex from "/array-find-last-index.js"; __arrayFindLastIndex(/* isOptionalObject */false,foo,callback)" `); expect(transform("foo?.findLastIndex(callback)")).toMatchInlineSnapshot(` "import __arrayFindLastIndex from "/array-find-last-index.js"; __arrayFindLastIndex(/* isOptionalObject */true,foo,callback)" `); // Not supported expect( transform("foo.findLastIndex(callback, thisArgument)"), ).toMatchInlineSnapshot(`"foo.findLastIndex(callback, thisArgument)"`); }); test("Array#toReversed", () => { expect(transform("foo.toReversed()")).toMatchInlineSnapshot(` "import __arrayToReversed from "/array-to-reversed.js"; __arrayToReversed(/* isOptionalObject */false,foo)" `); expect(transform("foo?.toReversed()")).toMatchInlineSnapshot(` "import __arrayToReversed from "/array-to-reversed.js"; __arrayToReversed(/* isOptionalObject */true,foo)" `); expect(transform("foo.toReversed(extraArgument)")).toMatchInlineSnapshot( `"foo.toReversed(extraArgument)"`, ); });