Skip to content

Commit 698f651

Browse files
committed
Strip Common Indentation from BlockString Descriptions
Multi-line descriptions need to have their common indentation level (which results from indentation of that part of the schema, rather than being intentional for the description text) removed to ensure the descriptions use the correct value, are formatted correctly etc This is to meet the condition documented in the GraphQL spec: https://graphql.github.io/graphql-spec/June2018/#sec-String-Value > Since block strings represent freeform text often used in indented > positions, the string value semantics of a block string excludes > uniform indentation and blank initial and trailing lines via > BlockStringValue().
1 parent 38a077b commit 698f651

File tree

3 files changed

+219
-4
lines changed

3 files changed

+219
-4
lines changed

internal/common/blockstring.go

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
// MIT License
2+
//
3+
// Copyright (c) 2019 GraphQL Contributors
4+
//
5+
// Permission is hereby granted, free of charge, to any person obtaining a copy
6+
// of this software and associated documentation files (the "Software"), to deal
7+
// in the Software without restriction, including without limitation the rights
8+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
// copies of the Software, and to permit persons to whom the Software is
10+
// furnished to do so, subject to the following conditions:
11+
//
12+
// The above copyright notice and this permission notice shall be included in all
13+
// copies or substantial portions of the Software.
14+
//
15+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
// SOFTWARE.
22+
//
23+
// This implementation has been adapted from the graphql-js reference implementation
24+
// https://github.com/graphql/graphql-js/blob/5eb7c4ded7ceb83ac742149cbe0dae07a8af9a30/src/language/blockString.js
25+
// which is released under the MIT License above.
26+
27+
package common
28+
29+
import (
30+
"strings"
31+
)
32+
33+
// Produces the value of a block string from its parsed raw value, similar to
34+
// CoffeeScript's block string, Python's docstring trim or Ruby's strip_heredoc.
35+
//
36+
// This implements the GraphQL spec's BlockStringValue() static algorithm.
37+
func blockString(raw string) string {
38+
lines := strings.Split(raw, "\n")
39+
40+
// Remove common indentation from all lines except the first (which has none)
41+
ind := blockStringIndentation(lines)
42+
if ind > 0 {
43+
for i := 1; i < len(lines); i++ {
44+
l := lines[i]
45+
if len(l) < ind {
46+
lines[i] = ""
47+
continue
48+
}
49+
lines[i] = l[ind:]
50+
}
51+
}
52+
53+
// Remove leading and trailing blank lines
54+
trimStart := 0
55+
for i := 0; i < len(lines) && isBlank(lines[i]); i++ {
56+
trimStart++
57+
}
58+
lines = lines[trimStart:]
59+
trimEnd := 0
60+
for i := len(lines) - 1; i > 0 && isBlank(lines[i]); i-- {
61+
trimEnd++
62+
}
63+
lines = lines[:len(lines)-trimEnd]
64+
65+
return strings.Join(lines, "\n")
66+
}
67+
68+
func blockStringIndentation(lines []string) int {
69+
var commonIndent *int
70+
for i := 1; i < len(lines); i++ {
71+
l := lines[i]
72+
indent := leadingWhitespace(l)
73+
if indent == len(l) {
74+
// don't consider blank/empty lines
75+
continue
76+
}
77+
if indent == 0 {
78+
return 0
79+
}
80+
if commonIndent == nil || indent < *commonIndent {
81+
commonIndent = &indent
82+
}
83+
}
84+
if commonIndent == nil {
85+
return 0
86+
}
87+
return *commonIndent
88+
}
89+
90+
func isBlank(s string) bool {
91+
return len(s) == 0 || leadingWhitespace(s) == len(s)
92+
}
93+
94+
func leadingWhitespace(s string) int {
95+
i := 0
96+
for _, r := range s {
97+
if r != '\t' && r != ' ' {
98+
break
99+
}
100+
i++
101+
}
102+
return i
103+
}

internal/common/lexer.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,7 @@ func (l *Lexer) consumeTripleQuoteComment() string {
184184
}
185185
val := buf.String()
186186
val = val[:len(val)-numQuotes]
187-
val = strings.TrimSpace(val)
188-
return val
187+
return blockString(val)
189188
}
190189

