Skip to content

Commit 9bc4e1b

Browse files
authored
return code as-is in preprocess in case of parsing error, sort eslint/graphql-eslint errors in postprocess by line/column (#1168)
1 parent dfafdf7 commit 9bc4e1b

File tree

3 files changed

+62
-49
lines changed

3 files changed

+62
-49
lines changed

.changeset/little-hounds-kick.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphql-eslint/eslint-plugin': patch
3+
---
4+
5+
return code as-is in `preprocess` in case of parsing error, sort eslint/graphql-eslint errors in `postprocess` by line/column

packages/plugin/src/processor.ts

+25-17
Original file line numberDiff line numberDiff line change
@@ -39,24 +39,30 @@ export const processor: Linter.Processor<Block | string> = {
3939
if (RELEVANT_KEYWORDS.every(keyword => !code.includes(keyword))) {
4040
return [code];
4141
}
42-
const extractedDocuments = parseCode({
43-
code,
44-
filePath,
45-
options: {
46-
skipIndent: true,
47-
...graphQLTagPluckOptions,
48-
},
49-
});
5042

51-
const blocks: Block[] = extractedDocuments.map(item => ({
52-
filename: 'document.graphql',
53-
text: item.content,
54-
lineOffset: item.loc.start.line - 1,
55-
offset: item.start + 1,
56-
}));
57-
blocksMap.set(filePath, blocks);
43+
try {
44+
const extractedDocuments = parseCode({
45+
code,
46+
filePath,
47+
options: {
48+
skipIndent: true,
49+
...graphQLTagPluckOptions,
50+
},
51+
});
5852

59-
return [...blocks, code /* source code must be provided and be last */];
53+
const blocks: Block[] = extractedDocuments.map(item => ({
54+
filename: 'document.graphql',
55+
text: item.content,
56+
lineOffset: item.loc.start.line - 1,
57+
offset: item.start + 1,
58+
}));
59+
blocksMap.set(filePath, blocks);
60+
61+
return [...blocks, code /* source code must be provided and be last */];
62+
} catch {
63+
// in case of parsing error return code as is
64+
return [code];
65+
}
6066
},
6167
postprocess(messages, filePath) {
6268
const blocks = blocksMap.get(filePath) || [];
@@ -80,6 +86,8 @@ export const processor: Linter.Processor<Block | string> = {
8086
}
8187
}
8288

83-
return messages.flat();
89+
const result = messages.flat();
90+
// sort eslint/graphql-eslint messages by line/column
91+
return result.sort((a, b) => a.line - b.line || a.column - b.column);
8492
},
8593
};

packages/plugin/tests/__snapshots__/examples.spec.md

+32-32
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,6 @@ exports[`Examples should work in monorepo 1`] = `
3232
{
3333
filePath: examples/monorepo/client/pages/index.tsx,
3434
messages: [
35-
{
36-
column: 9,
37-
endColumn: 18,
38-
endLine: 9,
39-
line: 9,
40-
message: Cannot query field "firstname" on type "User". Did you mean "firstName" or "lastName"?,
41-
nodeType: null,
42-
ruleId: @graphql-eslint/fields-on-correct-type,
43-
severity: 2,
44-
},
4535
{
4636
column: 7,
4737
endColumn: 16,
@@ -53,6 +43,16 @@ exports[`Examples should work in monorepo 1`] = `
5343
ruleId: no-unused-vars,
5444
severity: 2,
5545
},
46+
{
47+
column: 9,
48+
endColumn: 18,
49+
endLine: 9,
50+
line: 9,
51+
message: Cannot query field "firstname" on type "User". Did you mean "firstName" or "lastName"?,
52+
nodeType: null,
53+
ruleId: @graphql-eslint/fields-on-correct-type,
54+
severity: 2,
55+
},
5656
],
5757
},
5858
{
@@ -805,6 +805,28 @@ exports[`Examples should work with \`graphql-config\` on \`.js\` files 1`] = `
805805
{
806806
filePath: examples/graphql-config-code-file/query.js,
807807
messages: [
808+
{
809+
column: 13,
810+
endColumn: 20,
811+
endLine: 1,
812+
line: 1,
813+
message: 'require' is not defined.,
814+
messageId: undef,
815+
nodeType: Identifier,
816+
ruleId: no-undef,
817+
severity: 2,
818+
},
819+
{
820+
column: 7,
821+
endColumn: 15,
822+
endLine: 3,
823+
line: 3,
824+
message: 'GET_USER' is assigned a value but never used.,
825+
messageId: unusedVar,
826+
nodeType: Identifier,
827+
ruleId: no-unused-vars,
828+
severity: 2,
829+
},
808830
{
809831
column: 3,
810832
endColumn: 8,
@@ -850,28 +872,6 @@ Include it in your selection set.,
850872
},
851873
],
852874
},
853-
{
854-
column: 13,
855-
endColumn: 20,
856-
endLine: 1,
857-
line: 1,
858-
message: 'require' is not defined.,
859-
messageId: undef,
860-
nodeType: Identifier,
861-
ruleId: no-undef,
862-
severity: 2,
863-
},
864-
{
865-
column: 7,
866-
endColumn: 15,
867-
endLine: 3,
868-
line: 3,
869-
message: 'GET_USER' is assigned a value but never used.,
870-
messageId: unusedVar,
871-
nodeType: Identifier,
872-
ruleId: no-unused-vars,
873-
severity: 2,
874-
},
875875
],
876876
},
877877
]

0 commit comments

Comments
 (0)