Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,39 @@ ruleTester.run(RULE_NAME, rule, {
}, []);
}
`,
tsx`
function Example() {
useEffect(() => {
const handleResize = () => {};
window.addEventListener("resize", handleResize as EventListener);
return () => {
window.removeEventListener("resize", handleResize as EventListener);
};
}, []);
}
`,
tsx`
function Example() {
useEffect(() => {
const handleResize = () => {};
window.addEventListener("resize", handleResize as EventListener);
return () => {
window.removeEventListener("resize", handleResize);
};
}, []);
}
`,
tsx`
function Example() {
useEffect(() => {
const handleResize = () => {};
window.addEventListener("resize", handleResize);
return () => {
window.removeEventListener("resize", handleResize as EventListener);
};
}, []);
}
`,
tsx`
function Example() {
useEffect(() => {
Expand Down
2 changes: 1 addition & 1 deletion packages/utilities/ast/src/__tests__/class.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { describe, expect, it } from "vitest";
import { getFixturesRootDir } from "../../../../../test";

import { getClassId } from "../class-id";
import type { TSESTreeClass } from "../node";
import type { TSESTreeClass } from "../node-types";

function parse(code: string) {
return parseForESLint(code, {
Expand Down
6 changes: 4 additions & 2 deletions packages/utilities/ast/src/__tests__/expression.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import path from "node:path";
import { describe, expect, it } from "vitest";

import { getFixturesRootDir } from "../../../../../test";
import { getNestedReturnStatements } from "../expression";
import { type TSESTreeFunction, isFunction } from "../node";

import { getNestedReturnStatements } from "../expression-nested";
import { isFunction } from "../node-is";
import type { TSESTreeFunction } from "../node-types";

function parse(code: string) {
return parseForESLint(code, {
Expand Down
3 changes: 2 additions & 1 deletion packages/utilities/ast/src/__tests__/function.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import { describe, expect, it } from "vitest";
import { getFixturesRootDir } from "../../../../../test";

import { getFunctionId } from "../function-id";
import { type TSESTreeFunction, isFunction } from "../node";
import { isFunction } from "../node-is";
import type { TSESTreeFunction } from "../node-types";

function parse(code: string) {
return parseForESLint(code, {
Expand Down
2 changes: 0 additions & 2 deletions packages/utilities/ast/src/array.ts

This file was deleted.

4 changes: 2 additions & 2 deletions packages/utilities/ast/src/class-id.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { unit } from "@eslint-react/eff";
import type { TSESTree } from "@typescript-eslint/types";
import type { TSESTreeClass } from "./node";

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

import type { TSESTreeClass } from "./node-types";

export function getClassId(node: TSESTreeClass): TSESTree.Identifier | unit {
if (node.id != null) return node.id;
if (node.parent.type === T.VariableDeclarator && node.parent.id.type === T.Identifier) {
Expand Down
4 changes: 3 additions & 1 deletion packages/utilities/ast/src/expression-base.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { TSESTree } from "@typescript-eslint/types";
import { type TSESTreeTypeExpression, isTypeExpression } from "./node";

import { isTypeExpression } from "./node-is";
import type { TSESTreeTypeExpression } from "./node-types";

/**
* Unwraps any type expressions to get the underlying JavaScript expression node.
Expand Down
2 changes: 1 addition & 1 deletion packages/utilities/ast/src/expression-is.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { TSESTree } from "@typescript-eslint/types";

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

import { getUnderlyingExpression } from "./expression-base";

/**
Expand Down
3 changes: 2 additions & 1 deletion packages/utilities/ast/src/expression-nested.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { TSESTree } from "@typescript-eslint/types";
import { AST_NODE_TYPES as T } from "@typescript-eslint/types";
import { simpleTraverse } from "@typescript-eslint/typescript-estree";
import { is, isFunction } from "./node";

import { is, isFunction } from "./node-is";
import { findParentNode } from "./traverse";

/**
Expand Down
3 changes: 0 additions & 3 deletions packages/utilities/ast/src/expression.ts

This file was deleted.

4 changes: 2 additions & 2 deletions packages/utilities/ast/src/function-id.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { unit } from "@eslint-react/eff";
import type { TSESTree } from "@typescript-eslint/types";
import { AST_NODE_TYPES as T } from "@typescript-eslint/types";

import type { TSESTreeFunction } from "./node";
import { isMethodOrProperty, isTypeAssertionExpression } from "./node";
import { isMethodOrProperty, isTypeAssertionExpression } from "./node-is";
import type { TSESTreeFunction } from "./node-types";

// Ported from https://github.com/eps1lon/react/blob/8b8d265bd9a4cab7bbd04a9a13950fdc946ea51c/packages/eslint-plugin-react-hooks/src/RulesOfHooks.js#L642
/**
Expand Down
2 changes: 1 addition & 1 deletion packages/utilities/ast/src/function-init-path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { unit } from "@eslint-react/eff";
import type { TSESTree } from "@typescript-eslint/types";
import { AST_NODE_TYPES as T } from "@typescript-eslint/types";

import type { TSESTreeFunction } from "./node";
import type { TSESTreeFunction } from "./node-types";

/**
* Represents various AST paths for React component function declarations.
Expand Down
2 changes: 1 addition & 1 deletion packages/utilities/ast/src/function-is.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AST_NODE_TYPES as T } from "@typescript-eslint/types";

import type { TSESTreeFunction } from "./node";
import type { TSESTreeFunction } from "./node-types";

export function isFunctionEmpty(node: TSESTreeFunction) {
return node.body.type === T.BlockStatement
Expand Down
3 changes: 0 additions & 3 deletions packages/utilities/ast/src/function.ts

This file was deleted.

6 changes: 0 additions & 6 deletions packages/utilities/ast/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
export * from "./array";
export * from "./array-index";
export * from "./array-method";
export * from "./class-id";
export * from "./expression";
export * from "./expression-base";
export * from "./expression-is";
export * from "./expression-nested";
export * from "./function";
export * from "./function-id";
export * from "./function-init-path";
export * from "./function-is";
export * from "./literal";
export * from "./misc";
export * from "./node";
export * from "./node-equal";
export * from "./node-format";
export * from "./node-is";
Expand All @@ -22,6 +18,4 @@ export * from "./process-env-node-env";
export * from "./promise-then";
export * from "./property-name";
export * from "./traverse";
export type * from "./traverse-down";
export * from "./traverse-up";
export * from "./vitest-mock";
2 changes: 1 addition & 1 deletion packages/utilities/ast/src/misc.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { TSESTree } from "@typescript-eslint/types";
import { AST_NODE_TYPES as T } from "@typescript-eslint/types";

import { isOneOf } from "./node";
import { isOneOf } from "./node-is";

/**
* Check if a node is multiline
Expand Down
5 changes: 5 additions & 0 deletions packages/utilities/ast/src/node-equal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { dual } from "@eslint-react/eff";
import type { TSESTree } from "@typescript-eslint/types";
import { AST_NODE_TYPES as T } from "@typescript-eslint/types";

import { getUnderlyingExpression } from "./expression-base";
import { isTypeExpression } from "./node-is";

/**
* Determines whether node equals to another node
* @param a node to compare
Expand All @@ -13,6 +16,8 @@ export const isNodeEqual: {
(a: TSESTree.Node): (b: TSESTree.Node) => boolean;
(a: TSESTree.Node, b: TSESTree.Node): boolean;
} = dual(2, (a: TSESTree.Node, b: TSESTree.Node): boolean => {
a = isTypeExpression(a) ? getUnderlyingExpression(a) : a;
b = isTypeExpression(b) ? getUnderlyingExpression(b) : b;
switch (true) {
case a === b:
return true;
Expand Down
4 changes: 0 additions & 4 deletions packages/utilities/ast/src/node.ts

This file was deleted.

1 change: 1 addition & 0 deletions packages/utilities/ast/src/process-env-node-env.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { unit } from "@eslint-react/eff";
import { AST_NODE_TYPES as T, type TSESTree } from "@typescript-eslint/types";

import { isLiteral } from "./literal";

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/utilities/ast/src/property-name.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { unit } from "@eslint-react/eff";
import { AST_NODE_TYPES as T, type TSESTree } from "@typescript-eslint/types";

import { getUnderlyingExpression } from "./expression";
import { isTypeExpression } from "./node";
import { getUnderlyingExpression } from "./expression-base";
import { isTypeExpression } from "./node-is";

export function getPropertyName(node: TSESTree.Node): string | unit {
if (isTypeExpression(node)) {
Expand Down
Empty file.
36 changes: 0 additions & 36 deletions packages/utilities/ast/src/traverse-up.ts

This file was deleted.

38 changes: 36 additions & 2 deletions packages/utilities/ast/src/traverse.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,36 @@
export type * from "./traverse-down";
export * from "./traverse-up";
import { unit } from "@eslint-react/eff";
import { AST_NODE_TYPES as T, type TSESTree } from "@typescript-eslint/types";

/**
* Find the parent node that satisfies the test function
* @param node The AST node
* @param test The test function
* @returns The parent node that satisfies the test function or `_` if not found
*/
function findParentNode<A extends TSESTree.Node>(
node: TSESTree.Node | unit,
test: (n: TSESTree.Node) => n is A,
): A | unit;
/**
* Find the parent node that satisfies the test function or `_` if not found
* @param node The AST node
* @param test The test function
* @returns The parent node that satisfies the test function
*/
function findParentNode(node: TSESTree.Node | unit, test: (node: TSESTree.Node) => boolean): TSESTree.Node | unit;
function findParentNode<A extends TSESTree.Node>(
node: TSESTree.Node | unit,
test: ((node: TSESTree.Node) => boolean) | ((n: TSESTree.Node) => n is A),
): TSESTree.Node | A | unit {
if (node == null) return unit;
let parent = node.parent;
while (parent != null && parent.type !== T.Program) {
if (test(parent)) {
return parent;
}
parent = parent.parent;
}
return unit;
}

export { findParentNode };
1 change: 1 addition & 0 deletions packages/utilities/ast/src/vitest-mock.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { unit } from "@eslint-react/eff";
import { AST_NODE_TYPES as T, type TSESTree } from "@typescript-eslint/types";

import { isFunction } from "./node-is";

/**
Expand Down
Loading