191190
func (l *Lexer) consumeStringComment() string {

internal/schema/schema_test.go

Lines changed: 115 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func TestParse(t *testing.T) {
7373
},
7474
},
7575
{
76-
name: "Parses type with multi-line description string",
76+
name: "Parses type with simple multi-line 'BlockString' description",
7777
sdl: `
7878
"""
7979
Multi-line description.
@@ -95,7 +95,120 @@ func TestParse(t *testing.T) {
9595
},
9696
},
9797
{
98-
name: "Parses type with multi-line description and ignores comments",
98+
name: "Parses type with empty multi-line 'BlockString' description",
99+
sdl: `
100+
"""
101+
"""
102+
type Type {
103+
field: String
104+
}`,
105+
useStringDescriptions: true,
106+
validateSchema: func(s *schema.Schema) error {
107+
const typeName = "Type"
108+
typ, ok := s.Types[typeName].(*schema.Object)
109+
if !ok {
110+
return fmt.Errorf("type %q not found", typeName)
111+
}
112+
if want, have := "", typ.Description(); want != have {
113+
return fmt.Errorf("invalid description: want %q, have %q", want, have)
114+
}
115+
return nil
116+
},
117+
},
118+
{
119+
name: "Parses type with multi-line 'BlockString' description",
120+
sdl: `
121+
"""
122+
First line of the description.
123+
124+
Second line of the description.
125+
126+
query {
127+
code {
128+
example
129+
}
130+
}
131+
132+
Notes:
133+
134+
* First note
135+
* Second note
136+
"""
137+
type Type {
138+
field: String
139+
}`,
140+
useStringDescriptions: true,
141+
validateSchema: func(s *schema.Schema) error {
142+
const typeName = "Type"
143+
typ, ok := s.Types[typeName].(*schema.Object)
144+
if !ok {
145+
return fmt.Errorf("type %q not found", typeName)
146+
}
147+
want := "First line of the description.\n\nSecond line of the description.\n\n\tquery {\n\t\tcode {\n\t\t\texample\n\t\t}\n\t}\n\nNotes:\n\n * First note\n * Second note"
148+
if have := typ.Description(); want != have {
149+
return fmt.Errorf("invalid description: want %q, have %q", want, have)
150+
}
151+
return nil
152+
},
153+
},
154+
{
155+
name: "Parses type with un-indented multi-line 'BlockString' description",
156+
sdl: `
157+
"""
158+
First line of the description.
159+
160+
Second line of the description.
161+
"""
162+
type Type {
163+
field: String
164+
}`,
165+
useStringDescriptions: true,
166+
validateSchema: func(s *schema.Schema) error {
167+
const typeName = "Type"
168+
typ, ok := s.Types[typeName].(*schema.Object)
169+
if !ok {
170+
return fmt.Errorf("type %q not found", typeName)
171+
}
172+
want := "First line of the description.\n\nSecond line of the description."
173+
if have := typ.Description(); want != have {
174+
return fmt.Errorf("invalid description: want %q, have %q", want, have)
175+
}
176+
return nil
177+
},
178+
},
179+
{
180+
name: "Parses type with space-indented multi-line 'BlockString' description",
181+
sdl: `
182+
"""
183+
First line of the description.
184+
185+
Second line of the description.
186+
187+
query {
188+
code {
189+
example
190+
}
191+
}
192+
"""
193+
type Type {
194+
field: String
195+
}`,
196+
useStringDescriptions: true,
197+
validateSchema: func(s *schema.Schema) error {
198+
const typeName = "Type"
199+
typ, ok := s.Types[typeName].(*schema.Object)
200+
if !ok {
201+
return fmt.Errorf("type %q not found", typeName)
202+
}
203+
want := "First line of the description.\n\nSecond line of the description.\n\n query {\n code {\n example\n }\n }"
204+
if have := typ.Description(); want != have {
205+
return fmt.Errorf("invalid description: want %q, have %q", want, have)
206+
}
207+
return nil
208+
},
209+
},
210+
{
211+
name: "Parses type with multi-line 'BlockString' description and ignores comments",
99212
sdl: `
100213
"""
101214
Multi-line description with ignored comments.

0 commit comments

Comments
 (0)