feat(compiler): support number separators in templates (#42672)

As of ES2021, JavaScript allows using underscores as separators inside numbers, in order to make them more readable (e.g. `1_000_000` vs `1000000`). TypeScript has had support for separators for a while so these changes expand the template parser to handle them as well.

PR Close #42672
This commit is contained in:
Kristiyan Kostadinov
2021-06-27 12:24:51 +02:00
committed by Jessica Janiuk
parent 234b5edcc7
commit 9f5cc7c808
7 changed files with 152 additions and 4 deletions
@@ -295,6 +295,36 @@ function expectErrorToken(token: Token, index: any, end: number, message: string
it('should tokenize ?? as operator', () => {
expectOperatorToken(lex('??')[0], 0, 2, '??');
});
it('should tokenize number with separator', () => {
expectNumberToken(lex('123_456')[0], 0, 7, 123_456);
expectNumberToken(lex('1_000_000_000')[0], 0, 13, 1_000_000_000);
expectNumberToken(lex('123_456.78')[0], 0, 10, 123_456.78);
expectNumberToken(lex('123_456_789.123_456_789')[0], 0, 23, 123_456_789.123_456_789);
expectNumberToken(lex('1_2_3_4')[0], 0, 7, 1_2_3_4);
expectNumberToken(lex('1_2_3_4.5_6_7_8')[0], 0, 15, 1_2_3_4.5_6_7_8);
});
it('should tokenize number starting with an underscore as an identifier', () => {
expectIdentifierToken(lex('_123')[0], 0, 4, '_123');
expectIdentifierToken(lex('_123_')[0], 0, 5, '_123_');
expectIdentifierToken(lex('_1_2_3_')[0], 0, 7, '_1_2_3_');
});
it('should throw error for invalid number separators', () => {
expectErrorToken(
lex('123_')[0], 3, 3,
'Lexer Error: Invalid numeric separator at column 3 in expression [123_]');
expectErrorToken(
lex('12__3')[0], 2, 2,
'Lexer Error: Invalid numeric separator at column 2 in expression [12__3]');
expectErrorToken(
lex('1_2_3_.456')[0], 5, 5,
'Lexer Error: Invalid numeric separator at column 5 in expression [1_2_3_.456]');
expectErrorToken(
lex('1_2_3._456')[0], 6, 6,
'Lexer Error: Invalid numeric separator at column 6 in expression [1_2_3._456]');
});
});
});
}