Skip to content

Commit 082e493

Browse files
authored
Fix compileMembersOnly ignoring preceding variable path (#662)
1 parent 1850e6c commit 082e493

2 files changed

Lines changed: 43 additions & 5 deletions

File tree

src/LuaTransformer.ts

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1675,16 +1675,27 @@ export class LuaTransformer {
16751675
for (const enumMember of this.computeEnumMembers(enumDeclaration)) {
16761676
const memberName = this.transformPropertyName(enumMember.name);
16771677
if (membersOnly) {
1678+
const enumSymbol = this.checker.getSymbolAtLocation(enumDeclaration.name);
1679+
const exportScope = enumSymbol ? this.getSymbolExportScope(enumSymbol) : undefined;
1680+
16781681
if (tstl.isIdentifier(memberName)) {
16791682
result.push(
1680-
...this.createLocalOrExportedOrGlobalDeclaration(memberName, enumMember.value, enumDeclaration)
1683+
...this.createLocalOrExportedOrGlobalDeclaration(
1684+
memberName,
1685+
enumMember.value,
1686+
enumDeclaration,
1687+
undefined,
1688+
exportScope
1689+
)
16811690
);
16821691
} else {
16831692
result.push(
16841693
...this.createLocalOrExportedOrGlobalDeclaration(
16851694
tstl.createIdentifier(enumMember.name.getText(), enumMember.name),
16861695
enumMember.value,
1687-
enumDeclaration
1696+
enumDeclaration,
1697+
undefined,
1698+
exportScope
16881699
)
16891700
);
16901701
}
@@ -4087,7 +4098,16 @@ export class LuaTransformer {
40874098
const decorators = tsHelper.getCustomDecorators(type, this.checker);
40884099
// Do not output path for member only enums
40894100
if (decorators.has(DecoratorKind.CompileMembersOnly)) {
4090-
return tstl.createIdentifier(property, expression);
4101+
if (ts.isPropertyAccessExpression(expression.expression)) {
4102+
// in case of ...x.enum.y transform to ...x.y
4103+
return tstl.createTableIndexExpression(
4104+
this.transformExpression(expression.expression.expression),
4105+
tstl.createStringLiteral(property),
4106+
expression
4107+
);
4108+
} else {
4109+
return tstl.createIdentifier(property, expression);
4110+
}
40914111
}
40924112

40934113
if (decorators.has(DecoratorKind.LuaTable)) {
@@ -5161,7 +5181,8 @@ export class LuaTransformer {
51615181
lhs: tstl.Identifier | tstl.Identifier[],
51625182
rhs?: tstl.Expression | tstl.Expression[],
51635183
tsOriginal?: ts.Node,
5164-
parent?: tstl.Node
5184+
parent?: tstl.Node,
5185+
overrideExportScope?: ts.SourceFile | ts.ModuleDeclaration
51655186
): tstl.Statement[] {
51665187
let declaration: tstl.VariableDeclarationStatement | undefined;
51675188
let assignment: tstl.AssignmentStatement | undefined;
@@ -5173,7 +5194,7 @@ export class LuaTransformer {
51735194
return [];
51745195
}
51755196

5176-
const exportScope = this.getIdentifierExportScope(identifiers[0]);
5197+
const exportScope = overrideExportScope || this.getIdentifierExportScope(identifiers[0]);
51775198
if (exportScope) {
51785199
// exported
51795200
if (!rhs) {

test/unit/lualib/lualib.spec.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,20 @@ test("lualibs should not include tstl header", () => {
150150

151151
expect(util.transpileString(code)).not.toMatch("Generated with");
152152
});
153+
154+
test("compileMembersOnly in namespace", () => {
155+
const header = `
156+
namespace wifi {
157+
/** @compileMembersOnly */
158+
export enum WifiMode {
159+
NULLMODE = 0,
160+
STATION = 1,
161+
SOFTAP = 2
162+
}
163+
}`;
164+
const code = `
165+
return wifi.WifiMode.STATION;
166+
`;
167+
168+
expect(util.transpileAndExecute(code, undefined, undefined, header)).toBe(1);
169+
});

0 commit comments

Comments
 (0)