Skip to content
Open
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
13 changes: 13 additions & 0 deletions src/transformation/utils/function-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,19 @@ function computeDeclarationContextType(context: TransformationContext, signature
return ContextType.NonVoid;
}

// Call signature inside a class or interface respects @noSelf on the enclosing class/interface
if (ts.isCallSignatureDeclaration(signatureDeclaration)) {
const scopeDeclaration = findFirstNodeAbove(
signatureDeclaration,
(n): n is ts.ClassLikeDeclaration | ts.InterfaceDeclaration =>
ts.isClassDeclaration(n) || ts.isClassExpression(n) || ts.isInterfaceDeclaration(n)
);

if (scopeDeclaration !== undefined && getNodeAnnotations(scopeDeclaration).has(AnnotationKind.NoSelf)) {
return ContextType.Void;
}
}

// When using --noImplicitSelf and the signature is defined in a file targeted by the program apply the @noSelf rule.
const program = context.program;
const options = program.getCompilerOptions() as CompilerOptions;
Expand Down
44 changes: 44 additions & 0 deletions test/unit/functions/noSelfAnnotation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,50 @@ test("@noSelf on static class methods with string key access", () => {
`.expectLuaToMatchSnapshot();
});

// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1661
// A Lua-side function observes the actual argc, so a missing @noSelf would
// surface as a phantom leading nil (argc 2 instead of 1).
const argcProbeHeader = `
function probe(...)
return select("#", ...)
end
`;

test("@noSelf on interface call signature: Lua probe sees correct argc", () => {
util.testModule`
/** @noSelf */
interface Probe { (a: string): number; }
declare const probe: Probe;
export const result = probe("hi");
`
.setLuaHeader(argcProbeHeader)
.expectToEqual({ result: 1 });
});

test("@noSelf parent interface, property typed by call-signature interface: Lua probe sees correct argc", () => {
util.testModule`
/** @noSelf */
interface CallSignature { (a: string): number; }
/** @noSelf */
interface Holder { fn: CallSignature; }
declare const holder: Holder;
export const result = holder.fn("hi");
`
.setLuaHeader(`${argcProbeHeader}\nholder = { fn = probe }`)
.expectToEqual({ result: 1 });
});

test("@noSelf parent interface, property typed by type-literal call signature: Lua probe sees correct argc", () => {
util.testModule`
/** @noSelf */
interface Holder { fn: { (a: string): number }; }
declare const holder: Holder;
export const result = holder.fn("hi");
`
.setLuaHeader(`${argcProbeHeader}\nholder = { fn = probe }`)
.expectToEqual({ result: 1 });
});

// additional coverage for https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1292
test("explicit this parameter respected over @noSelf", () => {
util.testModule`
Expand Down
Loading