Skip to content

Commit 3583007

Browse files
crisbetokirjs
authored andcommitted
refactor(compiler): handle spread operators in the lexer
Updates the expression lexer to handle spread operators.
1 parent 1c00ab4 commit 3583007

2 files changed

Lines changed: 38 additions & 3 deletions

File tree

packages/compiler/src/expression_parser/lexer.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,9 +288,21 @@ class _Scanner {
288288
switch (peek) {
289289
case chars.$PERIOD:
290290
this.advance();
291-
return chars.isDigit(this.peek)
292-
? this.scanNumber(start)
293-
: newCharacterToken(start, this.index, chars.$PERIOD);
291+
292+
if (chars.isDigit(this.peek)) {
293+
return this.scanNumber(start);
294+
}
295+
296+
if (this.peek !== chars.$PERIOD) {
297+
return newCharacterToken(start, this.index, chars.$PERIOD);
298+
}
299+
300+
this.advance();
301+
if (this.peek === chars.$PERIOD) {
302+
this.advance();
303+
return newOperatorToken(start, this.index, '...');
304+
}
305+
return this.error(`Unexpected character [${String.fromCharCode(peek)}]`, 0);
294306
case chars.$LPAREN:
295307
case chars.$RPAREN:
296308
case chars.$LBRACKET:

packages/compiler/test/expression_parser/lexer_spec.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,29 @@ describe('lexer', () => {
430430
expectOperatorToken(lex('??=')[0], 0, 3, '??=');
431431
});
432432

433+
it('should tokenize a spread operator', () => {
434+
const tokens = lex('{...foo}');
435+
expect(tokens.length).toEqual(4);
436+
expectCharacterToken(tokens[0], 0, 1, '{');
437+
expectOperatorToken(tokens[1], 1, 4, '...');
438+
expectIdentifierToken(tokens[2], 4, 7, 'foo');
439+
expectCharacterToken(tokens[3], 7, 8, '}');
440+
});
441+
442+
it('should produce an error for a spread with two dots', () => {
443+
const tokens = lex('{..foo}');
444+
expect(tokens.length).toEqual(4);
445+
expectCharacterToken(tokens[0], 0, 1, '{');
446+
expectErrorToken(
447+
tokens[1],
448+
3,
449+
3,
450+
'Lexer Error: Unexpected character [.] at column 3 in expression [{..foo}]',
451+
);
452+
expectIdentifierToken(tokens[2], 3, 6, 'foo');
453+
expectCharacterToken(tokens[3], 6, 7, '}');
454+
});
455+
433456
describe('template literals', () => {
434457
it('should tokenize template literal with no interpolations', () => {
435458
const tokens: Token[] = lex('`hello world`');

0 commit comments

Comments
 (0)