From c41f59c794349d7cee42fec9f693b9cd8e197942 Mon Sep 17 00:00:00 2001 From: vsavkin Date: Tue, 4 Nov 2014 09:06:46 -0800 Subject: [PATCH] feat(parser): change Parser to return null when one of the operands is null --- modules/change_detection/src/parser/ast.js | 14 +----------- .../test/parser/parser_spec.js | 22 ++++++++++++++++++- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/modules/change_detection/src/parser/ast.js b/modules/change_detection/src/parser/ast.js index 1eaa33e872..a041b7a987 100644 --- a/modules/change_detection/src/parser/ast.js +++ b/modules/change_detection/src/parser/ast.js @@ -86,19 +86,7 @@ export class Binary extends AST { var right = this.right.eval(context, formatters); // Null check for the operations. - if (left == null || right == null) { - switch (this.operation) { - case '+': - if (left != null) return left; - if (right != null) return right; - return 0; - case '-': - if (left != null) return left; - if (right != null) return 0 - right; - return 0; - } - return null; - } + if (left == null || right == null) return null; switch (this.operation) { case '+' : return autoConvertAdd(left, right); diff --git a/modules/change_detection/test/parser/parser_spec.js b/modules/change_detection/test/parser/parser_spec.js index f788b6c9ec..61f9583d14 100644 --- a/modules/change_detection/test/parser/parser_spec.js +++ b/modules/change_detection/test/parser/parser_spec.js @@ -18,6 +18,10 @@ export function main() { var context = td(); var formatters; + function createParser() { + return new Parser(new Lexer(), new ClosureMap()); + } + function _eval(text) { return new Parser(new Lexer(), new ClosureMap()).parse(text) .eval(context, formatters); @@ -36,7 +40,7 @@ export function main() { var parser; beforeEach(() => { - parser = new Parser(new Lexer(), new ClosureMap()); + parser = createParser(); }); it("should parse field access",() => { @@ -119,6 +123,22 @@ export function main() { expectEval("4 + 4 + ' str'").toEqual("8 str"); expectEval("'str ' + 4 + 4").toEqual("str 44"); }); + + it('should behave gracefully with a null scope', () => { + var exp = createParser().parse("null"); + expect(exp.eval(null, null)).toEqual(null); + }); + + it('should eval binary operators with null as null', () => { + expectEval("null < 0").toBeNull(); + expectEval("null * 3").toBeNull(); + expectEval("null + 6").toBeNull(); + expectEval("5 + null").toBeNull(); + expectEval("null - 4").toBeNull(); + expectEval("3 - null").toBeNull(); + expectEval("null + null").toBeNull(); + expectEval("null - null").toBeNull(); + }); }); describe("error handling", () => {