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
6 changes: 3 additions & 3 deletions src/transformation/builtins/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,14 @@ export function transformArrayPrototypeCall(
return transformLuaLibFunction(context, LuaLibFeature.ArrayEntries, node, caller);
case "push":
if (node.arguments.length === 1) {
const param = params[0];
const param = params[0] ?? lua.createNilLiteral();
if (isUnpackCall(param)) {
return transformLuaLibFunction(
context,
LuaLibFeature.ArrayPushArray,
node,
caller,
(param as lua.CallExpression).params[0]
(param as lua.CallExpression).params[0] ?? lua.createNilLiteral()
);
}
if (!lua.isDotsLiteral(param)) {
Expand Down Expand Up @@ -162,7 +162,7 @@ export function transformArrayPrototypeCall(
typeAlwaysHasSomeOfFlags(context, elementType, ts.TypeFlags.StringLike | ts.TypeFlags.NumberLike)
) {
const defaultSeparatorLiteral = lua.createStringLiteral(",");
const param = params[0];
const param = params[0] ?? lua.createNilLiteral();
const parameters = [
caller,
node.arguments.length === 0
Expand Down
19 changes: 16 additions & 3 deletions src/transformation/builtins/math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,20 +66,33 @@ export function transformMathCall(
case "log1p": {
const log = lua.createStringLiteral("log");
const one = lua.createNumericLiteral(1);
const add = lua.createBinaryExpression(one, params[0], lua.SyntaxKind.AdditionOperator);
const add = lua.createBinaryExpression(
one,
params[0] ?? lua.createNilLiteral(),
lua.SyntaxKind.AdditionOperator
);
return lua.createCallExpression(lua.createTableIndexExpression(math, log), [add], node);
}

case "pow": {
// Translate to base ^ power
return lua.createBinaryExpression(params[0], params[1], lua.SyntaxKind.PowerOperator, node);
return lua.createBinaryExpression(
params[0] ?? lua.createNilLiteral(),
params[1] ?? lua.createNilLiteral(),
lua.SyntaxKind.PowerOperator,
node
);
}

// math.floor(x + 0.5)
case "round": {
const floor = lua.createStringLiteral("floor");
const half = lua.createNumericLiteral(0.5);
const add = lua.createBinaryExpression(params[0], half, lua.SyntaxKind.AdditionOperator);
const add = lua.createBinaryExpression(
params[0] ?? lua.createNilLiteral(),
half,
lua.SyntaxKind.AdditionOperator
);
return lua.createCallExpression(lua.createTableIndexExpression(math, floor), [add], node);
}

Expand Down
15 changes: 11 additions & 4 deletions src/transformation/builtins/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export function transformStringPrototypeCall(
"find",
node,
caller,
params[0],
params[0] ?? lua.createNilLiteral(),
params[1]
? // string.find handles negative indexes by making it relative to string end, but for indexOf it's the same as 0
lua.createCallExpression(
Expand Down Expand Up @@ -110,7 +110,7 @@ export function transformStringPrototypeCall(
const literalValue = getNumberLiteralValue(params[0]);
// Inline string.sub call if we know that parameter is pure and isn't negative
if (literalValue !== undefined && literalValue >= 0) {
const firstParamPlusOne = addToNumericExpression(params[0], 1);
const firstParamPlusOne = addToNumericExpression(params[0] ?? lua.createNilLiteral(), 1);
return createStringCall("sub", node, caller, firstParamPlusOne, firstParamPlusOne);
}

Expand All @@ -122,7 +122,12 @@ export function transformStringPrototypeCall(
// Inline string.sub call if we know that parameter is pure and isn't negative
if (literalValue !== undefined && literalValue >= 0) {
return lua.createBinaryExpression(
createStringCall("byte", node, caller, addToNumericExpression(params[0], 1)),
createStringCall(
"byte",
node,
caller,
addToNumericExpression(params[0] ?? lua.createNilLiteral(), 1)
),
createNaN(),
lua.SyntaxKind.OrOperator
);
Expand All @@ -140,7 +145,9 @@ export function transformStringPrototypeCall(
case "repeat":
const math = lua.createIdentifier("math");
const floor = lua.createStringLiteral("floor");
const parameter = lua.createCallExpression(lua.createTableIndexExpression(math, floor), [params[0]]);
const parameter = lua.createCallExpression(lua.createTableIndexExpression(math, floor), [
params[0] ?? lua.createNilLiteral(),
]);
return createStringCall("rep", node, caller, parameter);
case "padStart":
return transformLuaLibFunction(context, LuaLibFeature.StringPadStart, node, caller, ...params);
Expand Down
18 changes: 10 additions & 8 deletions src/transformation/utils/lua-ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,15 +305,17 @@ function getJSDocCommentFromTSNode(
// https://stevedonovan.github.io/ldoc/manual/doc.md.html
// LDoc comments require that the first line starts with three hyphens.
// Thus, need to add a hyphen to the first line.
const firstLine = lines[0];
if (firstLine.startsWith(" @")) {
lines.unshift("-");
} else {
lines.shift();
lines.unshift("-" + firstLine);
}
if (lines.length > 0) {
const firstLine = lines[0];
if (firstLine.startsWith(" @")) {
lines.unshift("-");
} else {
lines.shift();
lines.unshift("-" + firstLine);
}

return lines;
return lines;
}
}

export const createNaN = (tsOriginal?: ts.Node) =>
Expand Down
12 changes: 12 additions & 0 deletions test/unit/builtins/string.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -398,3 +398,15 @@ test.each(["string | undefined", "string | null", "null | string", "null | undef
.expectToMatchJsResult();
}
);

// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1406
test("string.indexOf without arguments (#1406)", () => {
// Just test we do not throw here
util.testExpression`"".indexOf()`.expectToHaveDiagnostics();
});

// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1406
test("string.repeat without arguments (#1406)", () => {
// Just test we do not throw here
util.testExpression`"".repeat()`.expectToHaveDiagnostics();
});