Skip to content

Commit 3c50532

Browse files
authored
refactor: improve code readability (#907)
1 parent 4b96702 commit 3c50532

File tree

120 files changed

+1028
-1007
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

120 files changed

+1028
-1007
lines changed

packages/core/src/component/component-collector-legacy.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as AST from "@eslint-react/ast";
22
import { O } from "@eslint-react/eff";
3-
import { AST_NODE_TYPES } from "@typescript-eslint/types";
3+
import { AST_NODE_TYPES as T } from "@typescript-eslint/types";
44
import type { ESLintUtils, TSESTree } from "@typescript-eslint/utils";
55
import { match, P } from "ts-pattern";
66

@@ -18,11 +18,11 @@ export function isClassComponent(node: TSESTree.Node): node is AST.TSESTreeClass
1818
const { superClass } = node;
1919
return match(superClass)
2020
.with({
21-
type: AST_NODE_TYPES.Identifier,
21+
type: T.Identifier,
2222
name: P.string,
2323
}, ({ name }) => /^(?:Pure)?Component$/u.test(name))
2424
.with({
25-
type: AST_NODE_TYPES.MemberExpression,
25+
type: T.MemberExpression,
2626
property: { name: P.string },
2727
}, ({ property }) => /^(?:Pure)?Component$/u.test(property.name))
2828
.otherwise(() => false);
@@ -37,11 +37,11 @@ export function isPureComponent(node: TSESTree.Node) {
3737
if ("superClass" in node && node.superClass) {
3838
return match(node.superClass)
3939
.with({
40-
type: AST_NODE_TYPES.Identifier,
40+
type: T.Identifier,
4141
name: P.string,
4242
}, ({ name }) => /^PureComponent$/u.test(name))
4343
.with({
44-
type: AST_NODE_TYPES.MemberExpression,
44+
type: T.MemberExpression,
4545
property: { name: P.string },
4646
}, ({ property }) => /^PureComponent$/u.test(property.name))
4747
.otherwise(() => false);
@@ -52,16 +52,16 @@ export function isPureComponent(node: TSESTree.Node) {
5252
export function isComponentDidMount(
5353
node: TSESTree.Node,
5454
): node is TSESTree.MethodDefinition | TSESTree.PropertyDefinition {
55-
return AST.isOneOf([AST_NODE_TYPES.MethodDefinition, AST_NODE_TYPES.PropertyDefinition])(node)
56-
&& node.key.type === AST_NODE_TYPES.Identifier
55+
return AST.isOneOf([T.MethodDefinition, T.PropertyDefinition])(node)
56+
&& node.key.type === T.Identifier
5757
&& node.key.name === "componentDidMount";
5858
}
5959

6060
export function isComponentWillUnmount(
6161
node: TSESTree.Node,
6262
): node is TSESTree.MethodDefinition | TSESTree.PropertyDefinition {
63-
return AST.isOneOf([AST_NODE_TYPES.MethodDefinition, AST_NODE_TYPES.PropertyDefinition])(node)
64-
&& node.key.type === AST_NODE_TYPES.Identifier
63+
return AST.isOneOf([T.MethodDefinition, T.PropertyDefinition])(node)
64+
&& node.key.type === T.Identifier
6565
&& node.key.name === "componentWillUnmount";
6666
}
6767

packages/core/src/component/component-collector.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { O } from "@eslint-react/eff";
33
import * as JSX from "@eslint-react/jsx";
44
import type { RuleContext } from "@eslint-react/types";
55
import type { TSESTree } from "@typescript-eslint/types";
6-
import { AST_NODE_TYPES } from "@typescript-eslint/types";
6+
import { AST_NODE_TYPES as T } from "@typescript-eslint/types";
77
import type { ESLintUtils } from "@typescript-eslint/utils";
88
import { match } from "ts-pattern";
99

@@ -35,17 +35,17 @@ function hasValidHierarchy(node: AST.TSESTreeFunction, context: RuleContext, hin
3535
return false;
3636
}
3737
return !O.exists(
38-
AST.traverseUp(
38+
AST.findParentNode(
3939
node,
4040
AST.isOneOf([
41-
AST_NODE_TYPES.JSXExpressionContainer,
42-
AST_NODE_TYPES.ArrowFunctionExpression,
43-
AST_NODE_TYPES.FunctionExpression,
44-
AST_NODE_TYPES.Property,
45-
AST_NODE_TYPES.ClassBody,
41+
T.JSXExpressionContainer,
42+
T.ArrowFunctionExpression,
43+
T.FunctionExpression,
44+
T.Property,
45+
T.ClassBody,
4646
]),
4747
),
48-
AST.is(AST_NODE_TYPES.JSXExpressionContainer),
48+
AST.is(T.JSXExpressionContainer),
4949
);
5050
}
5151

@@ -138,7 +138,7 @@ export function useComponentCollector(
138138
) {
139139
const { left, right } = node;
140140
const componentName = match(left.object)
141-
.with({ type: AST_NODE_TYPES.Identifier }, n => O.some(n.name))
141+
.with({ type: T.Identifier }, n => O.some(n.name))
142142
.otherwise(O.none);
143143
O.match(componentName, {
144144
onNone() {},

packages/core/src/component/component-id.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ import * as AST from "@eslint-react/ast";
22
import { O } from "@eslint-react/eff";
33
import type { RuleContext } from "@eslint-react/types";
44
import type { TSESTree } from "@typescript-eslint/types";
5-
import { AST_NODE_TYPES } from "@typescript-eslint/types";
5+
import { AST_NODE_TYPES as T } from "@typescript-eslint/types";
66

77
import { isReactHookCallWithNameLoose } from "../hook";
88
import { isForwardRefCall, isMemoCall } from "../utils";
99

1010
function isComponentWrapperCall(node: TSESTree.Node, context: RuleContext) {
11-
if (node.type !== AST_NODE_TYPES.CallExpression) return false;
11+
if (node.type !== T.CallExpression) return false;
1212
return isMemoCall(node, context)
1313
|| isForwardRefCall(node, context)
1414
|| isReactHookCallWithNameLoose(node)("useCallback");
@@ -23,21 +23,21 @@ export function getFunctionComponentIdentifier(
2323
const { parent } = node;
2424
// Get function component identifier from `const Component = memo(() => {});`
2525
if (
26-
parent.type === AST_NODE_TYPES.CallExpression
26+
parent.type === T.CallExpression
2727
&& isComponentWrapperCall(parent, context)
28-
&& parent.parent.type === AST_NODE_TYPES.VariableDeclarator
29-
&& parent.parent.id.type === AST_NODE_TYPES.Identifier
28+
&& parent.parent.type === T.VariableDeclarator
29+
&& parent.parent.id.type === T.Identifier
3030
) {
3131
return O.some(parent.parent.id);
3232
}
3333
// Get function component identifier from `const Component = memo(forwardRef(() => {}));`
3434
if (
35-
parent.type === AST_NODE_TYPES.CallExpression
35+
parent.type === T.CallExpression
3636
&& isComponentWrapperCall(parent, context)
37-
&& parent.parent.type === AST_NODE_TYPES.CallExpression
37+
&& parent.parent.type === T.CallExpression
3838
&& isComponentWrapperCall(parent.parent, context)
39-
&& parent.parent.parent.type === AST_NODE_TYPES.VariableDeclarator
40-
&& parent.parent.parent.id.type === AST_NODE_TYPES.Identifier
39+
&& parent.parent.parent.type === T.VariableDeclarator
40+
&& parent.parent.parent.id.type === T.Identifier
4141
) {
4242
return O.some(parent.parent.parent.id);
4343
}

packages/core/src/component/component-render-method.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
import * as AST from "@eslint-react/ast";
22
import { O } from "@eslint-react/eff";
3-
import { AST_NODE_TYPES } from "@typescript-eslint/types";
3+
import { AST_NODE_TYPES as T } from "@typescript-eslint/types";
44
import type { TSESTree } from "@typescript-eslint/utils";
55
import { isMatching, P } from "ts-pattern";
66

77
import { isClassComponent } from "./component-collector-legacy";
88

99
const isRenderMethodLike = isMatching({
1010
key: {
11-
type: AST_NODE_TYPES.Identifier,
11+
type: T.Identifier,
1212
name: "render",
1313
},
14-
type: P.union(AST_NODE_TYPES.MethodDefinition, AST_NODE_TYPES.PropertyDefinition),
14+
type: P.union(T.MethodDefinition, T.PropertyDefinition),
1515
parent: {
16-
type: AST_NODE_TYPES.ClassBody,
16+
type: T.ClassBody,
1717
parent: {
18-
type: AST_NODE_TYPES.ClassDeclaration,
18+
type: T.ClassDeclaration,
1919
},
2020
},
2121
});
@@ -42,7 +42,7 @@ export function isFunctionOfRenderMethod(node: AST.TSESTreeFunction) {
4242
* @returns `true` if node is inside class component's render block, `false` if not
4343
*/
4444
export function isInsideRenderMethod(node: TSESTree.Node) {
45-
return O.isSome(AST.traverseUp(node, (node: TSESTree.Node) => {
45+
return O.isSome(AST.findParentNode(node, (node: TSESTree.Node) => {
4646
return isRenderMethodLike(node) && isClassComponent(node.parent.parent);
4747
}));
4848
}

packages/core/src/effect/is.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
import * as AST from "@eslint-react/ast";
22
import { O } from "@eslint-react/eff";
33
import type { TSESTree } from "@typescript-eslint/types";
4-
import { AST_NODE_TYPES } from "@typescript-eslint/types";
4+
import { AST_NODE_TYPES as T } from "@typescript-eslint/types";
55

66
import { isUseEffectCallLoose } from "../hook";
77

88
export function isSetupFunction(node: TSESTree.Node) {
9-
return node.parent?.type === AST_NODE_TYPES.CallExpression
9+
return node.parent?.type === T.CallExpression
1010
&& node.parent.callee !== node
11-
&& node.parent.callee.type === AST_NODE_TYPES.Identifier
11+
&& node.parent.callee.type === T.Identifier
1212
&& node.parent.arguments.at(0) === node
1313
&& isUseEffectCallLoose(node.parent);
1414
}
1515

1616
export function isCleanupFunction(node: TSESTree.Node) {
17-
const nearestRet = O.getOrNull(AST.traverseUpGuard(node, AST.is(AST_NODE_TYPES.ReturnStatement)));
17+
const nearestRet = O.getOrNull(AST.findParentNodeGuard(node, AST.is(T.ReturnStatement)));
1818
if (!nearestRet) return false;
19-
const nearestFunction = O.getOrNull(AST.traverseUpGuard(node, AST.isFunction));
20-
const nearestFunctionOfRet = O.getOrNull(AST.traverseUpGuard(nearestRet, AST.isFunction));
19+
const nearestFunction = O.getOrNull(AST.findParentNodeGuard(node, AST.isFunction));
20+
const nearestFunctionOfRet = O.getOrNull(AST.findParentNodeGuard(nearestRet, AST.isFunction));
2121
if (!nearestFunction || !nearestFunctionOfRet) return false;
2222
return nearestFunction === nearestFunctionOfRet && isSetupFunction(nearestFunction);
2323
}

packages/core/src/element/hierarchy.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as AST from "@eslint-react/ast";
22
import { F, O } from "@eslint-react/eff";
33
import type { RuleContext } from "@eslint-react/types";
44
import type { TSESTree } from "@typescript-eslint/types";
5-
import { AST_NODE_TYPES } from "@typescript-eslint/types";
5+
import { AST_NODE_TYPES as T } from "@typescript-eslint/types";
66

77
import { isCreateElementCall } from "../utils";
88

@@ -18,8 +18,8 @@ export function isInsideCreateElementProps(
1818
) {
1919
return F.pipe(
2020
O.Do,
21-
O.bind("call", () => AST.traverseUpGuard(node, isCreateElementCall(context))),
22-
O.bind("prop", () => AST.traverseUpGuard(node, AST.is(AST_NODE_TYPES.ObjectExpression))),
21+
O.bind("call", () => AST.findParentNodeGuard(node, isCreateElementCall(context))),
22+
O.bind("prop", () => AST.findParentNodeGuard(node, AST.is(T.ObjectExpression))),
2323
O.bind("arg1", ({ call }) => O.fromNullable(call.arguments[1])),
2424
O.exists(({ arg1, prop }) => prop === arg1),
2525
);
@@ -37,7 +37,7 @@ export function isChildrenOfCreateElement(
3737
) {
3838
return F.pipe(
3939
O.fromNullable(node.parent),
40-
O.filter(AST.is(AST_NODE_TYPES.CallExpression)),
40+
O.filter(AST.is(T.CallExpression)),
4141
O.filter(isCreateElementCall(context)),
4242
O.exists(n =>
4343
n.arguments

packages/core/src/hook/hierarchy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ import type { TSESTree } from "@typescript-eslint/types";
55
import { isReactHook } from "./is";
66

77
export function isInsideReactHook(node: TSESTree.Node) {
8-
return O.exists(AST.traverseUpGuard(node, AST.isFunction), isReactHook);
8+
return O.exists(AST.findParentNodeGuard(node, AST.isFunction), isReactHook);
99
}

packages/core/src/hook/is.ts

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { F, O } from "@eslint-react/eff";
33
import { getSettingsFromContext } from "@eslint-react/shared";
44
import type { RuleContext } from "@eslint-react/types";
55
import type { TSESTree } from "@typescript-eslint/types";
6-
import { AST_NODE_TYPES } from "@typescript-eslint/types";
6+
import { AST_NODE_TYPES as T } from "@typescript-eslint/types";
77

88
import { isInitializedFromReact } from "../utils";
99
import { isReactHookName } from "./hook-name";
@@ -22,10 +22,10 @@ export function isReactHook(node: AST.TSESTreeFunction) {
2222
* @returns `true` if the node is a React Hook call, `false` otherwise.
2323
*/
2424
export function isReactHookCall(node: TSESTree.Node) {
25-
if (node.type !== AST_NODE_TYPES.CallExpression) return false;
26-
if (node.callee.type === AST_NODE_TYPES.Identifier) return isReactHookName(node.callee.name);
27-
if (node.callee.type === AST_NODE_TYPES.MemberExpression) {
28-
return node.callee.property.type === AST_NODE_TYPES.Identifier && isReactHookName(node.callee.property.name);
25+
if (node.type !== T.CallExpression) return false;
26+
if (node.callee.type === T.Identifier) return isReactHookName(node.callee.name);
27+
if (node.callee.type === T.MemberExpression) {
28+
return node.callee.property.type === T.Identifier && isReactHookName(node.callee.property.name);
2929
}
3030
return false;
3131
}
@@ -35,11 +35,11 @@ export function isReactHookCallWithName(node: TSESTree.CallExpression, context:
3535
return (name: string) => {
3636
const initialScope = context.sourceCode.getScope(node);
3737
switch (true) {
38-
case node.callee.type === AST_NODE_TYPES.Identifier
38+
case node.callee.type === T.Identifier
3939
&& node.callee.name === name:
4040
return isInitializedFromReact(name, initialScope, settings);
41-
case node.callee.type === AST_NODE_TYPES.MemberExpression
42-
&& node.callee.property.type === AST_NODE_TYPES.Identifier
41+
case node.callee.type === T.MemberExpression
42+
&& node.callee.property.type === T.Identifier
4343
&& node.callee.property.name === name
4444
&& "name" in node.callee.object:
4545
return isInitializedFromReact(node.callee.object.name, initialScope, settings);
@@ -52,10 +52,10 @@ export function isReactHookCallWithName(node: TSESTree.CallExpression, context:
5252
export function isReactHookCallWithNameLoose(node: TSESTree.CallExpression) {
5353
return (name: string) => {
5454
switch (node.callee.type) {
55-
case AST_NODE_TYPES.Identifier:
55+
case T.Identifier:
5656
return node.callee.name === name;
57-
case AST_NODE_TYPES.MemberExpression:
58-
return node.callee.property.type === AST_NODE_TYPES.Identifier && node.callee.property.name === name;
57+
case T.MemberExpression:
58+
return node.callee.property.type === T.Identifier && node.callee.property.name === name;
5959
default:
6060
return false;
6161
}
@@ -67,11 +67,11 @@ export function isReactHookCallWithNameAlias(name: string, context: RuleContext,
6767
return (node: TSESTree.CallExpression) => {
6868
const initialScope = context.sourceCode.getScope(node);
6969
switch (true) {
70-
case node.callee.type === AST_NODE_TYPES.Identifier
70+
case node.callee.type === T.Identifier
7171
&& node.callee.name === name:
7272
return isInitializedFromReact(name, initialScope, settings);
73-
case node.callee.type === AST_NODE_TYPES.MemberExpression
74-
&& node.callee.property.type === AST_NODE_TYPES.Identifier
73+
case node.callee.type === T.MemberExpression
74+
&& node.callee.property.type === T.Identifier
7575
&& node.callee.property.name === name
7676
&& "name" in node.callee.object:
7777
return isInitializedFromReact(node.callee.object.name, initialScope, settings);
@@ -82,12 +82,12 @@ export function isReactHookCallWithNameAlias(name: string, context: RuleContext,
8282
}
8383

8484
export function isUseEffectCallLoose(node: TSESTree.Node) {
85-
if (node.type !== AST_NODE_TYPES.CallExpression) return false;
85+
if (node.type !== T.CallExpression) return false;
8686
switch (node.callee.type) {
87-
case AST_NODE_TYPES.Identifier:
87+
case T.Identifier:
8888
return /^use\w*Effect$/u.test(node.callee.name);
89-
case AST_NODE_TYPES.MemberExpression:
90-
return node.callee.property.type === AST_NODE_TYPES.Identifier
89+
case T.MemberExpression:
90+
return node.callee.property.type === T.Identifier
9191
&& /^use\w*Effect$/u.test(node.callee.property.name);
9292
default:
9393
return false;

packages/core/src/render-prop/hierarchy.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as AST from "@eslint-react/ast";
22
import { F, O } from "@eslint-react/eff";
33
import type { TSESTree } from "@typescript-eslint/types";
4-
import { AST_NODE_TYPES } from "@typescript-eslint/types";
4+
import { AST_NODE_TYPES as T } from "@typescript-eslint/types";
55
import { isMatching, P } from "ts-pattern";
66

77
/**
@@ -19,10 +19,10 @@ import { isMatching, P } from "ts-pattern";
1919
export function isDirectValueOfRenderPropertyLoose(node: TSESTree.Node) {
2020
const matching = isMatching({
2121
key: {
22-
type: AST_NODE_TYPES.Identifier,
22+
type: T.Identifier,
2323
name: P.string.startsWith("render"),
2424
},
25-
type: AST_NODE_TYPES.Property,
25+
type: T.Property,
2626
});
2727

2828
return matching(node) || matching(node.parent);
@@ -45,12 +45,12 @@ export function isDeclaredInRenderPropLoose(node: TSESTree.Node) {
4545
}
4646

4747
return F.pipe(
48-
AST.traverseUpGuard(node, AST.is(AST_NODE_TYPES.JSXExpressionContainer)),
48+
AST.findParentNodeGuard(node, AST.is(T.JSXExpressionContainer)),
4949
O.flatMapNullable(c => c.parent),
50-
O.filter(AST.is(AST_NODE_TYPES.JSXAttribute)),
50+
O.filter(AST.is(T.JSXAttribute)),
5151
O.flatMapNullable(a => a.name),
5252
O.exists(isMatching({
53-
type: AST_NODE_TYPES.JSXIdentifier,
53+
type: T.JSXIdentifier,
5454
name: P.string.startsWith("render"),
5555
})),
5656
);

0 commit comments

Comments
 (0)