forked from graphql/graphql-js
/
printLocation.js
66 lines (55 loc) · 1.93 KB
/
printLocation.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// @flow strict
import { type Source } from '../language/source';
import { type Location } from '../language/ast';
import { type SourceLocation, getLocation } from '../language/location';
/**
* Render a helpful description of the location in the GraphQL Source document.
*/
export function printLocation(location: Location): string {
return printSourceLocation(
location.source,
getLocation(location.source, location.start),
);
}
/**
* Render a helpful description of the location in the GraphQL Source document.
*/
export function printSourceLocation(
source: Source,
sourceLocation: SourceLocation,
): string {
const firstLineColumnOffset = source.locationOffset.column - 1;
const body = whitespace(firstLineColumnOffset) + source.body;
const lineIndex = sourceLocation.line - 1;
const lineOffset = source.locationOffset.line - 1;
const lineNum = sourceLocation.line + lineOffset;
const columnOffset = sourceLocation.line === 1 ? firstLineColumnOffset : 0;
const columnNum = sourceLocation.column + columnOffset;
const lines = body.split(/\r\n|[\n\r]/g);
return (
`${source.name}:${lineNum}:${columnNum}\n` +
printPrefixedLines([
// Lines specified like this: ["prefix", "string"],
[`${lineNum - 1}: `, lines[lineIndex - 1]],
[`${lineNum}: `, lines[lineIndex]],
['', whitespace(columnNum - 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);
}
return existingLines
.map(([prefix, line]) => lpad(padLen, prefix) + line)
.join('\n');
}
function whitespace(len: number): string {
return Array(len + 1).join(' ');
}
function lpad(len: number, str: string): string {
return whitespace(len - str.length) + str;
}