From afe0e454537f9252f9cf313647e649cfa464f96f Mon Sep 17 00:00:00 2001 From: Pawel Kozlowski Date: Wed, 22 Apr 2015 11:45:33 +0200 Subject: [PATCH] feat(parser): support === and !== operators Closes #1496 Closes #1500 --- .../src/change_detection/parser/ast.js | 2 ++ .../src/change_detection/parser/lexer.js | 4 +++- .../src/change_detection/parser/parser.js | 6 +++++- .../change_detection/parser/lexer_spec.js | 6 ++++-- .../change_detection/parser/parser_spec.js | 19 ++++++++++++++++++- 5 files changed, 32 insertions(+), 5 deletions(-) diff --git a/modules/angular2/src/change_detection/parser/ast.js b/modules/angular2/src/change_detection/parser/ast.js index 988ec6016f..3a7cbe1099 100644 --- a/modules/angular2/src/change_detection/parser/ast.js +++ b/modules/angular2/src/change_detection/parser/ast.js @@ -281,6 +281,8 @@ export class Binary extends AST { case '%' : return left % right; case '==' : return left == right; case '!=' : return left != right; + case '===' : return left === right; + case '!==' : return left !== right; case '<' : return left < right; case '>' : return left > right; case '<=' : return left <= right; diff --git a/modules/angular2/src/change_detection/parser/lexer.js b/modules/angular2/src/change_detection/parser/lexer.js index ba13cde156..36038967fa 100644 --- a/modules/angular2/src/change_detection/parser/lexer.js +++ b/modules/angular2/src/change_detection/parser/lexer.js @@ -304,7 +304,7 @@ class _Scanner { assert(this.peek == StringWrapper.charCodeAt(one, 0)); this.advance(); var str:string = one; - if (this.peek == code) { + while (this.peek == code) { this.advance(); str += two; } @@ -461,6 +461,8 @@ var OPERATORS = SetWrapper.createFromList([ '=', '==', '!=', + '===', + '!==', '<', '>', '<=', diff --git a/modules/angular2/src/change_detection/parser/parser.js b/modules/angular2/src/change_detection/parser/parser.js index 58b803f47a..57bf05b7bb 100644 --- a/modules/angular2/src/change_detection/parser/parser.js +++ b/modules/angular2/src/change_detection/parser/parser.js @@ -277,13 +277,17 @@ class _ParseAST { } parseEquality() { - // '==','!=' + // '==','!=','===','!==' var result = this.parseRelational(); while (true) { if (this.optionalOperator('==')) { result = new Binary('==', result, this.parseRelational()); + } else if (this.optionalOperator('===')) { + result = new Binary('===', result, this.parseRelational()); } else if (this.optionalOperator('!=')) { result = new Binary('!=', result, this.parseRelational()); + } else if (this.optionalOperator('!==')) { + result = new Binary('!==', result, this.parseRelational()); } else { return result; } diff --git a/modules/angular2/test/change_detection/parser/lexer_spec.js b/modules/angular2/test/change_detection/parser/lexer_spec.js index ae14b18dc0..4d49686762 100644 --- a/modules/angular2/test/change_detection/parser/lexer_spec.js +++ b/modules/angular2/test/change_detection/parser/lexer_spec.js @@ -1,4 +1,4 @@ -import {describe, it, expect} from 'angular2/test_lib'; +import {ddescribe, describe, it, expect} from 'angular2/test_lib'; import {Lexer, Token} from 'angular2/src/change_detection/parser/lexer'; @@ -150,7 +150,7 @@ export function main() { }); it('should tokenize relation', function() { - var tokens:List = lex("! == != < > <= >="); + var tokens:List = lex("! == != < > <= >= === !=="); expectOperatorToken(tokens[0], 0, '!'); expectOperatorToken(tokens[1], 2, '=='); expectOperatorToken(tokens[2], 5, '!='); @@ -158,6 +158,8 @@ export function main() { expectOperatorToken(tokens[4], 10, '>'); expectOperatorToken(tokens[5], 12, '<='); expectOperatorToken(tokens[6], 15, '>='); + expectOperatorToken(tokens[7], 18, '==='); + expectOperatorToken(tokens[8], 22, '!=='); }); it('should tokenize statements', function() { diff --git a/modules/angular2/test/change_detection/parser/parser_spec.js b/modules/angular2/test/change_detection/parser/parser_spec.js index 7533c79a50..7213e49aa0 100644 --- a/modules/angular2/test/change_detection/parser/parser_spec.js +++ b/modules/angular2/test/change_detection/parser/parser_spec.js @@ -104,6 +104,7 @@ export function main() { it('should parse unary ! expressions', () => { expectEval("!true").toEqual(!true); expectEval("!!true").toEqual(!!true); + expectEval("!!!true").toEqual(!!!true); }); it('should parse multiplicative expressions', () => { @@ -123,7 +124,23 @@ export function main() { it('should parse equality expressions', () => { expectEval("2==3").toEqual(2 == 3); + expectEval("2=='2'").toEqual(2 == '2'); + expectEval("2=='3'").toEqual(2 == '3'); expectEval("2!=3").toEqual(2 != 3); + expectEval("2!='3'").toEqual(2 != '3'); + expectEval("2!='2'").toEqual(2 != '2'); + expectEval("2!=!false").toEqual(2!=!false); + }); + + it('should parse strict equality expressions', () => { + expectEval("2===3").toEqual(2 === 3); + expectEval("2==='3'").toEqual(2 === '3'); + expectEval("2==='2'").toEqual(2 === '2'); + expectEval("2!==3").toEqual(2 !== 3); + expectEval("2!=='3'").toEqual(2 !== '3'); + expectEval("2!=='2'").toEqual(2 !== '2'); + expectEval("false===!true").toEqual(false===!true); + expectEval("false!==!!true").toEqual(false!==!!true); }); it('should parse logicalAND expressions', () => { @@ -608,4 +625,4 @@ export function main() { }); }); }); -} \ No newline at end of file +}