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
16 changes: 10 additions & 6 deletions src/transformation/visitors/call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,12 +271,9 @@ export const transformCallExpression: FunctionVisitor<ts.CallExpression> = (node
);
}

const signatureDeclaration = signature?.getDeclaration();

let callPath: lua.Expression;
let parameters: lua.Expression[];
const isContextualCall =
!signatureDeclaration || getDeclarationContextType(context, signatureDeclaration) !== ContextType.Void;
const isContextualCall = isContextualCallExpression(context, signature);
if (!isContextualCall) {
[callPath, parameters] = transformCallAndArguments(context, calledExpression, node.arguments, signature);
} else {
Expand All @@ -288,8 +285,7 @@ export const transformCallExpression: FunctionVisitor<ts.CallExpression> = (node
calledExpression,
node.arguments,
signature,
// Only pass context if noImplicitSelf is not configured
context.options.noImplicitSelf ? undefined : callContext
callContext
);
}

Expand All @@ -300,6 +296,14 @@ export const transformCallExpression: FunctionVisitor<ts.CallExpression> = (node
return wrapResultInTable ? wrapInTable(callExpression) : callExpression;
};

function isContextualCallExpression(context: TransformationContext, signature: ts.Signature | undefined): boolean {
const declaration = signature?.getDeclaration();
if (!declaration) {
return !context.options.noImplicitSelf;
}
return getDeclarationContextType(context, declaration) !== ContextType.Void;
}

export function getCalledExpression(node: ts.CallExpression): ts.Expression {
function unwrapExpression(expression: ts.Expression): ts.Expression {
expression = ts.skipOuterExpressions(expression);
Expand Down
14 changes: 14 additions & 0 deletions test/unit/functions/noImplicitSelfOption.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,17 @@ test("generates declaration files with @noSelfInFile", () => {
.ignoreDiagnostics([couldNotResolveRequire.code]) // no foo implementation in the project to create foo.lua
.expectToHaveNoDiagnostics();
});

// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1292
test("explicit this parameter respected over noImplicitSelf", () => {
util.testModule`
function foo(this: unknown, arg: any) {
return {self: this, arg};
}
export const result = foo(1);
`
.setOptions({
noImplicitSelf: true,
})
.expectToMatchJsResult();
});
11 changes: 11 additions & 0 deletions test/unit/functions/noSelfAnnotation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,14 @@ test("@noSelf on parent namespace declaration removes context argument", () => {
MyNamespace.myMethod();
`.expectLuaToMatchSnapshot();
});

// additional coverage for https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1292
test("explicit this parameter respected over @noSelf", () => {
util.testModule`
/** @noSelfInFile */
function foo(this: unknown, arg: any) {
return {self: this, arg};
}
export const result = foo(1);
`.expectToMatchJsResult();
});