Skip to content

Commit 9675efe

Browse files
authored
refactor(ast): restructure the module (#1075)
1 parent e051f25 commit 9675efe

32 files changed

+114
-134
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export function isReactAPI(api: string): isReactAPI.ReturnType {
1818
{
1919
if (node == null) return false;
2020
const getText = (n: TSESTree.Node) => context.sourceCode.getText(n);
21-
const name = AST.stringify(node, getText);
21+
const name = AST.toString(node, getText);
2222
if (name === api) return true;
2323
if (name.substring(name.indexOf(".") + 1) === api) return true;
2424
return false;

packages/plugins/eslint-plugin-react-hooks-extra/src/hooks/use-no-direct-set-state-in-use-effect.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,15 +221,15 @@ export function useNoDirectSetStateInUseEffect<Ctx extends RuleContext>(
221221
const setStateCalls = getSetStateCalls(name, context.sourceCode.getScope(callee));
222222
for (const setStateCall of setStateCalls) {
223223
onViolation(context, setStateCall, {
224-
name: AST.stringify(setStateCall, (n) => context.sourceCode.getText(n)),
224+
name: AST.toString(setStateCall, (n) => context.sourceCode.getText(n)),
225225
});
226226
}
227227
}
228228
for (const id of setupFunctionIdentifiers) {
229229
const setStateCalls = getSetStateCalls(id.name, context.sourceCode.getScope(id));
230230
for (const setStateCall of setStateCalls) {
231231
onViolation(context, setStateCall, {
232-
name: AST.stringify(setStateCall, (n) => context.sourceCode.getText(n)),
232+
name: AST.toString(setStateCall, (n) => context.sourceCode.getText(n)),
233233
});
234234
}
235235
}

packages/plugins/eslint-plugin-react-x/src/rules/no-duplicate-key.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export function create(context: RuleContext<MessageID, []>): RuleListener {
7474
if (!AST.isFunction(iter)) return;
7575
const arg0 = call?.arguments[0];
7676
if (call == null || arg0 == null) return;
77-
if (AST.getEcmaExpression(arg0) !== iter) {
77+
if (AST.getJSExpression(arg0) !== iter) {
7878
return;
7979
}
8080
keyedEntries.set(call, {

packages/utilities/ast/src/__tests__/get-class-identifier.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { TSESTreeClass } from "../types";
1+
import type { TSESTreeClass } from "../ast-node";
22

33
import path from "node:path";
44
import { parseForESLint } from "@typescript-eslint/parser";
@@ -7,7 +7,7 @@ import { simpleTraverse } from "@typescript-eslint/typescript-estree";
77

88
import { describe, expect, it } from "vitest";
99
import { getFixturesRootDir } from "../../../../../test";
10-
import { getClassIdentifier } from "../get-class-identifier";
10+
import { getClassIdentifier } from "../ast-class-id";
1111

1212
function parse(code: string) {
1313
return parseForESLint(code, {

packages/utilities/ast/src/__tests__/get-function-identifier.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { TSESTreeFunction } from "../types";
1+
import type { TSESTreeFunction } from "../ast-node";
22

33
import path from "node:path";
44
import { parseForESLint } from "@typescript-eslint/parser";
@@ -7,8 +7,8 @@ import { simpleTraverse } from "@typescript-eslint/typescript-estree";
77

88
import { describe, expect, it } from "vitest";
99
import { getFixturesRootDir } from "../../../../../test";
10-
import { getFunctionIdentifier } from "../get-function-identifier";
11-
import { isFunction } from "../is";
10+
import { getFunctionIdentifier } from "../ast-function-id";
11+
import { isFunction } from "../ast-is";
1212

1313
function parse(code: string) {
1414
return parseForESLint(code, {

packages/utilities/ast/src/__tests__/get-nested-return-statements.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { TSESTreeFunction } from "../types";
1+
import type { TSESTreeFunction } from "../ast-node";
22

33
import path from "node:path";
44
import { parseForESLint } from "@typescript-eslint/parser";
@@ -8,8 +8,8 @@ import tsx from "dedent";
88

99
import { describe, expect, it } from "vitest";
1010
import { getFixturesRootDir } from "../../../../../test";
11-
import { getNestedReturnStatements } from "../get-nested-return-statements";
12-
import { isFunction } from "../is";
11+
import { isFunction } from "../ast-is";
12+
import { getNestedReturnStatements } from "../ast-return-statement";
1313

1414
function parse(code: string) {
1515
return parseForESLint(code, {

packages/utilities/ast/src/is-array-from-call.ts renamed to packages/utilities/ast/src/ast-array-method.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,11 @@ export function isArrayFromCall(node: TSESTree.Node, loose = true): node is TSES
88
const name = node.callee.property.name;
99
return name === "from" || (loose && name.startsWith("from"));
1010
}
11+
12+
export function isArrayMapCall(node: TSESTree.Node, loose = true): node is TSESTree.CallExpression {
13+
if (node.type !== T.CallExpression) return false;
14+
if (node.callee.type !== T.MemberExpression) return false;
15+
if (node.callee.property.type !== T.Identifier) return false;
16+
const name = node.callee.property.name;
17+
return name === "map" || (loose && name.endsWith("Map"));
18+
}

packages/utilities/ast/src/get-class-identifier.ts renamed to packages/utilities/ast/src/ast-class-id.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { TSESTree } from "@typescript-eslint/types";
2-
import type { TSESTreeClass } from "./types";
2+
import type { TSESTreeClass } from "./ast-node";
33
import { _ } from "@eslint-react/eff";
44

55
import { AST_NODE_TYPES as T } from "@typescript-eslint/types";

packages/utilities/ast/src/get-nested-expressions.ts renamed to packages/utilities/ast/src/ast-expression.ts

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,27 @@
11
import type { TSESTree } from "@typescript-eslint/types";
2-
import { AST_NODE_TYPES as T } from "@typescript-eslint/typescript-estree";
3-
import { ASTUtils } from "@typescript-eslint/utils";
2+
import type { TSESTreeTypeExpression } from "./ast-node";
3+
4+
import { AST_NODE_TYPES as T } from "@typescript-eslint/types";
5+
import { is, isTypeExpression } from "./ast-is";
6+
7+
/**
8+
* Recursively get the inner expression until it's not a TypeExpression
9+
* @param node - The node to get the expression from
10+
* @returns The inner expression
11+
*/
12+
export function getJSExpression(node: TSESTree.Node): Exclude<
13+
TSESTree.Node,
14+
TSESTreeTypeExpression
15+
> {
16+
if (isTypeExpression(node)) {
17+
return getJSExpression(node.expression);
18+
}
19+
return node;
20+
}
21+
22+
export function isThisExpression(node: TSESTree.Expression) {
23+
return getJSExpression(node).type === T.ThisExpression;
24+
}
425

526
/**
627
* Get all nested expressions of type T in an expression like node
@@ -10,7 +31,7 @@ import { ASTUtils } from "@typescript-eslint/utils";
1031
*/
1132
// dprint-ignore
1233
export function getNestedExpressionsOfType<TNodeType extends T>(type: TNodeType): (node: TSESTree.Node) => Extract<TSESTree.Node, { type: TNodeType }>[] {
13-
const isNodeOfType = ASTUtils.isNodeOfType(type);
34+
const isNodeOfType = is(type);
1435
return function(node) {
1536
const boundGetNestedExpressionsOfType = getNestedExpressionsOfType(type);
1637
const expressions: Extract<TSESTree.Node, { type: TNodeType }>[] = [];

0 commit comments

Comments
 (0)