Skip to content

Commit

Permalink
Fix missing constant references (#658)
Browse files Browse the repository at this point in the history
  • Loading branch information
TwitchBronBron authored Aug 3, 2022
1 parent 2695401 commit 2afde57
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 2 deletions.
15 changes: 15 additions & 0 deletions src/Scope.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,21 @@ describe('Scope', () => {
]);
});

it('detects unknown const in assignment operator', () => {
program.setFile('source/main.bs', `
sub main()
value = ""
value += constants.API_KEY
value += API_URL
end sub
`);
program.validate();
expectDiagnostics(program, [
DiagnosticMessages.cannotFindName('constants'),
DiagnosticMessages.cannotFindName('API_URL')
]);
});

it('detects unknown local var names', () => {
program.setFile('source/lib.bs', `
sub libFunc(param1)
Expand Down
30 changes: 30 additions & 0 deletions src/bscPlugin/hover/HoverProcessor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,36 @@ describe('HoverProcessor', () => {
expect(hover?.contents).to.eql(fence('const SOME_VALUE = true'));
});

it('finds top-level constant in assignment expression', () => {
program.setFile('source/main.bs', `
sub main()
value = ""
value += SOME_VALUE
end sub
const SOME_VALUE = "value"
`);
// value += SOME|_VALUE
let hover = program.getHover('source/main.bs', util.createPosition(3, 33))[0];
expect(hover?.range).to.eql(util.createRange(3, 29, 3, 39));
expect(hover?.contents).to.eql(fence('const SOME_VALUE = "value"'));
});

it('finds namespaced constant in assignment expression', () => {
program.setFile('source/main.bs', `
sub main()
value = ""
value += someNamespace.SOME_VALUE
end sub
namespace someNamespace
const SOME_VALUE = "value"
end namespace
`);
// value += SOME|_VALUE
let hover = program.getHover('source/main.bs', util.createPosition(3, 47))[0];
expect(hover?.range).to.eql(util.createRange(3, 43, 3, 53));
expect(hover?.contents).to.eql(fence('const someNamespace.SOME_VALUE = "value"'));
});

it('finds namespaced constant value', () => {
program.setFile('source/main.bs', `
sub main()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,4 +320,49 @@ describe('BrsFileSemanticTokensProcessor', () => {
tokenModifiers: [SemanticTokenModifiers.readonly, SemanticTokenModifiers.static]
}]);
});

it('matches consts in assignment expressions', () => {
const file = program.setFile<BrsFile>('source/main.bs', `
sub main()
value = ""
value += constants.API_KEY
value += API_URL
end sub
namespace constants
const API_KEY = "test"
end namespace
const API_URL = "url"
`);
expectSemanticTokens(file, [
// value += |constants|.API_KEY
{
range: util.createRange(3, 25, 3, 34),
tokenType: SemanticTokenTypes.namespace
},
// value += constants.|API_KEY|
{
range: util.createRange(3, 35, 3, 42),
tokenType: SemanticTokenTypes.variable,
tokenModifiers: [SemanticTokenModifiers.readonly, SemanticTokenModifiers.static]
},
// value += |API_URL|
{
range: util.createRange(4, 25, 4, 32),
tokenType: SemanticTokenTypes.variable,
tokenModifiers: [SemanticTokenModifiers.readonly, SemanticTokenModifiers.static]
},
// const |API_KEY| = "test"
{
range: util.createRange(7, 22, 7, 29),
tokenType: SemanticTokenTypes.variable,
tokenModifiers: [SemanticTokenModifiers.readonly, SemanticTokenModifiers.static]
},
//const |API_URL| = "url"
{
range: util.createRange(9, 18, 9, 25),
tokenType: SemanticTokenTypes.variable,
tokenModifiers: [SemanticTokenModifiers.readonly, SemanticTokenModifiers.static]
}
]);
});
});
4 changes: 4 additions & 0 deletions src/parser/Parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ describe('parser', () => {

it('works for references.expressions', () => {
const parser = Parser.parse(`
b += "plus-equal"
a += 1 + 2
b += getValue1() + getValue2()
increment++
Expand All @@ -81,6 +82,9 @@ describe('parser', () => {
end function
`);
const expected = [
'"plus-equal"',
'b',
'b += "plus-equal"',
'1',
'2',
'a',
Expand Down
6 changes: 4 additions & 2 deletions src/parser/Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1053,8 +1053,10 @@ export class Parser {
this.currentFunctionExpression
);
this.addExpressionsToReferences(nameExpression);
//remove the right-hand-side expression from this assignment operator, and replace with the full assignment expression
this._references.expressions.delete(value);
if (isBinaryExpression(value)) {
//remove the right-hand-side expression from this assignment operator, and replace with the full assignment expression
this._references.expressions.delete(value);
}
this._references.expressions.add(result);
}

Expand Down
20 changes: 20 additions & 0 deletions src/parser/tests/statement/ConstStatement.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,26 @@ describe('ConstStatement', () => {
end sub
`);
});

it('transpiles within += operator', () => {
testTranspile(`
namespace constants
const API_KEY = "test"
end namespace
const API_URL = "url"
sub main()
value = ""
value += constants.API_KEY
value += API_URL
end sub
`, `
sub main()
value = ""
value += "test"
value += "url"
end sub
`);
});
});

describe('completions', () => {
Expand Down

0 comments on commit 2afde57

Please sign in to comment.