Skip to content

Commit 07cc624

Browse files
committed
Clearer lexer errors for common cases.
This adds a clearer error specifically for the common case of using single quotes for string literals. It also makes minor clarity improvement to existing errors. Fixes graphql/graphiql#198
1 parent 1806976 commit 07cc624

File tree

2 files changed

+47
-12
lines changed

2 files changed

+47
-12
lines changed

src/language/__tests__/lexer-test.js

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ describe('Lexer', () => {
2323

2424
expect(() => lexOne('\u0007')
2525
).to.throw(
26-
'Syntax Error GraphQL (1:1) Invalid character "\\u0007"'
26+
'Syntax Error GraphQL (1:1) ' +
27+
'Cannot contain the invalid character "\\u0007"'
2728
);
2829

2930
});
@@ -106,7 +107,8 @@ describe('Lexer', () => {
106107
107108
`)
108109
).to.throw(
109-
'Syntax Error GraphQL (3:5) Unexpected character "?".\n' +
110+
'Syntax Error GraphQL (3:5) ' +
111+
'Cannot parse the unexpected character "?".\n' +
110112
'\n' +
111113
'2: \n' +
112114
'3: ?\n' +
@@ -178,11 +180,18 @@ describe('Lexer', () => {
178180

179181
expect(
180182
() => lexOne('"')
181-
).to.throw('Syntax Error GraphQL (1:2) Unterminated string');
183+
).to.throw('Syntax Error GraphQL (1:2) Unterminated string.');
182184

183185
expect(
184186
() => lexOne('"no end quote')
185-
).to.throw('Syntax Error GraphQL (1:14) Unterminated string');
187+
).to.throw('Syntax Error GraphQL (1:14) Unterminated string.');
188+
189+
expect(
190+
() => lexOne('\'single quotes\'')
191+
).to.throw(
192+
'Syntax Error GraphQL (1:1) Unexpected single quote character (\'), ' +
193+
'did you mean to use a double quote (")?'
194+
);
186195

187196
expect(
188197
() => lexOne('"contains unescaped \u0007 control char"')
@@ -406,7 +415,9 @@ describe('Lexer', () => {
406415

407416
expect(
408417
() => lexOne('+1')
409-
).to.throw('Syntax Error GraphQL (1:1) Unexpected character "+"');
418+
).to.throw(
419+
'Syntax Error GraphQL (1:1) Cannot parse the unexpected character "+".'
420+
);
410421

411422
expect(
412423
() => lexOne('1.')
@@ -417,7 +428,9 @@ describe('Lexer', () => {
417428

418429
expect(
419430
() => lexOne('.123')
420-
).to.throw('Syntax Error GraphQL (1:1) Unexpected character "."');
431+
).to.throw(
432+
'Syntax Error GraphQL (1:1) Cannot parse the unexpected character ".".'
433+
);
421434

422435
expect(
423436
() => lexOne('1.A')
@@ -572,19 +585,29 @@ describe('Lexer', () => {
572585

573586
expect(
574587
() => lexOne('..')
575-
).to.throw('Syntax Error GraphQL (1:1) Unexpected character "."');
588+
).to.throw(
589+
'Syntax Error GraphQL (1:1) Cannot parse the unexpected character ".".'
590+
);
576591

577592
expect(
578593
() => lexOne('?')
579-
).to.throw('Syntax Error GraphQL (1:1) Unexpected character "?"');
594+
).to.throw(
595+
'Syntax Error GraphQL (1:1) Cannot parse the unexpected character "?".'
596+
);
580597

581598
expect(
582599
() => lexOne('\u203B')
583-
).to.throw('Syntax Error GraphQL (1:1) Unexpected character "\\u203B"');
600+
).to.throw(
601+
'Syntax Error GraphQL (1:1) ' +
602+
'Cannot parse the unexpected character "\\u203B".'
603+
);
584604

585605
expect(
586606
() => lexOne('\u200b')
587-
).to.throw('Syntax Error GraphQL (1:1) Unexpected character "\\u200B"');
607+
).to.throw(
608+
'Syntax Error GraphQL (1:1) ' +
609+
'Cannot parse the unexpected character "\\u200B".'
610+
);
588611
});
589612

590613
it('lex reports useful information for dashes in names', () => {

src/language/lexer.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ function readToken(lexer: Lexer<*>, prev: Token): Token {
211211
throw syntaxError(
212212
source,
213213
position,
214-
`Invalid character ${printCharCode(code)}.`
214+
`Cannot contain the invalid character ${printCharCode(code)}.`
215215
);
216216
}
217217

@@ -276,10 +276,22 @@ function readToken(lexer: Lexer<*>, prev: Token): Token {
276276
throw syntaxError(
277277
source,
278278
position,
279-
`Unexpected character ${printCharCode(code)}.`
279+
unexpectedCharacterMessage(code)
280280
);
281281
}
282282

283+
/**
284+
* Report a message that an unexpected character was encountered.
285+
*/
286+
function unexpectedCharacterMessage(code) {
287+
if (code === 39) { // '
288+
return 'Unexpected single quote character (\'), did you mean to use ' +
289+
'a double quote (")?';
290+
}
291+
292+
return 'Cannot parse the unexpected character ' + printCharCode(code) + '.';
293+
}
294+
283295
/**
284296
* Reads from body starting at startPosition until it finds a non-whitespace
285297
* or commented character, then returns the position of that character for

0 commit comments

Comments
 (0)