Skip to content

Commit 8537aa0

Browse files
committed
Fix negative Position.Column for definitions with block string descriptions (#254)
1 parent d828d64 commit 8537aa0

2 files changed

Lines changed: 70 additions & 5 deletions

File tree

parser/schema.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,6 @@ func (p *parser) parseFieldsDefinition() (FieldList, *CommentGroup) {
231231

232232
func (p *parser) parseFieldDefinition() *FieldDefinition {
233233
var def FieldDefinition
234-
def.Position = p.peekPos()
235234

236235
desc := p.parseDescription()
237236
if desc.text != "" {
@@ -241,6 +240,7 @@ func (p *parser) parseFieldDefinition() *FieldDefinition {
241240

242241
p.peek() // peek to set p.comment
243242
def.AfterDescriptionComment = p.comment
243+
def.Position = p.peekPos()
244244
def.Name = p.parseName()
245245
def.Arguments = p.parseArgumentDefs()
246246
p.expect(lexer.Colon)
@@ -260,7 +260,6 @@ func (p *parser) parseArgumentDefs() ArgumentDefinitionList {
260260

261261
func (p *parser) parseArgumentDef() *ArgumentDefinition {
262262
var def ArgumentDefinition
263-
def.Position = p.peekPos()
264263

265264
desc := p.parseDescription()
266265
if desc.text != "" {
@@ -270,6 +269,7 @@ func (p *parser) parseArgumentDef() *ArgumentDefinition {
270269

271270
p.peek() // peek to set p.comment
272271
def.AfterDescriptionComment = p.comment
272+
def.Position = p.peekPos()
273273
def.Name = p.parseName()
274274
p.expect(lexer.Colon)
275275
def.Type = p.parseTypeReference()
@@ -282,7 +282,6 @@ func (p *parser) parseArgumentDef() *ArgumentDefinition {
282282

283283
func (p *parser) parseInputValueDef() *FieldDefinition {
284284
var def FieldDefinition
285-
def.Position = p.peekPos()
286285

287286
desc := p.parseDescription()
288287
if desc.text != "" {
@@ -292,6 +291,7 @@ func (p *parser) parseInputValueDef() *FieldDefinition {
292291

293292
p.peek() // peek to set p.comment
294293
def.AfterDescriptionComment = p.comment
294+
def.Position = p.peekPos()
295295
def.Name = p.parseName()
296296
p.expect(lexer.Colon)
297297
def.Type = p.parseTypeReference()
@@ -372,7 +372,6 @@ func (p *parser) parseEnumValuesDefinition() (EnumValueList, *CommentGroup) {
372372

373373
func (p *parser) parseEnumValueDefinition() *EnumValueDefinition {
374374
var def EnumValueDefinition
375-
def.Position = p.peekPos()
376375
desc := p.parseDescription()
377376
if desc.text != "" {
378377
def.BeforeDescriptionComment = desc.comment
@@ -381,7 +380,7 @@ func (p *parser) parseEnumValueDefinition() *EnumValueDefinition {
381380

382381
p.peek() // peek to set p.comment
383382
def.AfterDescriptionComment = p.comment
384-
383+
def.Position = p.peekPos()
385384
def.Name = p.parseName()
386385
def.Directives = p.parseDirectives(true)
387386

parser/schema_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,72 @@ func TestSchemaDocument(t *testing.T) {
3030
})
3131
}
3232

33+
func TestFieldPositionWithBlockStringDescription(t *testing.T) {
34+
schema, parseErr := ParseSchema(&ast.Source{
35+
Input: `type Query {
36+
"""
37+
multi
38+
line
39+
"""
40+
myField: String
41+
}`,
42+
})
43+
assert.NoError(t, parseErr)
44+
field := schema.Definitions.ForName("Query").Fields.ForName("myField")
45+
assert.Equal(t, 6, field.Position.Line)
46+
assert.Equal(t, 6, field.Position.Column)
47+
}
48+
49+
func TestArgumentPositionWithBlockStringDescription(t *testing.T) {
50+
schema, parseErr := ParseSchema(&ast.Source{
51+
Input: `type Query {
52+
myField(
53+
"""
54+
multi
55+
line
56+
"""
57+
myArg: String
58+
): String
59+
}`,
60+
})
61+
assert.NoError(t, parseErr)
62+
arg := schema.Definitions.ForName("Query").Fields.ForName("myField").Arguments.ForName("myArg")
63+
assert.Equal(t, 7, arg.Position.Line)
64+
assert.True(t, arg.Position.Column > 0)
65+
}
66+
67+
func TestInputValuePositionWithBlockStringDescription(t *testing.T) {
68+
schema, parseErr := ParseSchema(&ast.Source{
69+
Input: `input MyInput {
70+
"""
71+
multi
72+
line
73+
"""
74+
myField: String
75+
}`,
76+
})
77+
assert.NoError(t, parseErr)
78+
field := schema.Definitions.ForName("MyInput").Fields.ForName("myField")
79+
assert.Equal(t, 6, field.Position.Line)
80+
assert.True(t, field.Position.Column > 0)
81+
}
82+
83+
func TestEnumValuePositionWithBlockStringDescription(t *testing.T) {
84+
schema, parseErr := ParseSchema(&ast.Source{
85+
Input: `enum MyEnum {
86+
"""
87+
multi
88+
line
89+
"""
90+
MY_VALUE
91+
}`,
92+
})
93+
assert.NoError(t, parseErr)
94+
val := schema.Definitions.ForName("MyEnum").EnumValues.ForName("MY_VALUE")
95+
assert.Equal(t, 6, val.Position.Line)
96+
assert.True(t, val.Position.Column > 0)
97+
}
98+
3399
func TestTypePosition(t *testing.T) {
34100
t.Run("type line number with no bang", func(t *testing.T) {
35101
schema, parseErr := ParseSchema(&ast.Source{

0 commit comments

Comments
 (0)