Skip to content

Commit 8d8f38a

Browse files
committed
improve errors around around dots
extracted from graphql#3807
1 parent 5c7d4d1 commit 8d8f38a

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

src/language/__tests__/lexer-test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -852,7 +852,8 @@ describe('Lexer', () => {
852852
});
853853

854854
expectSyntaxError('.123').to.deep.equal({
855-
message: 'Syntax Error: Unexpected character: ".".',
855+
message:
856+
'Syntax Error: Invalid number, expected digit before ".", did you mean "0.123"?',
856857
locations: [{ line: 1, column: 1 }],
857858
});
858859

@@ -1030,7 +1031,7 @@ describe('Lexer', () => {
10301031

10311032
it('lex reports useful unknown character error', () => {
10321033
expectSyntaxError('..').to.deep.equal({
1033-
message: 'Syntax Error: Unexpected character: ".".',
1034+
message: 'Syntax Error: Unexpected "..", did you mean "..."?',
10341035
locations: [{ line: 1, column: 1 }],
10351036
});
10361037

src/language/lexer.ts

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -258,14 +258,31 @@ function readNextToken(lexer: Lexer, start: number): Token {
258258
return createToken(lexer, TokenKind.PAREN_L, position, position + 1);
259259
case 0x0029: // )
260260
return createToken(lexer, TokenKind.PAREN_R, position, position + 1);
261-
case 0x002e: // .
262-
if (
263-
body.charCodeAt(position + 1) === 0x002e &&
264-
body.charCodeAt(position + 2) === 0x002e
265-
) {
261+
case 0x002e: {
262+
// .
263+
const nextCode = body.charCodeAt(position + 1);
264+
if (nextCode === 0x002e && body.charCodeAt(position + 2) === 0x002e) {
266265
return createToken(lexer, TokenKind.SPREAD, position, position + 3);
267266
}
267+
if (nextCode === 0x002e) {
268+
throw syntaxError(
269+
lexer.source,
270+
position,
271+
'Unexpected "..", did you mean "..."?',
272+
);
273+
} else if (isDigit(nextCode)) {
274+
const digits = lexer.source.body.slice(
275+
position + 1,
276+
readDigits(lexer, position + 1, nextCode),
277+
);
278+
throw syntaxError(
279+
lexer.source,
280+
position,
281+
`Invalid number, expected digit before ".", did you mean "0.${digits}"?`,
282+
);
283+
}
268284
break;
285+
}
269286
case 0x003a: // :
270287
return createToken(lexer, TokenKind.COLON, position, position + 1);
271288
case 0x003d: // =

0 commit comments

Comments
 (0)