UNPKG

eslint-plugin-react-debug

Version:

ESLint React's ESLint plugin for debugging related rules.

314 lines (306 loc) • 8.78 kB
import { getDocsUrl, getSettingsFromContext, DEFAULT_ESLINT_REACT_SETTINGS } from '@eslint-react/shared'; import * as ER2 from '@eslint-react/core'; import { ESLintUtils, AST_NODE_TYPES } from '@typescript-eslint/utils'; import { JsxConfig } from '@eslint-react/kit'; import { match, P } from 'ts-pattern'; import { JsxEmit } from 'typescript'; var __defProp = Object.defineProperty; var __export = (target, all) => { for (var name3 in all) __defProp(target, name3, { get: all[name3], enumerable: true }); }; // src/configs/all.ts var all_exports = {}; __export(all_exports, { name: () => name, rules: () => rules, settings: () => settings }); var name = "react-debug/all"; var rules = { "react-debug/class-component": "warn", "react-debug/function-component": "warn", "react-debug/hook": "warn", "react-debug/is-from-react": "warn", "react-debug/jsx": "warn" }; var settings = { "react-x": DEFAULT_ESLINT_REACT_SETTINGS }; // package.json var name2 = "eslint-plugin-react-debug"; var version = "1.41.0"; var createRule = ESLintUtils.RuleCreator(getDocsUrl("debug")); // src/rules/class-component.ts var RULE_NAME = "class-component"; var RULE_FEATURES = [ "DBG" ]; var class_component_default = createRule({ meta: { type: "problem", docs: { description: "Reports all class components.", [Symbol.for("rule_features")]: RULE_FEATURES }, messages: { classComponent: "[class component] name: {{name}}." }, schema: [] }, name: RULE_NAME, create, defaultOptions: [] }); function create(context) { const { ctx, listeners } = ER2.useComponentCollectorLegacy(); return { ...listeners, "Program:exit"(node) { const components = ctx.getAllComponents(node); for (const { name: name3 = "anonymous", node: component } of components.values()) { context.report({ messageId: "classComponent", node: component, data: { name: name3 } }); } } }; } var RULE_NAME2 = "function-component"; var RULE_FEATURES2 = [ "DBG" ]; var function_component_default = createRule({ meta: { type: "problem", docs: { description: "Reports all function components.", [Symbol.for("rule_features")]: RULE_FEATURES2 }, messages: { functionComponent: "[function component] name: {{name}}, memo: {{memo}}, forwardRef: {{forwardRef}}, hookCalls: {{hookCalls}}, displayName: {{displayName}}." }, schema: [] }, name: RULE_NAME2, create: create2, defaultOptions: [] }); function create2(context) { const { ctx, listeners } = ER2.useComponentCollector( context, { collectDisplayName: true, collectHookCalls: true, hint: ER2.DEFAULT_COMPONENT_DETECTION_HINT } ); return { ...listeners, "Program:exit"(node) { const components = ctx.getAllComponents(node); for (const { name: name3 = "anonymous", node: node2, displayName, flag, hookCalls } of components.values()) { context.report({ messageId: "functionComponent", node: node2, data: { name: name3, displayName: displayName != null ? context.sourceCode.getText(displayName) : "none", forwardRef: (flag & ER2.ComponentFlag.ForwardRef) > 0n, hookCalls: hookCalls.length, memo: (flag & ER2.ComponentFlag.Memo) > 0n } }); } } }; } var RULE_NAME3 = "hook"; var RULE_FEATURES3 = [ "DBG" ]; var hook_default = createRule({ meta: { type: "problem", docs: { description: "Reports all React Hooks.", [Symbol.for("rule_features")]: RULE_FEATURES3 }, messages: { hook: "[hook] name: {{name}}, hookCalls: {{hookCalls}}." }, schema: [] }, name: RULE_NAME3, create: create3, defaultOptions: [] }); function create3(context) { const { ctx, listeners } = ER2.useHookCollector(); return { ...listeners, "Program:exit"(node) { const allHooks = ctx.getAllHooks(node); for (const { name: name3, node: node2, hookCalls } of allHooks.values()) { context.report({ messageId: "hook", node: node2, data: { name: name3, hookCalls: hookCalls.length } }); } } }; } var RULE_NAME4 = "is-from-react"; var RULE_FEATURES4 = [ "DBG" ]; var is_from_react_default = createRule({ meta: { type: "problem", docs: { description: "Report all identifiers that are initialized from React.", [Symbol.for("rule_features")]: RULE_FEATURES4 }, messages: { isFromReact: "[initialized from react] name: '{{name}}', import source: '{{importSource}}'." }, schema: [] }, name: RULE_NAME4, create: create4, defaultOptions: [] }); function create4(context) { const { importSource = "react" } = getSettingsFromContext(context); function isFromReact(node, initialScope) { const name3 = node.name; switch (true) { case (node.parent.type === AST_NODE_TYPES.MemberExpression && node.parent.property === node && node.parent.object.type === AST_NODE_TYPES.Identifier): return ER2.isInitializedFromReact(node.parent.object.name, importSource, initialScope); case (node.parent.type === AST_NODE_TYPES.JSXMemberExpression && node.parent.property === node && node.parent.object.type === AST_NODE_TYPES.JSXIdentifier): return ER2.isInitializedFromReact(node.parent.object.name, importSource, initialScope); default: return ER2.isInitializedFromReact(name3, importSource, initialScope); } } function visitorFunction(node) { const shouldSkipDuplicate = node.parent.type === AST_NODE_TYPES.ImportSpecifier && node.parent.imported === node && node.parent.imported.name === node.parent.local.name; if (shouldSkipDuplicate) return; const name3 = node.name; const initialScope = context.sourceCode.getScope(node); if (!isFromReact(node, initialScope)) return; context.report({ messageId: "isFromReact", node, data: { type: node.type, name: name3, importSource } }); } return { Identifier: visitorFunction, JSXIdentifier: visitorFunction }; } var RULE_NAME5 = "jsx"; var RULE_FEATURES5 = [ "DBG" ]; var jsx_default = createRule({ meta: { type: "problem", docs: { description: "Reports all React Hooks.", [Symbol.for("rule_features")]: RULE_FEATURES5 }, messages: { jsx: "[jsx] jsx: '{{jsx}}', jsxFactory: '{{jsxFactory}}', jsxFragmentFactory: '{{jsxFragmentFactory}}', jsxRuntime: '{{jsxRuntime}}' jsxImportSource: '{{jsxImportSource}}'" }, schema: [] }, name: RULE_NAME5, create: create5, defaultOptions: [] }); function create5(context) { const jsxConfigFromContext = JsxConfig.getFromContext(context); const jsxConfigFromAnnotation = JsxConfig.getFromAnnotation(context); const jsxConfig = { ...jsxConfigFromContext, ...jsxConfigFromAnnotation }; const baseDescriptor = { messageId: "jsx", data: { jsx: match(jsxConfig.jsx).with(JsxEmit.None, () => "none").with(JsxEmit.ReactJSX, () => "react-jsx").with(JsxEmit.ReactJSXDev, () => "react-jsx-dev").with(JsxEmit.React, () => "react").with(JsxEmit.ReactNative, () => "react-native").with(JsxEmit.Preserve, () => "preserve").otherwise(() => "unknown"), jsxFactory: jsxConfig.jsxFactory, jsxFragmentFactory: jsxConfig.jsxFragmentFactory, jsxImportSource: jsxConfig.jsxImportSource, jsxRuntime: match(jsxConfig.jsx).with(P.union(JsxEmit.None, JsxEmit.ReactJSX, JsxEmit.ReactJSXDev), () => "automatic").otherwise(() => "classic") } }; return { JSXElement(node) { context.report({ ...baseDescriptor, node }); }, JSXFragment(node) { context.report({ ...baseDescriptor, node }); } }; } // src/plugin.ts var plugin = { meta: { name: name2, version }, rules: { ["class-component"]: class_component_default, ["function-component"]: function_component_default, ["hook"]: hook_default, ["is-from-react"]: is_from_react_default, ["jsx"]: jsx_default, // Part: deprecated rules /** @deprecated Use `hook` instead */ "react-hooks": hook_default } }; // src/index.ts function makeConfig(config) { return { ...config, plugins: { "react-debug": plugin } }; } function makeLegacyConfig({ rules: rules2 }) { return { plugins: ["react-debug"], rules: rules2 }; } var index_default = { ...plugin, configs: { ["all"]: makeConfig(all_exports), ["all-legacy"]: makeLegacyConfig(all_exports) } }; export { index_default as default };