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
31 changes: 26 additions & 5 deletions src/LuaTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1675,16 +1675,27 @@ export class LuaTransformer {
for (const enumMember of this.computeEnumMembers(enumDeclaration)) {
const memberName = this.transformPropertyName(enumMember.name);
if (membersOnly) {
const enumSymbol = this.checker.getSymbolAtLocation(enumDeclaration.name);
const exportScope = enumSymbol ? this.getSymbolExportScope(enumSymbol) : undefined;

if (tstl.isIdentifier(memberName)) {
result.push(
...this.createLocalOrExportedOrGlobalDeclaration(memberName, enumMember.value, enumDeclaration)
...this.createLocalOrExportedOrGlobalDeclaration(
memberName,
enumMember.value,
enumDeclaration,
undefined,
exportScope
)
);
} else {
result.push(
...this.createLocalOrExportedOrGlobalDeclaration(
tstl.createIdentifier(enumMember.name.getText(), enumMember.name),
enumMember.value,
enumDeclaration
enumDeclaration,
undefined,
exportScope
)
);
}
Expand Down Expand Up @@ -4087,7 +4098,16 @@ export class LuaTransformer {
const decorators = tsHelper.getCustomDecorators(type, this.checker);
// Do not output path for member only enums
if (decorators.has(DecoratorKind.CompileMembersOnly)) {
return tstl.createIdentifier(property, expression);
if (ts.isPropertyAccessExpression(expression.expression)) {
// in case of ...x.enum.y transform to ...x.y
return tstl.createTableIndexExpression(
this.transformExpression(expression.expression.expression),
tstl.createStringLiteral(property),
expression
);
} else {
return tstl.createIdentifier(property, expression);
}
}

if (decorators.has(DecoratorKind.LuaTable)) {
Expand Down Expand Up @@ -5161,7 +5181,8 @@ export class LuaTransformer {
lhs: tstl.Identifier | tstl.Identifier[],
rhs?: tstl.Expression | tstl.Expression[],
tsOriginal?: ts.Node,
parent?: tstl.Node
parent?: tstl.Node,
overrideExportScope?: ts.SourceFile | ts.ModuleDeclaration
): tstl.Statement[] {
let declaration: tstl.VariableDeclarationStatement | undefined;
let assignment: tstl.AssignmentStatement | undefined;
Expand All @@ -5173,7 +5194,7 @@ export class LuaTransformer {
return [];
}

const exportScope = this.getIdentifierExportScope(identifiers[0]);
const exportScope = overrideExportScope || this.getIdentifierExportScope(identifiers[0]);
if (exportScope) {
// exported
if (!rhs) {
Expand Down
17 changes: 17 additions & 0 deletions test/unit/lualib/lualib.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,20 @@ test("lualibs should not include tstl header", () => {

expect(util.transpileString(code)).not.toMatch("Generated with");
});

test("compileMembersOnly in namespace", () => {
const header = `
namespace wifi {
/** @compileMembersOnly */
export enum WifiMode {
NULLMODE = 0,
STATION = 1,
SOFTAP = 2
}
}`;
const code = `
return wifi.WifiMode.STATION;
`;

expect(util.transpileAndExecute(code, undefined, undefined, header)).toBe(1);
});