Skip to content

Commit 7812f1f

Browse files
committed
Fix tests and formatting
1 parent 1615f12 commit 7812f1f

File tree

5 files changed

+39
-16
lines changed

5 files changed

+39
-16
lines changed

src/language/__tests__/parser-test.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ describe('Parser', () => {
409409

410410
expectJSON(result).toDeepEqual({
411411
kind: Kind.DOCUMENT,
412-
loc: { start: 0, end: 43},
412+
loc: { start: 0, end: 43 },
413413
definitions: [
414414
{
415415
kind: Kind.OPERATION_DEFINITION,
@@ -729,7 +729,7 @@ describe('Parser', () => {
729729
});
730730
});
731731

732-
describe('operation and variable definition descriptions', () => {
732+
describe('operation and variable definition descriptions', () => {
733733
it('parses operation with description and variable descriptions', () => {
734734
const result = parse(dedent`
735735
"Operation description"
@@ -772,6 +772,14 @@ describe('Parser', () => {
772772
}
773773
});
774774

775+
it('descriptions on a short-hand query produce a sensible error', () => {
776+
const input = `"""Invalid"""
777+
{ __typename }`;
778+
expect(() => parse(input)).to.throw(
779+
'Syntax Error: Unexpected description, descriptions are not supported on shorthand queries.',
780+
);
781+
});
782+
775783
it('parses variable definition with description, default value, and directives', () => {
776784
const result = parse(dedent`
777785
query (

src/language/__tests__/schema-parser-test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ describe('Schema Parser', () => {
331331
}
332332
`).to.deep.equal({
333333
message:
334-
'Syntax Error: Unexpected description, descriptions are not supported on type extensions and shorthand queries.',
334+
'Syntax Error: Unexpected description, descriptions are not supported on type extensions.',
335335
locations: [{ line: 2, column: 7 }],
336336
});
337337

@@ -353,7 +353,7 @@ describe('Schema Parser', () => {
353353
}
354354
`).to.deep.equal({
355355
message:
356-
'Syntax Error: Unexpected description, descriptions are not supported on type extensions and shorthand queries.',
356+
'Syntax Error: Unexpected description, descriptions are not supported on type extensions.',
357357
locations: [{ line: 2, column: 7 }],
358358
});
359359

src/language/ast.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,13 @@ export const QueryDocumentKeys: {
204204
'directives',
205205
'selectionSet',
206206
],
207-
VariableDefinition: ['description', 'variable', 'type', 'defaultValue', 'directives'],
207+
VariableDefinition: [
208+
'description',
209+
'variable',
210+
'type',
211+
'defaultValue',
212+
'directives',
213+
],
208214
Variable: ['name'],
209215
SelectionSet: ['selections'],
210216
Field: ['alias', 'name', 'arguments', 'directives', 'selectionSet'],

src/language/parser.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,13 @@ export class Parser {
268268
? this._lexer.lookahead()
269269
: this._lexer.token;
270270

271+
if (hasDescription && keywordToken.kind === TokenKind.BRACE_L) {
272+
throw syntaxError(
273+
this._lexer.source,
274+
this._lexer.token.start,
275+
'Unexpected description, descriptions are not supported on shorthand queries.',
276+
);
277+
}
271278
if (keywordToken.kind === TokenKind.NAME) {
272279
switch (keywordToken.value) {
273280
case 'schema':
@@ -301,7 +308,7 @@ export class Parser {
301308
throw syntaxError(
302309
this._lexer.source,
303310
this._lexer.token.start,
304-
'Unexpected description, descriptions are not supported on type extensions and shorthand queries.',
311+
'Unexpected description, descriptions are not supported on type extensions.',
305312
);
306313
}
307314

@@ -551,7 +558,7 @@ export class Parser {
551558
}
552559
return this.node<FragmentDefinitionNode>(start, {
553560
kind: Kind.FRAGMENT_DEFINITION,
554-
description,
561+
description,
555562
name: this.parseFragmentName(),
556563
typeCondition: (this.expectKeyword('on'), this.parseNamedType()),
557564
directives: this.parseDirectives(false),

src/language/printer.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,16 @@ const printDocASTReducer: ASTReducer<string> = {
3131
const varDefs = hasMultilineItems(node.variableDefinitions)
3232
? wrap('(\n', join(node.variableDefinitions, '\n'), '\n)')
3333
: wrap('(', join(node.variableDefinitions, ', '), ')');
34-
const prefix = wrap('', node.description, '\n') + join(
35-
[
36-
node.operation,
37-
join([node.name, varDefs]),
38-
join(node.directives, ' '),
39-
],
40-
' ',
41-
);
34+
const prefix =
35+
wrap('', node.description, '\n') +
36+
join(
37+
[
38+
node.operation,
39+
join([node.name, varDefs]),
40+
join(node.directives, ' '),
41+
],
42+
' ',
43+
);
4244

4345
// Anonymous queries with no directives or variable definitions can use
4446
// the query short form.
@@ -99,7 +101,7 @@ const printDocASTReducer: ASTReducer<string> = {
99101
variableDefinitions,
100102
directives,
101103
selectionSet,
102-
description
104+
description,
103105
}) =>
104106
wrap('', description, '\n') +
105107
// Note: fragment variable definitions are experimental and may be changed

0 commit comments

Comments
 (0)