From 37b76883d8e9f7a6cb85c12d1eb76702a53726af Mon Sep 17 00:00:00 2001 From: Perryvw Date: Sat, 24 May 2025 16:04:49 +0200 Subject: [PATCH] Fix decorator on class exported from namespace --- src/transformation/utils/export.ts | 8 ++++++++ src/transformation/visitors/class/index.ts | 4 ++-- test/unit/classes/decorators.spec.ts | 24 +++++++++++++++++++++- 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/src/transformation/utils/export.ts b/src/transformation/utils/export.ts index fa1c03649..f0ef437e5 100644 --- a/src/transformation/utils/export.ts +++ b/src/transformation/utils/export.ts @@ -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); diff --git a/src/transformation/visitors/class/index.ts b/src/transformation/visitors/class/index.ts index 0edfce050..d4633995e 100644 --- a/src/transformation/visitors/class/index.ts +++ b/src/transformation/visitors/class/index.ts @@ -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"; @@ -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); diff --git a/test/unit/classes/decorators.spec.ts b/test/unit/classes/decorators.spec.ts index 1923f8b2b..4782746be 100644 --- a/test/unit/classes/decorators.spec.ts +++ b/test/unit/classes/decorators.spec.ts @@ -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(); @@ -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"; pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy