Skip to content

Commit 977bc77

Browse files
committed
feat(Parser): improve error handling
1 parent ac060ed commit 977bc77

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

modules/change_detection/src/parser/ast.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ export class AST {
66
throw new BaseException("Not supported");
77
}
88

9+
get isAssignable() {
10+
return false;
11+
}
12+
913
assign(context, value) {
1014
throw new BaseException("Not supported");
1115
}
@@ -75,6 +79,10 @@ export class FieldRead extends AST {
7579
return this.getter(this.receiver.eval(context));
7680
}
7781

82+
get isAssignable() {
83+
return true;
84+
}
85+
7886
assign(context, value) {
7987
return this.setter(this.receiver.eval(context), value);
8088
}
@@ -101,6 +109,11 @@ export class KeyedAccess extends AST {
101109
throw new BaseException(`Cannot access ${key} on ${obj}`);
102110
}
103111
}
112+
113+
get isAssignable() {
114+
return true;
115+
}
116+
104117
assign(context, value) {
105118
var obj = this.obj.eval(context);
106119
var key = this.key.eval(context);

modules/change_detection/src/parser/parser.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,16 @@ class _ParseAST {
150150
}
151151

152152
parseExpression() {
153+
var start = this.inputIndex;
153154
var result = this.parseConditional();
154155

155156
while (this.next.isOperator('=')) {
157+
if (!result.isAssignable) {
158+
var end = this.inputIndex;
159+
var expression = this.input.substring(start, end);
160+
this.error(`Expression ${expression} is not assignable`);
161+
}
162+
156163
this.expectOperator('=');
157164
result = new Assignment(result, this.parseConditional());
158165
}

modules/change_detection/test/parser/parser_spec.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,10 @@ export function main() {
179179
expect(MapWrapper.get(context.a[0], "key")).toEqual(200);
180180
});
181181

182+
it('should throw on bad assignment', () => {
183+
expectEvalError("5=4").toThrowError(new RegExp("Expression 5 is not assignable"));
184+
});
185+
182186
it('should evaluate array', () => {
183187
expectEval("[1][0]").toEqual(1);
184188
expectEval("[[1]][0][0]").toEqual(1);
@@ -188,7 +192,7 @@ export function main() {
188192
});
189193

190194
it("should error when unfinished exception", () => {
191-
expectEvalError('a[0 = 200').toThrowError(new RegExp("Missing expected ]"));
195+
expectEvalError('a[0').toThrowError(new RegExp("Missing expected ]"));
192196
});
193197

194198
it('should evaluate map', () => {
@@ -201,8 +205,6 @@ export function main() {
201205
});
202206

203207
describe("parseBinding", () => {
204-
//throw on assignment
205-
206208
it("should parse formatters", function () {
207209
var exp = parseBinding("'Foo'|uppercase");
208210
expect(exp).toBeAnInstanceOf(Formatter);

0 commit comments

Comments
 (0)