UNPKG

@graphql-eslint/eslint-plugin

Version:
82 lines (80 loc) 3.02 kB
import "./chunk-U3TKCM4X.js"; import { relative } from "node:path"; import { gqlPluckFromCodeStringSync } from "@graphql-tools/graphql-tag-pluck"; import { asArray } from "@graphql-tools/utils"; import { loadOnDiskGraphQLConfig } from "./graphql-config.js"; import { CWD, REPORT_ON_FIRST_CHARACTER, truthy } from "./utils.js"; const blocksMap = /* @__PURE__ */ new Map(); let onDiskConfig, onDiskConfigLoaded = !1; const RELEVANT_KEYWORDS = ["gql", "graphql", "GraphQL"], processor = { supportsAutofix: !0, preprocess(code, filePath) { onDiskConfigLoaded || (onDiskConfig = loadOnDiskGraphQLConfig(filePath), onDiskConfigLoaded = !0); let keywords = RELEVANT_KEYWORDS; const pluckConfig = onDiskConfig?.getProjectForFile(filePath).extensions.pluckConfig; if (pluckConfig) { const { modules = [], globalGqlIdentifierName = ["gql", "graphql"], gqlMagicComment = "GraphQL" } = pluckConfig; keywords = [ ...new Set( [ ...modules.map(({ identifier }) => identifier), ...asArray(globalGqlIdentifierName), gqlMagicComment ].filter(truthy) ) ]; } if (keywords.every((keyword) => !code.includes(keyword))) return [code]; try { const blocks = gqlPluckFromCodeStringSync(filePath, code, { skipIndent: !0, ...pluckConfig }).map((item) => ({ filename: "document.graphql", text: item.body, lineOffset: item.locationOffset.line - 1, // @ts-expect-error -- `index` field exist but show ts error offset: item.locationOffset.index + 1 })); return blocksMap.set(filePath, blocks), [ ...blocks, code /* source code must be provided and be last */ ]; } catch (error) { return error instanceof Error && (error.message = `[graphql-eslint] Error while preprocessing "${relative( CWD, filePath )}" file ${error.message}`), console.error(error), [code]; } }, postprocess(messages, filePath) { const blocks = blocksMap.get(filePath) || []; for (let i = 0; i < blocks.length; i += 1) { const { lineOffset, offset } = blocks[i]; for (const message of messages[i] || []) { if (/\.(vue|svelte)$/.test(filePath)) { delete message.endLine, delete message.endColumn, delete message.fix, delete message.suggestions, Object.assign(message, REPORT_ON_FIRST_CHARACTER); continue; } message.line += lineOffset, typeof message.endLine == "number" && (message.endLine += lineOffset), message.fix && (message.fix.range[0] += offset, message.fix.range[1] += offset); for (const suggestion of message.suggestions || []) { const [start, end] = suggestion.fix.range; suggestion.fix.range = [start + offset, end + offset]; } } } return messages.flat().sort((a, b) => a.line - b.line || a.column - b.column); } }; export { processor };