Skip to content

Commit 6ce6dbb

Browse files
authored
fix broken suggestions on code files when there are multiple choices (#1349)
* fix broken suggestions on code files when there are multiple choices * update snapshots * better err0r
1 parent 1883cc4 commit 6ce6dbb

File tree

3 files changed

+20
-9
lines changed

3 files changed

+20
-9
lines changed

.changeset/seven-cobras-cheer.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+
fix broken suggestions on code files when there are multiple choices

packages/plugin/src/processor.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { Linter } from 'eslint';
2+
import { relative } from 'path';
23
import {
34
gqlPluckFromCodeStringSync,
45
GraphQLTagPluckOptions,
56
} from '@graphql-tools/graphql-tag-pluck';
67
import { asArray } from '@graphql-tools/utils';
78
import { GraphQLConfig } from 'graphql-config';
89
import { loadOnDiskGraphQLConfig } from './graphql-config.js';
9-
import { REPORT_ON_FIRST_CHARACTER } from './utils.js';
10+
import { CWD, REPORT_ON_FIRST_CHARACTER } from './utils.js';
1011

1112
export type Block = Linter.ProcessorFile & {
1213
lineOffset: number;
@@ -70,9 +71,13 @@ export const processor: Linter.Processor<Block | string> = {
7071
blocksMap.set(filePath, blocks);
7172

7273
return [...blocks, code /* source code must be provided and be last */];
73-
} catch (e) {
74+
} catch (error) {
75+
error.message = `[graphql-eslint] Error while preprocessing "${relative(
76+
CWD,
77+
filePath,
78+
)}" file\n\n${error.message}`;
7479
// eslint-disable-next-line no-console
75-
console.error(e);
80+
console.error(error);
7681
// in case of parsing error return code as is
7782
return [code];
7883
}
@@ -105,8 +110,9 @@ export const processor: Linter.Processor<Block | string> = {
105110
message.fix.range[1] += offset;
106111
}
107112
for (const suggestion of message.suggestions || []) {
108-
suggestion.fix.range[0] += offset;
109-
suggestion.fix.range[1] += offset;
113+
// DO NOT mutate until https://github.com/eslint/eslint/issues/16716
114+
const [start, end] = suggestion.fix.range;
115+
suggestion.fix.range = [start + offset, end + offset];
110116
}
111117
}
112118
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ exports[`Examples > should work in monorepo 1`] = `
5757
desc: Rename to \`firstName\`,
5858
fix: {
5959
range: [
60-
164,
61-
173,
60+
131,
61+
140,
6262
],
6363
text: firstName,
6464
},
@@ -67,8 +67,8 @@ exports[`Examples > should work in monorepo 1`] = `
6767
desc: Rename to \`lastName\`,
6868
fix: {
6969
range: [
70-
164,
71-
173,
70+
131,
71+
140,
7272
],
7373
text: lastName,
7474
},

0 commit comments

Comments
 (0)