Skip to content

Commit c23757a

Browse files
authored
Organize imports (#1442)
1 parent 7207b8b commit c23757a

159 files changed

Lines changed: 1491 additions & 1476 deletions

File tree

Some content is hidden

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

packages/core/docs/functions/findEnclosingComponentOrHook.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ Find the enclosing React component or hook for a given AST node
2828
\| `FunctionExpression`
2929
\| `undefined`
3030

31-
The enclosing component or hook node, or `null` if none is found
31+
The enclosing component or hook node, or `null` if none is ASAST.

packages/core/src/api/is-react-api.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/* eslint-disable function/function-return-boolean */
2-
import * as AST from "@eslint-react/ast";
2+
import * as ast from "@eslint-react/ast";
33
import { dual, type unit } from "@eslint-react/eff";
44
import type { RuleContext } from "@eslint-react/shared";
5-
import { AST_NODE_TYPES as T, type TSESTree } from "@typescript-eslint/types";
5+
import { AST_NODE_TYPES as AST, type TSESTree } from "@typescript-eslint/types";
66

77
export declare namespace isReactAPI {
88
type ReturnType = {
@@ -25,7 +25,7 @@ export function isReactAPI(api: string): isReactAPI.ReturnType {
2525
{
2626
if (node == null) return false;
2727
const getText = (n: TSESTree.Node) => context.sourceCode.getText(n);
28-
const name = AST.toStringFormat(node, getText);
28+
const name = ast.toStringFormat(node, getText);
2929
if (name === api) return true;
3030
if (name.substring(name.indexOf(".") + 1) === api) return true;
3131
return false;
@@ -48,7 +48,7 @@ export declare namespace isReactAPICall {
4848
export function isReactAPICall(api: string): isReactAPICall.ReturnType {
4949
const func = (context: RuleContext, node: unit | null | TSESTree.Node): node is TSESTree.CallExpression => {
5050
if (node == null) return false;
51-
if (node.type !== T.CallExpression) return false;
51+
if (node.type !== AST.CallExpression) return false;
5252
return isReactAPI(api)(context, node.callee);
5353
};
5454
return dual(2, func);

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import * as AST from "@eslint-react/ast";
1+
import * as ast from "@eslint-react/ast";
22
import { unit } from "@eslint-react/eff";
33
import { IdGenerator, type RuleContext } from "@eslint-react/shared";
44
import type { ESLintUtils, TSESTree } from "@typescript-eslint/utils";
5-
import { AST_NODE_TYPES as T } from "@typescript-eslint/utils";
5+
import { AST_NODE_TYPES as AST } from "@typescript-eslint/utils";
66

77
import { ComponentFlag } from "./component-flag";
88
import { isClassComponent, isPureComponent } from "./component-is";
@@ -35,13 +35,13 @@ export function useComponentCollectorLegacy(context: RuleContext): useComponentC
3535
} as const;
3636

3737
const getText = (n: TSESTree.Node) => context.sourceCode.getText(n);
38-
const collect = (node: AST.TSESTreeClass) => {
38+
const collect = (node: ast.TSESTreeClass) => {
3939
if (!isClassComponent(node)) {
4040
return;
4141
}
42-
const id = AST.getClassId(node);
42+
const id = ast.getClassId(node);
4343
const key = idGen.next();
44-
const name = id == null ? unit : AST.toStringFormat(id, getText);
44+
const name = id == null ? unit : ast.toStringFormat(id, getText);
4545
const flag = isPureComponent(node)
4646
? ComponentFlag.PureComponent
4747
: ComponentFlag.None;
@@ -79,9 +79,9 @@ export function useComponentCollectorLegacy(context: RuleContext): useComponentC
7979
export function isThisSetState(node: TSESTree.CallExpression) {
8080
const { callee } = node;
8181
return (
82-
callee.type === T.MemberExpression
83-
&& AST.isThisExpressionLoose(callee.object)
84-
&& callee.property.type === T.Identifier
82+
callee.type === AST.MemberExpression
83+
&& ast.isThisExpressionLoose(callee.object)
84+
&& callee.property.type === AST.Identifier
8585
&& callee.property.name === "setState"
8686
);
8787
}
@@ -93,7 +93,7 @@ export function isThisSetState(node: TSESTree.CallExpression) {
9393
*/
9494
export function isAssignmentToThisState(node: TSESTree.AssignmentExpression) {
9595
const { left } = node;
96-
return left.type === T.MemberExpression
97-
&& AST.isThisExpressionLoose(left.object)
98-
&& AST.getPropertyName(left.property) === "state";
96+
return left.type === AST.MemberExpression
97+
&& ast.isThisExpressionLoose(left.object)
98+
&& ast.getPropertyName(left.property) === "state";
9999
}

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import * as AST from "@eslint-react/ast";
1+
import * as ast from "@eslint-react/ast";
22
import { unit } from "@eslint-react/eff";
33
import type { RuleContext } from "@eslint-react/shared";
44
import { IdGenerator } from "@eslint-react/shared";
55
import type { TSESTree } from "@typescript-eslint/types";
6-
import { AST_NODE_TYPES as T } from "@typescript-eslint/types";
6+
import { AST_NODE_TYPES as AST } from "@typescript-eslint/types";
77
import type { ESLintUtils } from "@typescript-eslint/utils";
88
import type { ComponentDetectionHint } from "./component-detection-hint";
99
import type { FunctionComponentSemanticNode } from "./component-semantic-node";
@@ -57,15 +57,15 @@ export function useComponentCollector(
5757

5858
const getText = (n: TSESTree.Node) => context.sourceCode.getText(n);
5959
const getCurrentEntry = () => functionEntries.at(-1);
60-
const onFunctionEnter = (node: AST.TSESTreeFunction) => {
60+
const onFunctionEnter = (node: ast.TSESTreeFunction) => {
6161
const key = idGen.next();
62-
const exp = AST.findParentNode(node, (n) => n.type === T.ExportDefaultDeclaration);
62+
const exp = ast.findParentNode(node, (n) => n.type === AST.ExportDefaultDeclaration);
6363
const isExportDefault = exp != null;
64-
const isExportDefaultDeclaration = exp != null && AST.getUnderlyingExpression(exp.declaration) === node;
64+
const isExportDefaultDeclaration = exp != null && ast.getUnderlyingExpression(exp.declaration) === node;
6565
const id = getFunctionComponentId(context, node);
66-
const name = id == null ? unit : AST.toStringFormat(id, getText);
67-
const initPath = AST.getFunctionInitPath(node);
68-
const directives = AST.getFunctionDirectives(node);
66+
const name = id == null ? unit : ast.toStringFormat(id, getText);
67+
const initPath = ast.getFunctionInitPath(node);
68+
const directives = ast.getFunctionDirectives(node);
6969
const entry = {
7070
id: getFunctionComponentId(context, node),
7171
key,
@@ -111,18 +111,18 @@ export function useComponentCollector(
111111
const entry = getCurrentEntry();
112112
if (entry == null) return;
113113
const { body } = entry.node;
114-
if (body.type === T.BlockStatement) return;
114+
if (body.type === AST.BlockStatement) return;
115115
entry.rets.push(body);
116116
if (!entry.isComponentDefinition) return;
117117
if (!components.has(entry.key) && !isJsxLike(context.sourceCode, body, hint)) return;
118118
components.set(entry.key, entry);
119119
},
120120
...collectDisplayName
121121
? {
122-
[AST.SEL_DISPLAY_NAME_ASSIGNMENT_EXPRESSION](node: TSESTree.AssignmentExpression) {
122+
[ast.SEL_DISPLAY_NAME_ASSIGNMENT_EXPRESSION](node: TSESTree.AssignmentExpression) {
123123
const { left, right } = node;
124-
if (left.type !== T.MemberExpression) return;
125-
const componentName = left.object.type === T.Identifier
124+
if (left.type !== AST.MemberExpression) return;
125+
const componentName = left.object.type === AST.Identifier
126126
? left.object.name
127127
: unit;
128128
const component = [...components.values()].findLast(({ name }) => name != null && name === componentName);

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

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import * as AST from "@eslint-react/ast";
1+
import * as ast from "@eslint-react/ast";
22
import type { RuleContext } from "@eslint-react/shared";
3-
import { AST_NODE_TYPES as T, type TSESTree } from "@typescript-eslint/types";
3+
import { AST_NODE_TYPES as AST, type TSESTree } from "@typescript-eslint/types";
44

55
import { isCreateElementCall } from "../api";
66
import { ComponentDetectionHint } from "./component-detection-hint";
@@ -21,7 +21,7 @@ import { isRenderMethodLike } from "./component-render-method";
2121
* }
2222
* ```
2323
*/
24-
function isRenderMethodCallback(node: AST.TSESTreeFunction) {
24+
function isRenderMethodCallback(node: ast.TSESTreeFunction) {
2525
const parent = node.parent;
2626
const grandparent = parent.parent;
2727
const greatGrandparent = grandparent?.parent;
@@ -40,31 +40,31 @@ function isRenderMethodCallback(node: AST.TSESTreeFunction) {
4040
* @param hint Component detection hints as bit flags
4141
* @returns `true` if the function matches an exclusion hint
4242
*/
43-
function shouldExcludeBasedOnHint(node: AST.TSESTreeFunction, hint: bigint): boolean {
43+
function shouldExcludeBasedOnHint(node: ast.TSESTreeFunction, hint: bigint): boolean {
4444
switch (true) {
4545
case (hint & ComponentDetectionHint.SkipObjectMethod)
46-
&& AST.isOneOf([T.ArrowFunctionExpression, T.FunctionExpression])(node)
47-
&& node.parent.type === T.Property
48-
&& node.parent.parent.type === T.ObjectExpression:
46+
&& ast.isOneOf([AST.ArrowFunctionExpression, AST.FunctionExpression])(node)
47+
&& node.parent.type === AST.Property
48+
&& node.parent.parent.type === AST.ObjectExpression:
4949
return true;
5050
case (hint & ComponentDetectionHint.SkipClassMethod)
51-
&& AST.isOneOf([T.ArrowFunctionExpression, T.FunctionExpression])(node)
52-
&& node.parent.type === T.MethodDefinition:
51+
&& ast.isOneOf([AST.ArrowFunctionExpression, AST.FunctionExpression])(node)
52+
&& node.parent.type === AST.MethodDefinition:
5353
return true;
5454
case (hint & ComponentDetectionHint.SkipClassProperty)
55-
&& AST.isOneOf([T.ArrowFunctionExpression, T.FunctionExpression])(node)
56-
&& node.parent.type === T.Property:
55+
&& ast.isOneOf([AST.ArrowFunctionExpression, AST.FunctionExpression])(node)
56+
&& node.parent.type === AST.Property:
5757
return true;
5858
case (hint & ComponentDetectionHint.SkipArrayPattern)
59-
&& node.parent.type === T.ArrayPattern:
59+
&& node.parent.type === AST.ArrayPattern:
6060
return true;
6161
case (hint & ComponentDetectionHint.SkipArrayExpression)
62-
&& node.parent.type === T.ArrayExpression:
62+
&& node.parent.type === AST.ArrayExpression:
6363
return true;
6464
case (hint & ComponentDetectionHint.SkipArrayMapCallback)
65-
&& node.parent.type === T.CallExpression
66-
&& node.parent.callee.type === T.MemberExpression
67-
&& node.parent.callee.property.type === T.Identifier
65+
&& node.parent.type === AST.CallExpression
66+
&& node.parent.callee.type === AST.MemberExpression
67+
&& node.parent.callee.property.type === AST.Identifier
6868
&& node.parent.callee.property.name === "map":
6969
return true;
7070
}
@@ -81,7 +81,7 @@ function shouldExcludeBasedOnHint(node: AST.TSESTreeFunction, hint: bigint): boo
8181
function isChildrenOfCreateElement(context: RuleContext, node: TSESTree.Node): boolean {
8282
const parent = node.parent;
8383

84-
if (parent?.type !== T.CallExpression) {
84+
if (parent?.type !== AST.CallExpression) {
8585
return false;
8686
}
8787

@@ -105,7 +105,7 @@ function isChildrenOfCreateElement(context: RuleContext, node: TSESTree.Node): b
105105
*/
106106
export function isComponentDefinition(
107107
context: RuleContext,
108-
node: AST.TSESTreeFunction,
108+
node: ast.TSESTreeFunction,
109109
hint: bigint,
110110
) {
111111
// 1. Check for basic naming conventions
@@ -125,19 +125,19 @@ export function isComponentDefinition(
125125

126126
// 4. Check if the function is embedded directly inside JSX (e.g., inline callbacks)
127127
// We look for the closest parent that is significant (Function, Class, or JSXContainer)
128-
const significantParent = AST.findParentNode(
128+
const significantParent = ast.findParentNode(
129129
node,
130-
AST.isOneOf([
131-
T.JSXExpressionContainer,
132-
T.ArrowFunctionExpression,
133-
T.FunctionExpression,
134-
T.Property,
135-
T.ClassBody,
130+
ast.isOneOf([
131+
AST.JSXExpressionContainer,
132+
AST.ArrowFunctionExpression,
133+
AST.FunctionExpression,
134+
AST.Property,
135+
AST.ClassBody,
136136
]),
137137
);
138138

139139
if (significantParent == null) return true;
140140
// If the immediate significant parent is a JSX expression, this is likely an event handler or a render prop, not a component definition itself
141-
if (significantParent.type === T.JSXExpressionContainer) return false;
141+
if (significantParent.type === AST.JSXExpressionContainer) return false;
142142
return true;
143143
}

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import * as AST from "@eslint-react/ast";
1+
import * as ast from "@eslint-react/ast";
22
import { unit } from "@eslint-react/eff";
33
import type { RuleContext } from "@eslint-react/shared";
4-
import { AST_NODE_TYPES as T } from "@typescript-eslint/types";
4+
import { AST_NODE_TYPES as AST } from "@typescript-eslint/types";
55

66
import { isComponentWrapperCallLoose } from "./component-wrapper";
77

@@ -13,28 +13,28 @@ import { isComponentWrapperCallLoose } from "./component-wrapper";
1313
*/
1414
export function getFunctionComponentId(
1515
context: RuleContext,
16-
node: AST.TSESTreeFunction,
17-
): AST.FunctionID | unit {
18-
const functionId = AST.getFunctionId(node);
16+
node: ast.TSESTreeFunction,
17+
): ast.FunctionID | unit {
18+
const functionId = ast.getFunctionId(node);
1919
if (functionId != null) {
2020
return functionId;
2121
}
2222
const { parent } = node;
2323
// Get function component identifier from `const Component = memo(() => {});`
2424
if (
25-
parent.type === T.CallExpression
25+
parent.type === AST.CallExpression
2626
&& isComponentWrapperCallLoose(context, parent)
27-
&& parent.parent.type === T.VariableDeclarator
27+
&& parent.parent.type === AST.VariableDeclarator
2828
) {
2929
return parent.parent.id;
3030
}
3131
// Get function component identifier from `const Component = memo(forwardRef(() => {}));`
3232
if (
33-
parent.type === T.CallExpression
33+
parent.type === AST.CallExpression
3434
&& isComponentWrapperCallLoose(context, parent)
35-
&& parent.parent.type === T.CallExpression
35+
&& parent.parent.type === AST.CallExpression
3636
&& isComponentWrapperCallLoose(context, parent.parent)
37-
&& parent.parent.parent.type === T.VariableDeclarator
37+
&& parent.parent.parent.type === AST.VariableDeclarator
3838
) {
3939
return parent.parent.parent.id;
4040
}

packages/core/src/component/component-init-path.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as AST from "@eslint-react/ast";
1+
import * as ast from "@eslint-react/ast";
22

33
import { ComponentFlag } from "./component-flag";
44
import type { FunctionComponentSemanticNode } from "./component-semantic-node";
@@ -10,10 +10,10 @@ import type { FunctionComponentSemanticNode } from "./component-semantic-node";
1010
*/
1111
export function getComponentFlagFromInitPath(initPath: FunctionComponentSemanticNode["initPath"]) {
1212
let flag = ComponentFlag.None;
13-
if (initPath != null && AST.hasCallInFunctionInitPath("memo", initPath)) {
13+
if (initPath != null && ast.hasCallInFunctionInitPath("memo", initPath)) {
1414
flag |= ComponentFlag.Memo;
1515
}
16-
if (initPath != null && AST.hasCallInFunctionInitPath("forwardRef", initPath)) {
16+
if (initPath != null && ast.hasCallInFunctionInitPath("forwardRef", initPath)) {
1717
flag |= ComponentFlag.ForwardRef;
1818
}
1919
return flag;

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
import type * as AST from "@eslint-react/ast";
1+
import type * as ast from "@eslint-react/ast";
22
import type { TSESTree } from "@typescript-eslint/types";
3-
import { AST_NODE_TYPES as T } from "@typescript-eslint/types";
3+
import { AST_NODE_TYPES as AST } from "@typescript-eslint/types";
44

55
/**
66
* Check if a node is a React class component
77
* @param node The AST node to check
88
* @returns `true` if the node is a class component, `false` otherwise
99
*/
10-
export function isClassComponent(node: TSESTree.Node): node is AST.TSESTreeClass {
10+
export function isClassComponent(node: TSESTree.Node): node is ast.TSESTreeClass {
1111
if ("superClass" in node && node.superClass != null) {
1212
const re = /^(?:Pure)?Component$/u;
1313
switch (true) {
14-
case node.superClass.type === T.Identifier:
14+
case node.superClass.type === AST.Identifier:
1515
return re.test(node.superClass.name);
16-
case node.superClass.type === T.MemberExpression
17-
&& node.superClass.property.type === T.Identifier:
16+
case node.superClass.type === AST.MemberExpression
17+
&& node.superClass.property.type === AST.Identifier:
1818
return re.test(node.superClass.property.name);
1919
}
2020
}
@@ -30,10 +30,10 @@ export function isPureComponent(node: TSESTree.Node) {
3030
if ("superClass" in node && node.superClass != null) {
3131
const re = /^PureComponent$/u;
3232
switch (true) {
33-
case node.superClass.type === T.Identifier:
33+
case node.superClass.type === AST.Identifier:
3434
return re.test(node.superClass.name);
35-
case node.superClass.type === T.MemberExpression
36-
&& node.superClass.property.type === T.Identifier:
35+
case node.superClass.type === AST.MemberExpression
36+
&& node.superClass.property.type === AST.Identifier:
3737
return re.test(node.superClass.property.name);
3838
}
3939
}

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

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

44
import { isComponentDidMount, isComponentWillUnmount } from "./component-method-is";
@@ -9,7 +9,7 @@ import { isComponentDidMount, isComponentWillUnmount } from "./component-method-
99
* @returns True if the node is a componentDidMount callback, false otherwise
1010
*/
1111
export function isComponentDidMountCallback(node: TSESTree.Node) {
12-
return AST.isFunction(node)
12+
return ast.isFunction(node)
1313
&& isComponentDidMount(node.parent)
1414
&& node.parent.value === node;
1515
}
@@ -20,7 +20,7 @@ export function isComponentDidMountCallback(node: TSESTree.Node) {
2020
* @returns True if the node is a componentWillUnmount callback, false otherwise
2121
*/
2222
export function isComponentWillUnmountCallback(node: TSESTree.Node) {
23-
return AST.isFunction(node)
23+
return ast.isFunction(node)
2424
&& isComponentWillUnmount(node.parent)
2525
&& node.parent.value === node;
2626
}

0 commit comments

Comments
 (0)