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

feat: support end of definition comment and end of file comment #264

Merged
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 ast/definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type Definition struct {

BeforeDescriptionComment *CommentGroup
AfterDescriptionComment *CommentGroup
EndOfDefinitionComment *CommentGroup
}

func (d *Definition) IsLeafType() bool {
Expand Down
1 change: 1 addition & 0 deletions ast/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ type SchemaDefinition struct {

BeforeDescriptionComment *CommentGroup
AfterDescriptionComment *CommentGroup
EndOfDefinitionComment *CommentGroup
}

type OperationTypeDefinition struct {
Expand Down
49 changes: 37 additions & 12 deletions formatter/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,15 +196,16 @@ func (f *formatter) FormatSchemaDocument(doc *ast.SchemaDocument) {
return
}

f.FormatCommentGroup(doc.Comment)

f.FormatSchemaDefinitionList(doc.Schema, false)
f.FormatSchemaDefinitionList(doc.SchemaExtension, true)

f.FormatDirectiveDefinitionList(doc.Directives)

f.FormatDefinitionList(doc.Definitions, false)
f.FormatDefinitionList(doc.Extensions, true)

// doc.Comment is end of file comment, so emit last
f.FormatCommentGroup(doc.Comment)
}

func (f *formatter) FormatQueryDocument(doc *ast.QueryDocument) {
Expand All @@ -225,6 +226,30 @@ func (f *formatter) FormatSchemaDefinitionList(lists ast.SchemaDefinitionList, e
return
}

var (
beforeDescComment = new(ast.CommentGroup)
afterDescComment = new(ast.CommentGroup)
endOfDefinitionComment = new(ast.CommentGroup)
description string
)

for _, def := range lists {
if def.BeforeDescriptionComment != nil {
beforeDescComment.List = append(beforeDescComment.List, def.BeforeDescriptionComment.List...)
}
if def.AfterDescriptionComment != nil {
afterDescComment.List = append(afterDescComment.List, def.AfterDescriptionComment.List...)
}
if def.EndOfDefinitionComment != nil {
endOfDefinitionComment.List = append(endOfDefinitionComment.List, def.EndOfDefinitionComment.List...)
}
description += def.Description
}

f.FormatCommentGroup(beforeDescComment)
f.WriteDescription(description)
f.FormatCommentGroup(afterDescComment)

if extension {
f.WriteWord("extend")
}
Expand All @@ -235,17 +260,13 @@ func (f *formatter) FormatSchemaDefinitionList(lists ast.SchemaDefinitionList, e
f.FormatSchemaDefinition(def)
}

f.FormatCommentGroup(endOfDefinitionComment)

f.DecrementIndent()
f.WriteString("}").WriteNewline()
}

func (f *formatter) FormatSchemaDefinition(def *ast.SchemaDefinition) {
f.FormatCommentGroup(def.BeforeDescriptionComment)

f.WriteDescription(def.Description)

f.FormatCommentGroup(def.AfterDescriptionComment)

f.FormatDirectiveList(def.Directives)

f.FormatOperationTypeDefinitionList(def.OperationTypes)
Expand All @@ -264,7 +285,7 @@ func (f *formatter) FormatOperationTypeDefinition(def *ast.OperationTypeDefiniti
f.WriteNewline()
}

func (f *formatter) FormatFieldList(fieldList ast.FieldList) {
func (f *formatter) FormatFieldList(fieldList ast.FieldList, endOfDefComment *ast.CommentGroup) {
if len(fieldList) == 0 {
return
}
Expand All @@ -276,6 +297,8 @@ func (f *formatter) FormatFieldList(fieldList ast.FieldList) {
f.FormatFieldDefinition(field)
}

f.FormatCommentGroup(endOfDefComment)

f.DecrementIndent()
f.WriteString("}")
}
Expand Down Expand Up @@ -458,14 +481,14 @@ func (f *formatter) FormatDefinition(def *ast.Definition, extend bool) {
f.WriteWord("=").WriteWord(strings.Join(def.Types, " | "))
}

f.FormatFieldList(def.Fields)
f.FormatFieldList(def.Fields, def.EndOfDefinitionComment)

f.FormatEnumValueList(def.EnumValues)
f.FormatEnumValueList(def.EnumValues, def.EndOfDefinitionComment)

f.WriteNewline()
}

func (f *formatter) FormatEnumValueList(lists ast.EnumValueList) {
func (f *formatter) FormatEnumValueList(lists ast.EnumValueList, endOfDefComment *ast.CommentGroup) {
if len(lists) == 0 {
return
}
Expand All @@ -477,6 +500,8 @@ func (f *formatter) FormatEnumValueList(lists ast.EnumValueList) {
f.FormatEnumValueDefinition(v)
}

f.FormatCommentGroup(endOfDefComment)

f.DecrementIndent()
f.WriteString("}")
}
Expand Down
4 changes: 4 additions & 0 deletions formatter/testdata/baseline/FormatSchema/definition.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ scalar Cat0
type Cat1 {
# Cat1 name comment
name: String
# end of Cat1 comment
}
# Cat2 comment
interface Cat2 {
# Cat2 name comment
name: String
# end of Cat2 comment
}
# Cat3 comment
union Cat3 = Cat3_0 | Cat3_1 | Cat3_2
Expand All @@ -35,9 +37,11 @@ enum Cat4 {
NFC
# Cat4 MAINECOON comment
MAINECOON
# end of Cat4 comment
}
# Cat5 comment
input Cat5 {
# Cat5 name comment
name: String
# end of Cat5 comment
}
4 changes: 4 additions & 0 deletions formatter/testdata/baseline/FormatSchema/extensions.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,23 @@ type Dog {
name: String!
# Dog owner comment
owner: Person! @permission(permission: "admin")
# end of type comment
}
# Person comment
type Person @key(fields: "name") {
# Person name comment
name: String!
# end of type comment
}
# query extends comment
type Query @extends {
# dogs comment
dogs: [Dog!]!
# end of type comment
}
# subscription comment
type Subscription {
# dogEvents comment
dogEvents: [Dog!]!
# end of type comment
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ scalar Cat0
type Cat1 {
# Cat1 name comment
name: String
# end of Cat1 comment
}
# Cat2 comment
interface Cat2 {
# Cat2 name comment
name: String
# end of Cat2 comment
}
# Cat3_0 comment
type Cat3_0 {
Expand All @@ -35,9 +37,13 @@ enum Cat4 {
NFC
# Cat4 MAINECOON comment
MAINECOON
# end of Cat4 comment
}
# Cat5 comment
input Cat5 {
# Cat5 name comment
name: String
# end of Cat5 comment
}
# end of file comment
# end of file comment 2
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ type Cat {
# field comment
name: String
}
# end of file comment
# end of file comment 2
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
directive @foo on FIELD | OBJECT
directive @bar repeatable on FIELD | OBJECT
# end of file comment
# end of file comment 2
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ enum ConnectionStatus @foo {
ERROR
}
union PersonUnion @foo = Person
# end of file comment
# end of file comment 2
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,31 @@ directive @key(fields: String!) on OBJECT | INTERFACE
type Query @extends {
# dogs comment
dogs: [Dog!]!
# end of type comment
}
# subscription comment
type Subscription {
# dogEvents comment
dogEvents: [Dog!]!
# end of type comment
}
# Dog comment
type Dog {
# Dog name comment
name: String!
# end of type comment
}
# Person comment
type Person @key(fields: "name") {
# Person name comment
name: String!
# end of type comment
}
# extend type Dog comment
extend type Dog {
# Dog owner comment
owner: Person! @permission(permission: "admin")
# end of type comment
}
# end of file comment
# end of file comment 2
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
input CatInput {
food: String = "fish & meat"
}
# end of file comment
# end of file comment 2
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
# before schema description comment
"""schema description"""
# after schema description comment
schema {
# before query comment
query: TopQuery
# before mutation comment
mutation: TopMutation
# before subscription comment
subscription: TopSubscription
# end of schema comment
}
type TopMutation {
noop: Boolean
Expand Down Expand Up @@ -39,3 +46,5 @@ type TopSubscription {
arg2: String
): Boolean
}
# end of file comment
# end of file comment 2
7 changes: 6 additions & 1 deletion formatter/testdata/source/schema/definition.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ scalar Cat0
type Cat1 {
# Cat1 name comment
name: String
# end of Cat1 comment
}
# Cat2 comment
interface Cat2 {
# Cat2 name comment
name: String
# end of Cat2 comment
}
# Cat3_0 comment
type Cat3_0 {
Expand All @@ -35,10 +37,13 @@ enum Cat4 {
NFC
# Cat4 MAINECOON comment
MAINECOON
# end of Cat4 comment
}
# Cat5 comment
input Cat5 {
# Cat5 name comment
name: String
# end of Cat5 comment
}

# end of file comment
# end of file comment 2
2 changes: 2 additions & 0 deletions formatter/testdata/source/schema/description.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ type Cat {
# field comment
name: String
}
# end of file comment
# end of file comment 2
2 changes: 2 additions & 0 deletions formatter/testdata/source/schema/directive.graphql
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
directive @foo on FIELD|OBJECT
directive @bar repeatable on FIELD|OBJECT
# end of file comment
# end of file comment 2
2 changes: 2 additions & 0 deletions formatter/testdata/source/schema/directive_locations.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ enum ConnectionStatus @foo {
ERROR
}
union PersonUnion @foo = Person
# end of file comment
# end of file comment 2
7 changes: 7 additions & 0 deletions formatter/testdata/source/schema/extensions.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,35 @@ extend schema {
type Query @extends {
# dogs comment
dogs: [Dog!]!
# end of type comment
}

# subscription comment
type Subscription {
# dogEvents comment
dogEvents: [Dog!]!
# end of type comment
}

# Dog comment
type Dog {
# Dog name comment
name: String!
# end of type comment
}

# Person comment
type Person @key(fields: "name") {
# Person name comment
name: String!
# end of type comment
}

# extend type Dog comment
extend type Dog {
# Dog owner comment
owner: Person! @permission(permission: "admin")
# end of type comment
}

# directive @persmission comment
Expand All @@ -44,3 +49,5 @@ directive @permission(permission: String!) on FIELD_DEFINITION
directive @extends on OBJECT
# directive @key comment
directive @key(fields: String!) on OBJECT | INTERFACE
# end of file comment
# end of file comment 2
2 changes: 2 additions & 0 deletions formatter/testdata/source/schema/field_definition.graphql
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
input CatInput {
food: String = "fish & meat"
}
# end of file comment
# end of file comment 2
9 changes: 9 additions & 0 deletions formatter/testdata/source/schema/schema.graphql
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
# before schema description comment
"schema description"
# after schema description comment
schema {
# before query comment
query: TopQuery
# before mutation comment
mutation: TopMutation
# before subscription comment
subscription: TopSubscription
# end of schema comment
}

type TopMutation {
Expand Down Expand Up @@ -47,3 +54,5 @@ type TopSubscription {
arg2: String
): Boolean
}
# end of file comment
# end of file comment 2
Loading