UNPKG

@graphql-eslint/eslint-plugin

Version:
119 lines (116 loc) 3.68 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: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__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: true }), mod); var unique_fragment_name_exports = {}; __export(unique_fragment_name_exports, { checkNode: () => checkNode, rule: () => rule }); module.exports = __toCommonJS(unique_fragment_name_exports); var import_path = require("path"); var import_graphql = require("graphql"); var import_utils = require("../utils.js"); const RULE_ID = "unique-fragment-name"; const checkNode = (context, node, ruleId) => { const documentName = node.name.value; const siblings = (0, import_utils.requireSiblingsOperations)(ruleId, context); const siblingDocuments = node.kind === import_graphql.Kind.FRAGMENT_DEFINITION ? siblings.getFragment(documentName) : siblings.getOperation(documentName); const filepath = context.getFilename(); const conflictingDocuments = siblingDocuments.filter((f) => { var _a; const isSameName = ((_a = f.document.name) == null ? void 0 : _a.value) === documentName; const isSamePath = (0, import_utils.normalizePath)(f.filePath) === (0, import_utils.normalizePath)(filepath); return isSameName && !isSamePath; }); if (conflictingDocuments.length > 0) { context.report({ messageId: ruleId, data: { documentName, summary: conflictingDocuments.map((f) => ` ${(0, import_path.relative)(import_utils.CWD, f.filePath.replace(import_utils.VIRTUAL_DOCUMENT_REGEX, ""))}`).join("\n") }, // @ts-expect-error name will exist node: node.name }); } }; const 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: true, 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:\n{{ 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 });