|
1 | 1 | import * as AST from "@eslint-react/ast"; |
2 | | -import { _ } from "@eslint-react/eff"; |
| 2 | +import { _, identity } from "@eslint-react/eff"; |
3 | 3 | import * as VAR from "@eslint-react/var"; |
4 | 4 | import type { Scope } from "@typescript-eslint/scope-manager"; |
5 | 5 | import type { TSESTree } from "@typescript-eslint/types"; |
6 | 6 | import { AST_NODE_TYPES as T } from "@typescript-eslint/types"; |
| 7 | +import { match, P } from "ts-pattern"; |
| 8 | + |
| 9 | +/** |
| 10 | + * Get the arguments of a require expression |
| 11 | + * @param node The node to match |
| 12 | + * @returns The require expression arguments or undefined if the node is not a require expression |
| 13 | + */ |
| 14 | +function getRequireExpressionArguments(node: TSESTree.Node) { |
| 15 | + return match<typeof node, TSESTree.CallExpressionArgument[] | _>(node) |
| 16 | + // require("source") |
| 17 | + .with({ type: T.CallExpression, arguments: P.select(), callee: { type: T.Identifier, name: "require" } }, identity) |
| 18 | + // require("source").variable |
| 19 | + .with({ type: T.MemberExpression, object: P.select() }, getRequireExpressionArguments) |
| 20 | + .otherwise(() => _); |
| 21 | +} |
7 | 22 |
|
8 | 23 | /** |
9 | 24 | * Check if an identifier is initialized from react |
@@ -46,19 +61,3 @@ export function isInitializedFromReact( |
46 | 61 | // latest definition is an import declaration: import { variable } from 'source' |
47 | 62 | return parent?.type === T.ImportDeclaration && parent.source.value === source; |
48 | 63 | } |
49 | | - |
50 | | -function getRequireExpressionArguments(node: TSESTree.Node): TSESTree.CallExpressionArgument[] | _ { |
51 | | - switch (true) { |
52 | | - // require('source') |
53 | | - case node.type === T.CallExpression |
54 | | - && node.callee.type === T.Identifier |
55 | | - && node.callee.name === "require": { |
56 | | - return node.arguments; |
57 | | - } |
58 | | - // require('source').variable |
59 | | - case node.type === T.MemberExpression: { |
60 | | - return getRequireExpressionArguments(node.object); |
61 | | - } |
62 | | - } |
63 | | - return _; |
64 | | -} |
0 commit comments