Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit a02c886

Browse files
jbedardpetebacondarwin
authored andcommitted
fix($parse): validate assignment lval in parser phase
The parser always threw an error in the case of an invalid left-value assignment but it was an unhelpful: ``` Cannot set property 'undefined' of undefined ``` This commit provides a more meaningful error message, so it is not a breaking change. Closes #15234
1 parent faf0c3e commit a02c886

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

src/ng/parse.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,10 @@ AST.prototype = {
338338
assignment: function() {
339339
var result = this.ternary();
340340
if (this.expect('=')) {
341+
if (!isAssignable(result)) {
342+
throw $parseMinErr('lval', 'Trying to assign a value to a non l-value');
343+
}
344+
341345
result = { type: AST.AssignmentExpression, left: result, right: this.assignment(), operator: '='};
342346
}
343347
return result;
@@ -1024,9 +1028,6 @@ ASTCompiler.prototype = {
10241028
case AST.AssignmentExpression:
10251029
right = this.nextId();
10261030
left = {};
1027-
if (!isAssignable(ast.left)) {
1028-
throw $parseMinErr('lval', 'Trying to assign a value to a non l-value');
1029-
}
10301031
this.recurse(ast.left, undefined, left, function() {
10311032
self.if_(self.notNull(left.context), function() {
10321033
self.recurse(ast.right, right);

test/ng/parseSpec.js

+14
Original file line numberDiff line numberDiff line change
@@ -2129,6 +2129,20 @@ describe('parser', function() {
21292129
expect(scope.b).toEqual(234);
21302130
});
21312131

2132+
it('should throw with invalid left-val in assignments', function() {
2133+
expect(function() { scope.$eval('1 = 1'); }).toThrowMinErr('$parse', 'lval');
2134+
expect(function() { scope.$eval('{} = 1'); }).toThrowMinErr('$parse', 'lval');
2135+
expect(function() { scope.$eval('[] = 1'); }).toThrowMinErr('$parse', 'lval');
2136+
expect(function() { scope.$eval('true = 1'); }).toThrowMinErr('$parse', 'lval');
2137+
expect(function() { scope.$eval('(a=b) = 1'); }).toThrowMinErr('$parse', 'lval');
2138+
expect(function() { scope.$eval('(1<2) = 1'); }).toThrowMinErr('$parse', 'lval');
2139+
expect(function() { scope.$eval('(1+2) = 1'); }).toThrowMinErr('$parse', 'lval');
2140+
expect(function() { scope.$eval('!v = 1'); }).toThrowMinErr('$parse', 'lval');
2141+
expect(function() { scope.$eval('this = 1'); }).toThrowMinErr('$parse', 'lval');
2142+
expect(function() { scope.$eval('+v = 1'); }).toThrowMinErr('$parse', 'lval');
2143+
expect(function() { scope.$eval('(1?v1:v2) = 1'); }).toThrowMinErr('$parse', 'lval');
2144+
});
2145+
21322146
it('should evaluate assignments in ternary operator', function() {
21332147
scope.$eval('a = 1 ? 2 : 3');
21342148
expect(scope.a).toBe(2);

0 commit comments

Comments
 (0)