X Tutup
Skip to content

Commit e545192

Browse files
authored
Exception trying to read symbol from undefined type (#1326)
* Fix excaption due to undefined call symbol * Add reproducing test * Hopefully actually sustainable solution
1 parent 0870791 commit e545192

File tree

2 files changed

+31
-14
lines changed

2 files changed

+31
-14
lines changed

src/transformation/builtins/index.ts

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,7 @@ import { TransformationContext } from "../context";
44
import { createNaN } from "../utils/lua-ast";
55
import { importLuaLibFeature, LuaLibFeature } from "../utils/lualib";
66
import { getIdentifierSymbolId } from "../utils/symbols";
7-
import {
8-
isStandardLibraryType,
9-
isStandardLibraryDeclaration,
10-
isStringType,
11-
isArrayType,
12-
isFunctionType,
13-
} from "../utils/typescript";
7+
import { isStandardLibraryType, isStringType, isArrayType, isFunctionType } from "../utils/typescript";
148
import { getCalledExpression } from "../visitors/call";
159
import { transformArrayConstructorCall, transformArrayProperty, transformArrayPrototypeCall } from "./array";
1610
import { transformConsoleCall } from "./console";
@@ -85,9 +79,7 @@ function tryTransformBuiltinGlobalMethodCall(
8579
calledMethod: ts.PropertyAccessExpression
8680
) {
8781
const ownerType = context.checker.getTypeAtLocation(calledMethod.expression);
88-
if (!isStandardLibraryType(context, ownerType, undefined)) return;
89-
90-
const ownerSymbol = ownerType.symbol;
82+
const ownerSymbol = tryGetStandardLibrarySymbolOfType(context, ownerType);
9183
if (!ownerSymbol || ownerSymbol.parent) return;
9284

9385
let result: lua.Expression | undefined;
@@ -129,10 +121,9 @@ function tryTransformBuiltinPropertyCall(
129121
node: ts.CallExpression,
130122
calledMethod: ts.PropertyAccessExpression
131123
) {
132-
const signatureDeclaration = context.checker.getResolvedSignature(node)?.declaration;
133-
if (!signatureDeclaration || !isStandardLibraryDeclaration(context, signatureDeclaration)) return;
134-
135-
const callSymbol = context.checker.getTypeAtLocation(signatureDeclaration).symbol;
124+
const functionType = context.checker.getTypeAtLocation(node.expression);
125+
const callSymbol = tryGetStandardLibrarySymbolOfType(context, functionType);
126+
if (!callSymbol) return;
136127
const ownerSymbol = callSymbol.parent;
137128
if (!ownerSymbol || ownerSymbol.parent) return;
138129

@@ -217,3 +208,16 @@ export function checkForLuaLibType(context: TransformationContext, type: ts.Type
217208
importLuaLibFeature(context, LuaLibFeature.Error);
218209
}
219210
}
211+
212+
function tryGetStandardLibrarySymbolOfType(context: TransformationContext, type: ts.Type): ts.Symbol | undefined {
213+
if (type.isUnionOrIntersection()) {
214+
for (const subType of type.types) {
215+
const symbol = tryGetStandardLibrarySymbolOfType(context, subType);
216+
if (symbol) return symbol;
217+
}
218+
} else if (isStandardLibraryType(context, type, undefined)) {
219+
return type.symbol;
220+
}
221+
222+
return undefined;
223+
}

test/unit/functions/functions.spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as ts from "typescript";
12
import * as tstl from "../../../src";
23
import * as util from "../../util";
34
import { unsupportedForTarget } from "../../../src/transformation/utils/diagnostics";
@@ -512,3 +513,15 @@ test("top-level function declaration is global", () => {
512513
.addExtraFile("a.ts", 'function foo() { return "foo" }')
513514
.expectToEqual({ result: "foo" });
514515
});
516+
517+
// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1325
518+
test("call expression should not throw (#1325)", () => {
519+
util.testModule`
520+
function test<T>(iterator:Iterator<T>) {
521+
iterator.return?.();
522+
}
523+
`
524+
// Note: does not reproduce without strict=true
525+
.setOptions({ target: ts.ScriptTarget.ESNext, strict: true })
526+
.expectToHaveNoDiagnostics();
527+
});

0 commit comments

Comments
 (0)
X Tutup