UNPKG

@graphql-eslint/eslint-plugin

Version:
206 lines (204 loc) • 6.2 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 require_description_exports = {}; __export(require_description_exports, { RULE_ID: () => RULE_ID, rule: () => rule }); module.exports = __toCommonJS(require_description_exports); var import_utils = require("@graphql-tools/utils"); var import_graphql = require("graphql"); var import_utils2 = require("../utils.js"); const RULE_ID = "require-description"; const ALLOWED_KINDS = [ ...import_utils2.TYPES_KINDS, import_graphql.Kind.DIRECTIVE_DEFINITION, import_graphql.Kind.FIELD_DEFINITION, import_graphql.Kind.INPUT_VALUE_DEFINITION, import_graphql.Kind.ENUM_VALUE_DEFINITION, import_graphql.Kind.OPERATION_DEFINITION ]; const schema = { type: "array", minItems: 1, maxItems: 1, items: { type: "object", additionalProperties: false, minProperties: 1, properties: { types: { type: "boolean", description: `Includes: ${import_utils2.TYPES_KINDS.map((kind) => `- \`${kind}\``).join("\n")}` }, rootField: { type: "boolean", description: "Definitions within `Query`, `Mutation`, and `Subscription` root types." }, ...Object.fromEntries( [...ALLOWED_KINDS].sort().map((kind) => { let description = `Read more about this kind on [spec.graphql.org](https://spec.graphql.org/October2021/#${kind}).`; if (kind === import_graphql.Kind.OPERATION_DEFINITION) { description += '\n> You must use only comment syntax `#` and not description syntax `"""` or `"`.'; } return [kind, { type: "boolean", description }]; }) ) } } }; const rule = { meta: { docs: { category: "Schema", description: "Enforce descriptions in type definitions and operations.", url: `https://the-guild.dev/graphql/eslint/rules/${RULE_ID}`, examples: [ { title: "Incorrect", usage: [{ types: true, FieldDefinition: true }], code: ( /* GraphQL */ ` type someTypeName { name: String } ` ) }, { title: "Correct", usage: [{ types: true, FieldDefinition: true }], code: ( /* GraphQL */ ` """ Some type description """ type someTypeName { """ Name description """ name: String } ` ) }, { title: "Correct", usage: [{ OperationDefinition: true }], code: ( /* GraphQL */ ` # Create a new user mutation createUser { # ... } ` ) }, { title: "Correct", usage: [{ rootField: true }], code: ( /* GraphQL */ ` type Mutation { "Create a new user" createUser: User } type User { name: String } ` ) } ], configOptions: [ { types: true, [import_graphql.Kind.DIRECTIVE_DEFINITION]: true // rootField: true TODO enable in graphql-eslint v4 } ], recommended: true }, type: "suggestion", messages: { [RULE_ID]: "Description is required for {{ nodeName }}" }, schema }, create(context) { const { types, rootField, ...restOptions } = context.options[0] || {}; const kinds = new Set(types ? import_utils2.TYPES_KINDS : []); for (const [kind, isEnabled] of Object.entries(restOptions)) { if (isEnabled) { kinds.add(kind); } else { kinds.delete(kind); } } if (rootField) { const schema2 = (0, import_utils2.requireGraphQLSchemaFromContext)(RULE_ID, context); const rootTypeNames = (0, import_utils.getRootTypeNames)(schema2); kinds.add( `:matches(ObjectTypeDefinition, ObjectTypeExtension)[name.value=/^(${[ ...rootTypeNames ].join(",")})$/] > FieldDefinition` ); } const selector = [...kinds].join(","); return { [selector](node) { var _a; let description = ""; const isOperation = node.kind === import_graphql.Kind.OPERATION_DEFINITION; if (isOperation) { const rawNode = node.rawNode(); const { prev, line } = rawNode.loc.startToken; if ((prev == null ? void 0 : prev.kind) === import_graphql.TokenKind.COMMENT) { const value = prev.value.trim(); const linesBefore = line - prev.line; if (!value.startsWith("eslint") && linesBefore === 1) { description = value; } } } else { description = ((_a = node.description) == null ? void 0 : _a.value.trim()) || ""; } if (description.length === 0) { context.report({ loc: isOperation ? (0, import_utils2.getLocation)(node.loc.start, node.operation) : node.name.loc, messageId: RULE_ID, data: { nodeName: (0, import_utils2.getNodeName)(node) } }); } } }; } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { RULE_ID, rule });