Skip to content

Commit

Permalink
Fix error locations for queries with leading whitespace (#122)
Browse files Browse the repository at this point in the history
  • Loading branch information
dfreeman authored and jnwng committed May 1, 2018
1 parent 55ed1f3 commit 5fc6a8c
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Change log
### vNEXT
- Fix error location information for literal .graphql files and strings with leading newlines in [#122](https://github.com/apollographql/eslint-plugin-graphql/pull/122) by [Dan Freeman](https://github.com/dfreeman)

### v2.1.0
- Retrieves `.graphqlconfig` relative to the file being linted, which re-enables support for `vscode-eslint` using `.graphqlconfig` in [#108](https://github.com/apollographql/eslint-plugin-graphql/pull/108) by [Jon Wong][https://github.com/jnwng/]
Expand Down
8 changes: 4 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -418,11 +418,11 @@ function locFrom(node, error) {

let line;
let column;
if (location.line === 1) {
if (location.line === 1 && node.tag.name !== internalTag) {
line = node.loc.start.line;
column = node.loc.start.col + location.col;
column = node.tag.loc.end.column + location.column;
} else {
line = node.loc.start.line + location.line;
line = node.loc.start.line + location.line - 1;
column = location.column - 1;
}

Expand Down Expand Up @@ -489,7 +489,7 @@ function replaceExpressions(node, context, env) {
}
});

return chunks.join('').trim();
return chunks.join('');
}

function strWithLen(len) {
Expand Down
6 changes: 5 additions & 1 deletion test/__fixtures__/required-fields-invalid-no-id.graphql
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
query { greetings { hello } }
query {
greetings {
hello
}
}
30 changes: 30 additions & 0 deletions test/makeProcessors.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,35 @@ describe('processors', () => {
});
});
});

describe('error line/column locations', () => {
it('populates correctly for a single-line document', () => {
const results = execute('required-fields-invalid-array');
assert.equal(results.errorCount, 1);
assert.deepEqual(results.results[0].messages[0], {
column: 9,
line: 1,
message: "'id' field required on 'stories'",
nodeType: 'TaggedTemplateExpression',
ruleId: 'graphql/required-fields',
severity: 2,
source: 'ESLintPluginGraphQLFile`query { stories { comments { text } } }'
});
});

it('populates correctly for a multi-line document', () => {
const results = execute('required-fields-invalid-no-id');
assert.equal(results.errorCount, 1);
assert.deepEqual(results.results[0].messages[0], {
column: 3,
line: 2,
message: "'id' field required on 'greetings'",
nodeType: 'TaggedTemplateExpression',
ruleId: 'graphql/required-fields',
severity: 2,
source: ' greetings {'
});
});
});
});
});
6 changes: 4 additions & 2 deletions test/makeRule.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,12 @@ const parserOptions = {
{
options,
parserOptions,
code: 'const x = gql`{ nonExistentQuery }`',
code: 'const x = gql`\n query {\n nonExistentQuery\n }\n`',
errors: [{
message: 'Cannot query field "nonExistentQuery" on type "Query".',
type: 'TaggedTemplateExpression'
type: 'TaggedTemplateExpression',
line: 3,
column: 5
}]
},
{
Expand Down

0 comments on commit 5fc6a8c

Please sign in to comment.