Skip to content

Commit 8da9203

Browse files
author
Daniel Woelfel
committed
1-indexed columns and account for column in error
1 parent b176d67 commit 8da9203

File tree

3 files changed

+30
-9
lines changed

3 files changed

+30
-9
lines changed

src/error/syntaxError.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ export function syntaxError(
2323
): GraphQLError {
2424
const location = getLocation(source, position);
2525
const line = location.line + source.locationOffset.line - 1;
26+
const column = location.column + source.locationOffset.column - 1;
2627
const error = new GraphQLError(
27-
`Syntax Error ${source.name} (${line}:${location.column}) ${description}` +
28+
`Syntax Error ${source.name} (${line}:${column}) ${description}` +
2829
'\n\n' +
2930
highlightSourceAtLocation(source, location),
3031
undefined,
@@ -41,22 +42,28 @@ export function syntaxError(
4142
function highlightSourceAtLocation(source, location) {
4243
const line = location.line;
4344
const lineOffset = source.locationOffset.line - 1;
45+
const columnOffset = source.locationOffset.column - 1;
4446
const contextLine = line + lineOffset;
4547
const prevLineNum = (contextLine - 1).toString();
4648
const lineNum = contextLine.toString();
4749
const nextLineNum = (contextLine + 1).toString();
4850
const padLen = nextLineNum.length;
4951
const lines = source.body.split(/\r\n|[\n\r]/g);
52+
lines[0] = whitespace(columnOffset) + lines[0];
5053
return (
5154
(line >= 2 ?
5255
lpad(padLen, prevLineNum) + ': ' + lines[line - 2] + '\n' : '') +
5356
lpad(padLen, lineNum) + ': ' + lines[line - 1] + '\n' +
54-
Array(2 + padLen + location.column).join(' ') + '^\n' +
57+
whitespace(2 + padLen + location.column + columnOffset - 1) + '^\n' +
5558
(line < lines.length ?
5659
lpad(padLen, nextLineNum) + ': ' + lines[line] + '\n' : '')
5760
);
5861
}
5962

63+
function whitespace(len) {
64+
return Array(len + 1).join(' ');
65+
}
66+
6067
function lpad(len, str) {
61-
return Array(len - str.length + 1).join(' ') + str;
68+
return whitespace(len - str.length) + str;
6269
}

src/language/__tests__/lexer-test.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,21 +125,34 @@ describe('Lexer', () => {
125125
const str = '' +
126126
'\n' +
127127
'\n' +
128-
' ?\n' +
128+
' ?\n' +
129129
'\n';
130-
const source = new Source(str, 'foo.js', { line: 11, column: 0 });
130+
const source = new Source(str, 'foo.js', { line: 11, column: 1 });
131131
return createLexer(source).advance();
132132
}).to.throw(
133-
'Syntax Error foo.js (13:5) ' +
133+
'Syntax Error foo.js (13:6) ' +
134134
'Cannot parse the unexpected character "?".\n' +
135135
'\n' +
136136
'12: \n' +
137-
'13: ?\n' +
138-
' ^\n' +
137+
'13: ?\n' +
138+
' ^\n' +
139139
'14: \n'
140140
);
141141
});
142142

143+
it('updates column numbers in error for file context', () => {
144+
expect(() => {
145+
const source = new Source('?', 'foo.js', { line: 1, column: 5 });
146+
return createLexer(source).advance();
147+
}).to.throw(
148+
'Syntax Error foo.js (1:5) ' +
149+
'Cannot parse the unexpected character "?".\n' +
150+
'\n' +
151+
'1: ?\n' +
152+
' ^\n'
153+
);
154+
});
155+
143156
it('lexes strings', () => {
144157

145158
expect(

src/language/source.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ type Location = {
1919
* store GraphQL documents in source files; for example, if the GraphQL input
2020
* starts at line 40 in a file named Foo.graphql, it might be useful for name to
2121
* be "Foo.graphql" and location to be `{ line: 40, column: 0 }`.
22+
* line and columns in locationOffset are 1-indexed
2223
*/
2324
export class Source {
2425
body: string;
@@ -28,6 +29,6 @@ export class Source {
2829
constructor(body: string, name?: string, locationOffset?: Location): void {
2930
this.body = body;
3031
this.name = name || 'GraphQL request';
31-
this.locationOffset = locationOffset || { line: 1, column: 0 };
32+
this.locationOffset = locationOffset || { line: 1, column: 1 };
3233
}
3334
}

0 commit comments

Comments
 (0)