Skip to content

Commit a93ac8b

Browse files
authored
Fix use of exported compileMembersOnly enums causing syntax errors (#1573)
* Fix use of exported compileMembersOnly enums causing syntax errors * fix test * fix prettier
1 parent a07fed5 commit a93ac8b

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

src/transformation/visitors/access.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
unsupportedOptionalCompileMembersOnly,
1010
} from "../utils/diagnostics";
1111
import { getExtensionKindForNode } from "../utils/language-extensions";
12-
import { addToNumericExpression } from "../utils/lua-ast";
12+
import { addToNumericExpression, createExportsIdentifier } from "../utils/lua-ast";
1313
import { LuaLibFeature, transformLuaLibFunction } from "../utils/lualib";
1414
import { isArrayType, isNumberType, isStringType } from "../utils/typescript";
1515
import { tryGetConstEnumValue } from "./enum";
@@ -24,6 +24,7 @@ import {
2424
} from "./optional-chaining";
2525
import { SyntaxKind } from "typescript";
2626
import { getCustomNameFromSymbol } from "./identifier";
27+
import { getSymbolExportScope, isSymbolExported } from "../utils/export";
2728

2829
function addOneToArrayAccessArgument(
2930
context: TransformationContext,
@@ -138,6 +139,7 @@ export function transformPropertyAccessExpressionWithCapture(
138139
if (isOptionalLeft) {
139140
context.diagnostics.push(unsupportedOptionalCompileMembersOnly(node));
140141
}
142+
141143
if (ts.isPropertyAccessExpression(node.expression)) {
142144
// in case of ...x.enum.y transform to ...x.y
143145
const expression = lua.createTableIndexExpression(
@@ -147,7 +149,21 @@ export function transformPropertyAccessExpressionWithCapture(
147149
);
148150
return { expression };
149151
} else {
150-
return { expression: lua.createIdentifier(property, node) };
152+
// Check if we need to account for enum being exported int his file
153+
if (
154+
isSymbolExported(context, type.symbol) &&
155+
getSymbolExportScope(context, type.symbol) === node.expression.getSourceFile()
156+
) {
157+
return {
158+
expression: lua.createTableIndexExpression(
159+
createExportsIdentifier(),
160+
lua.createStringLiteral(property),
161+
node
162+
),
163+
};
164+
} else {
165+
return { expression: lua.createIdentifier(property, node) };
166+
}
151167
}
152168
}
153169

test/unit/modules/modules.spec.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,3 +291,25 @@ test("import expression", () => {
291291
.setOptions({ module: ts.ModuleKind.ESNext })
292292
.expectToEqual({ result: "foo" });
293293
});
294+
295+
// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1572
296+
test("correctly exports @compileMembersOnly enums (#1572)", () => {
297+
util.testModule`
298+
export { val } from "./otherfile";
299+
`
300+
.addExtraFile(
301+
"otherfile.ts",
302+
`
303+
// Would put this in the main file, but we cannot transfer enum types over lua/js boundary
304+
// but we still need to have an exported enum, hence it is in another file
305+
/** @compileMembersOnly */
306+
export enum MyEnum {
307+
A = 0,
308+
B = 1,
309+
C = 2
310+
}
311+
export const val = MyEnum.B | MyEnum.C;
312+
`
313+
)
314+
.expectToEqual({ val: 3 });
315+
});

0 commit comments

Comments
 (0)