fix(ivy): support equality operators in static interpreter (#31145)

Previously, the usage of equality operators ==, ===, != and !== was not
supported in ngtsc's static interpreter. This commit adds support for
such operators and includes tests.

Fixes #31076

PR Close #31145
This commit is contained in:
JoostK 2019-06-19 21:06:13 +02:00 committed by Kara Erickson
parent a5dd4edab9
commit 3fb73ac62b
2 changed files with 45 additions and 0 deletions

View File

@ -52,6 +52,10 @@ const BINARY_OPERATORS = new Map<ts.SyntaxKind, BinaryOperatorDef>([
[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)],

View File

@ -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); });