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
5 changes: 5 additions & 0 deletions src/transformation/utils/function-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ export function getDeclarationContextType(
: ContextType.NonVoid;
}

// noSelf declaration on function signature
if (getNodeAnnotations(signatureDeclaration).has(AnnotationKind.NoSelf)) {
return ContextType.Void;
}

if (
ts.isMethodSignature(signatureDeclaration) ||
ts.isMethodDeclaration(signatureDeclaration) ||
Expand Down
15 changes: 15 additions & 0 deletions test/unit/functions/__snapshots__/noSelfAnnotation.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`@noSelf on declared function removes context argument 1`] = `"myFunction()"`;

exports[`@noSelf on method inside class declaration removes context argument 1`] = `"holder.myMethod()"`;

exports[`@noSelf on method inside interface declaration removes context argument 1`] = `"holder.myMethod()"`;

exports[`@noSelf on method inside namespace declaration removes context argument 1`] = `"MyNamespace.myMethod()"`;

exports[`@noSelf on parent class declaration removes context argument 1`] = `"holder.myMethod()"`;

exports[`@noSelf on parent interface declaration removes context argument 1`] = `"holder.myMethod()"`;

exports[`@noSelf on parent namespace declaration removes context argument 1`] = `"MyNamespace.myMethod()"`;
53 changes: 53 additions & 0 deletions test/unit/functions/noSelfAnnotation.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import * as util from "../../util";

const methodHolders = ["class", "interface"];

test("@noSelf on declared function removes context argument", () => {
util.testModule`
/** @noSelf */
declare function myFunction(): void;
myFunction();
`.expectLuaToMatchSnapshot();
});

test.each(methodHolders)("@noSelf on method inside %s declaration removes context argument", holderType => {
util.testModule`
declare ${holderType} MethodHolder {
/** @noSelf */
myMethod(): void;
}
declare const holder: MethodHolder;
holder.myMethod();
`.expectLuaToMatchSnapshot();
});

test.each(methodHolders)("@noSelf on parent %s declaration removes context argument", holderType => {
util.testModule`
/** @noSelf */
declare ${holderType} MethodHolder {
myMethod(): void;
}
declare const holder: MethodHolder;
holder.myMethod();
`.expectLuaToMatchSnapshot();
});

test("@noSelf on method inside namespace declaration removes context argument", () => {
util.testModule`
declare namespace MyNamespace {
/** @noSelf */
export function myMethod(): void;
}
MyNamespace.myMethod();
`.expectLuaToMatchSnapshot();
});

test("@noSelf on parent namespace declaration removes context argument", () => {
util.testModule`
/** @noSelf */
declare namespace MyNamespace {
export function myMethod(): void;
}
MyNamespace.myMethod();
`.expectLuaToMatchSnapshot();
});
Loading
X Tutup