Skip to content

Commit 596a87a

Browse files
authored
Fix decorator on class exported from namespace (#1636)
1 parent ec84670 commit 596a87a

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

src/transformation/utils/export.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ export function hasExportModifier(node: ts.Node): boolean {
2020
);
2121
}
2222

23+
export function shouldBeExported(node: ts.Node): boolean {
24+
if (hasExportModifier(node)) {
25+
// Don't export if we're inside a namespace (module declaration)
26+
return ts.findAncestor(node, ts.isModuleDeclaration) === undefined;
27+
}
28+
return false;
29+
}
30+
2331
export const createDefaultExportStringLiteral = (original?: ts.Node): lua.StringLiteral =>
2432
lua.createStringLiteral("default", original);
2533

src/transformation/visitors/class/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import {
55
createDefaultExportExpression,
66
createExportedIdentifier,
77
hasDefaultExportModifier,
8-
hasExportModifier,
98
isSymbolExported,
9+
shouldBeExported,
1010
} from "../../utils/export";
1111
import { createSelfIdentifier } from "../../utils/lua-ast";
1212
import { createSafeName, isUnsafeName } from "../../utils/safe-names";
@@ -214,7 +214,7 @@ function transformClassLikeDeclaration(
214214
const decoratingStatement = lua.createAssignmentStatement(localClassName, decoratingExpression);
215215
result.push(decoratingStatement);
216216

217-
if (hasExportModifier(classDeclaration)) {
217+
if (shouldBeExported(classDeclaration)) {
218218
const exportExpression = hasDefaultExportModifier(classDeclaration)
219219
? createDefaultExportExpression(classDeclaration)
220220
: createExportedIdentifier(context, className);

test/unit/classes/decorators.spec.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ test("Exported class decorator", () => {
138138
});
139139

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

167+
// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1634
168+
test("namespaced exported class with decorator (#1634)", () => {
169+
util.testModule`
170+
function myDecorator(target: {new(): any}, context: ClassDecoratorContext) {
171+
return class extends target {
172+
foo() {
173+
return "overridden";
174+
}
175+
}
176+
}
177+
178+
namespace ns {
179+
@myDecorator
180+
export class MyClass {
181+
foo() {
182+
return "foo";
183+
}
184+
}
185+
}
186+
`.expectNoExecutionError();
187+
});
188+
167189
test("default exported class with decorator", () => {
168190
util.testModule`
169191
import MyClass from "./other";

0 commit comments

Comments
 (0)