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
3 changes: 3 additions & 0 deletions src/lualib/OptionalMethodCall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ function __TS__OptionalMethodCall<TArgs extends any[], TReturn>(
this: void,
table: Record<string, (...args: [...TArgs]) => TReturn>,
methodName: string,
isMethodOptional: boolean,
...args: [...TArgs]
): TReturn | undefined {
if (table) {
const method = table[methodName];
if (method) {
return method.call(table, ...args);
} else if (!isMethodOptional) {
throw `${methodName} is not a function`;
}
}
return undefined;
Expand Down
6 changes: 4 additions & 2 deletions src/transformation/visitors/call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ export function transformContextualCallExpression(
node,
table,
lua.createStringLiteral(left.name.text, left.name),
lua.createBooleanLiteral(node.questionDotToken !== undefined), // Require method is present if no ?.() call
...transformArguments(context, args, signature)
);
} else {
Expand Down Expand Up @@ -331,7 +332,7 @@ function wrapIfRequired(
shouldWrapInTable: boolean,
shouldWrapOptional: boolean,
call: lua.CallExpression | lua.MethodCallExpression,
node: ts.Node
node: ts.CallExpression
): lua.Expression {
const wrappedOptional = shouldWrapOptional ? wrapOptionalCall(context, call, node) : call;
return shouldWrapInTable ? wrapInTable(wrappedOptional) : wrappedOptional;
Expand All @@ -340,7 +341,7 @@ function wrapIfRequired(
function wrapOptionalCall(
context: TransformationContext,
call: lua.CallExpression | lua.MethodCallExpression,
node: ts.Node
node: ts.CallExpression
): lua.CallExpression {
if (lua.isMethodCallExpression(call)) {
return transformLuaLibFunction(
Expand All @@ -349,6 +350,7 @@ function wrapOptionalCall(
node,
call.prefixExpression,
lua.createStringLiteral(call.name.text),
lua.createBooleanLiteral(node.questionDotToken !== undefined), // Require method is present if no ?.() call
...call.params
);
} else {
Expand Down
12 changes: 10 additions & 2 deletions test/unit/optionalChaining.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ describe("optional chaining function calls", () => {
const objWithMethods: typeWithOptional = {};
const objWithMethods2: typeWithOptional = { a: { b: { c: () => 4 } } };

return {
expectNil: objWithMethods.a?.b.c(),
return {
expectNil: objWithMethods.a?.b.c(),
expectFour: objWithMethods2.a?.b.c()
};
`.expectToMatchJsResult();
Expand All @@ -167,4 +167,12 @@ describe("optional chaining function calls", () => {
return [obj.a?.().b.c?.() ?? "nil", obj2.a?.().b.c?.() ?? "nil", obj3.a?.().b.c?.() ?? "nil"];
`.expectToMatchJsResult();
});

// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1085
test("incorrect type, method is not a function", () => {
util.testFunction`
const obj: any = {};
obj?.foo();
`.expectToEqual(new util.ExecutionError("foo is not a function"));
});
});