Skip to content

Commit 70b61d9

Browse files
authored
Fix call context (#1294)
* Add tests for #1292 * Fix #1292 * Always use noImplicitSelf for node without signature declaration
1 parent 66bb36e commit 70b61d9

File tree

3 files changed

+35
-6
lines changed

3 files changed

+35
-6
lines changed

src/transformation/visitors/call.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -271,12 +271,9 @@ export const transformCallExpression: FunctionVisitor<ts.CallExpression> = (node
271271
);
272272
}
273273

274-
const signatureDeclaration = signature?.getDeclaration();
275-
276274
let callPath: lua.Expression;
277275
let parameters: lua.Expression[];
278-
const isContextualCall =
279-
!signatureDeclaration || getDeclarationContextType(context, signatureDeclaration) !== ContextType.Void;
276+
const isContextualCall = isContextualCallExpression(context, signature);
280277
if (!isContextualCall) {
281278
[callPath, parameters] = transformCallAndArguments(context, calledExpression, node.arguments, signature);
282279
} else {
@@ -288,8 +285,7 @@ export const transformCallExpression: FunctionVisitor<ts.CallExpression> = (node
288285
calledExpression,
289286
node.arguments,
290287
signature,
291-
// Only pass context if noImplicitSelf is not configured
292-
context.options.noImplicitSelf ? undefined : callContext
288+
callContext
293289
);
294290
}
295291

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

299+
function isContextualCallExpression(context: TransformationContext, signature: ts.Signature | undefined): boolean {
300+
const declaration = signature?.getDeclaration();
301+
if (!declaration) {
302+
return !context.options.noImplicitSelf;
303+
}
304+
return getDeclarationContextType(context, declaration) !== ContextType.Void;
305+
}
306+
303307
export function getCalledExpression(node: ts.CallExpression): ts.Expression {
304308
function unwrapExpression(expression: ts.Expression): ts.Expression {
305309
expression = ts.skipOuterExpressions(expression);

test/unit/functions/noImplicitSelfOption.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,17 @@ test("generates declaration files with @noSelfInFile", () => {
7979
.ignoreDiagnostics([couldNotResolveRequire.code]) // no foo implementation in the project to create foo.lua
8080
.expectToHaveNoDiagnostics();
8181
});
82+
83+
// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1292
84+
test("explicit this parameter respected over noImplicitSelf", () => {
85+
util.testModule`
86+
function foo(this: unknown, arg: any) {
87+
return {self: this, arg};
88+
}
89+
export const result = foo(1);
90+
`
91+
.setOptions({
92+
noImplicitSelf: true,
93+
})
94+
.expectToMatchJsResult();
95+
});

test/unit/functions/noSelfAnnotation.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,14 @@ test("@noSelf on parent namespace declaration removes context argument", () => {
5151
MyNamespace.myMethod();
5252
`.expectLuaToMatchSnapshot();
5353
});
54+
55+
// additional coverage for https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1292
56+
test("explicit this parameter respected over @noSelf", () => {
57+
util.testModule`
58+
/** @noSelfInFile */
59+
function foo(this: unknown, arg: any) {
60+
return {self: this, arg};
61+
}
62+
export const result = foo(1);
63+
`.expectToMatchJsResult();
64+
});

0 commit comments

Comments
 (0)