Skip to content
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
42 changes: 41 additions & 1 deletion src/parser/Parser.Class.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import { Parser, ParseMode } from './Parser';
import type { FunctionStatement, AssignmentStatement, FieldStatement } from './Statement';
import { ClassStatement } from './Statement';
import { NewExpression } from './Expression';
import { expectDiagnosticsIncludes, expectZeroDiagnostics } from '../testHelpers.spec';
import { expectDiagnostics, expectDiagnosticsIncludes, expectZeroDiagnostics } from '../testHelpers.spec';
import { isClassStatement } from '../astUtils/reflection';
import util from '../util';

describe('parser class', () => {
it('throws exception when used in brightscript scope', () => {
Expand Down Expand Up @@ -94,6 +95,45 @@ describe('parser class', () => {
expect(parser.diagnostics[0]?.message).to.eql(DiagnosticMessages.cannotUseReservedWordAsIdentifier('throw').message);
});

it('does not allow function param named "type"', () => {
const parser = Parser.parse(`
sub test(type as string)
end sub
`);

expectDiagnostics(parser, [{
...DiagnosticMessages.cannotUseReservedWordAsIdentifier('type'),
range: util.createRange(1, 21, 1, 25)
}]);
});

it('does not allow class method named "type"', () => {
const parser = Parser.parse(`
class Person
sub test(type as string)
end sub
end class
`, { mode: ParseMode.BrighterScript });

expectDiagnostics(parser, [{
...DiagnosticMessages.cannotUseReservedWordAsIdentifier('type'),
range: util.createRange(2, 25, 2, 29)
}]);
});

it('does not allow interface method named "type"', () => {
const parser = Parser.parse(`
interface Person
sub test(type as string)
end interface
`, { mode: ParseMode.BrighterScript });

expectDiagnostics(parser, [{
...DiagnosticMessages.cannotUseReservedWordAsIdentifier('type'),
range: util.createRange(2, 25, 2, 29)
}]);
});

it('supports the try/catch keywords in various places', () => {
const parser = Parser.parse(`
sub main()
Expand Down
8 changes: 8 additions & 0 deletions src/parser/Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -998,6 +998,14 @@ export class Parser {
// force the name into an identifier so the AST makes some sense
name.kind = TokenKind.Identifier;

//add diagnostic if name is a reserved word that cannot be used as an identifier
if (DisallowedLocalIdentifiersText.has(name.text.toLowerCase())) {
this.diagnostics.push({
...DiagnosticMessages.cannotUseReservedWordAsIdentifier(name.text),
range: name.range
});
}

let typeToken: Token | undefined;
let defaultValue;

Expand Down
Loading