UNPKG

@graphql-eslint/eslint-plugin

Version:
99 lines (98 loc) 3 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_typename_prefix_exports = {}; __export(no_typename_prefix_exports, { rule: () => rule }); module.exports = __toCommonJS(no_typename_prefix_exports); const NO_TYPENAME_PREFIX = "NO_TYPENAME_PREFIX"; const rule = { meta: { type: "suggestion", hasSuggestions: true, docs: { category: "Schema", description: "Enforces users to avoid using the type name in a field name while defining your schema.", recommended: true, url: "https://the-guild.dev/graphql/eslint/rules/no-typename-prefix", examples: [ { title: "Incorrect", code: ( /* GraphQL */ ` type User { userId: ID! } ` ) }, { title: "Correct", code: ( /* GraphQL */ ` type User { id: ID! } ` ) } ] }, messages: { [NO_TYPENAME_PREFIX]: 'Field "{{ fieldName }}" starts with the name of the parent type "{{ typeName }}"' }, schema: [] }, create(context) { return { "ObjectTypeDefinition, ObjectTypeExtension, InterfaceTypeDefinition, InterfaceTypeExtension"(node) { const typeName = node.name.value; const lowerTypeName = typeName.toLowerCase(); for (const field of node.fields || []) { const fieldName = field.name.value; if (fieldName.toLowerCase().startsWith(lowerTypeName)) { context.report({ data: { fieldName, typeName }, messageId: NO_TYPENAME_PREFIX, node: field.name, suggest: [ { desc: `Remove \`${fieldName.slice(0, typeName.length)}\` prefix`, fix: (fixer) => fixer.replaceText( field.name, fieldName.replace(new RegExp(`^${typeName}`, "i"), "") ) } ] }); } } } }; } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { rule });