Skip to content

Commit e07bd29

Browse files
authored
Fix comment parsing with unattached comments (#104)
1 parent f2b5af3 commit e07bd29

File tree

5 files changed

+1019
-961
lines changed

5 files changed

+1019
-961
lines changed

src/GraphQLParser.ApiTests/GraphQL-Parser.approved.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ namespace GraphQLParser.AST
8282
public GraphQLDocument() { }
8383
public System.Collections.Generic.List<GraphQLParser.AST.ASTNode>? Definitions { get; set; }
8484
public override GraphQLParser.AST.ASTNodeKind Kind { get; }
85+
public System.Collections.Generic.List<GraphQLParser.AST.GraphQLComment>? UnattachedComments { get; set; }
8586
}
8687
public class GraphQLEnumTypeDefinition : GraphQLParser.AST.GraphQLTypeDefinition, GraphQLParser.AST.IHasDirectivesNode
8788
{

src/GraphQLParser.Tests/ParserTests.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,50 @@ public class ParserTests
1212
{
1313
private static readonly string _nl = Environment.NewLine;
1414

15+
[Fact]
16+
public void Extra_Comments_Should_Read_Correclty()
17+
{
18+
const string query = @"
19+
query _ {
20+
person {
21+
name
22+
#comment1
23+
}
24+
#comment2
25+
test {
26+
alt
27+
}
28+
#comment3
29+
}
30+
#comment4
31+
";
32+
33+
var parser = new Parser(new Lexer());
34+
var document = parser.Parse(new Source(query));
35+
document.Definitions.Count().ShouldBe(1);
36+
// query
37+
var def = document.Definitions.First() as GraphQLOperationDefinition;
38+
def.SelectionSet.Selections.Count().ShouldBe(2);
39+
// person
40+
var field = def.SelectionSet.Selections.First() as GraphQLFieldSelection;
41+
field.SelectionSet.Selections.Count().ShouldBe(1);
42+
// name
43+
var subField = field.SelectionSet.Selections.First() as GraphQLFieldSelection;
44+
subField.Comment.ShouldBeNull();
45+
// test
46+
field = def.SelectionSet.Selections.Last() as GraphQLFieldSelection;
47+
field.SelectionSet.Selections.Count().ShouldBe(1);
48+
field.Comment.Text.ShouldBe("comment2");
49+
// alt
50+
subField = field.SelectionSet.Selections.First() as GraphQLFieldSelection;
51+
subField.Comment.ShouldBeNull();
52+
// extra document comments
53+
document.UnattachedComments.Count().ShouldBe(3);
54+
document.UnattachedComments[0].Text.ShouldBe("comment1");
55+
document.UnattachedComments[1].Text.ShouldBe("comment3");
56+
document.UnattachedComments[2].Text.ShouldBe("comment4");
57+
}
58+
1559
[Fact]
1660
public void Comments_on_FragmentSpread_Should_Read_Correclty()
1761
{
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Collections.Generic;
1+
using System.Collections.Generic;
22

33
namespace GraphQLParser.AST
44
{
@@ -7,5 +7,6 @@ public class GraphQLDocument : ASTNode
77
public List<ASTNode>? Definitions { get; set; }
88

99
public override ASTNodeKind Kind => ASTNodeKind.Document;
10+
public List<GraphQLComment>? UnattachedComments { get; set; }
1011
}
11-
}
12+
}

src/GraphQLParser/Parser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public Parser(ILexer lexer)
1313

1414
public GraphQLDocument Parse(ISource source)
1515
{
16-
using var context = new ParserContext(source, _lexer);
16+
var context = new ParserContext(source, _lexer);
1717
return context.Parse();
1818
}
1919
}

0 commit comments

Comments
 (0)