Skip to content

Commit

Permalink
fix broken suggestions on code files when there are multiple choices (#…
Browse files Browse the repository at this point in the history
…1349)

* fix broken suggestions on code files when there are multiple choices

* update snapshots

* better err0r
  • Loading branch information
dimaMachina committed Dec 28, 2022
1 parent 1883cc4 commit 6ce6dbb
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/seven-cobras-cheer.md
@@ -0,0 +1,5 @@
---
'@graphql-eslint/eslint-plugin': patch
---

fix broken suggestions on code files when there are multiple choices
16 changes: 11 additions & 5 deletions packages/plugin/src/processor.ts
@@ -1,12 +1,13 @@
import { Linter } from 'eslint';
import { relative } from 'path';
import {
gqlPluckFromCodeStringSync,
GraphQLTagPluckOptions,
} from '@graphql-tools/graphql-tag-pluck';
import { asArray } from '@graphql-tools/utils';
import { GraphQLConfig } from 'graphql-config';
import { loadOnDiskGraphQLConfig } from './graphql-config.js';
import { REPORT_ON_FIRST_CHARACTER } from './utils.js';
import { CWD, REPORT_ON_FIRST_CHARACTER } from './utils.js';

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

return [...blocks, code /* source code must be provided and be last */];
} catch (e) {
} catch (error) {
error.message = `[graphql-eslint] Error while preprocessing "${relative(
CWD,
filePath,
)}" file\n\n${error.message}`;
// eslint-disable-next-line no-console
console.error(e);
console.error(error);
// in case of parsing error return code as is
return [code];
}
Expand Down Expand Up @@ -105,8 +110,9 @@ export const processor: Linter.Processor<Block | string> = {
message.fix.range[1] += offset;
}
for (const suggestion of message.suggestions || []) {
suggestion.fix.range[0] += offset;
suggestion.fix.range[1] += offset;
// DO NOT mutate until https://github.com/eslint/eslint/issues/16716
const [start, end] = suggestion.fix.range;
suggestion.fix.range = [start + offset, end + offset];
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions packages/plugin/tests/__snapshots__/examples.spec.md
Expand Up @@ -57,8 +57,8 @@ exports[`Examples > should work in monorepo 1`] = `
desc: Rename to \`firstName\`,
fix: {
range: [
164,
173,
131,
140,
],
text: firstName,
},
Expand All @@ -67,8 +67,8 @@ exports[`Examples > should work in monorepo 1`] = `
desc: Rename to \`lastName\`,
fix: {
range: [
164,
173,
131,
140,
],
text: lastName,
},
Expand Down

0 comments on commit 6ce6dbb

Please sign in to comment.