diff --git a/packages/compiler-cli/src/ngtsc/partial_evaluator/src/interpreter.ts b/packages/compiler-cli/src/ngtsc/partial_evaluator/src/interpreter.ts index 07d0f6452b..8b4b53540b 100644 --- a/packages/compiler-cli/src/ngtsc/partial_evaluator/src/interpreter.ts +++ b/packages/compiler-cli/src/ngtsc/partial_evaluator/src/interpreter.ts @@ -52,6 +52,10 @@ const BINARY_OPERATORS = new Map([ [ts.SyntaxKind.LessThanEqualsToken, literalBinaryOp((a, b) => a <= b)], [ts.SyntaxKind.GreaterThanToken, literalBinaryOp((a, b) => a > b)], [ts.SyntaxKind.GreaterThanEqualsToken, literalBinaryOp((a, b) => a >= b)], + [ts.SyntaxKind.EqualsEqualsToken, literalBinaryOp((a, b) => a == b)], + [ts.SyntaxKind.EqualsEqualsEqualsToken, literalBinaryOp((a, b) => a === b)], + [ts.SyntaxKind.ExclamationEqualsToken, literalBinaryOp((a, b) => a != b)], + [ts.SyntaxKind.ExclamationEqualsEqualsToken, literalBinaryOp((a, b) => a !== b)], [ts.SyntaxKind.LessThanLessThanToken, literalBinaryOp((a, b) => a << b)], [ts.SyntaxKind.GreaterThanGreaterThanToken, literalBinaryOp((a, b) => a >> b)], [ts.SyntaxKind.GreaterThanGreaterThanGreaterThanToken, literalBinaryOp((a, b) => a >>> b)], diff --git a/packages/compiler-cli/src/ngtsc/partial_evaluator/test/evaluator_spec.ts b/packages/compiler-cli/src/ngtsc/partial_evaluator/test/evaluator_spec.ts index 3c1ab32600..01fe596eea 100644 --- a/packages/compiler-cli/src/ngtsc/partial_evaluator/test/evaluator_spec.ts +++ b/packages/compiler-cli/src/ngtsc/partial_evaluator/test/evaluator_spec.ts @@ -100,6 +100,47 @@ describe('ngtsc metadata', () => { expect(evaluate(`const a = 'hello', b = 0;`, 'a || b')).toEqual('hello'); }); + it('evaluates arithmetic operators', () => { + expect(evaluate('const a = 6, b = 3;', 'a + b')).toEqual(9); + expect(evaluate('const a = 6, b = 3;', 'a - b')).toEqual(3); + expect(evaluate('const a = 6, b = 3;', 'a * b')).toEqual(18); + expect(evaluate('const a = 6, b = 3;', 'a / b')).toEqual(2); + expect(evaluate('const a = 6, b = 3;', 'a % b')).toEqual(0); + expect(evaluate('const a = 6, b = 3;', 'a & b')).toEqual(2); + expect(evaluate('const a = 6, b = 3;', 'a | b')).toEqual(7); + expect(evaluate('const a = 6, b = 3;', 'a ^ b')).toEqual(5); + expect(evaluate('const a = 6, b = 3;', 'a ** b')).toEqual(216); + expect(evaluate('const a = 6, b = 3;', 'a << b')).toEqual(48); + expect(evaluate('const a = -6, b = 2;', 'a >> b')).toEqual(-2); + expect(evaluate('const a = -6, b = 2;', 'a >>> b')).toEqual(1073741822); + }); + + it('evaluates comparison operators', () => { + expect(evaluate('const a = 2, b = 3;', 'a < b')).toEqual(true); + expect(evaluate('const a = 3, b = 3;', 'a < b')).toEqual(false); + + expect(evaluate('const a = 3, b = 3;', 'a <= b')).toEqual(true); + expect(evaluate('const a = 4, b = 3;', 'a <= b')).toEqual(false); + + expect(evaluate('const a = 4, b = 3;', 'a > b')).toEqual(true); + expect(evaluate('const a = 3, b = 3;', 'a > b')).toEqual(false); + + expect(evaluate('const a = 3, b = 3;', 'a >= b')).toEqual(true); + expect(evaluate('const a = 2, b = 3;', 'a >= b')).toEqual(false); + + expect(evaluate('const a: any = 3, b = "3";', 'a == b')).toEqual(true); + expect(evaluate('const a: any = 2, b = "3";', 'a == b')).toEqual(false); + + expect(evaluate('const a: any = 2, b = "3";', 'a != b')).toEqual(true); + expect(evaluate('const a: any = 3, b = "3";', 'a != b')).toEqual(false); + + expect(evaluate('const a: any = 3, b = 3;', 'a === b')).toEqual(true); + expect(evaluate('const a: any = 3, b = "3";', 'a === b')).toEqual(false); + + expect(evaluate('const a: any = 3, b = "3";', 'a !== b')).toEqual(true); + expect(evaluate('const a: any = 3, b = 3;', 'a !== b')).toEqual(false); + }); + it('parentheticals work', () => { expect(evaluate(`const a = 3, b = 4;`, 'a * (a + b)')).toEqual(21); });