UNPKG

@graphql-eslint/eslint-plugin

Version:
216 lines (214 loc) • 5.82 kB
import { Kind, TokenKind } from "graphql"; import { getRootTypeNames } from "@graphql-tools/utils"; import { ARRAY_DEFAULT_OPTIONS, eslintSelectorsTip, getLocation, getNodeName, requireGraphQLSchema, TYPES_KINDS } from "../../utils.js"; const RULE_ID = "require-description", ALLOWED_KINDS = [ ...TYPES_KINDS, Kind.DIRECTIVE_DEFINITION, Kind.FIELD_DEFINITION, Kind.INPUT_VALUE_DEFINITION, Kind.ENUM_VALUE_DEFINITION, Kind.OPERATION_DEFINITION ], schema = { type: "array", minItems: 1, maxItems: 1, items: { type: "object", additionalProperties: !1, minProperties: 1, properties: { types: { type: "boolean", enum: [!0], description: `Includes: ${TYPES_KINDS.map((kind) => `- \`${kind}\``).join(` `)}` }, rootField: { type: "boolean", enum: [!0], description: "Definitions within `Query`, `Mutation`, and `Subscription` root types." }, ignoredSelectors: { ...ARRAY_DEFAULT_OPTIONS, description: ["Ignore specific selectors", eslintSelectorsTip].join(` `) }, ...Object.fromEntries( [...ALLOWED_KINDS].sort().map((kind) => { let description = `> [!NOTE] > > Read more about this kind on [spec.graphql.org](https://spec.graphql.org/October2021/#${kind}).`; return kind === Kind.OPERATION_DEFINITION && (description += [ "", "", "> [!WARNING]", ">", '> You must use only comment syntax `#` and not description syntax `"""` or `"`.' ].join(` `)), [kind, { type: "boolean", description }]; }) ) } } }, 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: !0, FieldDefinition: !0 }], code: ( /* GraphQL */ ` type someTypeName { name: String } ` ) }, { title: "Correct", usage: [{ types: !0, FieldDefinition: !0 }], code: ( /* GraphQL */ ` """ Some type description """ type someTypeName { """ Name description """ name: String } ` ) }, { title: "Correct", usage: [{ OperationDefinition: !0 }], code: ( /* GraphQL */ ` # Create a new user mutation createUser { # ... } ` ) }, { title: "Correct", usage: [{ rootField: !0 }], code: ( /* GraphQL */ ` type Mutation { "Create a new user" createUser: User } type User { name: String } ` ) }, { title: "Correct", usage: [ { ignoredSelectors: [ "[type=ObjectTypeDefinition][name.value=PageInfo]", "[type=ObjectTypeDefinition][name.value=/(Connection|Edge)$/]" ] } ], code: ( /* GraphQL */ ` type FriendConnection { edges: [FriendEdge] pageInfo: PageInfo! } type FriendEdge { cursor: String! node: Friend! } type PageInfo { hasPreviousPage: Boolean! hasNextPage: Boolean! startCursor: String endCursor: String } ` ) } ], configOptions: [ { types: !0, [Kind.DIRECTIVE_DEFINITION]: !0, rootField: !0 } ], recommended: !0 }, type: "suggestion", messages: { [RULE_ID]: "Description is required for {{ nodeName }}" }, schema }, create(context) { const { types, rootField, ignoredSelectors = [], ...restOptions } = context.options[0] || {}, kinds = new Set(types ? TYPES_KINDS : []); for (const [kind, isEnabled] of Object.entries(restOptions)) isEnabled ? kinds.add(kind) : kinds.delete(kind); if (rootField) { const schema2 = requireGraphQLSchema(RULE_ID, context), rootTypeNames = getRootTypeNames(schema2); kinds.add( `:matches(ObjectTypeDefinition, ObjectTypeExtension)[name.value=/^(${[ ...rootTypeNames ].join(",")})$/] > FieldDefinition` ); } let selector = `:matches(${[...kinds]})`; for (const str of ignoredSelectors) selector += `:not(${str})`; return { [selector](node) { let description = ""; const isOperation = node.kind === Kind.OPERATION_DEFINITION; if (isOperation) { const rawNode = node.rawNode(), { prev, line } = rawNode.loc.startToken; if (prev?.kind === TokenKind.COMMENT) { const value = prev.value.trim(), linesBefore = line - prev.line; !value.startsWith("eslint") && linesBefore === 1 && (description = value); } } else description = node.description?.value.trim() || ""; description.length === 0 && context.report({ loc: isOperation ? getLocation(node.loc.start, node.operation) : node.name.loc, messageId: RULE_ID, data: { nodeName: getNodeName(node) } }); } }; } }; export { RULE_ID, rule };