UNPKG

@graphql-eslint/eslint-plugin

Version:
106 lines (105 loc) 3.31 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: !0 }); }, __copyProps = (to, from, except, desc) => { if (from && typeof from == "object" || typeof from == "function") for (let key of __getOwnPropNames(from)) !__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: !0 }), mod); var lone_executable_definition_exports = {}; __export(lone_executable_definition_exports, { rule: () => rule }); module.exports = __toCommonJS(lone_executable_definition_exports); var import_graphql = require("graphql"), import_utils = require("../utils.js"); const RULE_ID = "lone-executable-definition", definitionTypes = ["fragment", ...Object.values(import_graphql.OperationTypeNode)], schema = { type: "array", maxItems: 1, items: { type: "object", minProperties: 1, additionalProperties: !1, properties: { ignore: { ...import_utils.ARRAY_DEFAULT_OPTIONS, maxItems: 3, // ignore all 4 types is redundant items: { enum: definitionTypes }, description: "Allow certain definitions to be placed alongside others." } } } }, rule = { meta: { type: "suggestion", docs: { category: "Operations", description: "Require queries, mutations, subscriptions or fragments to be located in separate files.", url: `https://the-guild.dev/graphql/eslint/rules/${RULE_ID}`, examples: [ { title: "Incorrect", code: ( /* GraphQL */ ` query Foo { id } fragment Bar on Baz { id } ` ) }, { title: "Correct", code: ( /* GraphQL */ ` query Foo { id } ` ) } ] }, messages: { [RULE_ID]: "{{name}} should be in a separate file." }, schema }, create(context) { const ignore = new Set(context.options[0]?.ignore || []), definitions = []; return { ":matches(OperationDefinition, FragmentDefinition)"(node) { const type = "operation" in node ? node.operation : "fragment"; ignore.has(type) || definitions.push({ type, node }); }, "Document:exit"() { for (const { node, type } of definitions.slice(1)) { let name = (0, import_utils.pascalCase)(type); const definitionName = node.name?.value; definitionName && (name += ` "${definitionName}"`), context.report({ loc: node.name?.loc || (0, import_utils.getLocation)(node.loc.start, type), messageId: RULE_ID, data: { name } }); } } }; } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { rule });