Skip to content

Commit

Permalink
fix: original file should not be considered as file block (#394)
Browse files Browse the repository at this point in the history
* fix: original file should not be considered as file block

ESLint supports text directly

* docs: add changeset for the fix change
  • Loading branch information
JounQin committed Apr 22, 2021
1 parent 8844593 commit 1257d51
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
11 changes: 11 additions & 0 deletions .changeset/empty-donuts-destroy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
"@graphql-eslint/eslint-plugin": patch
---

fix: original file should not be considered as file block

Related #88

ESLint supports `text` directly, no need to use the hacky way. See https://github.com/eslint/eslint/blob/master/lib/linter/linter.js#L1298

Related `eslint-plugin-prettier`'s issue hae been fixed at https://github.com/prettier/eslint-plugin-prettier/pull/401
28 changes: 14 additions & 14 deletions packages/plugin/src/processors/code-files.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,24 @@
import { parseCode } from '@graphql-tools/graphql-tag-pluck';
import { basename } from 'path';

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

type Block = {
text: string;
filename: string;
lineOffset?: number;
offset?: number;
lineOffset: number;
offset: number;
};

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

return {
supportsAutofix: true,
preprocess: (text: string, filename: string): Array<{ text: string; filename: string }> => {
const blocks: Block[] = [];
preprocess: (text: string, filename: string): Array<{ text: string; filename: string } | string> => {
const blocks: Array<Block | string> = [];
blocksMap.set(filename, blocks);

// WORKAROUND: This restores the original filename for the block representing original code.
// This allows to be compatible with other eslint plugins relying on filesystem
// This is temporary, waiting for https://github.com/eslint/eslint/issues/11989
const originalFileBlock = { text, filename: `/../../${basename(filename)}` };

if (filename && text && RELEVANT_KEYWORDS.some(keyword => text.includes(keyword))) {
try {
const extractedDocuments = parseCode({
Expand All @@ -46,7 +40,7 @@ export function createGraphqlProcessor() {
});
}

blocks.push(originalFileBlock);
blocks.push(text);

return blocks;
}
Expand All @@ -56,15 +50,21 @@ export function createGraphqlProcessor() {
}
}

return [originalFileBlock];
return [text];
},
postprocess: (messageLists: any[], filename: string): any[] => {
const blocks = blocksMap.get(filename);

if (blocks && blocks.length > 0) {
for (let i = 0; i < messageLists.length; ++i) {
const messages = messageLists[i];
const { lineOffset, offset } = blocks[i];
const block = blocks[i];

if (typeof block === 'string') {
continue;
}

const { lineOffset, offset } = block;

for (const message of messages) {
message.line += lineOffset;
Expand Down

0 comments on commit 1257d51

Please sign in to comment.