Skip to content

Commit 713e0e8

Browse files
authored
Merge pull request #951 from dwwoelfel/errors-fix
Fix incorrect column number in errors
2 parents b42a8ea + 6592442 commit 713e0e8

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

src/error/syntaxError.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import { getLocation } from '../language/location';
1212
import type { Source } from '../language/source';
1313
import { GraphQLError } from './GraphQLError';
1414

15+
import type {SourceLocation} from '../language/location';
16+
1517
/**
1618
* Produces a GraphQLError representing a syntax error, containing useful
1719
* descriptive information about the syntax error's position in the source.
@@ -23,7 +25,8 @@ export function syntaxError(
2325
): GraphQLError {
2426
const location = getLocation(source, position);
2527
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;
2730
const error = new GraphQLError(
2831
`Syntax Error ${source.name} (${line}:${column}) ${description}` +
2932
'\n\n' + highlightSourceAtLocation(source, location),
@@ -41,14 +44,14 @@ export function syntaxError(
4144
function highlightSourceAtLocation(source, location) {
4245
const line = location.line;
4346
const lineOffset = source.locationOffset.line - 1;
44-
const columnOffset = source.locationOffset.column - 1;
47+
const columnOffset = getColumnOffset(source, location);
4548
const contextLine = line + lineOffset;
4649
const prevLineNum = (contextLine - 1).toString();
4750
const lineNum = contextLine.toString();
4851
const nextLineNum = (contextLine + 1).toString();
4952
const padLen = nextLineNum.length;
5053
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];
5255
return (
5356
(line >= 2 ?
5457
lpad(padLen, prevLineNum) + ': ' + lines[line - 2] + '\n' : '') +
@@ -59,6 +62,13 @@ function highlightSourceAtLocation(source, location) {
5962
);
6063
}
6164

65+
function getColumnOffset(
66+
source: Source,
67+
location: SourceLocation
68+
): number {
69+
return location.line === 1 ? source.locationOffset.column - 1 : 0;
70+
}
71+
6272
function whitespace(len) {
6373
return Array(len + 1).join(' ');
6474
}

src/language/__tests__/lexer-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ describe('Lexer', () => {
127127
'\n' +
128128
' ?\n' +
129129
'\n';
130-
const source = new Source(str, 'foo.js', { line: 11, column: 1 });
130+
const source = new Source(str, 'foo.js', { line: 11, column: 12 });
131131
return createLexer(source).advance();
132132
}).to.throw(
133133
'Syntax Error foo.js (13:6) ' +

src/language/location.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import type { Source } from './source';
1313
/**
1414
* Represents a location in a Source.
1515
*/
16-
type SourceLocation = {
16+
export type SourceLocation = {
1717
line: number;
1818
column: number;
1919
};

0 commit comments

Comments
 (0)