Skip to content

Commit d8c1dfd

Browse files
committed
printLocation: Add special support for minified documents
1 parent e171bbc commit d8c1dfd

File tree

2 files changed

+67
-3
lines changed

2 files changed

+67
-3
lines changed

src/language/__tests__/printLocation-test.js

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,48 @@ import dedent from '../../jsutils/dedent';
77
import { Source } from '../../language';
88
import { printSourceLocation } from '../printLocation';
99

10-
describe('printLocation', () => {
10+
describe('printSourceLocation', () => {
11+
it('prints minified documents', () => {
12+
const minifiedSource = new Source(
13+
'query SomeMiniFiedQueryWithErrorInside($foo:String!=FIRST_ERROR_HERE$bar:String){someField(foo:$foo bar:$bar baz:SECOND_ERROR_HERE){fieldA fieldB{fieldC fieldD...on THIRD_ERROR_HERE}}}',
14+
);
15+
16+
const firstLocation = printSourceLocation(minifiedSource, {
17+
line: 1,
18+
column: minifiedSource.body.indexOf('FIRST_ERROR_HERE') + 1,
19+
});
20+
expect(firstLocation + '\n').to.equal(dedent`
21+
GraphQL request:1:53
22+
1 | query SomeMiniFiedQueryWithErrorInside($foo:String!=FIRST_ERROR_HERE$bar:String)
23+
| ^
24+
| {someField(foo:$foo bar:$bar baz:SECOND_ERROR_HERE){fieldA fieldB{fieldC fieldD.
25+
`);
26+
27+
const secondLocation = printSourceLocation(minifiedSource, {
28+
line: 1,
29+
column: minifiedSource.body.indexOf('SECOND_ERROR_HERE') + 1,
30+
});
31+
expect(secondLocation + '\n').to.equal(dedent`
32+
GraphQL request:1:114
33+
1 | query SomeMiniFiedQueryWithErrorInside($foo:String!=FIRST_ERROR_HERE$bar:String)
34+
| {someField(foo:$foo bar:$bar baz:SECOND_ERROR_HERE){fieldA fieldB{fieldC fieldD.
35+
| ^
36+
| ..on THIRD_ERROR_HERE}}}
37+
`);
38+
39+
const thirdLocation = printSourceLocation(minifiedSource, {
40+
line: 1,
41+
column: minifiedSource.body.indexOf('THIRD_ERROR_HERE') + 1,
42+
});
43+
expect(thirdLocation + '\n').to.equal(dedent`
44+
GraphQL request:1:166
45+
1 | query SomeMiniFiedQueryWithErrorInside($foo:String!=FIRST_ERROR_HERE$bar:String)
46+
| {someField(foo:$foo bar:$bar baz:SECOND_ERROR_HERE){fieldA fieldB{fieldC fieldD.
47+
| ..on THIRD_ERROR_HERE}}}
48+
| ^
49+
`);
50+
});
51+
1152
it('prints single digit line number with no padding', () => {
1253
const result = printSourceLocation(
1354
new Source('*', 'Test', { line: 9, column: 1 }),

src/language/printLocation.js

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,37 @@ export function printSourceLocation(
3030

3131
const columnOffset = sourceLocation.line === 1 ? firstLineColumnOffset : 0;
3232
const columnNum = sourceLocation.column + columnOffset;
33+
const locationStr = `${source.name}:${lineNum}:${columnNum}\n`;
3334

3435
const lines = body.split(/\r\n|[\n\r]/g);
36+
const locationLine = lines[lineIndex];
37+
38+
// Special case for minified documents
39+
if (locationLine.length > 120) {
40+
const sublineIndex = Math.floor(columnNum / 80);
41+
const sublineColumnNum = columnNum % 80;
42+
const sublines = [];
43+
for (let i = 0; i < locationLine.length; i += 80) {
44+
sublines.push(locationLine.slice(i, i + 80));
45+
}
46+
47+
return (
48+
locationStr +
49+
printPrefixedLines([
50+
[`${lineNum}`, sublines[0]],
51+
...sublines.slice(1, sublineIndex + 1).map(subline => ['', subline]),
52+
[' ', whitespace(sublineColumnNum - 1) + '^'],
53+
['', sublines[sublineIndex + 1]],
54+
])
55+
);
56+
}
57+
3558
return (
36-
`${source.name}:${lineNum}:${columnNum}\n` +
59+
locationStr +
3760
printPrefixedLines([
3861
// Lines specified like this: ["prefix", "string"],
3962
[`${lineNum - 1}`, lines[lineIndex - 1]],
40-
[`${lineNum}`, lines[lineIndex]],
63+
[`${lineNum}`, locationLine],
4164
['', whitespace(columnNum - 1) + '^'],
4265
[`${lineNum + 1}`, lines[lineIndex + 1]],
4366
])

0 commit comments

Comments
 (0)