Skip to content

Commit 8a829d3

Browse files
committed
feat(parser): throw when expected an identifier
1 parent c41f59c commit 8a829d3

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

modules/change_detection/src/parser/parser.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,9 @@ class _ParseAST {
241241

242242
parseIdentifier():string {
243243
var n = this.next;
244+
if (!n.isIdentifier() && !n.isKeyword()) {
245+
this.error(`Unexpected token ${n}, expected identifier or keyword`)
246+
}
244247
this.advance();
245248
return n.toString();
246249
}
@@ -249,7 +252,7 @@ class _ParseAST {
249252
if (isBlank(index)) index = this.index;
250253

251254
var location = (index < this.tokens.length)
252-
? `at column ${tokens[index].index + 1} in`
255+
? `at column ${this.tokens[index].index + 1} in`
253256
: `at the end of the expression`;
254257

255258
throw new ParserError(`Parser Error: ${message} ${location} [${this.input}]`);

modules/change_detection/test/parser/parser_spec.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ class TestData {
1010
}
1111
}
1212

13+
class ContextWithErrors {
14+
get boo() {
15+
throw new Error("boo to you");
16+
}
17+
}
18+
1319
export function main() {
1420
function td(a = 0, b = 0) {
1521
return new TestData(a, b);
@@ -145,6 +151,18 @@ export function main() {
145151
it('should throw on incorrect ternary operator syntax', () => {
146152
expectEvalError("true?1").toThrowError(new RegExp('Parser Error: Conditional expression true\\?1 requires all 3 expressions'));
147153
});
154+
155+
it('should pass exceptions', () => {
156+
expect(() => {
157+
createParser().parse('boo').eval(new ContextWithErrors(), null);
158+
}).toThrowError('boo to you');
159+
});
160+
161+
it('should only allow identifier or keyword as member names', () => {
162+
expectEvalError('x.(').toThrowError(new RegExp('identifier or keyword'));
163+
expectEvalError('x. 1234').toThrowError(new RegExp('identifier or keyword'));
164+
expectEvalError('x."foo"').toThrowError(new RegExp('identifier or keyword'));
165+
});
148166
});
149167
});
150168
}

0 commit comments

Comments
 (0)