Skip to content

Commit

Permalink
printLocation: Use vertical bar as number column delimiter
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov committed Jun 25, 2019
1 parent b904859 commit e171bbc
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 39 deletions.
16 changes: 8 additions & 8 deletions src/error/__tests__/GraphQLError-test.js
Expand Up @@ -217,16 +217,16 @@ describe('printError', () => {
Example error with two nodes
SourceA:2:10
1: type Foo {
2: field: String
^
3: }
1 | type Foo {
2 | field: String
| ^
3 | }
SourceB:2:10
1: type Foo {
2: field: Int
^
3: }
1 | type Foo {
2 | field: Int
| ^
3 | }
`);
});
});
20 changes: 10 additions & 10 deletions src/language/__tests__/lexer-test.js
Expand Up @@ -122,10 +122,10 @@ describe('Lexer', () => {
Syntax Error: Cannot parse the unexpected character "?".
GraphQL request:3:5
2:
3: ?
^
4:
2 |
3 | ?
| ^
4 |
`);
});

Expand All @@ -142,10 +142,10 @@ describe('Lexer', () => {
Syntax Error: Cannot parse the unexpected character "?".
foo.js:13:6
12:
13: ?
^
14:
12 |
13 | ?
| ^
14 |
`);
});

Expand All @@ -161,8 +161,8 @@ describe('Lexer', () => {
Syntax Error: Cannot parse the unexpected character "?".
foo.js:1:5
1: ?
^
1 | ?
| ^
`);
});

Expand Down
8 changes: 4 additions & 4 deletions src/language/__tests__/parser-test.js
Expand Up @@ -48,8 +48,8 @@ describe('Parser', () => {
Syntax Error: Expected Name, found <EOF>
GraphQL request:1:2
1: {
^
1 | {
| ^
`);

expectSyntaxError(
Expand Down Expand Up @@ -85,8 +85,8 @@ describe('Parser', () => {
Syntax Error: Expected {, found <EOF>
MyQuery.graphql:1:6
1: query
^
1 | query
| ^
`);
});

Expand Down
10 changes: 5 additions & 5 deletions src/language/__tests__/printLocation-test.js
Expand Up @@ -16,8 +16,8 @@ describe('printLocation', () => {

expect(result + '\n').to.equal(dedent`
Test:9:1
9: *
^
9 | *
| ^
`);
});

Expand All @@ -29,9 +29,9 @@ describe('printLocation', () => {

expect(result + '\n').to.equal(dedent`
Test:9:1
9: *
^
10:
9 | *
| ^
10 |
`);
});
});
14 changes: 5 additions & 9 deletions src/language/printLocation.js
Expand Up @@ -36,24 +36,20 @@ export function printSourceLocation(
`${source.name}:${lineNum}:${columnNum}\n` +
printPrefixedLines([
// Lines specified like this: ["prefix", "string"],
[`${lineNum - 1}: `, lines[lineIndex - 1]],
[`${lineNum}: `, lines[lineIndex]],
[`${lineNum - 1}`, lines[lineIndex - 1]],
[`${lineNum}`, lines[lineIndex]],
['', whitespace(columnNum - 1) + '^'],
[`${lineNum + 1}: `, lines[lineIndex + 1]],
[`${lineNum + 1}`, lines[lineIndex + 1]],
])
);
}

function printPrefixedLines(lines: Array<[string, string]>): string {
const existingLines = lines.filter(([_, line]) => line !== undefined);

let padLen = 0;
for (const [prefix] of existingLines) {
padLen = Math.max(padLen, prefix.length);
}

const padLen = Math.max(...existingLines.map(([prefix]) => prefix.length));
return existingLines
.map(([prefix, line]) => lpad(padLen, prefix) + line)
.map(([prefix, line]) => lpad(padLen, prefix) + ' | ' + line)
.join('\n');
}

Expand Down
6 changes: 3 additions & 3 deletions src/utilities/__tests__/stripIgnoredCharacters-test.js
Expand Up @@ -157,9 +157,9 @@ describe('stripIgnoredCharacters', () => {
Syntax Error: Unterminated string.
GraphQL request:1:13
1: { foo(arg: "
^
2: "
1 | { foo(arg: "
| ^
2 | "
`);
});

Expand Down

0 comments on commit e171bbc

Please sign in to comment.