From a4202e58d53293d01bc2daefb78010d402721421 Mon Sep 17 00:00:00 2001 From: Perryvw Date: Sun, 6 Oct 2019 18:39:29 +0200 Subject: [PATCH 1/4] Refactored test formatting helper causing some tests to start (correctly) failing --- package-lock.json | 12 +++-- package.json | 1 + test/unit/builtins/array.spec.ts | 68 +++++++++++++++++---------- test/unit/builtins/object.spec.ts | 5 +- test/unit/builtins/string.spec.ts | 23 +++++---- test/unit/builtins/symbol.spec.ts | 4 +- test/unit/destructuring.spec.ts | 4 +- test/unit/functions/functions.spec.ts | 2 +- test/util.ts | 10 ++-- 9 files changed, 73 insertions(+), 56 deletions(-) diff --git a/package-lock.json b/package-lock.json index 8b6468a77..e3b501293 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2484,9 +2484,9 @@ "dev": true }, "handlebars": { - "version": "4.1.2", - "resolved": "https://registry.npmjs.org/handlebars/-/handlebars-4.1.2.tgz", - "integrity": "sha512-nvfrjqvt9xQ8Z/w0ijewdD/vvWDTOweBUm96NTr66Wfvo1mJenBLwcYmPs3TIBP5ruzYGD7Hx/DaM9RmhroGPw==", + "version": "4.4.2", + "resolved": "https://registry.npmjs.org/handlebars/-/handlebars-4.4.2.tgz", + "integrity": "sha512-cIv17+GhL8pHHnRJzGu2wwcthL5sb8uDKBHvZ2Dtu5s1YNt0ljbzKbamnc+gr69y7bzwQiBdr5+hOpRd5pnOdg==", "dev": true, "requires": { "neo-async": "^2.6.0", @@ -2944,6 +2944,12 @@ "handlebars": "^4.1.2" } }, + "javascript-stringify": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/javascript-stringify/-/javascript-stringify-2.0.0.tgz", + "integrity": "sha512-zzK8+ByrzvOL6N92hRewwUKL0wN0TOaIuUjX0Jj8lraxWvr5wHYs2YTjaj2lstF+8qMv5cmPPef47va8NT8lDw==", + "dev": true + }, "jest": { "version": "24.8.0", "resolved": "https://registry.npmjs.org/jest/-/jest-24.8.0.tgz", diff --git a/package.json b/package.json index 768745cb9..eb409eb1a 100644 --- a/package.json +++ b/package.json @@ -49,6 +49,7 @@ "@types/node": "^11.13.14", "@types/resolve": "0.0.8", "fengari": "^0.1.4", + "javascript-stringify": "^2.0.0", "jest": "^24.8.0", "jest-circus": "^24.8.0", "prettier": "^1.18.2", diff --git a/test/unit/builtins/array.spec.ts b/test/unit/builtins/array.spec.ts index b81260516..00d831fdb 100644 --- a/test/unit/builtins/array.spec.ts +++ b/test/unit/builtins/array.spec.ts @@ -256,7 +256,7 @@ test.each([ { array: [0, 2, 4, 8], predicate: "false" }, ])("array.find (%p)", ({ array, predicate }) => { util.testFunction` - const array = ${util.valueToString(array)}; + const array = ${util.formatCode(array)}; return array.find((elem, index, arr) => ${predicate} && arr[index] === elem); `.expectToMatchJsResult(); }); @@ -268,7 +268,7 @@ test.each([ { array: [0, 2, 4, 8], searchElement: 8 }, ])("array.findIndex (%p)", ({ array, searchElement }) => { util.testFunction` - const array = ${util.valueToString(array)}; + const array = ${util.formatCode(array)}; return array.findIndex((elem, index, arr) => elem === ${searchElement} && arr[index] === elem); `.expectToMatchJsResult(); }); @@ -281,7 +281,7 @@ test.each([ { array: [0, 1, 2, 3], func: "x => x+2" }, { array: [0, 1, 2, 3], func: "x => x%2 == 0 ? x + 1 : x - 1" }, ])("array.map (%p)", ({ array, func }) => { - util.testExpression`${util.valueToString(array)}.map(${func})`.expectToMatchJsResult(); + util.testExpression`${util.formatCode(array)}.map(${func})`.expectToMatchJsResult(); }); test.each([ @@ -293,7 +293,7 @@ test.each([ { array: [0, 1, 2, 3], func: "() => true" }, { array: [0, 1, 2, 3], func: "() => false" }, ])("array.filter (%p)", ({ array, func }) => { - util.testExpression`${util.valueToString(array)}.filter(${func})`.expectToMatchJsResult(); + util.testExpression`${util.formatCode(array)}.filter(${func})`.expectToMatchJsResult(); }); test.each([ @@ -302,7 +302,7 @@ test.each([ { array: [false, true, false], func: "x => x" }, { array: [true, true, true], func: "x => x" }, ])("array.every (%p)", ({ array, func }) => { - util.testExpression`${util.valueToString(array)}.every(${func})`.expectToMatchJsResult(); + util.testExpression`${util.formatCode(array)}.every(${func})`.expectToMatchJsResult(); }); test.each([ @@ -311,7 +311,7 @@ test.each([ { array: [false, true, false], func: "x => x" }, { array: [true, true, true], func: "x => x" }, ])("array.some (%p)", ({ array, func }) => { - util.testExpression`${util.valueToString(array)}.some(${func})`.expectToMatchJsResult(); + util.testExpression`${util.formatCode(array)}.some(${func})`.expectToMatchJsResult(); }); test.each([ @@ -324,7 +324,8 @@ test.each([ { array: [0, 1, 2, 3, 4, 5], args: [1, 3] }, { array: [0, 1, 2, 3, 4, 5], args: [3] }, ])("array.slice (%p)", ({ array, args }) => { - util.testExpression`${util.valueToString(array)}.slice(${util.valuesToString(args)})`.expectToMatchJsResult(); + const argumentString = util.formatCode(...args); + util.testExpression`${util.formatCode(array)}.slice(${argumentString})`.expectToMatchJsResult(); }); test.each([ @@ -338,26 +339,37 @@ test.each([ { array: [0, 1, 2, 3], start: -3, deleteCount: 0, newElements: [8, 9] }, { array: [0, 1, 2, 3, 4, 5], start: 5, deleteCount: 9, newElements: [10, 11] }, { array: [0, 1, 2, 3, 4, 5], start: 3, deleteCount: 2, newElements: [3, 4, 5] }, + { array: [0, 1, 2, 3, 4, 5, 6, 7, 8], start: 5, deleteCount: 9, newElements: [10, 11] }, + { array: [0, 1, 2, 3, 4, 5, 6, 7, 8], start: 5, deleteCount: undefined, newElements: [10, 11] }, + // tslint:disable-next-line:no-null-keyword + { array: [0, 1, 2, 3, 4, 5, 6, 7, 8], start: 5, deleteCount: null, newElements: [10, 11] }, // Remove { array: [], start: 1, deleteCount: 1 }, { array: [0, 1, 2, 3], start: 1, deleteCount: 1 }, { array: [0, 1, 2, 3], start: 10, deleteCount: 1 }, - { array: [0, 1, 2, 3], start: 1, deleteCount: undefined }, - { array: [0, 1, 2, 3], start: 4 }, - { array: [0, 1, 2, 3, 4, 5], start: 3 }, - { array: [0, 1, 2, 3, 4, 5], start: -3 }, - { array: [0, 1, 2, 3, 4, 5], start: -2 }, { array: [0, 1, 2, 3, 4, 5], start: 2, deleteCount: 2 }, - { array: [0, 1, 2, 3, 4, 5, 6, 7, 8], start: 5, deleteCount: 9, newElements: [10, 11] }, + { array: [0, 1, 2, 3, 4, 5], start: -3, deleteCount: 2 }, + { array: [0, 1, 2, 3], start: 1, deleteCount: undefined }, + // tslint:disable-next-line:no-null-keyword + { array: [0, 1, 2, 3], start: 1, deleteCount: null }, ])("array.splice (%p)", ({ array, start, deleteCount, newElements = [] }) => { util.testFunction` - const array = ${util.valueToString(array)}; - array.splice(${util.valuesToString([start, deleteCount, ...newElements])}); + const array = ${util.formatCode(array)}; + array.splice(${util.formatCode(start, deleteCount, ...newElements)}); return array; `.expectToMatchJsResult(); }); +test.each([ + { array: [0, 1, 2, 3], start: 4 }, + { array: [0, 1, 2, 3, 4, 5], start: 3 }, + { array: [0, 1, 2, 3, 4, 5], start: -3 }, + { array: [0, 1, 2, 3, 4, 5], start: -2 }, +])("array.splice no delete argument", ({ array, start }) => { + util.testExpression`${util.formatCode(array)}.splice(${start})`.expectToMatchJsResult(); +}); + test.each([ { array: [], args: [[]] }, { array: [1, 2, 3], args: [[]] }, @@ -371,8 +383,8 @@ test.each([ { array: [1, 2, "test"], args: ["test", ["test1", "test2"]] }, ])("array.concat (%p)", ({ array, args }) => { util.testFunction` - const array: any[] = ${util.valueToString(array)}; - return array.concat(${util.valuesToString(args)}); + const array: any[] = ${util.formatCode(array)}; + return array.concat(${util.formatCode(...args)}); `.expectToMatchJsResult(); }); @@ -383,7 +395,11 @@ test.each([ { array: ["test1", "test2"], separator: ";" }, { array: ["test1", "test2"], separator: "" }, ])("array.join (%p)", ({ array, separator }) => { - util.testExpression`${util.valueToString(array)}.join(${util.valueToString(separator)})`.expectToMatchJsResult(); + util.testExpression`${util.formatCode(array)}.join(${util.formatCode(separator)})`.expectToMatchJsResult(); +}); + +test("array.join without separator argument", () => { + util.testExpression`["test1", "test2"].join()`.expectToMatchJsResult(); }); test.each([ @@ -395,13 +411,13 @@ test.each([ { array: ["test1", "test2", "test3"], args: ["test1", -2] }, { array: ["test1", "test2", "test3"], args: ["test1", 12] }, ])("array.indexOf (%p)", ({ array, args }) => { - util.testExpression`${util.valueToString(array)}.indexOf(${util.valuesToString(args)})`.expectToMatchJsResult(); + util.testExpression`${util.formatCode(array)}.indexOf(${util.formatCode(...args)})`.expectToMatchJsResult(); }); test.each([{ args: [1] }, { args: [1, 2, 3] }])("array.push (%p)", ({ args }) => { util.testFunction` const array = [0]; - const value = array.push(${util.valuesToString(args)}); + const value = array.push(${util.formatCode(...args)}); return { array, value }; `.expectToMatchJsResult(); }); @@ -411,7 +427,7 @@ test.each([{ array: [1, 2, 3], expected: [3, 2] }, { array: [1, 2, 3, null], exp "array.pop (%p)", ({ array, expected }) => { util.testFunction` - const array = ${util.valueToString(array)}; + const array = ${util.formatCode(array)}; const value = array.pop(); return [value, array.length]; `.expectToEqual(expected); @@ -422,7 +438,7 @@ test.each([{ array: [1, 2, 3] }, { array: [1, 2, 3, 4] }, { array: [1] }, { arra "array.reverse (%p)", ({ array }) => { util.testFunction` - const array = ${util.valueToString(array)}; + const array = ${util.formatCode(array)}; array.reverse(); return array; `.expectToMatchJsResult(); @@ -431,7 +447,7 @@ test.each([{ array: [1, 2, 3] }, { array: [1, 2, 3, 4] }, { array: [1] }, { arra test.each([{ array: [1, 2, 3] }, { array: [1] }, { array: [] }])("array.shift (%p)", ({ array }) => { util.testFunction` - const array = ${util.valueToString(array)}; + const array = ${util.formatCode(array)}; const value = array.shift(); return { array, value }; `.expectToMatchJsResult(); @@ -444,8 +460,8 @@ test.each([ { array: [], args: [1] }, ])("array.unshift (%p)", ({ array, args }) => { util.testFunction` - const array = ${util.valueToString(array)}; - const value = array.unshift(${util.valuesToString(args)}); + const array = ${util.formatCode(array)}; + const value = array.unshift(${util.formatCode(...args)}); return { array, value }; `.expectToMatchJsResult(); }); @@ -499,7 +515,7 @@ test.each<[[(total: number, currentItem: number, index: number, array: number[]) [[(total, _, index, array) => total + array[index]]], [[(a, b) => a + b]], ])("array.reduce (%p)", args => { - util.testExpression`[1, 3, 5, 7].reduce(${util.valuesToString(args)})`.expectToMatchJsResult(); + util.testExpression`[1, 3, 5, 7].reduce(${util.formatCode(...args)})`.expectToMatchJsResult(); }); test("array.reduce empty undefined initial", () => { diff --git a/test/unit/builtins/object.spec.ts b/test/unit/builtins/object.spec.ts index ca1516254..5e0a973a4 100644 --- a/test/unit/builtins/object.spec.ts +++ b/test/unit/builtins/object.spec.ts @@ -6,9 +6,8 @@ test.each([ { initial: { a: 3 }, args: [{ a: 5 }] }, { initial: { a: 3 }, args: [{ b: 5 }, { c: 7 }] }, ])("Object.assign (%p)", ({ initial, args }) => { - util.testExpression`Object.assign(${util.valueToString(initial)}, ${util.valuesToString( - args - )})`.expectToMatchJsResult(); + const argsString = util.formatCode(...args); + util.testExpression`Object.assign(${util.formatCode(initial)}, ${argsString})`.expectToMatchJsResult(); }); test.each([{}, { abc: 3 }, { abc: 3, def: "xyz" }])("Object.entries (%p)", obj => { diff --git a/test/unit/builtins/string.spec.ts b/test/unit/builtins/string.spec.ts index af5b46762..9e6f73e28 100644 --- a/test/unit/builtins/string.spec.ts +++ b/test/unit/builtins/string.spec.ts @@ -20,7 +20,7 @@ test("Supported lua string function", () => { }); test.each([[], [65], [65, 66], [65, 66, 67]])("String.fromCharCode (%p)", (...args) => { - util.testExpression`String.fromCharCode(${util.valuesToString(args)})`.expectToMatchJsResult(); + util.testExpression`String.fromCharCode(${util.formatCode(...args)})`.expectToMatchJsResult(); }); test.each([ @@ -61,13 +61,13 @@ test.each([ { inp: "hello test", searchValue: "test", replaceValue: (): string => "%a" }, { inp: "aaa", searchValue: "a", replaceValue: "b" }, ])("string.replace (%p)", ({ inp, searchValue, replaceValue }) => { - util.testExpression`"${inp}".replace(${util.valuesToString([searchValue, replaceValue])})`.expectToMatchJsResult(); + util.testExpression`"${inp}".replace(${util.formatCode(searchValue, replaceValue)})`.expectToMatchJsResult(); }); test.each([["", ""], ["hello", "test"], ["hello", "test", "bye"], ["hello", 42], [42, "hello"]])( "string.concat[+] (%p)", (...elements) => { - util.testExpression(elements.map(e => util.valueToString(e)).join(" + ")).expectToMatchJsResult(); + util.testExpression(elements.map(e => util.formatCode(e)).join(" + ")).expectToMatchJsResult(); } ); @@ -77,7 +77,7 @@ test.each([ { str: "hello", args: [] }, { str: "hello", args: ["test", "bye"] }, ])("string.concatFct (%p)", ({ str, args }) => { - util.testExpression`"${str}".concat(${util.valuesToString(args)})`.expectToMatchJsResult(); + util.testExpression`"${str}".concat(${util.formatCode(...args)})`.expectToMatchJsResult(); }); test.each([ @@ -113,7 +113,7 @@ test.each([ { inp: "hello test", args: [1, 2] }, { inp: "hello test", args: [1, 5] }, ])("string.slice (%p)", ({ inp, args }) => { - util.testExpression`"${inp}".slice(${util.valuesToString(args)})`.expectToMatchJsResult(); + util.testExpression`"${inp}".slice(${util.formatCode(...args)})`.expectToMatchJsResult(); }); test.each([ @@ -122,7 +122,7 @@ test.each([ { inp: "hello test", args: [1, 2] }, { inp: "hello test", args: [1, 5] }, ])("string.substring (%p)", ({ inp, args }) => { - util.testExpression`"${inp}".substring(${util.valuesToString(args)})`.expectToMatchJsResult(); + util.testExpression`"${inp}".substring(${util.formatCode(...args)})`.expectToMatchJsResult(); }); test.each([{ inp: "hello test", start: 1, ignored: 0 }, { inp: "hello test", start: 3, ignored: 0, end: 5 }])( @@ -139,7 +139,7 @@ test.each([ { inp: "hello test", args: [1, 2] }, { inp: "hello test", args: [1, 5] }, ])("string.substr (%p)", ({ inp, args }) => { - util.testExpression`"${inp}".substr(${util.valuesToString(args)})`.expectToMatchJsResult(); + util.testExpression`"${inp}".substr(${util.formatCode(...args)})`.expectToMatchJsResult(); }); test.each([{ inp: "hello test", start: 1, ignored: 0 }, { inp: "hello test", start: 3, ignored: 0, end: 2 }])( @@ -207,7 +207,7 @@ test.each<{ inp: string; args: Parameters }>([ { inp: "hello test", args: ["test"] }, { inp: "hello test", args: ["test", 6] }, ])("string.startsWith (%p)", ({ inp, args }) => { - util.testExpression`"${inp}".startsWith(${util.valuesToString(args)})`.expectToMatchJsResult(); + util.testExpression`"${inp}".startsWith(${util.formatCode(...args)})`.expectToMatchJsResult(); }); test.each<{ inp: string; args: Parameters }>([ @@ -216,8 +216,7 @@ test.each<{ inp: string; args: Parameters }>([ { inp: "hello test", args: ["hello"] }, { inp: "hello test", args: ["hello", 5] }, ])("string.endsWith (%p)", ({ inp, args }) => { - const argsString = util.valuesToString(args); - util.testExpression`"${inp}".endsWith(${argsString})`.expectToMatchJsResult(); + util.testExpression`"${inp}".endsWith(${util.formatCode(...args)})`.expectToMatchJsResult(); }); test.each([ @@ -243,11 +242,11 @@ const padCases = [ ]; test.each(padCases)("string.padStart (%p)", ({ inp, args }) => { - util.testExpression`"${inp}".padStart(${util.valuesToString(args)})`.expectToMatchJsResult(); + util.testExpression`"${inp}".padStart(${util.formatCode(...args)})`.expectToMatchJsResult(); }); test.each(padCases)("string.padEnd (%p)", ({ inp, args }) => { - util.testExpression`"${inp}".padEnd(${util.valuesToString(args)})`.expectToMatchJsResult(); + util.testExpression`"${inp}".padEnd(${util.formatCode(...args)})`.expectToMatchJsResult(); }); test.each([ diff --git a/test/unit/builtins/symbol.spec.ts b/test/unit/builtins/symbol.spec.ts index 75da67a59..7faabef84 100644 --- a/test/unit/builtins/symbol.spec.ts +++ b/test/unit/builtins/symbol.spec.ts @@ -1,12 +1,12 @@ import * as util from "../../util"; test.each([undefined, 1, "name"])("symbol.toString() (%p)", description => { - util.testExpression`Symbol(${util.valueToString(description)}).toString()`.expectToMatchJsResult(); + util.testExpression`Symbol(${util.formatCode(description)}).toString()`.expectToMatchJsResult(); }); test.each([undefined, 1, "name"])("symbol.description (%p)", description => { // TODO: Supported since node 11 - util.testExpression`Symbol(${util.valueToString(description)}).description`.expectToEqual(description); + util.testExpression`Symbol(${util.formatCode(description)}).description`.expectToEqual(description); }); test("symbol uniqueness", () => { diff --git a/test/unit/destructuring.spec.ts b/test/unit/destructuring.spec.ts index 35519f299..6572d8b6b 100644 --- a/test/unit/destructuring.spec.ts +++ b/test/unit/destructuring.spec.ts @@ -24,7 +24,7 @@ const testCases = [ { binding: "{ y: [z = true] }", value: { y: [false] } }, { binding: "{ x: [x, y] }", value: { x: ["x", "y"] } }, { binding: "{ x: [{ y }] }", value: { x: [{ y: "y" }] } }, -].map(({ binding, value }) => ({ binding, value: util.valueToString(value) })); +].map(({ binding, value }) => ({ binding, value: util.formatCode(value) })); test.each([ ...testCases, @@ -70,7 +70,7 @@ const assignmentTestCases = [ { binding: "[{ x: obj.prop }]", value: [{ x: true }] }, { binding: "{ obj: { prop: obj.prop } }", value: { obj: { prop: true } } }, { binding: "{ x = true }", value: {} }, - ].map(({ binding, value }) => ({ binding, value: util.valueToString(value) })), + ].map(({ binding, value }) => ({ binding, value: util.formatCode(value) })), { binding: "{ x: { [(3).toString()]: y } }", value: "{ x: { [(3).toString()]: true } }" }, ]; diff --git a/test/unit/functions/functions.spec.ts b/test/unit/functions/functions.spec.ts index 8214e6bfd..f0f6fd6e0 100644 --- a/test/unit/functions/functions.spec.ts +++ b/test/unit/functions/functions.spec.ts @@ -267,7 +267,7 @@ test.each([{ args: ["bar"], expected: "foobar" }, { args: ["baz", "bar"], expect } }; const o = new O(); - return o.method(${util.valuesToString(args)}); + return o.method(${util.formatCode(...args)}); `.expectToEqual(expected); } ); diff --git a/test/util.ts b/test/util.ts index 8abc0dbc6..33e48b16f 100644 --- a/test/util.ts +++ b/test/util.ts @@ -5,6 +5,7 @@ import * as prettyFormat from "pretty-format"; import * as ts from "typescript"; import * as vm from "vm"; import * as tstl from "../src"; +import { stringify } from "javascript-stringify"; export * from "./legacy-utils"; @@ -49,12 +50,7 @@ export function expectToBeDefined(subject: T | null | undefined): subject is return true; // If this was false the expect would have thrown an error } -export const valueToString = (value: unknown) => - (typeof value === "number" && (!Number.isFinite(value) || Number.isNaN(value))) || typeof value === "function" - ? String(value) - : JSON.stringify(value); - -export const valuesToString = (values: unknown[]) => values.map(valueToString).join(", "); +export const formatCode = (...values: unknown[]) => values.map(e => stringify(e)).join(", "); export function testEachVersion( name: string | undefined, @@ -451,7 +447,7 @@ const createTestBuilderFactory = ( } else { let [template, ...substitutions] = args; if (serializeSubstitutions) { - substitutions = substitutions.map(valueToString); + substitutions = substitutions.map(s => formatCode(s)); } tsCode = template From 06d907835b3482808d0f8d0d09f60dbab2cd31e7 Mon Sep 17 00:00:00 2001 From: Perryvw Date: Sun, 6 Oct 2019 18:53:49 +0200 Subject: [PATCH 2/4] Fix array.join with unidentified argument --- src/LuaTransformer.ts | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/LuaTransformer.ts b/src/LuaTransformer.ts index cff5f45df..3a954525c 100644 --- a/src/LuaTransformer.ts +++ b/src/LuaTransformer.ts @@ -5134,8 +5134,18 @@ export class LuaTransformer { case "splice": return this.transformLuaLibFunction(LuaLibFeature.ArraySplice, node, caller, ...params); case "join": + const defaultSeparatorLiteral = tstl.createStringLiteral(","); const parameters = - node.arguments.length === 0 ? [caller, tstl.createStringLiteral(",")] : [caller].concat(params); + node.arguments.length === 0 + ? [caller, defaultSeparatorLiteral] + : [ + caller, + tstl.createBinaryExpression( + params[0], + defaultSeparatorLiteral, + tstl.SyntaxKind.OrOperator + ), + ]; return tstl.createCallExpression( tstl.createTableIndexExpression(tstl.createIdentifier("table"), tstl.createStringLiteral("concat")), From 8e5cb6a87791e725d5f24c0bfb5247d665c90476 Mon Sep 17 00:00:00 2001 From: Perryvw Date: Sun, 6 Oct 2019 21:02:19 +0200 Subject: [PATCH 3/4] Fixed splice implementation --- src/lualib/ArraySplice.ts | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/lualib/ArraySplice.ts b/src/lualib/ArraySplice.ts index bb2527583..862422e39 100644 --- a/src/lualib/ArraySplice.ts +++ b/src/lualib/ArraySplice.ts @@ -1,6 +1,11 @@ -function __TS__ArraySplice(this: void, list: T[], start: number, deleteCount: number, ...items: T[]): T[] { +// https://www.ecma-international.org/ecma-262/9.0/index.html#sec-array.prototype.splice +function __TS__ArraySplice(this: void, list: T[], ...args: Vararg): T[] { const len = list.length; + const actualArgumentCount = select("#", ...args); + const start = select(1, ...args) as number; + const deleteCount = select(2, ...args) as number; + let actualStart: number; if (start < 0) { @@ -9,16 +14,18 @@ function __TS__ArraySplice(this: void, list: T[], start: number, deleteCount: actualStart = Math.min(start, len); } - const itemCount = items.length; + const itemCount = Math.max(actualArgumentCount - 2, 0); let actualDeleteCount: number; - if (!start) { + if (actualArgumentCount === 0) { + // ECMA-spec line 5: if number of actual arguments is 0 actualDeleteCount = 0; - } else if (!deleteCount) { + } else if (actualArgumentCount === 1) { + // ECMA-spec line 6: if number of actual arguments is 1 actualDeleteCount = len - actualStart; } else { - actualDeleteCount = Math.min(Math.max(deleteCount, 0), len - actualStart); + actualDeleteCount = Math.min(Math.max(deleteCount || 0, 0), len - actualStart); } const out: T[] = []; @@ -59,8 +66,8 @@ function __TS__ArraySplice(this: void, list: T[], start: number, deleteCount: } let j = actualStart; - for (const e of items) { - list[j] = e; + for (const i of forRange(3, actualArgumentCount)) { + list[j] = select(i, ...args) as T; j++; } From bdb3e0f603ed54071907607e6341d8443f71eab5 Mon Sep 17 00:00:00 2001 From: Perryvw Date: Wed, 9 Oct 2019 21:39:31 +0200 Subject: [PATCH 4/4] Improved code from PR feedback --- src/LuaTransformer.ts | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/src/LuaTransformer.ts b/src/LuaTransformer.ts index 3a954525c..932b9c594 100644 --- a/src/LuaTransformer.ts +++ b/src/LuaTransformer.ts @@ -5135,17 +5135,12 @@ export class LuaTransformer { return this.transformLuaLibFunction(LuaLibFeature.ArraySplice, node, caller, ...params); case "join": const defaultSeparatorLiteral = tstl.createStringLiteral(","); - const parameters = + const parameters = [ + caller, node.arguments.length === 0 - ? [caller, defaultSeparatorLiteral] - : [ - caller, - tstl.createBinaryExpression( - params[0], - defaultSeparatorLiteral, - tstl.SyntaxKind.OrOperator - ), - ]; + ? defaultSeparatorLiteral + : tstl.createBinaryExpression(params[0], defaultSeparatorLiteral, tstl.SyntaxKind.OrOperator), + ]; return tstl.createCallExpression( tstl.createTableIndexExpression(tstl.createIdentifier("table"), tstl.createStringLiteral("concat")),