From 8a829d346b9340d86c6ebc9f990b323c7ff03743 Mon Sep 17 00:00:00 2001 From: vsavkin Date: Tue, 4 Nov 2014 09:21:28 -0800 Subject: [PATCH] feat(parser): throw when expected an identifier --- modules/change_detection/src/parser/parser.js | 5 ++++- .../test/parser/parser_spec.js | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/modules/change_detection/src/parser/parser.js b/modules/change_detection/src/parser/parser.js index ce5a15113e..246e0071cc 100644 --- a/modules/change_detection/src/parser/parser.js +++ b/modules/change_detection/src/parser/parser.js @@ -241,6 +241,9 @@ class _ParseAST { parseIdentifier():string { var n = this.next; + if (!n.isIdentifier() && !n.isKeyword()) { + this.error(`Unexpected token ${n}, expected identifier or keyword`) + } this.advance(); return n.toString(); } @@ -249,7 +252,7 @@ class _ParseAST { if (isBlank(index)) index = this.index; var location = (index < this.tokens.length) - ? `at column ${tokens[index].index + 1} in` + ? `at column ${this.tokens[index].index + 1} in` : `at the end of the expression`; throw new ParserError(`Parser Error: ${message} ${location} [${this.input}]`); diff --git a/modules/change_detection/test/parser/parser_spec.js b/modules/change_detection/test/parser/parser_spec.js index 61f9583d14..b55acf4efa 100644 --- a/modules/change_detection/test/parser/parser_spec.js +++ b/modules/change_detection/test/parser/parser_spec.js @@ -10,6 +10,12 @@ class TestData { } } +class ContextWithErrors { + get boo() { + throw new Error("boo to you"); + } +} + export function main() { function td(a = 0, b = 0) { return new TestData(a, b); @@ -145,6 +151,18 @@ export function main() { it('should throw on incorrect ternary operator syntax', () => { expectEvalError("true?1").toThrowError(new RegExp('Parser Error: Conditional expression true\\?1 requires all 3 expressions')); }); + + it('should pass exceptions', () => { + expect(() => { + createParser().parse('boo').eval(new ContextWithErrors(), null); + }).toThrowError('boo to you'); + }); + + it('should only allow identifier or keyword as member names', () => { + expectEvalError('x.(').toThrowError(new RegExp('identifier or keyword')); + expectEvalError('x. 1234').toThrowError(new RegExp('identifier or keyword')); + expectEvalError('x."foo"').toThrowError(new RegExp('identifier or keyword')); + }); }); }); }