8000 Fix element access exception caused by invalid TS (#1410) · TypeScriptToLua/TypeScriptToLua@4511e17 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4511e17

Browse files
authored
Fix element access exception caused by invalid TS (#1410)
* Fix element access exception caused by invalid TS * Chang test name
1 parent 3b30b09 commit 4511e17

File tree

5 files changed

+35
-9
lines changed

5 files changed

+35
-9
lines changed

src/transformation/utils/diagnostics.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as ts from "typescript";
2+
import * as lua from "../../LuaAST";
23
import { LuaTarget, TypeScriptToLuaOptions } from "../../CompilerOptions";
34
import { createSerialDiagnosticFactory } from "../../utils";
45
import { AnnotationKind } from "./annotations";
@@ -163,3 +164,7 @@ export const invalidMethodCallExtensionUse = createErrorDiagnosticFactory(
163164
export const invalidSpreadInCallExtension = createErrorDiagnosticFactory(
164165
"Spread elements are not supported in call extensions."
165166
);
167+
168+
export const cannotAssignToNodeOfKind = createErrorDiagnosticFactory(
169+
(kind: lua.SyntaxKind) => `Cannot create assignment assigning to a node of type ${lua.SyntaxKind[kind]}.`
170+
);

src/transformation/visitors/binary-expression/assignments.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import * as ts from "typescript";
22
import * as lua from "../../../LuaAST";
3-
import { cast } from "../../../utils";
43
import { TransformationContext } from "../../context";
54
import { validateAssignment } from "../../utils/assignment-validation";
65
import { createExportedIdentifier, getDependenciesOfSymbol, isSymbolExported } from "../../utils/export";
@@ -9,7 +8,7 @@ import { LuaLibFeature, transformLuaLibFunction } from "../../utils/lualib";
98
import { isArrayType, isDestructuringAssignment } from "../../utils/typescript";
109
import { isArrayLength, transformDestructuringAssignment } from "./destructuring-assignments";
1110
import { isMultiReturnCall } from "../language-extensions/multi";
12-
import { notAllowedOptionalAssignment } from "../../utils/diagnostics";
11+
import { cannotAssignToNodeOfKind, notAllowedOptionalAssignment } from "../../utils/diagnostics";
1312
import { transformElementAccessArgument } from "../access";
1413
import { moveToPrecedingTemp, transformExpressionList } from "../expression-list";
1514
import { transformInPrecedingStatementScope } from "../../utils/preceding-statements";
@@ -37,9 +36,16 @@ export function transformAssignmentLeftHandSideExpression(
3736
const symbol = context.checker.getSymbolAtLocation(node);
3837
const left = context.transformExpression(node);
3938

40-
return lua.isIdentifier(left) && symbol && isSymbolExported(context, symbol)
41-
? createExportedIdentifier(context, left)
42-
: cast(left, lua.isAssignmentLeftHandSideExpression);
39+
if (lua.isIdentifier(left) && symbol && isSymbolExported(context, symbol)) {
40+
return createExportedIdentifier(context, left);
41+
}
42+
43+
if (lua.isAssignmentLeftHandSideExpression(left)) {
44+
return left;
45+
} else {
46+
context.diagnostics.push(cannotAssignToNodeOfKind(node, left.kind));
47+
return lua.createAnonymousIdentifier();
48+
}
4349
}
4450

4551
export function transformAssignment(

test/unit/builtins/string.spec.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -401,12 +401,10 @@ test.each(["string | undefined", "string | null", "null | string", "null | undef
401401

402402
// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1406
403403
test("string.indexOf without arguments (#1406)", () => {
404-
// Just test we do not throw here
405-
util.testExpression`"".indexOf()`.expectToHaveDiagnostics();
404+
util.testExpression`"".indexOf()`.expectNoTranspileException();
406405
});
407406

408407
// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1406
409408
test("string.repeat without arguments (#1406)", () => {
410-
// Just test we do not throw here
411-
util.testExpression`"".repeat()`.expectToHaveDiagnostics();
409+
util.testExpression`"".repeat()`.expectNoTranspileException();
412410
});

test/unit/destructuring.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { cannotAssignToNodeOfKind, invalidMultiReturnAccess } from "../../src/transformation/utils/diagnostics";
12
import * as util from "../util";
23

34
const allBindings = "x, y, z, rest";
@@ -225,3 +226,14 @@ describe("array destructuring optimization", () => {
225226
.expectToMatchJsResult();
226227
});
227228
});
229+
230+
test("no exception from semantically invalid TS", () => {
231+
util.testModule`
232+
declare function testFunc(value: number): LuaMultiReturn<[number, number]>;
233+
let [a, b] = testFunc(5) // Missing ;
234+
[a, b] = testFunc(b) // Interpreted as testFunc(5)[a, b]
235+
`
236+
.withLanguageExtensions()
237+
.disableSemanticCheck()
238+
.expectToHaveDiagnostics([invalidMultiReturnAccess.code, cannotAssignToNodeOfKind.code]);
239+
});

test/util.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,11 @@ export abstract class TestBuilder {
346346
return this;
347347
}
348348

349+
public expectNoTranspileException(): this {
350+
expect(() => this.getLuaResult()).not.toThrow();
351+
return this;
352+
}
353+
349354
public expectNoExecutionError(): this {
350355
const luaResult = this.getLuaExecutionResult();
351356
if (luaResult instanceof ExecutionError) {

0 commit comments

Comments
 (0)
0