UNPKG

@graphql-eslint/eslint-plugin

Version:
96 lines (94 loc) 4.08 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: !0 }); }, __copyProps = (to, from, except, desc) => { if (from && typeof from == "object" || typeof from == "function") for (let key of __getOwnPropNames(from)) !__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: !0 }), mod); var processor_exports = {}; __export(processor_exports, { processor: () => processor }); module.exports = __toCommonJS(processor_exports); var import_node_path = require("node:path"), import_graphql_tag_pluck = require("@graphql-tools/graphql-tag-pluck"), import_utils = require("@graphql-tools/utils"), import_graphql_config2 = require("./graphql-config.js"), import_utils2 = require("./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 = (0, import_graphql_config2.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), ...(0, import_utils.asArray)(globalGqlIdentifierName), gqlMagicComment ].filter(import_utils2.truthy) ) ]; } if (keywords.every((keyword) => !code.includes(keyword))) return [code]; try { const blocks = (0, import_graphql_tag_pluck.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 "${(0, import_node_path.relative)( import_utils2.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, import_utils2.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); } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { processor });