Skip to content
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

Fix incorrect lexing and parsing of multi-byte comments #708

Closed
wants to merge 2 commits into from
Closed
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
2 changes: 1 addition & 1 deletion language/lexer/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ func positionAfterWhitespace(body []byte, startPosition int) (position int, rune
// SourceCharacter but not LineTerminator
(code > 0x001F || code == 0x0009) && code != 0x000A && code != 0x000D {
position += n
runePosition++
runePosition += n
continue
} else {
break
Expand Down
12 changes: 12 additions & 0 deletions language/lexer/lexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,18 @@ func TestLexer_SkipsWhiteSpace(t *testing.T) {
Value: "",
},
},
{
Body: `
#comment фы世界
foo#comment
`,
Expected: Token{
Kind: NAME,
Start: 30,
End: 33,
Value: "foo",
},
},
}
for _, test := range tests {
token, err := Lex(&source.Source{Body: []byte(test.Body)})(0)
Expand Down
78 changes: 78 additions & 0 deletions language/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,84 @@ func TestParsesMultiByteCharacters_UnicodeText(t *testing.T) {
}
}

func TestMultiByteCharactersAmongstOtherTokens(t *testing.T) {
doc := `
{
# This comment has a фы世界 multi-byte character.
field(arg: "Has a фы世界 multi-byte character.")
}
`
astDoc := parse(t, doc)

expectedASTDoc := ast.NewDocument(&ast.Document{
Loc: ast.NewLocation(&ast.Location{
Start: 67,
End: 121,
}),
Definitions: []ast.Node{
ast.NewOperationDefinition(&ast.OperationDefinition{
Loc: ast.NewLocation(&ast.Location{
Start: 67,
End: 119,
}),
Operation: "query",
SelectionSet: ast.NewSelectionSet(&ast.SelectionSet{
Loc: ast.NewLocation(&ast.Location{
Start: 67,
End: 119,
}),
Selections: []ast.Selection{
ast.NewField(&ast.Field{
Loc: ast.NewLocation(&ast.Location{
Start: 67,
End: 117,
}),
Name: ast.NewName(&ast.Name{
Loc: ast.NewLocation(&ast.Location{
Start: 69,
End: 74,
}),
Value: "field",
}),
Arguments: []*ast.Argument{
ast.NewArgument(&ast.Argument{
Loc: ast.NewLocation(&ast.Location{
Start: 75,
End: 116,
}),
Name: ast.NewName(&ast.Name{

Loc: ast.NewLocation(&ast.Location{
Start: 75,
End: 78,
}),
Value: "arg",
}),
Value: ast.NewStringValue(&ast.StringValue{

Loc: ast.NewLocation(&ast.Location{
Start: 80,
End: 116,
}),
Value: "Has a фы世界 multi-byte character.",
}),
}),
},
}),
},
}),
}),
},
})

astDocQuery := printer.Print(astDoc)
expectedASTDocQuery := printer.Print(expectedASTDoc)

if !reflect.DeepEqual(astDocQuery, expectedASTDocQuery) {
t.Fatalf("unexpected document, expected: %v, got: %v", expectedASTDocQuery, astDocQuery)
}
}

func TestParsesKitchenSink(t *testing.T) {
b, err := ioutil.ReadFile("../../kitchen-sink.graphql")
if err != nil {
Expand Down