Skip to content

Commit f031f56

Browse files
authored
report errors on first character for *.vue/*.svelte code, due graphql-tag-pluck limitation we can't know right location (#1346)
1 parent 069c437 commit f031f56

File tree

6 files changed

+45
-70
lines changed

6 files changed

+45
-70
lines changed

.changeset/breezy-walls-invent.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphql-eslint/eslint-plugin': patch
3+
---
4+
5+
report errors on first character for `*.vue`/`*.svelte` code, due graphql-tag-pluck limitation we can't know right location

.vscode/launch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"version": "0.2.0",
66
"configurations": [
77
{
8-
"type": "pwa-node",
8+
"type": "node",
99
"request": "launch",
1010
"name": "Debug Tests",
1111
"skipFiles": ["<node_internals>/**"],

.vscode/settings.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,14 @@
1616
"mode": "auto",
1717
"changeProcessCWD": true
1818
}
19+
],
20+
"eslint.validate": [
21+
"javascript",
22+
"javascriptreact",
23+
"typescript",
24+
"typescriptreact",
25+
"graphql",
26+
"vue",
27+
"svelte"
1928
]
2029
}

examples/vue-code-file/test.vue

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
<template>
2+
<span>test</span>
3+
</template>
14
<script>
25
/* eslint-disable no-unused-vars */
36

packages/plugin/src/processor.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
import { asArray } from '@graphql-tools/utils';
77
import { GraphQLConfig } from 'graphql-config';
88
import { loadOnDiskGraphQLConfig } from './graphql-config.js';
9+
import { REPORT_ON_FIRST_CHARACTER } from './utils.js';
910

1011
export type Block = Linter.ProcessorFile & {
1112
lineOffset: number;
@@ -58,19 +59,20 @@ export const processor: Linter.Processor<Block | string> = {
5859
skipIndent: true,
5960
...pluckConfig,
6061
});
61-
const isSvelte = filePath.endsWith('.svelte');
6262

6363
const blocks: Block[] = sources.map(item => ({
6464
filename: 'document.graphql',
6565
text: item.body,
66-
lineOffset: item.locationOffset.line - (isSvelte ? 3 : 1),
66+
lineOffset: item.locationOffset.line - 1,
6767
// @ts-expect-error -- `index` field exist but show ts error
68-
offset: item.locationOffset.index + (isSvelte ? -52 : 1),
68+
offset: item.locationOffset.index + 1,
6969
}));
7070
blocksMap.set(filePath, blocks);
7171

7272
return [...blocks, code /* source code must be provided and be last */];
73-
} catch {
73+
} catch (e) {
74+
// eslint-disable-next-line no-console
75+
console.error(e);
7476
// in case of parsing error return code as is
7577
return [code];
7678
}
@@ -80,7 +82,19 @@ export const processor: Linter.Processor<Block | string> = {
8082
for (let i = 0; i < blocks.length; i += 1) {
8183
const { lineOffset, offset } = blocks[i];
8284

83-
for (const message of messages[i]) {
85+
for (const message of messages[i] || []) {
86+
const isVueOrSvelte = /\.(vue|svelte)$/.test(filePath);
87+
if (isVueOrSvelte) {
88+
// We can't show correct report location because after processing with
89+
// graphql-tag-pluck location is incorrect, disable fixes as well
90+
delete message.endLine;
91+
delete message.endColumn;
92+
delete message.fix;
93+
delete message.suggestions;
94+
Object.assign(message, REPORT_ON_FIRST_CHARACTER);
95+
continue;
96+
}
97+
8498
message.line += lineOffset;
8599
// endLine can not exist if only `loc: { start, column }` was provided to context.report
86100
if (typeof message.endLine === 'number') {

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

Lines changed: 8 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -241,49 +241,21 @@ exports[`Examples > should work in svelte 1`] = `
241241
filePath: examples/svelte-code-file/test.svelte,
242242
messages: [
243243
{
244-
column: 3,
245-
endColumn: 8,
246-
endLine: 5,
247-
line: 5,
244+
column: 0,
245+
line: 1,
248246
message: Anonymous GraphQL operations are forbidden. Make sure to name your query!,
249247
messageId: no-anonymous-operations,
250248
nodeType: null,
251249
ruleId: @graphql-eslint/no-anonymous-operations,
252250
severity: 2,
253-
suggestions: [
254-
{
255-
desc: Rename to \`user\`,
256-
fix: {
257-
range: [
258-
78,
259-
78,
260-
],
261-
text: user,
262-
},
263-
},
264-
],
265251
},
266252
{
267-
column: 9,
268-
endColumn: 18,
269-
endLine: 13,
270-
line: 13,
253+
column: 0,
254+
line: 1,
271255
message: Operation "UserQuery" should not have "Query" suffix,
272256
nodeType: Name,
273257
ruleId: @graphql-eslint/naming-convention,
274258
severity: 2,
275-
suggestions: [
276-
{
277-
desc: Rename to \`User\`,
278-
fix: {
279-
range: [
280-
166,
281-
175,
282-
],
283-
text: User,
284-
},
285-
},
286-
],
287259
},
288260
],
289261
},
@@ -296,49 +268,21 @@ exports[`Examples > should work in vue 1`] = `
296268
filePath: examples/vue-code-file/test.vue,
297269
messages: [
298270
{
299-
column: 3,
300-
endColumn: 8,
301-
endLine: 5,
302-
line: 5,
271+
column: 0,
272+
line: 1,
303273
message: Anonymous GraphQL operations are forbidden. Make sure to name your query!,
304274
messageId: no-anonymous-operations,
305275
nodeType: null,
306276
ruleId: @graphql-eslint/no-anonymous-operations,
307277
severity: 2,
308-
suggestions: [
309-
{
310-
desc: Rename to \`user\`,
311-
fix: {
312-
range: [
313-
78,
314-
78,
315-
],
316-
text: user,
317-
},
318-
},
319-
],
320278
},
321279
{
322-
column: 9,
323-
endColumn: 18,
324-
endLine: 13,
325-
line: 13,
280+
column: 0,
281+
line: 1,
326282
message: Operation "UserQuery" should not have "Query" suffix,
327283
nodeType: Name,
328284
ruleId: @graphql-eslint/naming-convention,
329285
severity: 2,
330-
suggestions: [
331-
{
332-
desc: Rename to \`User\`,
333-
fix: {
334-
range: [
335-
166,
336-
175,
337-
],
338-
text: User,
339-
},
340-
},
341-
],
342286
},
343287
],
344288
},

0 commit comments

Comments
 (0)