X Tutup
Skip to content
Closed
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
5 changes: 4 additions & 1 deletion src/LuaTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5132,8 +5132,11 @@ export class LuaTransformer {
case "splice":
return this.transformLuaLibFunction(LuaLibFeature.ArraySplice, node, caller, ...params);
case "join":
const commaLiteral = tstl.createStringLiteral(",");
const parameters =
node.arguments.length === 0 ? [caller, tstl.createStringLiteral(",")] : [caller].concat(params);
node.arguments.length === 0
? [caller, commaLiteral]
: [caller, tstl.createBinaryExpression(params[0], commaLiteral, tstl.SyntaxKind.OrOperator)];

return tstl.createCallExpression(
tstl.createTableIndexExpression(tstl.createIdentifier("table"), tstl.createStringLiteral("concat")),
Expand Down
4 changes: 3 additions & 1 deletion test/unit/builtins/array.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,9 @@ test.each([
])("array.splice (%p)", ({ array, start, deleteCount, newElements = [] }) => {
util.testFunction`
const array = ${util.valueToString(array)};
array.splice(${util.valuesToString([start, deleteCount, ...newElements])});
array.splice(${util.valuesToString(
deleteCount ? [start, deleteCount, ...newElements] : [start, ...newElements]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like our implementation is actually incorrect:

[1, 2, 3].splice(1) // => [2, 3]
[1, 2, 3].splice(1, 0) // => []
[1, 2, 3].splice(1, undefined) // => js [], lua [2, 3]

)});
return array;
`.expectToMatchJsResult();
});
Expand Down
15 changes: 11 additions & 4 deletions test/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,17 @@ export function expectToBeDefined<T>(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 function valueToString(value: unknown): string {
if (
(typeof value === "number" && (!Number.isFinite(value) || Number.isNaN(value))) ||
typeof value === "function" ||
value === undefined
) {
return String(value);
} else {
return JSON.stringify(value);
}
}

export const valuesToString = (values: unknown[]) => values.map(valueToString).join(", ");

Expand Down
X Tutup