UNPKG

@graphql-eslint/eslint-plugin

Version:
83 lines (80 loc) 2.2 kB
import { relative } from "node:path"; import { visit } from "graphql"; import { CWD, requireGraphQLOperations } from "../../utils.js"; const RULE_ID = "no-one-place-fragments", rule = { meta: { type: "suggestion", docs: { category: "Operations", description: "Disallow fragments that are used only in one place.", url: `https://the-guild.dev/graphql/eslint/rules/${RULE_ID}`, examples: [ { title: "Incorrect", code: ( /* GraphQL */ ` fragment UserFields on User { id } { user { ...UserFields } } ` ) }, { title: "Correct", code: ( /* GraphQL */ ` fragment UserFields on User { id } { user { ...UserFields friends { ...UserFields } } } ` ) } ], requiresSiblings: !0 }, messages: { [RULE_ID]: 'Fragment `{{fragmentName}}` used only once. Inline him in "{{filePath}}".' }, schema: [] }, create(context) { const operations = requireGraphQLOperations(RULE_ID, context), allDocuments = [...operations.getOperations(), ...operations.getFragments()], usedFragmentsMap = /* @__PURE__ */ Object.create(null); for (const { document, filePath } of allDocuments) { const relativeFilePath = relative(CWD, filePath); visit(document, { FragmentSpread({ name }) { const spreadName = name.value; usedFragmentsMap[spreadName] ||= [], usedFragmentsMap[spreadName].push(relativeFilePath); } }); } return { "FragmentDefinition > Name"(node) { const fragmentName = node.value, fragmentUsage = usedFragmentsMap[fragmentName]; fragmentUsage.length === 1 && context.report({ node, messageId: RULE_ID, data: { fragmentName, filePath: fragmentUsage[0] } }); } }; } }; export { rule };