Skip to content

Fix decorator on class exported from namespace #1636

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 24, 2025
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
8 changes: 8 additions & 0 deletions src/transformation/utils/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ export function hasExportModifier(node: ts.Node): boolean {
);
}

export function shouldBeExported(node: ts.Node): boolean {
if (hasExportModifier(node)) {
// Don't export if we're inside a namespace (module declaration)
return ts.findAncestor(node, ts.isModuleDeclaration) === undefined;
}
return false;
}

export const createDefaultExportStringLiteral = (original?: ts.Node): lua.StringLiteral =>
lua.createStringLiteral("default", original);

Expand Down
4 changes: 2 additions & 2 deletions src/transformation/visitors/class/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import {
createDefaultExportExpression,
createExportedIdentifier,
hasDefaultExportModifier,
hasExportModifier,
isSymbolExported,
shouldBeExported,
} from "../../utils/export";
import { createSelfIdentifier } from "../../utils/lua-ast";
import { createSafeName, isUnsafeName } from "../../utils/safe-names";
Expand Down Expand Up @@ -214,7 +214,7 @@ function transformClassLikeDeclaration(
const decoratingStatement = lua.createAssignmentStatement(localClassName, decoratingExpression);
result.push(decoratingStatement);

if (hasExportModifier(classDeclaration)) {
if (shouldBeExported(classDeclaration)) {
const exportExpression = hasDefaultExportModifier(classDeclaration)
? createDefaultExportExpression(classDeclaration)
: createExportedIdentifier(context, className);
Expand Down
24 changes: 23 additions & 1 deletion test/unit/classes/decorators.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ test("Exported class decorator", () => {
});

// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1149
test("exported class with decorator", () => {
test("exported class with decorator (#1149)", () => {
util.testModule`
import { MyClass } from "./other";
const inst = new MyClass();
Expand All @@ -164,6 +164,28 @@ test("exported class with decorator", () => {
.expectToEqual({ result: "overridden" });
});

// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1634
test("namespaced exported class with decorator (#1634)", () => {
util.testModule`
function myDecorator(target: {new(): any}, context: ClassDecoratorContext) {
return class extends target {
foo() {
return "overridden";
}
}
}

namespace ns {
@myDecorator
export class MyClass {
foo() {
return "foo";
}
}
}
`.expectNoExecutionError();
});

test("default exported class with decorator", () => {
util.testModule`
import MyClass from "./other";
Expand Down
Loading