UNPKG

@graphql-eslint/eslint-plugin

Version:
120 lines (119 loc) 3.5 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 lone_executable_definition_exports = {}; __export(lone_executable_definition_exports, { rule: () => rule }); module.exports = __toCommonJS(lone_executable_definition_exports); var import_utils = require("../utils.js"); const RULE_ID = "lone-executable-definition"; const definitionTypes = ["fragment", "query", "mutation", "subscription"]; const schema = { type: "array", maxItems: 1, items: { type: "object", minProperties: 1, additionalProperties: false, 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." } } } }; const 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) { var _a; const ignore = new Set(((_a = context.options[0]) == null ? void 0 : _a.ignore) || []); const definitions = []; return { ":matches(OperationDefinition, FragmentDefinition)"(node) { const type = "operation" in node ? node.operation : "fragment"; if (!ignore.has(type)) { definitions.push({ type, node }); } }, "Document:exit"() { var _a2, _b; for (const { node, type } of definitions.slice(1)) { let name = (0, import_utils.pascalCase)(type); const definitionName = (_a2 = node.name) == null ? void 0 : _a2.value; if (definitionName) { name += ` "${definitionName}"`; } context.report({ loc: ((_b = node.name) == null ? void 0 : _b.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 });