Skip to content

Commit

Permalink
print/printSchema: remove trailing new line (#2997)
Browse files Browse the repository at this point in the history
Remove special behaviour for printing `DocumentNode` that added trailing
new line.
This change allows to tools to add newline only if it required by code
convention or OS convention.

Scripts and CLI tool can return previous behaviour by adding `\n`.
  • Loading branch information
IvanGoncharov committed Mar 28, 2021
1 parent a6a65b2 commit 1e46389
Show file tree
Hide file tree
Showing 12 changed files with 51 additions and 64 deletions.
27 changes: 8 additions & 19 deletions src/__testUtils__/__tests__/dedent-test.js
Expand Up @@ -25,7 +25,6 @@ describe('dedent', () => {
' id: ID',
' name: String',
'}',
'',
].join('\n'),
);
});
Expand All @@ -38,7 +37,7 @@ describe('dedent', () => {
fourth
`;
expect(output).to.equal(
['first', ' second', ' third', ' fourth', ''].join('\n'),
['first', ' second', ' third', ' fourth'].join('\n'),
);
});

Expand All @@ -53,7 +52,6 @@ describe('dedent', () => {
'type Root {',
' field(arg: String = "wi\th de\fault"): String',
'}',
'',
].join('\n'),
);
});
Expand All @@ -64,29 +62,20 @@ describe('dedent', () => {
\t\t me: User
\t\t }
`;
expect(output).to.equal(['type Query {', ' me: User', '}', ''].join('\n'));
expect(output).to.equal(['type Query {', ' me: User', '}'].join('\n'));
});

