Skip to content

Commit 5a758ea

Browse files
authored
Fix crash missing arguments (#1407)
* Fix exception when calling string indexOf or string splice without arguments * Fix some more potential undefined references
1 parent 45d91ec commit 5a758ea

File tree

5 files changed

+52
-18
lines changed

5 files changed

+52
-18
lines changed

src/transformation/builtins/array.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,14 @@ export function transformArrayPrototypeCall(
9494
return transformLuaLibFunction(context, LuaLibFeature.ArrayEntries, node, caller);
9595
case "push":
9696
if (node.arguments.length === 1) {
97-
const param = params[0];
97+
const param = params[0] ?? lua.createNilLiteral();
9898
if (isUnpackCall(param)) {
9999
return transformLuaLibFunction(
100100
context,
101101
LuaLibFeature.ArrayPushArray,
102102
node,
103103
caller,
104-
(param as lua.CallExpression).params[0]
104+
(param as lua.CallExpression).params[0] ?? lua.createNilLiteral()
105105
);
106106
}
107107
if (!lua.isDotsLiteral(param)) {
@@ -162,7 +162,7 @@ export function transformArrayPrototypeCall(
162162
typeAlwaysHasSomeOfFlags(context, elementType, ts.TypeFlags.StringLike | ts.TypeFlags.NumberLike)
163163
) {
164164
const defaultSeparatorLiteral = lua.createStringLiteral(",");
165-
const param = params[0];
165+
const param = params[0] ?? lua.createNilLiteral();
166166
const parameters = [
167167
caller,
168168
node.arguments.length === 0

src/transformation/builtins/math.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,20 +66,33 @@ export function transformMathCall(
6666
case "log1p": {
6767
const log = lua.createStringLiteral("log");
6868
const one = lua.createNumericLiteral(1);
69-
const add = lua.createBinaryExpression(one, params[0], lua.SyntaxKind.AdditionOperator);
69+
const add = lua.createBinaryExpression(
70+
one,
71+
params[0] ?? lua.createNilLiteral(),
72+
lua.SyntaxKind.AdditionOperator
73+
);
7074
return lua.createCallExpression(lua.createTableIndexExpression(math, log), [add], node);
7175
}
7276

7377
case "pow": {
7478
// Translate to base ^ power
75-
return lua.createBinaryExpression(params[0], params[1], lua.SyntaxKind.PowerOperator, node);
79+
return lua.createBinaryExpression(
80+
params[0] ?? lua.createNilLiteral(),
81+
params[1] ?? lua.createNilLiteral(),
82+
lua.SyntaxKind.PowerOperator,
83+
node
84+
);
7685
}
7786

7887
// math.floor(x + 0.5)
7988
case "round": {
8089
const floor = lua.createStringLiteral("floor");
8190
const half = lua.createNumericLiteral(0.5);
82-
const add = lua.createBinaryExpression(params[0], half, lua.SyntaxKind.AdditionOperator);
91+
const add = lua.createBinaryExpression(
92+
params[0] ?? lua.createNilLiteral(),
93+
half,
94+
lua.SyntaxKind.AdditionOperator
95+
);
8396
return lua.createCallExpression(lua.createTableIndexExpression(math, floor), [add], node);
8497
}
8598

src/transformation/builtins/string.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export function transformStringPrototypeCall(
4242
"find",
4343
node,
4444
caller,
45-
params[0],
45+
params[0] ?? lua.createNilLiteral(),
4646
params[1]
4747
? // string.find handles negative indexes by making it relative to string end, but for indexOf it's the same as 0
4848
lua.createCallExpression(
@@ -110,7 +110,7 @@ export function transformStringPrototypeCall(
110110
const literalValue = getNumberLiteralValue(params[0]);
111111
// Inline string.sub call if we know that parameter is pure and isn't negative
112112
if (literalValue !== undefined && literalValue >= 0) {
113-
const firstParamPlusOne = addToNumericExpression(params[0], 1);
113+
const firstParamPlusOne = addToNumericExpression(params[0] ?? lua.createNilLiteral(), 1);
114114
return createStringCall("sub", node, caller, firstParamPlusOne, firstParamPlusOne);
115115
}
116116

@@ -122,7 +122,12 @@ export function transformStringPrototypeCall(
122122
// Inline string.sub call if we know that parameter is pure and isn't negative
123123
if (literalValue !== undefined && literalValue >= 0) {
124124
return lua.createBinaryExpression(
125-
createStringCall("byte", node, caller, addToNumericExpression(params[0], 1)),
125+
createStringCall(
126+
"byte",
127+
node,
128+
caller,
129+
addToNumericExpression(params[0] ?? lua.createNilLiteral(), 1)
130+
),
126131
createNaN(),
127132
lua.SyntaxKind.OrOperator
128133
);
@@ -140,7 +145,9 @@ export function transformStringPrototypeCall(
140145
case "repeat":
141146
const math = lua.createIdentifier("math");
142147
const floor = lua.createStringLiteral("floor");
143-
const parameter = lua.createCallExpression(lua.createTableIndexExpression(math, floor), [params[0]]);
148+
const parameter = lua.createCallExpression(lua.createTableIndexExpression(math, floor), [
149+
params[0] ?? lua.createNilLiteral(),
150+
]);
144151
return createStringCall("rep", node, caller, parameter);
145152
case "padStart":
146153
return transformLuaLibFunction(context, LuaLibFeature.StringPadStart, node, caller, ...params);

src/transformation/utils/lua-ast.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -305,15 +305,17 @@ function getJSDocCommentFromTSNode(
305305
// https://stevedonovan.github.io/ldoc/manual/doc.md.html
306306
// LDoc comments require that the first line starts with three hyphens.
307307
// Thus, need to add a hyphen to the first line.
308-
const firstLine = lines[0];
309-
if (firstLine.startsWith(" @")) {
310-
lines.unshift("-");
311-
} else {
312-
lines.shift();
313-
lines.unshift("-" + firstLine);
314-
}
308+
if (lines.length > 0) {
309+
const firstLine = lines[0];
310+
if (firstLine.startsWith(" @")) {
311+
lines.unshift("-");
312+
} else {
313+
lines.shift();
314+
lines.unshift("-" + firstLine);
315+
}
315316

316-
return lines;
317+
return lines;
318+
}
317319
}
318320

319321
export const createNaN = (tsOriginal?: ts.Node) =>

test/unit/builtins/string.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,3 +398,15 @@ test.each(["string | undefined", "string | null", "null | string", "null | undef
398398
.expectToMatchJsResult();
399399
}
400400
);
401+
402+
// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1406
403+
test("string.indexOf without arguments (#1406)", () => {
404+
// Just test we do not throw here
405+
util.testExpression`"".indexOf()`.expectToHaveDiagnostics();
406+
});
407+
408+
// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1406
409+
test("string.repeat without arguments (#1406)", () => {
410+
// Just test we do not throw here
411+
util.testExpression`"".repeat()`.expectToHaveDiagnostics();
412+
});

0 commit comments

Comments
 (0)