Skip to content

Commit 5cfd0b0

Browse files
committed
refactor: getFunctionIdentifier add function in class support
1 parent 29015a1 commit 5cfd0b0

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,21 @@
22

33
### Release Notes
44

5+
#### Reduce false positives in rule `react/no-unstable-nested-components`
6+
7+
#### Reduce false positives in rule `debug/function-component`
8+
59
---
610

11+
#### 🏠 Internal
12+
13+
- `@eslint-react/ast`
14+
- Refactor `getFunctionIdentifier` function to follow spec convention for `IsAnonymousFunctionDefinition()` usage in ECMAScript spec.
15+
- Improve module structure.
16+
17+
- `@eslint-react/core`
18+
- Move construction detection from `@eslint-react/ast` to `@eslint-react/core`.
19+
720
#### Authors: 1
821

922
- Eva1ent ([@Rel1cx](https://github.com/Rel1cx))

packages/ast/src/function/function-identifier.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import { O } from "@eslint-react/tools";
1111
import type { TSESTree } from "@typescript-eslint/types";
1212

13-
import { NodeType, type TSESTreeFunction } from "../node";
13+
import { isOneOf, NodeType, type TSESTreeFunction } from "../node";
1414

1515
export function getFunctionIdentifier(node: TSESTreeFunction): O.Option<TSESTree.Identifier> {
1616
if (node.id) {
@@ -47,13 +47,16 @@ export function getFunctionIdentifier(node: TSESTreeFunction): O.Option<TSESTree
4747
// {useHook: () => {}}
4848
// {useHook() {}}
4949
return O.some(node.parent.key);
50+
}
5051

51-
// NOTE: We could also support `ClassProperty` and `MethodDefinition`
52-
// here to be pedantic. However, hooks in a class are an anti-pattern. So
53-
// we don't allow it to error early.
54-
//
52+
if (
53+
isOneOf([NodeType.MethodDefinition, NodeType.PropertyDefinition])(node.parent)
54+
&& node.parent.value === node
55+
&& node.parent.key.type === NodeType.Identifier
56+
) {
5557
// class {useHook = () => {}}
5658
// class {useHook() {}}
59+
return O.some(node.parent.key);
5760
}
5861

5962
if (

0 commit comments

Comments
 (0)