Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix transpile for non-namespaced enums in namespaced functions #985

Merged
merged 1 commit into from
Dec 8, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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