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

New statement namespace bugfix #208

Merged
merged 3 commits into from
Oct 12, 2020
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
62 changes: 62 additions & 0 deletions src/files/BrsFile.Class.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,30 @@ describe('BrsFile BrighterScript classes', () => {
expect(duckClass.memberMap['move']).to.exist;
});

it('supports various namespace configurations', async () => {
await program.addOrReplaceFile<BrsFile>({ src: `${rootDir}/source/main.bs`, dest: 'source/main.bs' }, `
class Animal
sub new()
bigBird = new Birds.Bird()
donald = new Birds.Duck()
end sub
end class

namespace Birds
class Bird
sub new()
dog = new Animal()
donald = new Duck()
end sub
end class
class Duck
end class
end namespace
`);
await program.validate();
expect(program.getDiagnostics()[0]?.message).not.to.exist;
});

describe('transpile', () => {
it('follows correct sequence for property initializers', async () => {
await testTranspile(`
Expand Down Expand Up @@ -305,6 +329,44 @@ describe('BrsFile BrighterScript classes', () => {
`, undefined, 'source/main.bs');
});

it('properly transpiles classes from outside current namespace', async () => {
await addFile('source/Animals.bs', `
namespace Animals
class Duck
end class
end namespace
class Bird
end class
`);
await testTranspile(`
namespace Animals
sub init()
donaldDuck = new Duck()
daffyDuck = new Animals.Duck()
bigBird = new Bird()
end sub
end namespace
`, `
sub Animals_init()
donaldDuck = Animals_Duck()
daffyDuck = Animals_Duck()
bigBird = Bird()
end sub
`, undefined, 'source/main.bs');
});

it('properly transpiles new statement for missing class ', async () => {
await testTranspile(`
sub main()
bob = new Human()
end sub
`, `
sub main()
bob = Human()
end sub
`, undefined, 'source/main.bs');
});

it('new keyword transpiles correctly', async () => {
await addFile('source/Animal.bs', `
class Animal
Expand Down
19 changes: 16 additions & 3 deletions src/parser/Expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ export class CallExpression extends Expression {
readonly openingParen: Token,
readonly closingParen: Token,
readonly args: Expression[],
/**
* The namespace that currently wraps this call expression. This is NOT the namespace of the callee...that will be represented in the callee expression itself.
*/
readonly namespaceName: NamespacedVariableNameExpression
) {
super();
Expand All @@ -75,11 +78,15 @@ export class CallExpression extends Expression {

public readonly range: Range;

transpile(state: TranspileState) {
transpile(state: TranspileState, nameOverride?: string) {
let result = [];

//transpile the name
result.push(...this.callee.transpile(state));
if (nameOverride) {
result.push(state.sourceNode(this.callee, nameOverride));
} else {
result.push(...this.callee.transpile(state));
}

result.push(
new SourceNode(this.openingParen.range.start.line + 1, this.openingParen.range.start.character, state.pathAbsolute, '(')
Expand Down Expand Up @@ -845,7 +852,13 @@ export class NewExpression extends Expression {
public readonly range: Range;

public transpile(state: TranspileState) {
return this.call.transpile(state);
const cls = state.file.getClassByName(
this.className.getName(ParseMode.BrighterScript),
this.namespaceName?.getName(ParseMode.BrighterScript)
);
//new statements within a namespace block can omit the leading namespace if the class resides in that same namespace.
//So we need to figure out if this is a namespace-omitted class, or if this class exists without a namespace.
return this.call.transpile(state, cls?.getName(ParseMode.BrightScript));
}

walk(visitor: WalkVisitor, options: WalkOptions) {
Expand Down
4 changes: 2 additions & 2 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -934,8 +934,8 @@ export class Util {
/**
* Given the class name text, return a namespace-prefixed name.
* If the name already has a period in it, or the namespaceName was not provided, return the class name as is.
* If the name does not have a period, and a namespaceName was provided, return the class name prepended
* by the namespace name
* If the name does not have a period, and a namespaceName was provided, return the class name prepended by the namespace name.
* If no namespace is provided, return the `className` unchanged.
*/
public getFulllyQualifiedClassName(className: string, namespaceName?: string) {
if (className.includes('.') === false && namespaceName) {
Expand Down
7 changes: 6 additions & 1 deletion src/validators/ClassValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ export class BsClassValidator {
*/
private getClassByName(className: string, namespaceName?: string) {
let fullName = util.getFulllyQualifiedClassName(className, namespaceName);
return this.classes[fullName.toLowerCase()];
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixes #203

let cls = this.classes[fullName.toLowerCase()];
//if we couldn't find the class by its full namespaced name, look for a global class with that name
if (!cls) {
cls = this.classes[className.toLowerCase()];
}
return cls;
}

/**
Expand Down