it('removes leading newlines', () => {
it('removes leading and trailing newlines', () => {
const output = dedent`
type Query {
me: User
}`;
expect(output).to.equal(['type Query {', ' me: User', '}'].join('\n'));
});

it('does not remove trailing newlines', () => {
const output = dedent`
type Query {
me: User
}
`;
expect(output).to.equal(
['type Query {', ' me: User', '}', '', ''].join('\n'),
);
expect(output).to.equal(['type Query {', ' me: User', '}'].join('\n'));
});

it('removes all trailing spaces and tabs', () => {
Expand All @@ -95,13 +84,14 @@ describe('dedent', () => {
me: User
}
\t\t \t `;
expect(output).to.equal(['type Query {', ' me: User', '}', ''].join('\n'));
expect(output).to.equal(['type Query {', ' me: User', '}'].join('\n'));
});

it('works on text without leading newline', () => {
const output = dedent` type Query {
me: User
}`;
}
`;
expect(output).to.equal(['type Query {', ' me: User', '}'].join('\n'));
});

Expand All @@ -124,7 +114,6 @@ describe('dedent', () => {
' "surname": "Doe"',
' }',
'}',
'',
].join('\n'),
);
});
Expand Down
7 changes: 3 additions & 4 deletions src/__testUtils__/dedent.js
@@ -1,14 +1,13 @@
/**
* An ES6 string tag that fixes indentation. Also removes leading newlines
* and trailing spaces and tabs, but keeps trailing newlines.
* An ES6 string tag that fixes indentation and also trims string.
*
* Example usage:
* const str = dedent`
* {
* test
* }
* `;
* str === "{\n test\n}\n";
* str === "{\n test\n}";
*/
export function dedent(
strings: $ReadOnlyArray<string>,
Expand All @@ -28,7 +27,7 @@ export function dedent(

const trimmedStr = str
.replace(/^\n*/m, '') // remove leading newline
.replace(/[ \t]*$/, ''); // remove trailing spaces and tabs
.replace(/[ \t\n]*$/, ''); // remove trailing spaces and tabs

// fixes indentation by removing leading spaces and tabs from each line
let indent = '';
Expand Down
2 changes: 1 addition & 1 deletion src/error/__tests__/GraphQLError-test.js
Expand Up @@ -182,7 +182,7 @@ describe('printError', () => {
fieldB.type,
]);

expect(printError(error) + '\n').to.equal(dedent`
expect(printError(error)).to.equal(dedent`
Example error with two nodes
SourceA:2:10
Expand Down
6 changes: 3 additions & 3 deletions src/language/__tests__/lexer-test.js
Expand Up @@ -165,7 +165,7 @@ describe('Lexer', () => {
} catch (error) {
caughtError = error;
}
expect(String(caughtError) + '\n').to.equal(dedent`
expect(String(caughtError)).to.equal(dedent`
Syntax Error: Cannot parse the unexpected character "?".
GraphQL request:3:5
Expand All @@ -185,7 +185,7 @@ describe('Lexer', () => {
} catch (error) {
caughtError = error;
}
expect(String(caughtError) + '\n').to.equal(dedent`
expect(String(caughtError)).to.equal(dedent`
Syntax Error: Cannot parse the unexpected character "?".
foo.js:13:6
Expand All @@ -204,7 +204,7 @@ describe('Lexer', () => {
} catch (error) {
caughtError = error;
}
expect(String(caughtError) + '\n').to.equal(dedent`
expect(String(caughtError)).to.equal(dedent`
Syntax Error: Cannot parse the unexpected character "?".
foo.js:1:5
Expand Down
8 changes: 4 additions & 4 deletions src/language/__tests__/parser-test.js
Expand Up @@ -32,7 +32,7 @@ describe('Parser', () => {
locations: [{ line: 1, column: 2 }],
});

expect(String(caughtError) + '\n').to.equal(dedent`
expect(String(caughtError)).to.equal(dedent`
Syntax Error: Expected Name, found <EOF>.
GraphQL request:1:2
Expand Down Expand Up @@ -76,7 +76,7 @@ describe('Parser', () => {
} catch (error) {
caughtError = error;
}
expect(String(caughtError) + '\n').to.equal(dedent`
expect(String(caughtError)).to.equal(dedent`
Syntax Error: Expected "{", found <EOF>.
MyQuery.graphql:1:6
Expand Down Expand Up @@ -217,7 +217,7 @@ describe('Parser', () => {

expect(toJSONDeep(result)).to.deep.equal({
kind: Kind.DOCUMENT,
loc: { start: 0, end: 41 },
loc: { start: 0, end: 40 },
definitions: [
{
kind: Kind.OPERATION_DEFINITION,
Expand Down Expand Up @@ -307,7 +307,7 @@ describe('Parser', () => {

expect(toJSONDeep(result)).to.deep.equal({
kind: Kind.DOCUMENT,
loc: { start: 0, end: 30 },
loc: { start: 0, end: 29 },
definitions: [
{
kind: Kind.OPERATION_DEFINITION,
Expand Down
10 changes: 5 additions & 5 deletions src/language/__tests__/printLocation-test.js
Expand Up @@ -16,7 +16,7 @@ describe('printSourceLocation', () => {
line: 1,
column: minifiedSource.body.indexOf('FIRST_ERROR_HERE') + 1,
});
expect(firstLocation + '\n').to.equal(dedent`
expect(firstLocation).to.equal(dedent`
GraphQL request:1:53
1 | query SomeMinifiedQueryWithErrorInside($foo:String!=FIRST_ERROR_HERE$bar:String)
| ^
Expand All @@ -27,7 +27,7 @@ describe('printSourceLocation', () => {
line: 1,
column: minifiedSource.body.indexOf('SECOND_ERROR_HERE') + 1,
});
expect(secondLocation + '\n').to.equal(dedent`
expect(secondLocation).to.equal(dedent`
GraphQL request:1:114
1 | query SomeMinifiedQueryWithErrorInside($foo:String!=FIRST_ERROR_HERE$bar:String)
| {someField(foo:$foo bar:$bar baz:SECOND_ERROR_HERE){fieldA fieldB{fieldC fieldD.
Expand All @@ -39,7 +39,7 @@ describe('printSourceLocation', () => {
line: 1,
column: minifiedSource.body.indexOf('THIRD_ERROR_HERE') + 1,
});
expect(thirdLocation + '\n').to.equal(dedent`
expect(thirdLocation).to.equal(dedent`
GraphQL request:1:166
1 | query SomeMinifiedQueryWithErrorInside($foo:String!=FIRST_ERROR_HERE$bar:String)
| {someField(foo:$foo bar:$bar baz:SECOND_ERROR_HERE){fieldA fieldB{fieldC fieldD.
Expand All @@ -54,7 +54,7 @@ describe('printSourceLocation', () => {
{ line: 1, column: 1 },
);

expect(result + '\n').to.equal(dedent`
expect(result).to.equal(dedent`
Test:9:1
9 | *
| ^
Expand All @@ -67,7 +67,7 @@ describe('printSourceLocation', () => {
{ line: 1, column: 1 },
);

expect(result + '\n').to.equal(dedent`
expect(result).to.equal(dedent`
Test:9:1
9 | *
| ^
Expand Down
16 changes: 8 additions & 8 deletions src/language/__tests__/schema-parser-test.js
Expand Up @@ -98,7 +98,7 @@ describe('Schema Parser', () => {
loc: { start: 0, end: 30 },
},
],
loc: { start: 0, end: 31 },
loc: { start: 0, end: 30 },
});
});

Expand Down Expand Up @@ -194,7 +194,7 @@ describe('Schema Parser', () => {
loc: { start: 0, end: 37 },
},
],
loc: { start: 0, end: 38 },
loc: { start: 0, end: 37 },
});
});

Expand Down Expand Up @@ -465,7 +465,7 @@ describe('Schema Parser', () => {
loc: { start: 0, end: 31 },
},
],
loc: { start: 0, end: 32 },
loc: { start: 0, end: 31 },
});
});

Expand Down Expand Up @@ -703,7 +703,7 @@ describe('Schema Parser', () => {
loc: { start: 0, end: 35 },
},
],
loc: { start: 0, end: 36 },
loc: { start: 0, end: 35 },
});
});

Expand Down Expand Up @@ -741,7 +741,7 @@ describe('Schema Parser', () => {
loc: { start: 0, end: 45 },
},
],
loc: { start: 0, end: 46 },
loc: { start: 0, end: 45 },
});
});

Expand Down Expand Up @@ -783,7 +783,7 @@ describe('Schema Parser', () => {
loc: { start: 0, end: 52 },
},
],
loc: { start: 0, end: 53 },
loc: { start: 0, end: 52 },
});
});

Expand Down Expand Up @@ -825,7 +825,7 @@ describe('Schema Parser', () => {
loc: { start: 0, end: 48 },
},
],
loc: { start: 0, end: 49 },
loc: { start: 0, end: 48 },
});
});

Expand Down Expand Up @@ -869,7 +869,7 @@ describe('Schema Parser', () => {
loc: { start: 0, end: 60 },
},
],
loc: { start: 0, end: 61 },
loc: { start: 0, end: 60 },
});
});

Expand Down
2 changes: 1 addition & 1 deletion src/language/printer.js
Expand Up @@ -21,7 +21,7 @@ const printDocASTReducer: any = {
// Document

Document: {
leave: (node) => join(node.definitions, '\n\n') + '\n',
leave: (node) => join(node.definitions, '\n\n'),
},

OperationDefinition: {
Expand Down
12 changes: 6 additions & 6 deletions src/utilities/__tests__/buildASTSchema-test.js
Expand Up @@ -728,7 +728,7 @@ describe('Schema Builder', () => {
`);

const someScalar = assertScalarType(schema.getType('SomeScalar'));
expect(printType(someScalar) + '\n').to.equal(dedent`
expect(printType(someScalar)).to.equal(dedent`
scalar SomeScalar
`);

Expand Down Expand Up @@ -757,7 +757,7 @@ describe('Schema Builder', () => {
`);

const someObject = assertObjectType(schema.getType('SomeObject'));
expect(printType(someObject) + '\n').to.equal(dedent`
expect(printType(someObject)).to.equal(dedent`
type SomeObject implements Foo & Bar & Baz {
first: String
second: Int
Expand Down Expand Up @@ -785,7 +785,7 @@ describe('Schema Builder', () => {
const schema = buildSchema(interfaceSDL);

const someInterface = assertInterfaceType(schema.getType('SomeInterface'));
expect(printType(someInterface) + '\n').to.equal(dedent`
expect(printType(someInterface)).to.equal(dedent`
interface SomeInterface {
first: String
second: Int
Expand All @@ -812,7 +812,7 @@ describe('Schema Builder', () => {
`);

const someUnion = assertUnionType(schema.getType('SomeUnion'));
expect(printType(someUnion) + '\n').to.equal(dedent`
expect(printType(someUnion)).to.equal(dedent`
union SomeUnion = FirstType | SecondType | ThirdType
`);

Expand All @@ -836,7 +836,7 @@ describe('Schema Builder', () => {
const schema = buildSchema(enumSDL);

const someEnum = assertEnumType(schema.getType('SomeEnum'));
expect(printType(someEnum) + '\n').to.equal(dedent`
expect(printType(someEnum)).to.equal(dedent`
enum SomeEnum {
FIRST
SECOND
Expand Down Expand Up @@ -864,7 +864,7 @@ describe('Schema Builder', () => {
const schema = buildSchema(inputSDL);

const someInput = assertInputObjectType(schema.getType('SomeInput'));
expect(printType(someInput) + '\n').to.equal(dedent`
expect(printType(someInput)).to.equal(dedent`
input SomeInput {
first: String
second: Int
Expand Down
7 changes: 4 additions & 3 deletions src/utilities/__tests__/extendSchema-test.js
Expand Up @@ -826,10 +826,11 @@ describe('extendSchema', () => {
interface AnotherNewInterface {
anotherNewField: String
}`;
}
`;
const schemaWithNewTypes = extendSchema(schema, parse(newTypesSDL));
expect(printSchemaChanges(schema, schemaWithNewTypes)).to.equal(
newTypesSDL + '\n',
newTypesSDL,
);

const extendAST = parse(`
Expand Down Expand Up @@ -1219,7 +1220,7 @@ describe('extendSchema', () => {

const queryType = extendedSchema.getQueryType();
expect(queryType).to.include({ name: 'Foo' });
expect(printASTNode(extendedSchema) + '\n').to.equal(extensionSDL);
expect(printASTNode(extendedSchema)).to.equal(extensionSDL);
});

it('adds new root types via schema extension', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/utilities/__tests__/stripIgnoredCharacters-test.js
Expand Up @@ -147,7 +147,7 @@ describe('stripIgnoredCharacters', () => {
caughtError = e;
}

expect(String(caughtError) + '\n').to.equal(dedent`
expect(String(caughtError)).to.equal(dedent`
Syntax Error: Unterminated string.
GraphQL request:1:13
Expand Down

0 comments on commit 1e46389

Please sign in to comment.