Skip to content

Commit 1257d51

Browse files
authored
fix: original file should not be considered as file block (#394)
* fix: original file should not be considered as file block ESLint supports text directly * docs: add changeset for the fix change
1 parent 8844593 commit 1257d51

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

.changeset/empty-donuts-destroy.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
"@graphql-eslint/eslint-plugin": patch
3+
---
4+
5+
fix: original file should not be considered as file block
6+
7+
Related #88
8+
9+
ESLint supports `text` directly, no need to use the hacky way. See https://github.com/eslint/eslint/blob/master/lib/linter/linter.js#L1298
10+
11+
Related `eslint-plugin-prettier`'s issue hae been fixed at https://github.com/prettier/eslint-plugin-prettier/pull/401

packages/plugin/src/processors/code-files.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,24 @@
11
import { parseCode } from '@graphql-tools/graphql-tag-pluck';
2-
import { basename } from 'path';
32

43
const RELEVANT_KEYWORDS = ['gql', 'graphql', '/* GraphQL */'];
54

65
type Block = {
76
text: string;
87
filename: string;
9-
lineOffset?: number;
10-
offset?: number;
8+
lineOffset: number;
9+
offset: number;
1110
};
1211

1312
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
1413
export function createGraphqlProcessor() {
15-
const blocksMap = new Map<string, Block[]>();
14+
const blocksMap = new Map<string, Array<Block | string>>();
1615

1716
return {
1817
supportsAutofix: true,
19-
preprocess: (text: string, filename: string): Array<{ text: string; filename: string }> => {
20-
const blocks: Block[] = [];
18+
preprocess: (text: string, filename: string): Array<{ text: string; filename: string } | string> => {
19+
const blocks: Array<Block | string> = [];
2120
blocksMap.set(filename, blocks);
2221

23-
// WORKAROUND: This restores the original filename for the block representing original code.
24-
// This allows to be compatible with other eslint plugins relying on filesystem
25-
// This is temporary, waiting for https://github.com/eslint/eslint/issues/11989
26-
const originalFileBlock = { text, filename: `/../../${basename(filename)}` };
27-
2822
if (filename && text && RELEVANT_KEYWORDS.some(keyword => text.includes(keyword))) {
2923
try {
3024
const extractedDocuments = parseCode({
@@ -46,7 +40,7 @@ export function createGraphqlProcessor() {
4640
});
4741
}
4842

49-
blocks.push(originalFileBlock);
43+
blocks.push(text);
5044

5145
return blocks;
5246
}
@@ -56,15 +50,21 @@ export function createGraphqlProcessor() {
5650
}
5751
}
5852

59-
return [originalFileBlock];
53+
return [text];
6054
},
6155
postprocess: (messageLists: any[], filename: string): any[] => {
6256
const blocks = blocksMap.get(filename);
6357

6458
if (blocks && blocks.length > 0) {
6559
for (let i = 0; i < messageLists.length; ++i) {
6660
const messages = messageLists[i];
67-
const { lineOffset, offset } = blocks[i];
61+
const block = blocks[i];
62+
63+
if (typeof block === 'string') {
64+
continue;
65+
}
66+
67+
const { lineOffset, offset } = block;
6868

6969
for (const message of messages) {
7070
message.line += lineOffset;

0 commit comments

Comments
 (0)