Skip to content

Commit d85f9e6

Browse files
Rel1cxCopilot
andauthored
Replace getId with IdGenerator across collectors (#1305)
Signed-off-by: REL1CX <[email protected]> Co-authored-by: Copilot <[email protected]>
1 parent 67e7be0 commit d85f9e6

File tree

6 files changed

+83
-10
lines changed

6 files changed

+83
-10
lines changed

packages/core/src/component/component-collector-legacy.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ import { unit } from "@eslint-react/eff";
33
import type { ESLintUtils, TSESTree } from "@typescript-eslint/utils";
44
import type { ClassComponent } from "./component-semantic-node";
55

6-
import { getId } from "@eslint-react/shared";
6+
import { IdGenerator } from "@eslint-react/shared";
77
import { ComponentFlag } from "./component-flag";
88
import { isClassComponent, isPureComponent } from "./component-is";
99

10+
const idGen = new IdGenerator("class_component_");
11+
1012
export declare namespace useComponentCollectorLegacy {
1113
type ReturnType = {
1214
ctx: {
@@ -35,7 +37,7 @@ export function useComponentCollectorLegacy(): useComponentCollectorLegacy.Retur
3537
return;
3638
}
3739
const id = AST.getClassId(node);
38-
const key = getId();
40+
const key = idGen.next();
3941
const flag = isPureComponent(node)
4042
? ComponentFlag.PureComponent
4143
: ComponentFlag.None;

packages/core/src/component/component-collector.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as AST from "@eslint-react/ast";
22
import { unit } from "@eslint-react/eff";
33
import { type RuleContext } from "@eslint-react/shared";
4-
import { getId } from "@eslint-react/shared";
4+
import { IdGenerator } from "@eslint-react/shared";
55
import type { TSESTree } from "@typescript-eslint/types";
66
import { AST_NODE_TYPES as T } from "@typescript-eslint/types";
77
import type { ESLintUtils } from "@typescript-eslint/utils";
@@ -16,6 +16,8 @@ import { getFunctionComponentId } from "./component-id";
1616
import { getComponentFlagFromInitPath } from "./component-init-path";
1717
import { getComponentNameFromId, hasNoneOrLooseComponentName } from "./component-name";
1818

19+
const idGen = new IdGenerator("function_component_");
20+
1921
type FunctionEntry = {
2022
key: string;
2123
node: AST.TSESTreeFunction;
@@ -60,7 +62,7 @@ export function useComponentCollector(
6062

6163
const getCurrentEntry = () => functionEntries.at(-1);
6264
const onFunctionEnter = (node: AST.TSESTreeFunction) => {
63-
const key = getId();
65+
const key = idGen.next();
6466
functionEntries.push({ key, node, hookCalls: [], isComponent: false });
6567
};
6668
const onFunctionExit = () => {
@@ -106,8 +108,8 @@ export function useComponentCollector(
106108
if (!isComponent) return;
107109
const initPath = AST.getFunctionInitPath(entry.node);
108110
const id = getFunctionComponentId(context, entry.node);
111+
const key = entry.key;
109112
const name = getComponentNameFromId(id);
110-
const key = getId();
111113
components.set(key, {
112114
id,
113115
key,

packages/core/src/hook/hook-collector.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ import * as AST from "@eslint-react/ast";
22
import type { ESLintUtils, TSESTree } from "@typescript-eslint/utils";
33
import type { Hook } from "./hook-semantic-node";
44

5-
import { getId } from "@eslint-react/shared";
5+
import { IdGenerator } from "@eslint-react/shared";
66
import { isReactHookCall } from "./hook-is";
77
import { isReactHookName } from "./hook-name";
88

9+
const idGen = new IdGenerator("hook_");
10+
911
type FunctionEntry = {
1012
key: string;
1113
node: AST.TSESTreeFunction;
@@ -26,7 +28,7 @@ export function useHookCollector(): useHookCollector.ReturnType {
2628
const functionEntries: FunctionEntry[] = [];
2729
const onFunctionEnter = (node: AST.TSESTreeFunction) => {
2830
const id = AST.getFunctionId(node);
29-
const key = getId();
31+
const key = idGen.next();
3032
const name = id?.name;
3133
if (name != null && isReactHookName(name)) {
3234
functionEntries.push({ key, node, isHook: true });

packages/shared/docs/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
# @eslint-react/shared
66

7+
## Classes
8+
9+
- [IdGenerator](classes/IdGenerator.md)
10+
711
## Interfaces
812

913
- [CompatibleConfig](interfaces/CompatibleConfig.md)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
[**@eslint-react/shared**](../README.md)
2+
3+
***
4+
5+
[@eslint-react/shared](../README.md) / IdGenerator
6+
7+
# Class: IdGenerator
8+
9+
A generator for unique ids.
10+
11+
## Constructors
12+
13+
### Constructor
14+
15+
> **new IdGenerator**(`prefix`): `IdGenerator`
16+
17+
#### Parameters
18+
19+
##### prefix
20+
21+
`string` = `"id_"`
22+
23+
Optional. A prefix of generated ids.
24+
25+
#### Returns
26+
27+
`IdGenerator`
28+
29+
## Methods
30+
31+
### next()
32+
33+
> **next**(): `string`
34+
35+
Generates id.
36+
37+
#### Returns
38+
39+
`string`
40+
41+
A generated id.

packages/shared/src/_id.ts

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,28 @@
1-
let id = 0n;
1+
// ------------------------------------------------------------------------------
2+
// Public Interface
3+
// ------------------------------------------------------------------------------
24

35
/**
4-
* @internal
6+
* A generator for unique ids.
57
*/
6-
export const getId = () => (id++).toString();
8+
class IdGenerator {
9+
private n: bigint;
10+
private prefix: string;
11+
/**
12+
* @param prefix Optional. A prefix of generated ids.
13+
*/
14+
constructor(prefix: string = "id_") {
15+
this.prefix = prefix;
16+
this.n = 0n;
17+
}
18+
/**
19+
* Generates an id.
20+
* @returns A generated id.
21+
*/
22+
next() {
23+
this.n = 1n + this.n;
24+
return this.prefix + this.n.toString(16);
25+
}
26+
}
27+
28+
export { IdGenerator };

0 commit comments

Comments
 (0)