diff --git a/src/transformation/visitors/access.ts b/src/transformation/visitors/access.ts index 8004d9f2d..c97c0f4c7 100644 --- a/src/transformation/visitors/access.ts +++ b/src/transformation/visitors/access.ts @@ -9,7 +9,7 @@ import { unsupportedOptionalCompileMembersOnly, } from "../utils/diagnostics"; import { getExtensionKindForNode } from "../utils/language-extensions"; -import { addToNumericExpression } from "../utils/lua-ast"; +import { addToNumericExpression, createExportsIdentifier } from "../utils/lua-ast"; import { LuaLibFeature, transformLuaLibFunction } from "../utils/lualib"; import { isArrayType, isNumberType, isStringType } from "../utils/typescript"; import { tryGetConstEnumValue } from "./enum"; @@ -24,6 +24,7 @@ import { } from "./optional-chaining"; import { SyntaxKind } from "typescript"; import { getCustomNameFromSymbol } from "./identifier"; +import { getSymbolExportScope, isSymbolExported } from "../utils/export"; function addOneToArrayAccessArgument( context: TransformationContext, @@ -138,6 +139,7 @@ export function transformPropertyAccessExpressionWithCapture( if (isOptionalLeft) { context.diagnostics.push(unsupportedOptionalCompileMembersOnly(node)); } + if (ts.isPropertyAccessExpression(node.expression)) { // in case of ...x.enum.y transform to ...x.y const expression = lua.createTableIndexExpression( @@ -147,7 +149,21 @@ export function transformPropertyAccessExpressionWithCapture( ); return { expression }; } else { - return { expression: lua.createIdentifier(property, node) }; + // Check if we need to account for enum being exported int his file + if ( + isSymbolExported(context, type.symbol) && + getSymbolExportScope(context, type.symbol) === node.expression.getSourceFile() + ) { + return { + expression: lua.createTableIndexExpression( + createExportsIdentifier(), + lua.createStringLiteral(property), + node + ), + }; + } else { + return { expression: lua.createIdentifier(property, node) }; + } } } diff --git a/test/unit/modules/modules.spec.ts b/test/unit/modules/modules.spec.ts index bbe978226..d15f18463 100644 --- a/test/unit/modules/modules.spec.ts +++ b/test/unit/modules/modules.spec.ts @@ -291,3 +291,25 @@ test("import expression", () => { .setOptions({ module: ts.ModuleKind.ESNext }) .expectToEqual({ result: "foo" }); }); + +// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1572 +test("correctly exports @compileMembersOnly enums (#1572)", () => { + util.testModule` + export { val } from "./otherfile"; + ` + .addExtraFile( + "otherfile.ts", + ` + // Would put this in the main file, but we cannot transfer enum types over lua/js boundary + // but we still need to have an exported enum, hence it is in another file + /** @compileMembersOnly */ + export enum MyEnum { + A = 0, + B = 1, + C = 2 + } + export const val = MyEnum.B | MyEnum.C; + ` + ) + .expectToEqual({ val: 3 }); +});