@@ -12,6 +12,8 @@ import { getLocation } from '../language/location';
12
12
import type { Source } from '../language/source' ;
13
13
import { GraphQLError } from './GraphQLError' ;
14
14
15
+ import type { SourceLocation } from '../language/location' ;
16
+
15
17
/**
16
18
* Produces a GraphQLError representing a syntax error, containing useful
17
19
* descriptive information about the syntax error's position in the source.
@@ -23,7 +25,8 @@ export function syntaxError(
23
25
) : GraphQLError {
24
26
const location = getLocation ( source , position ) ;
25
27
const line = location . line + source . locationOffset . line - 1 ;
26
- const column = location . column + source . locationOffset . column - 1 ;
28
+ const columnOffset = getColumnOffset ( source , location ) ;
29
+ const column = location . column + columnOffset ;
27
30
const error = new GraphQLError (
28
31
`Syntax Error ${ source . name } (${ line } :${ column } ) ${ description } ` +
29
32
'\n\n' + highlightSourceAtLocation ( source , location ) ,
@@ -41,14 +44,14 @@ export function syntaxError(
41
44
function highlightSourceAtLocation ( source , location ) {
42
45
const line = location . line ;
43
46
const lineOffset = source . locationOffset . line - 1 ;
44
- const columnOffset = source . locationOffset . column - 1 ;
47
+ const columnOffset = getColumnOffset ( source , location ) ;
45
48
const contextLine = line + lineOffset ;
46
49
const prevLineNum = ( contextLine - 1 ) . toString ( ) ;
47
50
const lineNum = contextLine . toString ( ) ;
48
51
const nextLineNum = ( contextLine + 1 ) . toString ( ) ;
49
52
const padLen = nextLineNum . length ;
50
53
const lines = source . body . split ( / \r \n | [ \n \r ] / g) ;
51
- lines [ 0 ] = whitespace ( columnOffset ) + lines [ 0 ] ;
54
+ lines [ 0 ] = whitespace ( source . locationOffset . column - 1 ) + lines [ 0 ] ;
52
55
return (
53
56
( line >= 2 ?
54
57
lpad ( padLen , prevLineNum ) + ': ' + lines [ line - 2 ] + '\n' : '' ) +
@@ -59,6 +62,13 @@ function highlightSourceAtLocation(source, location) {
59
62
) ;
60
63
}
61
64
65
+ function getColumnOffset (
66
+ source : Source ,
67
+ location : SourceLocation
68
+ ) : number {
69
+ return location . line === 1 ? source . locationOffset . column - 1 : 0 ;
70
+ }
71
+
62
72
function whitespace ( len ) {
63
73
return Array ( len + 1 ) . join ( ' ' ) ;
64
74
}
0 commit comments