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
20 changes: 18 additions & 2 deletions src/transformation/visitors/access.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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,
Expand Down Expand Up @@ -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(
Expand All @@ -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) };
}
}
}

Expand Down
22 changes: 22 additions & 0 deletions test/unit/modules/modules.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
});