UNPKG

@graphql-eslint/eslint-plugin

Version:
107 lines (104 loc) 3.56 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 unique_fragment_name_exports = {}; __export(unique_fragment_name_exports, { checkNode: () => checkNode, rule: () => rule }); module.exports = __toCommonJS(unique_fragment_name_exports); var import_node_path = require("node:path"), import_graphql = require("graphql"), import_utils = require("../utils.js"); const RULE_ID = "unique-fragment-name", checkNode = (context, node, ruleId) => { const documentName = node.name.value, siblings = (0, import_utils.requireSiblingsOperations)(ruleId, context), siblingDocuments = node.kind === import_graphql.Kind.FRAGMENT_DEFINITION ? siblings.getFragment(documentName) : siblings.getOperation(documentName), filepath = context.filename, conflictingDocuments = siblingDocuments.filter((f) => { const isSameName = f.document.name?.value === documentName, isSamePath = (0, import_utils.normalizePath)(f.filePath) === (0, import_utils.normalizePath)(filepath); return isSameName && !isSamePath; }); conflictingDocuments.length > 0 && context.report({ messageId: ruleId, data: { documentName, summary: conflictingDocuments.map((f) => ` ${(0, import_node_path.relative)(import_utils.CWD, f.filePath.replace(import_utils.VIRTUAL_DOCUMENT_REGEX, ""))}`).join(` `) }, // @ts-expect-error name will exist node: node.name }); }, rule = { meta: { type: "suggestion", docs: { category: "Operations", description: "Enforce unique fragment names across your project.", url: `https://the-guild.dev/graphql/eslint/rules/${RULE_ID}`, requiresSiblings: !0, recommended: !0, examples: [ { title: "Incorrect", code: ( /* GraphQL */ ` # user.fragment.graphql fragment UserFields on User { id name fullName } # user-fields.graphql fragment UserFields on User { id } ` ) }, { title: "Correct", code: ( /* GraphQL */ ` # user.fragment.graphql fragment AllUserFields on User { id name fullName } # user-fields.graphql fragment UserFields on User { id } ` ) } ] }, messages: { [RULE_ID]: `Fragment named "{{ documentName }}" already defined in: {{ summary }}` }, schema: [] }, create(context) { return { FragmentDefinition(node) { checkNode(context, node, RULE_ID); } }; } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { checkNode, rule });