UNPKG

@graphql-eslint/eslint-plugin

Version:
158 lines (153 loc) 4.67 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 no_unused_fields_exports = {}; __export(no_unused_fields_exports, { rule: () => rule }); module.exports = __toCommonJS(no_unused_fields_exports); var import_graphql = require("graphql"); var import_utils = require("../utils.js"); const RULE_ID = "no-unused-fields"; let usedFieldsCache; function getUsedFields(schema, operations) { if (usedFieldsCache) { return usedFieldsCache; } const usedFields = /* @__PURE__ */ Object.create(null); const typeInfo = new import_graphql.TypeInfo(schema); const visitor = (0, import_graphql.visitWithTypeInfo)(typeInfo, { Field(node) { var _a; const fieldDef = typeInfo.getFieldDef(); if (!fieldDef) { return false; } const parentTypeName = typeInfo.getParentType().name; const fieldName = node.name.value; (_a = usedFields[parentTypeName]) != null ? _a : usedFields[parentTypeName] = /* @__PURE__ */ new Set(); usedFields[parentTypeName].add(fieldName); } }); const allDocuments = [...operations.getOperations(), ...operations.getFragments()]; for (const { document } of allDocuments) { (0, import_graphql.visit)(document, visitor); } usedFieldsCache = usedFields; return usedFieldsCache; } const rule = { meta: { messages: { [RULE_ID]: 'Field "{{fieldName}}" is unused' }, docs: { description: "Requires all fields to be used at some level by siblings operations.", category: "Schema", url: `https://the-guild.dev/graphql/eslint/rules/${RULE_ID}`, requiresSiblings: true, requiresSchema: true, isDisabledForAllConfig: true, examples: [ { title: "Incorrect", code: ( /* GraphQL */ ` type User { id: ID! name: String someUnusedField: String } type Query { me: User } query { me { id name } } ` ) }, { title: "Correct", code: ( /* GraphQL */ ` type User { id: ID! name: String } type Query { me: User } query { me { id name } } ` ) } ] }, type: "suggestion", schema: [], hasSuggestions: true }, create(context) { const schema = (0, import_utils.requireGraphQLSchemaFromContext)(RULE_ID, context); const siblingsOperations = (0, import_utils.requireSiblingsOperations)(RULE_ID, context); const usedFields = getUsedFields(schema, siblingsOperations); return { FieldDefinition(node) { var _a; const fieldName = node.name.value; const parentTypeName = node.parent.name.value; const isUsed = (_a = usedFields[parentTypeName]) == null ? void 0 : _a.has(fieldName); if (isUsed) { return; } context.report({ node: node.name, messageId: RULE_ID, data: { fieldName }, suggest: [ { desc: `Remove \`${fieldName}\` field`, fix(fixer) { const sourceCode = context.getSourceCode(); const tokenBefore = sourceCode.getTokenBefore(node); const tokenAfter = sourceCode.getTokenAfter(node); const isEmptyType = tokenBefore.type === "{" && tokenAfter.type === "}"; return fixer.remove(isEmptyType ? node.parent : node); } } ] }); } }; } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { rule });