Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
9 changes: 7 additions & 2 deletions src/LuaTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5134,8 +5134,13 @@ export class LuaTransformer {
case "splice":
return this.transformLuaLibFunction(LuaLibFeature.ArraySplice, node, caller, ...params);
case "join":
const parameters =
node.arguments.length === 0 ? [caller, tstl.createStringLiteral(",")] : [caller].concat(params);
const defaultSeparatorLiteral = tstl.createStringLiteral(",");
const parameters = [
caller,
node.arguments.length === 0
? defaultSeparatorLiteral
: tstl.createBinaryExpression(params[0], defaultSeparatorLiteral, tstl.SyntaxKind.OrOperator),
];

return tstl.createCallExpression(
tstl.createTableIndexExpression(tstl.createIdentifier("table"), tstl.createStringLiteral("concat")),
Expand Down
21 changes: 14 additions & 7 deletions src/lualib/ArraySplice.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
function __TS__ArraySplice<T>(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<T>(this: void, list: T[], ...args: Vararg<unknown>): 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) {
Expand All @@ -9,16 +14,18 @@ function __TS__ArraySplice<T>(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[] = [];
Expand Down Expand Up @@ -59,8 +66,8 @@ function __TS__ArraySplice<T>(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++;
}

Expand Down
68 changes: 42 additions & 26 deletions test/unit/builtins/array.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand All @@ -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();
});
Expand All @@ -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([
Expand All @@ -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([
Expand All @@ -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([
Expand All @@ -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([
Expand All @@ -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([
Expand All @@ -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: [[]] },
Expand All @@ -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();
});

Expand All @@ -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([
Expand All @@ -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();
});
Expand All @@ -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);
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
});
Expand Down Expand Up @@ -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", () => {
Expand Down
5 changes: 2 additions & 3 deletions test/unit/builtins/object.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down
23 changes: 11 additions & 12 deletions test/unit/builtins/string.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down Expand Up @@ -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();
}
);

Expand All @@ -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([
Expand Down Expand Up @@ -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([
Expand All @@ -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 }])(
Expand All @@ -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 }])(
Expand Down Expand Up @@ -207,7 +207,7 @@ test.each<{ inp: string; args: Parameters<string["startsWith"]> }>([
{ 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<string["endsWith"]> }>([
Expand All @@ -216,8 +216,7 @@ test.each<{ inp: string; args: Parameters<string["endsWith"]> }>([
{ 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([
Expand All @@ -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([
Expand Down
Loading