Skip to content

Fix comment parsing with unattached comments #104

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/GraphQLParser.ApiTests/GraphQL-Parser.approved.txt
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ namespace GraphQLParser.AST
public GraphQLDocument() { }
public System.Collections.Generic.List<GraphQLParser.AST.ASTNode>? Definitions { get; set; }
public override GraphQLParser.AST.ASTNodeKind Kind { get; }
public System.Collections.Generic.List<GraphQLParser.AST.GraphQLComment>? UnattachedComments { get; set; }
}
public class GraphQLEnumTypeDefinition : GraphQLParser.AST.GraphQLTypeDefinition, GraphQLParser.AST.IHasDirectivesNode
{
Expand Down
44 changes: 44 additions & 0 deletions src/GraphQLParser.Tests/ParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,50 @@ public class ParserTests
{
private static readonly string _nl = Environment.NewLine;

[Fact]
public void Extra_Comments_Should_Read_Correclty()
{
const string query = @"
query _ {
person {
name
#comment1
}
#comment2
test {
alt
}
#comment3
}
#comment4
";

var parser = new Parser(new Lexer());
var document = parser.Parse(new Source(query));
document.Definitions.Count().ShouldBe(1);
// query
var def = document.Definitions.First() as GraphQLOperationDefinition;
def.SelectionSet.Selections.Count().ShouldBe(2);
// person
var field = def.SelectionSet.Selections.First() as GraphQLFieldSelection;
field.SelectionSet.Selections.Count().ShouldBe(1);
// name
var subField = field.SelectionSet.Selections.First() as GraphQLFieldSelection;
subField.Comment.ShouldBeNull();
// test
field = def.SelectionSet.Selections.Last() as GraphQLFieldSelection;
field.SelectionSet.Selections.Count().ShouldBe(1);
field.Comment.Text.ShouldBe("comment2");
// alt
subField = field.SelectionSet.Selections.First() as GraphQLFieldSelection;
subField.Comment.ShouldBeNull();
// extra document comments
document.UnattachedComments.Count().ShouldBe(3);
document.UnattachedComments[0].Text.ShouldBe("comment1");
document.UnattachedComments[1].Text.ShouldBe("comment3");
document.UnattachedComments[2].Text.ShouldBe("comment4");
}

[Fact]
public void Comments_on_FragmentSpread_Should_Read_Correclty()
{
Expand Down
5 changes: 3 additions & 2 deletions src/GraphQLParser/AST/GraphQLDocument.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Collections.Generic;
using System.Collections.Generic;

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

public override ASTNodeKind Kind => ASTNodeKind.Document;
public List<GraphQLComment>? UnattachedComments { get; set; }
}
}
}
2 changes: 1 addition & 1 deletion src/GraphQLParser/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public Parser(ILexer lexer)

public GraphQLDocument Parse(ISource source)
{
using var context = new ParserContext(source, _lexer);
var context = new ParserContext(source, _lexer);
return context.Parse();
}
}
Expand Down
Loading