X Tutup
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
32 changes: 18 additions & 14 deletions src/transformation/builtins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,7 @@ import { TransformationContext } from "../context";
import { createNaN } from "../utils/lua-ast";
import { importLuaLibFeature, LuaLibFeature } from "../utils/lualib";
import { getIdentifierSymbolId } from "../utils/symbols";
import {
isStandardLibraryType,
isStandardLibraryDeclaration,
isStringType,
isArrayType,
isFunctionType,
} from "../utils/typescript";
import { isStandardLibraryType, isStringType, isArrayType, isFunctionType } from "../utils/typescript";
import { getCalledExpression } from "../visitors/call";
import { transformArrayConstructorCall, transformArrayProperty, transformArrayPrototypeCall } from "./array";
import { transformConsoleCall } from "./console";
Expand Down Expand Up @@ -85,9 +79,7 @@ function tryTransformBuiltinGlobalMethodCall(
calledMethod: ts.PropertyAccessExpression
) {
const ownerType = context.checker.getTypeAtLocation(calledMethod.expression);
if (!isStandardLibraryType(context, ownerType, undefined)) return;

const ownerSymbol = ownerType.symbol;
const ownerSymbol = tryGetStandardLibrarySymbolOfType(context, ownerType);
if (!ownerSymbol || ownerSymbol.parent) return;

let result: lua.Expression | undefined;
Expand Down Expand Up @@ -129,10 +121,9 @@ function tryTransformBuiltinPropertyCall(
node: ts.CallExpression,
calledMethod: ts.PropertyAccessExpression
) {
const signatureDeclaration = context.checker.getResolvedSignature(node)?.declaration;
if (!signatureDeclaration || !isStandardLibraryDeclaration(context, signatureDeclaration)) return;

const callSymbol = context.checker.getTypeAtLocation(signatureDeclaration).symbol;
const functionType = context.checker.getTypeAtLocation(node.expression);
const callSymbol = tryGetStandardLibrarySymbolOfType(context, functionType);
if (!callSymbol) return;
const ownerSymbol = callSymbol.parent;
if (!ownerSymbol || ownerSymbol.parent) return;

Expand Down Expand Up @@ -217,3 +208,16 @@ export function checkForLuaLibType(context: TransformationContext, type: ts.Type
importLuaLibFeature(context, LuaLibFeature.Error);
}
}

function tryGetStandardLibrarySymbolOfType(context: TransformationContext, type: ts.Type): ts.Symbol | undefined {
if (type.isUnionOrIntersection()) {
for (const subType of type.types) {
const symbol = tryGetStandardLibrarySymbolOfType(context, subType);
if (symbol) return symbol;
}
} else if (isStandardLibraryType(context, type, undefined)) {
return type.symbol;
}

return undefined;
}
13 changes: 13 additions & 0 deletions test/unit/functions/functions.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as ts from "typescript";
import * as tstl from "../../../src";
import * as util from "../../util";
import { unsupportedForTarget } from "../../../src/transformation/utils/diagnostics";
Expand Down Expand Up @@ -512,3 +513,15 @@ test("top-level function declaration is global", () => {
.addExtraFile("a.ts", 'function foo() { return "foo" }')
.expectToEqual({ result: "foo" });
});

// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1325
test("call expression should not throw (#1325)", () => {
util.testModule`
function test<T>(iterator:Iterator<T>) {
iterator.return?.();
}
`
// Note: does not reproduce without strict=true
.setOptions({ target: ts.ScriptTarget.ESNext, strict: true })
.expectToHaveNoDiagnostics();
});
X Tutup