Skip to content

Commit c41f59c

Browse files
committed
feat(parser): change Parser to return null when one of the operands is null
1 parent a7fe25d commit c41f59c

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

modules/change_detection/src/parser/ast.js

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -86,19 +86,7 @@ export class Binary extends AST {
8686
var right = this.right.eval(context, formatters);
8787

8888
// Null check for the operations.
89-
if (left == null || right == null) {
90-
switch (this.operation) {
91-
case '+':
92-
if (left != null) return left;
93-
if (right != null) return right;
94-
return 0;
95-
case '-':
96-
if (left != null) return left;
97-
if (right != null) return 0 - right;
98-
return 0;
99-
}
100-
return null;
101-
}
89+
if (left == null || right == null) return null;
10290

10391
switch (this.operation) {
10492
case '+' : return autoConvertAdd(left, right);

modules/change_detection/test/parser/parser_spec.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ export function main() {
1818
var context = td();
1919
var formatters;
2020

21+
function createParser() {
22+
return new Parser(new Lexer(), new ClosureMap());
23+
}
24+
2125
function _eval(text) {
2226
return new Parser(new Lexer(), new ClosureMap()).parse(text)
2327
.eval(context, formatters);
@@ -36,7 +40,7 @@ export function main() {
3640
var parser;
3741

3842
beforeEach(() => {
39-
parser = new Parser(new Lexer(), new ClosureMap());
43+
parser = createParser();
4044
});
4145

4246
it("should parse field access",() => {
@@ -119,6 +123,22 @@ export function main() {
119123
expectEval("4 + 4 + ' str'").toEqual("8 str");
120124
expectEval("'str ' + 4 + 4").toEqual("str 44");
121125
});
126+
127+
it('should behave gracefully with a null scope', () => {
128+
var exp = createParser().parse("null");
129+
expect(exp.eval(null, null)).toEqual(null);
130+
});
131+
132+
it('should eval binary operators with null as null', () => {
133+
expectEval("null < 0").toBeNull();
134+
expectEval("null * 3").toBeNull();
135+
expectEval("null + 6").toBeNull();
136+
expectEval("5 + null").toBeNull();
137+
expectEval("null - 4").toBeNull();
138+
expectEval("3 - null").toBeNull();
139+
expectEval("null + null").toBeNull();
140+
expectEval("null - null").toBeNull();
141+
});
122142
});
123143

124144
describe("error handling", () => {

0 commit comments

Comments
 (0)