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 cross namespace collision detection #1008

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
45 changes: 45 additions & 0 deletions src/Scope.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,51 @@ describe('Scope', () => {
expectZeroDiagnostics(program);
});

it('does not flag unrelated namespace const and function collision', () => {
program.setFile('source/main.bs', `
namespace alpha
const options = {}
end namespace
namespace beta
sub options()
end sub
end namespace
`);
program.validate();
expectZeroDiagnostics(program);
});

it('does flag related namespace const and function collision', () => {
program.setFile('source/main.bs', `
namespace alpha
const options = {}
end namespace
namespace alpha
sub options()
end sub
end namespace
`);
program.validate();
expectDiagnostics(program, [
DiagnosticMessages.nameCollision('Const', 'Function', 'options').message
]);
});

it('does flag namespaced const and un-namespaced function collision', () => {
program.setFile('source/main.bs', `
namespace alpha
const options = {}
end namespace

sub options()
end sub
`);
program.validate();
expectDiagnostics(program, [
DiagnosticMessages.nameCollision('Const', 'Function', 'options').message
]);
});

it('flags parameter with same name as namespace', () => {
program.setFile('source/main.bs', `
namespace NameA.NameB
Expand Down
7 changes: 5 additions & 2 deletions src/Scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,10 +264,13 @@ export class Scope {
links.push({ item: nsContainersWithStatement?.namespaceStatements?.[0], file: nsContainersWithStatement?.file as BrsFile });
}
}

const fullNameLower = (containingNamespace ? `${containingNamespace}.${name}` : name).toLowerCase();
const callable = this.getCallableByName(name);
TwitchBronBron marked this conversation as resolved.
Show resolved Hide resolved
if (callable) {
links.push({ item: callable.functionStatement, file: callable.file as BrsFile });
if (!callable.hasNamespace || callable.getName(ParseMode.BrighterScript).toLowerCase() === fullNameLower) {
// this callable has no namespace, or has same namespace
links.push({ item: callable.functionStatement, file: callable.file as BrsFile });
}
}
// remove empty links
return links.filter(link => link);
Expand Down