Skip to content

Add initial comment support [resurrected] #26

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 6 commits into from
Mar 20, 2019
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
15 changes: 9 additions & 6 deletions src/GraphQLParser.Tests/LexerTests.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
namespace GraphQLParser.Tests
{
using GraphQLParser;
using System;
using Xunit;

public class LexerTests
{
private static readonly string NL = Environment.NewLine;

[Fact]
public void Lex_ATPunctuation_HasCorrectEnd()
{
Expand Down Expand Up @@ -297,28 +300,28 @@ public void Lex_MultipleDecimalsIntToken_HasIntKind()
public void Lex_NameTokenWithComments_HasCorrectEnd()
{
var token = GetSingleNameTokenLexerWithComments();
Assert.Equal(13, token.End);
Assert.Equal(10, token.End);
}

[Fact]
public void Lex_NameTokenWithComments_HasCorrectStart()
{
var token = GetSingleNameTokenLexerWithComments();
Assert.Equal(10, token.Start);
Assert.Equal(1, token.Start);
}

[Fact]
public void Lex_NameTokenWithComments_HasCorrectValue()
{
var token = GetSingleNameTokenLexerWithComments();
Assert.Equal("foo", token.Value);
Assert.Equal("comment", token.Value);
}

[Fact]
public void Lex_NameTokenWithComments_HasNameKind()
{
var token = GetSingleNameTokenLexerWithComments();
Assert.Equal(TokenKind.NAME, token.Kind);
Assert.Equal(TokenKind.COMMENT, token.Kind);
}

[Fact]
Expand Down Expand Up @@ -1062,12 +1065,12 @@ private static Token GetSingleNameSurroundedByCommasTokenLexer()

private static Token GetSingleNameTokenLexerSurroundedWithWhitespaces()
{
return new Lexer().Lex(new Source("\r\n foo\r\n\r\n "));
return new Lexer().Lex(new Source($"{NL} foo{NL}{NL} "));
}

private static Token GetSingleNameTokenLexerWithComments()
{
return new Lexer().Lex(new Source("\r\n#comment\r\nfoo#comment"));
return new Lexer().Lex(new Source($"{NL}#comment{NL}foo#comment"));
}

private static Token GetSingleNameWithBOMHeaderTokenLexer()
Expand Down
28 changes: 20 additions & 8 deletions src/GraphQLParser.Tests/ParserTests.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@


namespace GraphQLParser.Tests
namespace GraphQLParser.Tests
{
using System.Linq;
using GraphQLParser;
using GraphQLParser.AST;
using System;
using System.Linq;
using Xunit;


public class ParserTests
{
private static readonly string NL = Environment.NewLine;

[Fact]
public void Parse_FieldInput_HasCorrectEndLocationAttribute()
{
Expand Down Expand Up @@ -125,7 +125,13 @@ public void Parse_FieldWithOperationTypeAndNameInput_SelectionSetContainsSingleF
[Fact]
public void Parse_KitchenSink_DoesNotThrowError()
{
new Parser(new Lexer()).Parse(new Source(LoadKitchenSink()));
var document = new Parser(new Lexer()).Parse(new Source(LoadKitchenSink()));
if (document != null)
{
var typeDef = document.Definitions.OfType<GraphQLObjectTypeDefinition>().First(d => d.Name.Value == "Foo");
var fieldDef = typeDef.Fields.First(d => d.Name.Value == "three");
Assert.Equal($" multiline comments{NL} with very importand description #{NL} # and symbol # and ##", fieldDef.Comment.Text);
}
}

[Fact]
Expand Down Expand Up @@ -227,15 +233,20 @@ fragment frag on Friend {
# LICENSE file in the root directory of this source tree. An additional grant
# of patent rights can be found in the PATENTS file in the same directory.

schema {
schema {
query: QueryType
mutation: MutationType
}

type Foo implements Bar
{
one: Type
# comment 1
one: Type
# comment 2
two(argument: InputType!): Type
# multiline comments
# with very importand description #
# # and symbol # and ##
three(argument: InputType, other: String): Int
four(argument: String = ""string""): String
five(argument: [String] = [""string"", ""string""]): String
Expand All @@ -244,6 +255,7 @@ type Foo implements Bar

type AnnotatedObject @onObject(arg: ""value"")
{
# a comment
annotatedField(arg: Type = ""default"" @onArg): Type @onField
}

Expand Down
1 change: 1 addition & 0 deletions src/GraphQLParser/AST/ASTNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ public abstract class ASTNode
{
public abstract ASTNodeKind Kind { get; }
public GraphQLLocation Location { get; set; }
public GraphQLComment Comment { get; set; }
}
}
1 change: 1 addition & 0 deletions src/GraphQLParser/AST/Enums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{
public enum ASTNodeKind
{
Comment,
Name,
Document,
OperationDefinition,
Expand Down
9 changes: 2 additions & 7 deletions src/GraphQLParser/AST/GraphQLArgument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,10 @@
{
public class GraphQLArgument : ASTNode
{
public override ASTNodeKind Kind
{
get
{
return ASTNodeKind.Argument;
}
}
public override ASTNodeKind Kind => ASTNodeKind.Argument;

public GraphQLName Name { get; set; }

public GraphQLValue Value { get; set; }
}
}
11 changes: 11 additions & 0 deletions src/GraphQLParser/AST/GraphQLComment.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace GraphQLParser.AST
{
public class GraphQLComment : ASTNode
{
public GraphQLComment(string text) => Text = text;

public override ASTNodeKind Kind => ASTNodeKind.Comment;

public string Text { get; set; }
}
}
8 changes: 1 addition & 7 deletions src/GraphQLParser/AST/GraphQLDirective.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,7 @@ public class GraphQLDirective : ASTNode
{
public IEnumerable<GraphQLArgument> Arguments { get; set; }

public override ASTNodeKind Kind
{
get
{
return ASTNodeKind.Directive;
}
}
public override ASTNodeKind Kind => ASTNodeKind.Directive;

public GraphQLName Name { get; set; }
}
Expand Down
8 changes: 1 addition & 7 deletions src/GraphQLParser/AST/GraphQLDirectiveDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,7 @@ public class GraphQLDirectiveDefinition : GraphQLTypeDefinition
public IEnumerable<GraphQLInputValueDefinition> Arguments { get; set; }
public IEnumerable<GraphQLInputValueDefinition> Definitions { get; set; }

public override ASTNodeKind Kind
{
get
{
return ASTNodeKind.DirectiveDefinition;
}
}
public override ASTNodeKind Kind => ASTNodeKind.DirectiveDefinition;

public IEnumerable<GraphQLName> Locations { get; set; }
public GraphQLName Name { get; set; }
Expand Down
8 changes: 1 addition & 7 deletions src/GraphQLParser/AST/GraphQLDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@ public class GraphQLDocument : ASTNode
{
public IEnumerable<ASTNode> Definitions { get; set; }

public override ASTNodeKind Kind
{
get
{
return ASTNodeKind.Document;
}
}
public override ASTNodeKind Kind => ASTNodeKind.Document;
}
}
8 changes: 1 addition & 7 deletions src/GraphQLParser/AST/GraphQLEnumTypeDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,7 @@ public class GraphQLEnumTypeDefinition : GraphQLTypeDefinition
{
public IEnumerable<GraphQLDirective> Directives { get; set; }

public override ASTNodeKind Kind
{
get
{
return ASTNodeKind.EnumTypeDefinition;
}
}
public override ASTNodeKind Kind => ASTNodeKind.EnumTypeDefinition;

public GraphQLName Name { get; set; }
public IEnumerable<GraphQLEnumValueDefinition> Values { get; set; }
Expand Down
8 changes: 1 addition & 7 deletions src/GraphQLParser/AST/GraphQLEnumValueDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,7 @@ public class GraphQLEnumValueDefinition : GraphQLTypeDefinition
{
public IEnumerable<GraphQLDirective> Directives { get; set; }

public override ASTNodeKind Kind
{
get
{
return ASTNodeKind.EnumValueDefinition;
}
}
public override ASTNodeKind Kind => ASTNodeKind.EnumValueDefinition;

public GraphQLName Name { get; set; }
}
Expand Down
8 changes: 1 addition & 7 deletions src/GraphQLParser/AST/GraphQLFieldDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,7 @@ public class GraphQLFieldDefinition : GraphQLTypeDefinition

public IEnumerable<GraphQLDirective> Directives { get; set; }

public override ASTNodeKind Kind
{
get
{
return ASTNodeKind.FieldDefinition;
}
}
public override ASTNodeKind Kind => ASTNodeKind.FieldDefinition;

public GraphQLName Name { get; set; }
public GraphQLType Type { get; set; }
Expand Down
8 changes: 1 addition & 7 deletions src/GraphQLParser/AST/GraphQLFieldSelection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,7 @@ public class GraphQLFieldSelection : ASTNode

public IEnumerable<GraphQLDirective> Directives { get; set; }

public override ASTNodeKind Kind
{
get
{
return ASTNodeKind.Field;
}
}
public override ASTNodeKind Kind => ASTNodeKind.Field;

public GraphQLName Name { get; set; }
public GraphQLSelectionSet SelectionSet { get; set; }
Expand Down
8 changes: 1 addition & 7 deletions src/GraphQLParser/AST/GraphQLFragmentDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,7 @@
{
public class GraphQLFragmentDefinition : GraphQLInlineFragment
{
public override ASTNodeKind Kind
{
get
{
return ASTNodeKind.FragmentDefinition;
}
}
public override ASTNodeKind Kind => ASTNodeKind.FragmentDefinition;

public GraphQLName Name { get; set; }
}
Expand Down
8 changes: 1 addition & 7 deletions src/GraphQLParser/AST/GraphQLFragmentSpread.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,7 @@ public class GraphQLFragmentSpread : ASTNode
{
public IEnumerable<GraphQLDirective> Directives { get; set; }

public override ASTNodeKind Kind
{
get
{
return ASTNodeKind.FragmentSpread;
}
}
public override ASTNodeKind Kind => ASTNodeKind.FragmentSpread;

public GraphQLName Name { get; set; }
}
Expand Down
8 changes: 1 addition & 7 deletions src/GraphQLParser/AST/GraphQLInlineFragment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,7 @@ public class GraphQLInlineFragment : ASTNode
{
public IEnumerable<GraphQLDirective> Directives { get; set; }

public override ASTNodeKind Kind
{
get
{
return ASTNodeKind.InlineFragment;
}
}
public override ASTNodeKind Kind => ASTNodeKind.InlineFragment;

public GraphQLSelectionSet SelectionSet { get; set; }
public GraphQLNamedType TypeCondition { get; set; }
Expand Down
8 changes: 1 addition & 7 deletions src/GraphQLParser/AST/GraphQLInputObjectTypeDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,7 @@ public class GraphQLInputObjectTypeDefinition : GraphQLTypeDefinition

public IEnumerable<GraphQLInputValueDefinition> Fields { get; set; }

public override ASTNodeKind Kind
{
get
{
return ASTNodeKind.InputObjectTypeDefinition;
}
}
public override ASTNodeKind Kind => ASTNodeKind.InputObjectTypeDefinition;

public GraphQLName Name { get; set; }
}
Expand Down
8 changes: 1 addition & 7 deletions src/GraphQLParser/AST/GraphQLInputValueDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,7 @@ public class GraphQLInputValueDefinition : GraphQLTypeDefinition

public IEnumerable<GraphQLDirective> Directives { get; set; }

public override ASTNodeKind Kind
{
get
{
return ASTNodeKind.InputValueDefinition;
}
}
public override ASTNodeKind Kind => ASTNodeKind.InputValueDefinition;

public GraphQLName Name { get; set; }
public GraphQLType Type { get; set; }
Expand Down
8 changes: 1 addition & 7 deletions src/GraphQLParser/AST/GraphQLInterfaceTypeDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,7 @@ public class GraphQLInterfaceTypeDefinition : GraphQLTypeDefinition

public IEnumerable<GraphQLFieldDefinition> Fields { get; set; }

public override ASTNodeKind Kind
{
get
{
return ASTNodeKind.InterfaceTypeDefinition;
}
}
public override ASTNodeKind Kind => ASTNodeKind.InterfaceTypeDefinition;

public GraphQLName Name { get; set; }
}
Expand Down
13 changes: 2 additions & 11 deletions src/GraphQLParser/AST/GraphQLListType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,10 @@
{
public class GraphQLListType : GraphQLType
{
public override ASTNodeKind Kind
{
get
{
return ASTNodeKind.ListType;
}
}
public override ASTNodeKind Kind => ASTNodeKind.ListType;

public GraphQLType Type { get; set; }

public override string ToString()
{
return $"[{this.Type}]";
}
public override string ToString() => $"[{Type}]";
}
}
Loading