Skip to content

Commit 8635893

Browse files
committed
Clean up the code structure of the AST utils and fix an issue where
isNodeEqual did not handle TypeScript expressions correctly, closes #1340
1 parent b9ab289 commit 8635893

21 files changed

+91
-68
lines changed

packages/plugins/eslint-plugin-react-web-api/src/rules/no-leaked-event-listener.spec.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,39 @@ ruleTester.run(RULE_NAME, rule, {
689689
}, []);
690690
}
691691
`,
692+
tsx`
693+
function Example() {
694+
useEffect(() => {
695+
const handleResize = () => {};
696+
window.addEventListener("resize", handleResize as EventListener);
697+
return () => {
698+
window.removeEventListener("resize", handleResize as EventListener);
699+
};
700+
}, []);
701+
}
702+
`,
703+
tsx`
704+
function Example() {
705+
useEffect(() => {
706+
const handleResize = () => {};
707+
window.addEventListener("resize", handleResize as EventListener);
708+
return () => {
709+
window.removeEventListener("resize", handleResize);
710+
};
711+
}, []);
712+
}
713+
`,
714+
tsx`
715+
function Example() {
716+
useEffect(() => {
717+
const handleResize = () => {};
718+
window.addEventListener("resize", handleResize);
719+
return () => {
720+
window.removeEventListener("resize", handleResize as EventListener);
721+
};
722+
}, []);
723+
}
724+
`,
692725
tsx`
693726
function Example() {
694727
useEffect(() => {

packages/utilities/ast/src/array.ts

Lines changed: 0 additions & 2 deletions
This file was deleted.

packages/utilities/ast/src/class-id.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { unit } from "@eslint-react/eff";
22
import type { TSESTree } from "@typescript-eslint/types";
3-
import type { TSESTreeClass } from "./node";
4-
53
import { AST_NODE_TYPES as T } from "@typescript-eslint/types";
64

5+
import type { TSESTreeClass } from "./node-types";
6+
77
export function getClassId(node: TSESTreeClass): TSESTree.Identifier | unit {
88
if (node.id != null) return node.id;
99
if (node.parent.type === T.VariableDeclarator && node.parent.id.type === T.Identifier) {

packages/utilities/ast/src/expression-base.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import type { TSESTree } from "@typescript-eslint/types";
2-
import { type TSESTreeTypeExpression, isTypeExpression } from "./node";
2+
3+
import { isTypeExpression } from "./node-is";
4+
import type { TSESTreeTypeExpression } from "./node-types";
35

46
/**
57
* Unwraps any type expressions to get the underlying JavaScript expression node.

packages/utilities/ast/src/expression-is.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { TSESTree } from "@typescript-eslint/types";
2-
32
import { AST_NODE_TYPES as T } from "@typescript-eslint/types";
3+
44
import { getUnderlyingExpression } from "./expression-base";
55

66
/**

packages/utilities/ast/src/expression-nested.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import type { TSESTree } from "@typescript-eslint/types";
22
import { AST_NODE_TYPES as T } from "@typescript-eslint/types";
33
import { simpleTraverse } from "@typescript-eslint/typescript-estree";
4-
import { is, isFunction } from "./node";
4+
5+
import { is, isFunction } from "./node-is";
56
import { findParentNode } from "./traverse";
67

78
/**

packages/utilities/ast/src/expression.ts

Lines changed: 0 additions & 3 deletions
This file was deleted.

packages/utilities/ast/src/function-id.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { unit } from "@eslint-react/eff";
33
import type { TSESTree } from "@typescript-eslint/types";
44
import { AST_NODE_TYPES as T } from "@typescript-eslint/types";
55

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

99
// Ported from https://github.com/eps1lon/react/blob/8b8d265bd9a4cab7bbd04a9a13950fdc946ea51c/packages/eslint-plugin-react-hooks/src/RulesOfHooks.js#L642
1010
/**

packages/utilities/ast/src/function-init-path.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { unit } from "@eslint-react/eff";
22
import type { TSESTree } from "@typescript-eslint/types";
33
import { AST_NODE_TYPES as T } from "@typescript-eslint/types";
44

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

77
/**
88
* Represents various AST paths for React component function declarations.

packages/utilities/ast/src/function-is.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { AST_NODE_TYPES as T } from "@typescript-eslint/types";
22

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

55
export function isFunctionEmpty(node: TSESTreeFunction) {
66
return node.body.type === T.BlockStatement

0 commit comments

Comments
 (0)