UNPKG

@graphql-eslint/eslint-plugin

Version:
141 lines (139 loc) 4.88 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_hashtag_description_exports = {}; __export(no_hashtag_description_exports, { RULE_ID: () => RULE_ID, rule: () => rule }); module.exports = __toCommonJS(no_hashtag_description_exports); var import_graphql = require("graphql"); var import_utils = require("../utils.js"); const RULE_ID = "HASHTAG_COMMENT"; const rule = { meta: { type: "suggestion", hasSuggestions: true, schema: [], messages: { [RULE_ID]: 'Unexpected GraphQL descriptions as hashtag `#` for {{ nodeName }}.\nPrefer using `"""` for multiline, or `"` for a single line description.' }, docs: { description: 'Requires to use `"""` or `"` for adding a GraphQL description instead of `#`.\nAllows to use hashtag for comments, as long as it\'s not attached to an AST definition.', category: "Schema", url: "https://the-guild.dev/graphql/eslint/rules/no-hashtag-description", examples: [ { title: "Incorrect", code: ( /* GraphQL */ ` # Represents a user type User { id: ID! name: String } ` ) }, { title: "Correct", code: ( /* GraphQL */ ` " Represents a user " type User { id: ID! name: String } ` ) }, { title: "Correct", code: ( /* GraphQL */ ` # This file defines the basic User type. # This comment is valid because it's not attached specifically to an AST object. " Represents a user " type User { id: ID! # This one is also valid, since it comes after the AST object name: String } ` ) } ], recommended: true } }, create(context) { const selector = "Document[definitions.0.kind!=/^(OperationDefinition|FragmentDefinition)$/]"; return { [selector](node) { const rawNode = node.rawNode(); let token = rawNode.loc.startToken; while (token) { const { kind, prev, next, value, line, column } = token; if (kind === import_graphql.TokenKind.COMMENT && prev && next) { const isEslintComment = value.trimStart().startsWith("eslint"); const linesAfter = next.line - line; if (!isEslintComment && line !== prev.line && next.kind === import_graphql.TokenKind.NAME && linesAfter < 2) { const sourceCode = context.getSourceCode(); const { tokens } = sourceCode.ast; const t = tokens.find( (token2) => token2.loc.start.line === next.line && token2.loc.start.column === next.column - 1 ); const nextNode = sourceCode.getNodeByRangeIndex(t.range[1] + 1); context.report({ messageId: RULE_ID, data: { nodeName: (0, import_utils.getNodeName)( "name" in nextNode ? nextNode : nextNode.parent ) }, loc: { line, column: column - 1 }, suggest: ['"""', '"'].map((descriptionSyntax) => ({ desc: `Replace with \`${descriptionSyntax}\` description syntax`, fix: (fixer) => fixer.replaceTextRange( [token.start, token.end], // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion -- TODO: remove `!` when drop support of graphql@15 [descriptionSyntax, value.trim(), descriptionSyntax].join("") ) })) }); } } if (!next) { break; } token = next; } } }; } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { RULE_ID, rule });