UNPKG

@graphql-eslint/eslint-plugin

Version:
160 lines (159 loc) 5.69 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf, __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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: !0 }) : target, mod )), __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: !0 }), mod); var selection_set_depth_exports = {}; __export(selection_set_depth_exports, { rule: () => rule }); module.exports = __toCommonJS(selection_set_depth_exports); var import_graphql = require("graphql"), import_graphql_depth_limit = __toESM(require("graphql-depth-limit")), import_utils = require("../utils.js"); const RULE_ID = "selection-set-depth", schema = { type: "array", minItems: 1, maxItems: 1, items: { type: "object", additionalProperties: !1, required: ["maxDepth"], properties: { maxDepth: { type: "number" }, ignore: import_utils.ARRAY_DEFAULT_OPTIONS } } }, rule = { meta: { type: "suggestion", hasSuggestions: !0, docs: { category: "Operations", description: "Limit the complexity of the GraphQL operations solely by their depth. Based on [graphql-depth-limit](https://npmjs.com/package/graphql-depth-limit).", url: `https://the-guild.dev/graphql/eslint/rules/${RULE_ID}`, requiresSiblings: !0, examples: [ { title: "Incorrect", usage: [{ maxDepth: 1 }], code: ` query deep2 { viewer { # Level 0 albums { # Level 1 title # Level 2 } } } ` }, { title: "Correct", usage: [{ maxDepth: 4 }], code: ` query deep2 { viewer { # Level 0 albums { # Level 1 title # Level 2 } } } ` }, { title: "Correct (ignored field)", usage: [{ maxDepth: 1, ignore: ["albums"] }], code: ` query deep2 { viewer { # Level 0 albums { # Level 1 title # Level 2 } } } ` } ], recommended: !0, configOptions: [{ maxDepth: 7 }] }, schema }, create(context) { let siblings = null; try { siblings = (0, import_utils.requireSiblingsOperations)(RULE_ID, context); } catch { import_utils.logger.warn( `Rule "${RULE_ID}" works best with siblings operations loaded. For more info: https://bit.ly/graphql-eslint-operations` ); } const { maxDepth, ignore = [] } = context.options[0], checkFn = (0, import_graphql_depth_limit.default)(maxDepth, { ignore }); return { "OperationDefinition, FragmentDefinition"(node) { try { const rawNode = node.rawNode(), fragmentsInUse = siblings ? siblings.getFragmentsInUse(rawNode) : [], document = { kind: import_graphql.Kind.DOCUMENT, definitions: [rawNode, ...fragmentsInUse] }; checkFn({ getDocument: () => document, reportError(error) { const { line, column } = error.locations[0], token = context.getAncestors()[0].tokens.find( (token2) => token2.loc.start.line === line && token2.loc.start.column === column - 1 ); context.report({ loc: { line, column: column - 1 }, message: error.message, // Don't provide suggestions for fragment that can be in a separate file ...token && { suggest: [ { desc: "Remove selections", fix(fixer) { const foundNode = context.getSourceCode().getNodeByRangeIndex(token.range[0]), parentNode = foundNode.parent.parent; return fixer.remove( foundNode.kind === "Name" ? parentNode.parent : parentNode ); } } ] } }); } }); } catch (e) { import_utils.logger.warn( `Rule "${RULE_ID}" check failed due to a missing siblings operations. For more info: https://bit.ly/graphql-eslint-operations`, e ); } } }; } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { rule });