Skip to content

Commit

Permalink
Fix broken enum transpiling (#985)
Browse files Browse the repository at this point in the history
  • Loading branch information
TwitchBronBron authored Dec 8, 2023
1 parent d07de5c commit 390e91f
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 18 deletions.
1 change: 0 additions & 1 deletion src/Scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,6 @@ export class Scope {
let member = enumeration.item.findChild<EnumMemberStatement>((child) => isEnumMemberStatement(child) && child.name?.toLowerCase() === memberName);
return member ? { item: member, file: enumeration.file } : undefined;
}
return enumeration;
}

/**
Expand Down
32 changes: 15 additions & 17 deletions src/bscPlugin/transpile/BrsFilePreTranspileProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,32 +39,30 @@ export class BrsFilePreTranspileProcessor {
* For example, all of these would return the enum: `SomeNamespace.SomeEnum.SomeMember`, SomeEnum.SomeMember, `SomeEnum`
*/
private getEnumInfo(name: string, containingNamespace: string, scope: Scope) {
//look for the enum directly
let result = scope?.getEnumFileLink(name, containingNamespace);

if (result) {
//do we have an enum MEMBER reference? (i.e. SomeEnum.someMember or SomeNamespace.SomeEnum.SomeMember)
let memberLink = scope.getEnumMemberFileLink(name, containingNamespace);
if (memberLink) {
const value = memberLink.item.getValue();
return {
enum: result.item
};
}
//assume we've been given the enum.member syntax, so pop the member and try again
const parts = name.toLowerCase().split('.');
const memberName = parts.pop();
if (containingNamespace && parts[0] !== containingNamespace.toLowerCase()) {
parts.unshift(containingNamespace.toLowerCase());
}
result = scope?.getEnumMap().get(parts.join('.'));
if (result) {
const value = result.item.getMemberValue(memberName);
return {
enum: result.item,
enum: memberLink.item.parent,
value: new LiteralExpression(createToken(
//just use float literal for now...it will transpile properly with any literal value
value.startsWith('"') ? TokenKind.StringLiteral : TokenKind.FloatLiteral,
value
))
};
}

//do we have an enum reference? (i.e. SomeEnum or SomeNamespace.SomeEnum)
let enumLink = scope?.getEnumFileLink(name, containingNamespace);

if (enumLink) {
return {
enum: enumLink.item
};
}

}

private processExpression(expression: Expression, scope: Scope) {
Expand Down
7 changes: 7 additions & 0 deletions src/parser/Statement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2574,6 +2574,13 @@ export class EnumMemberStatement extends Statement implements TypedefProvider {
);
}

/**
* Get the value of this enum. Requires that `.parent` is set
*/
public getValue() {
return (this.parent as EnumStatement).getMemberValue(this.name);
}

public transpile(state: BrsTranspileState): TranspileResult {
return [];
}
Expand Down
18 changes: 18 additions & 0 deletions src/parser/tests/statement/Enum.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,24 @@ describe('EnumStatement', () => {
});

describe('transpile', () => {
it('supports non-namespaced enum from within a namespace', () => {
program.options.autoImportComponentScript = true;
testTranspile(`
namespace alpha
sub test()
print Direction.up
end sub
end namespace
enum Direction
up = "up"
end enum
`, `
sub alpha_test()
print "up"
end sub
`);
});

it('transpiles negative number', () => {
testTranspile(`
sub main()
Expand Down

0 comments on commit 390e91f

Please sign in to comment.