Skip to content

Commit

Permalink
refactor: code optimization (#945)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rel1cx authored Feb 25, 2025
1 parent e6d4a93 commit 46c0272
Show file tree
Hide file tree
Showing 37 changed files with 471 additions and 320 deletions.
2 changes: 1 addition & 1 deletion .pkgs/eslint-plugin-local/dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function isInitializedFromSource(name2, source, initialScope) {
}
const args = getRequireExpressionArguments(init);
const arg0 = args?.[0];
if (arg0 == null || !AST.isStringLiteral(arg0)) {
if (arg0 == null || !AST.isKindOfLiteral(arg0, "string")) {
return false;
}
return arg0.value === source || arg0.value.startsWith(`${source}/`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export function isInitializedFromSource(
// check for: `variable = require('source')` or `variable = require('source').variable`
const args = getRequireExpressionArguments(init);
const arg0 = args?.[0];
if (arg0 == null || !AST.isStringLiteral(arg0)) {
if (arg0 == null || !AST.isKindOfLiteral(arg0, "string")) {
return false;
}
// check for: `require('source')` or `require('source/...')`
Expand Down
2 changes: 1 addition & 1 deletion packages/core/docs/-internal-/interfaces/TSTypeOperator.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ The loc property is defined as nullable by ESTree, but ESLint requires this prop

### operator

> **operator**: `"keyof"` \| `"readonly"` \| `"unique"`
> **operator**: `"readonly"` \| `"keyof"` \| `"unique"`
***

Expand Down
6 changes: 6 additions & 0 deletions packages/core/docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
- [getComponentNameFromIdentifier](functions/getComponentNameFromIdentifier.md)
- [getFunctionComponentIdentifier](functions/getFunctionComponentIdentifier.md)
- [hasNoneOrValidComponentName](functions/hasNoneOrValidComponentName.md)
- [hasValidHierarchy](functions/hasValidHierarchy.md)
- [isCallFromReact](functions/isCallFromReact.md)
- [isCallFromReactMember](functions/isCallFromReactMember.md)
- [isChildrenCount](functions/isChildrenCount.md)
Expand Down Expand Up @@ -83,6 +84,11 @@
- [isForwardRef](functions/isForwardRef.md)
- [isForwardRefCall](functions/isForwardRefCall.md)
- [isFromReact](functions/isFromReact.md)
- [isFromReactLoose](functions/isFromReactLoose.md)
- [isFromReactMember](functions/isFromReactMember.md)
- [isFromReactMemberLoose](functions/isFromReactMemberLoose.md)
- [isFromReactMemberStrict](functions/isFromReactMemberStrict.md)
- [isFromReactStrict](functions/isFromReactStrict.md)
- [isFunctionOfComponentDidMount](functions/isFunctionOfComponentDidMount.md)
- [isFunctionOfComponentWillUnmount](functions/isFunctionOfComponentWillUnmount.md)
- [isFunctionOfRenderMethod](functions/isFunctionOfRenderMethod.md)
Expand Down
29 changes: 29 additions & 0 deletions packages/core/docs/functions/hasValidHierarchy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
[**@eslint-react/core**](../README.md)

***

[@eslint-react/core](../README.md) / hasValidHierarchy

# Function: hasValidHierarchy()

> **hasValidHierarchy**(`node`, `context`, `hint`): `boolean`
internal

## Parameters

### node

[`TSESTreeFunction`](../-internal-/type-aliases/TSESTreeFunction.md)

### context

[`Readonly`](../-internal-/type-aliases/Readonly.md)

### hint

`bigint`

## Returns

`boolean`
6 changes: 0 additions & 6 deletions packages/core/docs/functions/isFromReact.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,16 @@

> **isFromReact**(`name`): (`node`, `context`) => `boolean`
Checks if the given node is a call expression to the given function or method of the pragma

## Parameters

### name

`string`

The name of the function or method to check

## Returns

`Function`

A predicate that checks if the given node is a call expression to the given function or method

### Parameters

#### node
Expand Down
23 changes: 23 additions & 0 deletions packages/core/docs/functions/isFromReactLoose.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[**@eslint-react/core**](../README.md)

***

[@eslint-react/core](../README.md) / isFromReactLoose

# Function: isFromReactLoose()

> **isFromReactLoose**(`node`, `name`): `boolean`
## Parameters

### node

[`Identifier`](../-internal-/interfaces/Identifier.md) | [`MemberExpression`](../-internal-/type-aliases/MemberExpression.md)

### name

`string`

## Returns

`boolean`
37 changes: 37 additions & 0 deletions packages/core/docs/functions/isFromReactMember.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
[**@eslint-react/core**](../README.md)

***

[@eslint-react/core](../README.md) / isFromReactMember

# Function: isFromReactMember()

> **isFromReactMember**(`memberName`, `name`): (`node`, `context`) => `boolean`
## Parameters

### memberName

`string`

### name

`string`

## Returns

`Function`

### Parameters

#### node

[`MemberExpression`](../-internal-/type-aliases/MemberExpression.md)

#### context

[`Readonly`](../-internal-/type-aliases/Readonly.md)

### Returns

`boolean`
27 changes: 27 additions & 0 deletions packages/core/docs/functions/isFromReactMemberLoose.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[**@eslint-react/core**](../README.md)

***

[@eslint-react/core](../README.md) / isFromReactMemberLoose

# Function: isFromReactMemberLoose()

> **isFromReactMemberLoose**(`node`, `memberName`, `name`): `boolean`
## Parameters

### node

[`MemberExpression`](../-internal-/type-aliases/MemberExpression.md)

### memberName

`string`

### name

`string`

## Returns

`boolean`
35 changes: 35 additions & 0 deletions packages/core/docs/functions/isFromReactMemberStrict.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
[**@eslint-react/core**](../README.md)

***

[@eslint-react/core](../README.md) / isFromReactMemberStrict

# Function: isFromReactMemberStrict()

> **isFromReactMemberStrict**(`node`, `memberName`, `name`, `importSource`, `initialScope`): `boolean`
## Parameters

### node

[`MemberExpression`](../-internal-/type-aliases/MemberExpression.md)

### memberName

`string`

### name

`string`

### importSource

`string`

### initialScope

[`Scope`](../-internal-/type-aliases/Scope.md)

## Returns

`boolean`
31 changes: 31 additions & 0 deletions packages/core/docs/functions/isFromReactStrict.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
[**@eslint-react/core**](../README.md)

***

[@eslint-react/core](../README.md) / isFromReactStrict

# Function: isFromReactStrict()

> **isFromReactStrict**(`node`, `name`, `importSource`, `initialScope`): `boolean`
## Parameters

### node

[`Identifier`](../-internal-/interfaces/Identifier.md) | [`MemberExpression`](../-internal-/type-aliases/MemberExpression.md)

### name

`string`

### importSource

`string`

### initialScope

[`Scope`](../-internal-/type-aliases/Scope.md)

## Returns

`boolean`
34 changes: 2 additions & 32 deletions packages/core/src/component/component-collector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ import type { TSESTree } from "@typescript-eslint/types";
import { AST_NODE_TYPES as T } from "@typescript-eslint/types";
import type { ESLintUtils } from "@typescript-eslint/utils";

import { isChildrenOfCreateElement } from "../element";
import { isReactHookCall } from "../hook";
import { DISPLAY_NAME_ASSIGNMENT_SELECTOR } from "../utils";
import { DEFAULT_COMPONENT_HINT, ERComponentHint } from "./component-collector-hint";
import { DEFAULT_COMPONENT_HINT } from "./component-collector-hint";
import { ERComponentFlag } from "./component-flag";
import { getFunctionComponentIdentifier } from "./component-id";
import { isFunctionOfRenderMethod } from "./component-lifecycle";
import { getComponentNameFromIdentifier, hasNoneOrValidComponentName } from "./component-name";
import type { ERFunctionComponent } from "./component-semantic-node";
import { hasValidHierarchy } from "./hierarchy";

type FunctionEntry = {
key: string;
Expand Down Expand Up @@ -172,35 +171,6 @@ export function useComponentCollector(
return { ctx, listeners } as const;
}

function hasValidHierarchy(node: AST.TSESTreeFunction, context: RuleContext, hint: bigint) {
if (isChildrenOfCreateElement(node, context) || isFunctionOfRenderMethod(node)) {
return false;
}
if (hint & ERComponentHint.SkipMapCallback && AST.isMapCallLoose(node.parent)) {
return false;
}
if (hint & ERComponentHint.SkipObjectMethod && AST.isFunctionOfObjectMethod(node.parent)) {
return false;
}
if (hint & ERComponentHint.SkipClassMethod && AST.isFunctionOfClassMethod(node.parent)) {
return false;
}
if (hint & ERComponentHint.SkipClassProperty && AST.isFunctionOfClassProperty(node.parent)) {
return false;
}
const boundaryNode = AST.findParentNode(
node,
AST.isOneOf([
T.JSXExpressionContainer,
T.ArrowFunctionExpression,
T.FunctionExpression,
T.Property,
T.ClassBody,
]),
);
return boundaryNode == null || boundaryNode.type !== T.JSXExpressionContainer;
}

function getComponentFlag(initPath: ERFunctionComponent["initPath"]) {
let flag = ERComponentFlag.None;
if (initPath != null && AST.hasCallInFunctionInitPath("memo", initPath)) {
Expand Down
61 changes: 61 additions & 0 deletions packages/core/src/component/hierarchy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/* eslint-disable jsdoc/require-param */
import * as AST from "@eslint-react/ast";
import { type RuleContext } from "@eslint-react/shared";
import type { TSESTree } from "@typescript-eslint/types";
import { AST_NODE_TYPES as T } from "@typescript-eslint/types";

import { isChildrenOfCreateElement } from "../element";
import { ERComponentHint } from "./component-collector-hint";
import { isFunctionOfRenderMethod } from "./component-lifecycle";

/** internal */
export function hasValidHierarchy(node: AST.TSESTreeFunction, context: RuleContext, hint: bigint) {
if (isChildrenOfCreateElement(node, context) || isFunctionOfRenderMethod(node)) {
return false;
}
if (hint & ERComponentHint.SkipMapCallback && AST.isMapCallLoose(node.parent)) {
return false;
}
if (hint & ERComponentHint.SkipObjectMethod && isFunctionOfObjectMethod(node.parent)) {
return false;
}
if (hint & ERComponentHint.SkipClassMethod && isFunctionOfClassMethod(node.parent)) {
return false;
}
if (hint & ERComponentHint.SkipClassProperty && isFunctionOfClassProperty(node.parent)) {
return false;
}
const boundaryNode = AST.findParentNode(
node,
AST.isOneOf([
T.JSXExpressionContainer,
T.ArrowFunctionExpression,
T.FunctionExpression,
T.Property,
T.ClassBody,
]),
);
return boundaryNode == null || boundaryNode.type !== T.JSXExpressionContainer;
}

function isFunctionOfClassMethod(node: TSESTree.Node): node is
| TSESTree.ArrowFunctionExpression
| TSESTree.FunctionExpression
{
return (node.type === T.FunctionExpression || node.type === T.ArrowFunctionExpression)
&& node.parent.type === T.MethodDefinition;
}

function isFunctionOfClassProperty(node: TSESTree.Node): node is
| TSESTree.ArrowFunctionExpression
| TSESTree.FunctionExpression
{
return (node.type === T.FunctionExpression || node.type === T.ArrowFunctionExpression)
&& node.parent.type === T.Property;
}

function isFunctionOfObjectMethod(node: TSESTree.Node) {
return (node.type === T.FunctionExpression || node.type === T.ArrowFunctionExpression)
&& node.parent.type === T.Property
&& node.parent.parent.type === T.ObjectExpression;
}
1 change: 1 addition & 0 deletions packages/core/src/component/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ export * from "./component-phase";
export * from "./component-render-prop";
export type * from "./component-semantic-node";
export * from "./component-state";
export * from "./hierarchy";
export * from "./is";
1 change: 1 addition & 0 deletions packages/core/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./is-call-from-react";
export * from "./is-display-name-assignment";
export * from "./is-from-react";
export * from "./is-initialized-from-react";
Expand Down
Loading

0 comments on commit 46c0272

Please sign in to comment.