Skip to content

Commit ddeec0c

Browse files
committed
refactor: minor improvements
1 parent 70e1a58 commit ddeec0c

File tree

1 file changed

+16
-17
lines changed

1 file changed

+16
-17
lines changed

packages/core/src/utils/is-initialized-from-react.ts

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,24 @@
11
import * as AST from "@eslint-react/ast";
2-
import { _ } from "@eslint-react/eff";
2+
import { _, identity } from "@eslint-react/eff";
33
import * as VAR from "@eslint-react/var";
44
import type { Scope } from "@typescript-eslint/scope-manager";
55
import type { TSESTree } from "@typescript-eslint/types";
66
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+
}
722

823
/**
924
* Check if an identifier is initialized from react
@@ -46,19 +61,3 @@ export function isInitializedFromReact(
4661
// latest definition is an import declaration: import { variable } from 'source'
4762
return parent?.type === T.ImportDeclaration && parent.source.value === source;
4863
}
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

Comments
 (0)