Skip to content

Commit 0f022e5

Browse files
ysmolskiYury Smolski
andauthored
fix: print indent once per level by default (#1147)
For v1 and v2 this fixes the default behaviour of the PrintIndent and PrintStringIndent functions of astprinter. Instead of printing indent twice for every depth level, it prints it once per level. The fix does not alter output for other parts, golden tests are intact. Tests were modified by passing correctly sized indent. Fixes #405 Co-authored-by: Yury Smolski <yury@smolsky.by>
1 parent ced33fc commit 0f022e5

29 files changed

Lines changed: 44 additions & 43 deletions

File tree

execution/graphql/normalization_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func TestRequest_Normalize(t *testing.T) {
4848
name
4949
}
5050
}`
51-
op, _ := astprinter.PrintStringIndent(&request.document, " ")
51+
op, _ := astprinter.PrintStringIndent(&request.document, " ")
5252
assert.Equal(t, normalizedOperation, op)
5353
})
5454

@@ -64,7 +64,7 @@ func TestRequest_Normalize(t *testing.T) {
6464
assert.True(t, result.Successful)
6565
assert.True(t, request.isNormalized)
6666

67-
op, _ := astprinter.PrintStringIndent(&request.document, " ")
67+
op, _ := astprinter.PrintStringIndent(&request.document, " ")
6868
assert.Equal(t, expectedNormalizedOperation, op)
6969
}
7070

execution/graphql/schema.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func (s *Schema) Normalize() (result NormalizationResult, err error) {
105105
}
106106

107107
normalizedSchemaBuffer := &bytes.Buffer{}
108-
err = astprinter.PrintIndent(&s.document, []byte(" "), normalizedSchemaBuffer)
108+
err = astprinter.PrintIndent(&s.document, []byte(" "), normalizedSchemaBuffer)
109109
if err != nil {
110110
return NormalizationResult{
111111
Successful: false,
@@ -402,7 +402,7 @@ func createSchema(schemaContent []byte, mergeWithBaseSchema bool) (*Schema, erro
402402
}
403403

404404
rawSchemaBuffer := &bytes.Buffer{}
405-
err = astprinter.PrintIndent(&document, []byte(" "), rawSchemaBuffer)
405+
err = astprinter.PrintIndent(&document, []byte(" "), rawSchemaBuffer)
406406
if err != nil {
407407
return nil, err
408408
}

execution/graphql/schema_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ func TestSchema_Document(t *testing.T) {
292292
require.NoError(t, err)
293293

294294
expectedSchemaBytesBuffer := &bytes.Buffer{}
295-
err = astprinter.PrintIndent(&document, []byte(" "), expectedSchemaBytesBuffer)
295+
err = astprinter.PrintIndent(&document, []byte(" "), expectedSchemaBytesBuffer)
296296
require.NoError(t, err)
297297

298298
assert.Equal(t, expectedSchemaBytesBuffer.Bytes(), schema.RawSchema())

pkg/ast/ast_description.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ func (d *Document) PrintDescription(description Description, indent []byte, dept
4545
skippedWhitespace += 1
4646
continue
4747
case runes.SPACE:
48-
skippedWhitespace += 0.5
4948
continue
5049
}
5150
}

pkg/ast/ast_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ func TestCopying(t *testing.T) {
182182
}
183183
}
184184

185-
out, err := astprinter.PrintStringIndent(&doc, nil, " ")
185+
out, err := astprinter.PrintStringIndent(&doc, nil, " ")
186186

187187
assert.NoError(t, err)
188188

pkg/astprinter/astprinter.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ func Print(document, definition *ast.Document, out io.Writer) error {
1818
}
1919

2020
// PrintIndent is the same as Print but accepts an additional indent parameter to set indentation.
21+
// Indent is written once for every depth level.
2122
func PrintIndent(document, definition *ast.Document, indent []byte, out io.Writer) error {
2223
printer := Printer{
2324
indent: indent,
@@ -93,12 +94,12 @@ func (p *printVisitor) indentationDepth() (depth int) {
9394
case ast.NodeKindOperationDefinition,
9495
ast.NodeKindFragmentDefinition:
9596
default:
96-
return 2
97+
return 1
9798
}
9899

99100
for i := range p.Ancestors {
100101
if p.Ancestors[i].Kind == ast.NodeKindSelectionSet {
101-
depth += 2
102+
depth += 1
102103
}
103104
}
104105

pkg/astprinter/astprinter_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ func TestPrintSchemaDefinition(t *testing.T) {
516516
doc := unsafeparser.ParseGraphqlDocumentFile("./testdata/starwars.schema.graphql")
517517

518518
buff := bytes.Buffer{}
519-
err := PrintIndent(&doc, nil, []byte(" "), &buff)
519+
err := PrintIndent(&doc, nil, []byte(" "), &buff)
520520
if err != nil {
521521
t.Fatal(err)
522522
}
@@ -540,7 +540,7 @@ func TestPrintOperationDefinition(t *testing.T) {
540540
operation := unsafeparser.ParseGraphqlDocumentFile("./testdata/introspectionquery.graphql")
541541

542542
buff := bytes.Buffer{}
543-
err := PrintIndent(&operation, &schema, []byte(" "), &buff)
543+
err := PrintIndent(&operation, &schema, []byte(" "), &buff)
544544
if err != nil {
545545
t.Fatal(err)
546546
}

pkg/asttransform/baseschema_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func runTestMerge(definition, fixtureName string) func(t *testing.T) {
2020
panic(err)
2121
}
2222
buf := bytes.Buffer{}
23-
err = astprinter.PrintIndent(&doc, nil, []byte(" "), &buf)
23+
err = astprinter.PrintIndent(&doc, nil, []byte(" "), &buf)
2424
if err != nil {
2525
panic(err)
2626
}

pkg/astvalidation/reference/testsgo/harness_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ func ExtendSchema(schema string, sdlStr string) string {
248248
report := operationreport.Report{}
249249
parser.Parse(&definition, &report)
250250

251-
res, _ := astprinter.PrintStringIndent(&definition, nil, " ")
251+
res, _ := astprinter.PrintStringIndent(&definition, nil, " ")
252252

253253
return res
254254
}

pkg/engine/datasourcetesting/datasourcetesting.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ func RunTest(definition, operation, operationName string, expectedPlan plan.Plan
4040
p := plan.NewPlanner(ctx, config)
4141
actualPlan := p.Plan(&op, &def, operationName, &report)
4242
if report.HasErrors() {
43-
_, err := astprinter.PrintStringIndent(&def, nil, " ")
43+
_, err := astprinter.PrintStringIndent(&def, nil, " ")
4444
if err != nil {
4545
t.Fatal(err)
4646
}
47-
_, err = astprinter.PrintStringIndent(&op, &def, " ")
47+
_, err = astprinter.PrintStringIndent(&op, &def, " ")
4848
if err != nil {
4949
t.Fatal(err)
5050
}

0 commit comments

Comments
 (0)