Skip to content

Commit

Permalink
Allow const as variable name (#670)
Browse files Browse the repository at this point in the history
  • Loading branch information
TwitchBronBron authored Aug 18, 2022
1 parent a9e5e26 commit 44847eb
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/parser/Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ export class Parser {
return this.libraryStatement();
}

if (this.check(TokenKind.Const)) {
if (this.check(TokenKind.Const) && this.checkAnyNext(TokenKind.Identifier, ...this.allowedLocalIdentifiers)) {
return this.constDeclaration();
}

Expand Down Expand Up @@ -1471,8 +1471,9 @@ export class Parser {
}

private constDeclaration(): ConstStatement | undefined {
this.warnIfNotBrighterScriptMode('const declaration');
const constToken = this.advance();
const nameToken = this.identifier();
const nameToken = this.identifier(...this.allowedLocalIdentifiers);
const equalToken = this.consumeToken(TokenKind.Equal);
const expression = this.expression();
const statement = new ConstStatement({
Expand Down
17 changes: 15 additions & 2 deletions src/parser/tests/statement/ConstStatement.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { expectCompletionsIncludes, expectZeroDiagnostics, getTestGetTypedef, ge
import util, { standardizePath as s } from '../../../util';
import { Program } from '../../../Program';
import { createSandbox } from 'sinon';
import { Parser } from '../../Parser';
import { ParseMode, Parser } from '../../Parser';
import { expect } from 'chai';
import type { ConstStatement } from '../../Statement';
import { TokenKind } from '../../../lexer/TokenKind';
Expand All @@ -27,8 +27,21 @@ describe('ConstStatement', () => {
program.dispose();
});

it('does not prevent using `const` as a variable name in .brs files', () => {
program.setFile('source/main.brs', `
sub main()
const = {
name: "Bob"
}
print const.name = {}
end sub
`);
program.validate();
expectZeroDiagnostics(program);
});

it('supports basic structure', () => {
parser.parse('const API_KEY = "abc"');
parser.parse('const API_KEY = "abc"', { mode: ParseMode.BrighterScript });
expectZeroDiagnostics(parser);
const statement = parser.ast.statements[0] as ConstStatement;
expect(statement.tokens.const?.kind).to.eql(TokenKind.Const);
Expand Down

0 comments on commit 44847eb

Please sign in to comment.