UNPKG

eslint-plugin-react-naming-convention

Version:

ESLint React's ESLint plugin for naming convention related rules.

177 lines (167 loc) • 5.29 kB
import { ESLintUtils } from "@typescript-eslint/utils"; import * as core from "@eslint-react/core"; import { merge } from "@eslint-react/eslint"; import { resolveEnclosingAssignmentTarget } from "@eslint-react/var"; import { AST_NODE_TYPES } from "@typescript-eslint/types"; import { P, match } from "ts-pattern"; import { Extract } from "@eslint-react/ast"; //#region \0rolldown/runtime.js var __defProp = Object.defineProperty; var __exportAll = (all, no_symbols) => { let target = {}; for (var name in all) { __defProp(target, name, { get: all[name], enumerable: true }); } if (!no_symbols) { __defProp(target, Symbol.toStringTag, { value: "Module" }); } return target; }; //#endregion //#region package.json var name$1 = "eslint-plugin-react-naming-convention"; var version = "5.8.12"; //#endregion //#region src/utils/create-rule.ts function getDocsUrl(ruleName) { return `https://eslint-react.xyz/docs/rules/naming-convention-${ruleName}`; } const createRule = ESLintUtils.RuleCreator(getDocsUrl); //#endregion //#region src/rules/context-name/context-name.ts const RULE_NAME$2 = "context-name"; var context_name_default = createRule({ meta: { type: "suggestion", docs: { description: "Enforces identifier names assigned from `createContext` calls to be a valid component name with the suffix `Context`." }, messages: { invalidContextName: "A context name must be a valid component name with the suffix 'Context'." }, schema: [] }, name: RULE_NAME$2, create: create$2, defaultOptions: [] }); function create$2(context) { if (!context.sourceCode.text.includes("createContext")) return {}; return merge({ CallExpression(node) { if (!core.isCreateContextCall(context, node)) return; const [id, name] = match(resolveEnclosingAssignmentTarget(node)).with({ type: AST_NODE_TYPES.Identifier, name: P.string }, (id) => [id, id.name]).with({ type: AST_NODE_TYPES.MemberExpression, property: { name: P.string } }, (id) => [id, id.property.name]).otherwise(() => [null, null]); if (id == null) return; if (core.isFunctionComponentName(name) && name.endsWith("Context")) return; context.report({ messageId: "invalidContextName", node: id }); } }); } //#endregion //#region src/rules/id-name/id-name.ts const RULE_NAME$1 = "id-name"; var id_name_default = createRule({ meta: { type: "suggestion", docs: { description: "Enforces identifier names assigned from 'useId' calls to be either 'id' or end with 'Id'." }, messages: { invalidIdName: "An identifier assigned from 'useId' must be named 'id' or end with 'Id'." }, schema: [] }, name: RULE_NAME$1, create: create$1, defaultOptions: [] }); function create$1(context) { if (!context.sourceCode.text.includes("useId")) return {}; return merge({ CallExpression(node) { if (!core.isUseIdCall(context, node)) return; const [id, name] = match(resolveEnclosingAssignmentTarget(node)).with({ type: AST_NODE_TYPES.Identifier, name: P.string }, (id) => [id, id.name]).with({ type: AST_NODE_TYPES.MemberExpression, property: { name: P.string } }, (id) => [id, id.property.name]).otherwise(() => [null, null]); if (id == null) return; if (name.endsWith("Id") || name === "id") return; context.report({ messageId: "invalidIdName", node: id }); } }); } //#endregion //#region src/rules/ref-name/ref-name.ts const RULE_NAME = "ref-name"; var ref_name_default = createRule({ meta: { type: "suggestion", docs: { description: "Enforces identifier names assigned from 'useRef' calls to be either 'ref' or end with 'Ref'." }, messages: { invalidRefName: "A ref identifier must be named 'ref' or ending in 'Ref'." }, schema: [] }, name: RULE_NAME, create, defaultOptions: [] }); function create(context) { if (!context.sourceCode.text.includes("useRef")) return {}; return merge({ CallExpression(node) { if (!core.isUseRefCall(context, node)) return; if (Extract.unwrap(node.parent).type === AST_NODE_TYPES.MemberExpression) return; const [id, name] = match(resolveEnclosingAssignmentTarget(node)).with({ type: AST_NODE_TYPES.Identifier, name: P.string }, (id) => [id, id.name]).with({ type: AST_NODE_TYPES.MemberExpression, property: { name: P.string } }, (id) => [id, id.property.name]).otherwise(() => [null, null]); if (id == null) return; if (name.endsWith("Ref") || name === "ref") return; context.report({ messageId: "invalidRefName", node: id }); } }); } //#endregion //#region src/plugin.ts const plugin = { meta: { name: name$1, version }, rules: { ["context-name"]: context_name_default, ["id-name"]: id_name_default, ["ref-name"]: ref_name_default } }; //#endregion //#region src/configs/recommended.ts var recommended_exports = /* @__PURE__ */ __exportAll({ name: () => name, plugins: () => plugins, rules: () => rules }); const name = "react-naming-convention/recommended"; const rules = { "react-naming-convention/context-name": "warn", "react-naming-convention/id-name": "warn", "react-naming-convention/ref-name": "warn" }; const plugins = { "react-naming-convention": plugin }; //#endregion //#region src/index.ts const finalPlugin = { ...plugin, configs: { ["recommended"]: recommended_exports } }; //#endregion export { finalPlugin as default };