Skip to content

Commit 2f76c09

Browse files
Flag param names that are reserved words (#1556)
1 parent 453aa49 commit 2f76c09

2 files changed

Lines changed: 49 additions & 1 deletion

File tree

src/parser/Parser.Class.spec.ts

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ import { Parser, ParseMode } from './Parser';
66
import type { FunctionStatement, AssignmentStatement, FieldStatement } from './Statement';
77
import { ClassStatement } from './Statement';
88
import { NewExpression } from './Expression';
9-
import { expectDiagnosticsIncludes, expectZeroDiagnostics } from '../testHelpers.spec';
9+
import { expectDiagnostics, expectDiagnosticsIncludes, expectZeroDiagnostics } from '../testHelpers.spec';
1010
import { isClassStatement } from '../astUtils/reflection';
11+
import util from '../util';
1112

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

98+
it('does not allow function param named "type"', () => {
99+
const parser = Parser.parse(`
100+
sub test(type as string)
101+
end sub
102+
`);
103+
104+
expectDiagnostics(parser, [{
105+
...DiagnosticMessages.cannotUseReservedWordAsIdentifier('type'),
106+
range: util.createRange(1, 21, 1, 25)
107+
}]);
108+
});
109+
110+
it('does not allow class method named "type"', () => {
111+
const parser = Parser.parse(`
112+
class Person
113+
sub test(type as string)
114+
end sub
115+
end class
116+
`, { mode: ParseMode.BrighterScript });
117+
118+
expectDiagnostics(parser, [{
119+
...DiagnosticMessages.cannotUseReservedWordAsIdentifier('type'),
120+
range: util.createRange(2, 25, 2, 29)
121+
}]);
122+
});
123+
124+
it('does not allow interface method named "type"', () => {
125+
const parser = Parser.parse(`
126+
interface Person
127+
sub test(type as string)
128+
end interface
129+
`, { mode: ParseMode.BrighterScript });
130+
131+
expectDiagnostics(parser, [{
132+
...DiagnosticMessages.cannotUseReservedWordAsIdentifier('type'),
133+
range: util.createRange(2, 25, 2, 29)
134+
}]);
135+
});
136+
97137
it('supports the try/catch keywords in various places', () => {
98138
const parser = Parser.parse(`
99139
sub main()

src/parser/Parser.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -998,6 +998,14 @@ export class Parser {
998998
// force the name into an identifier so the AST makes some sense
999999
name.kind = TokenKind.Identifier;
10001000

1001+
//add diagnostic if name is a reserved word that cannot be used as an identifier
1002+
if (DisallowedLocalIdentifiersText.has(name.text.toLowerCase())) {
1003+
this.diagnostics.push({
1004+
...DiagnosticMessages.cannotUseReservedWordAsIdentifier(name.text),
1005+
range: name.range
1006+
});
1007+
}
1008+
10011009
let typeToken: Token | undefined;
10021010
let defaultValue;
10031011

0 commit comments

Comments
 (0)