Skip to content

Commit 822d8b0

Browse files
SukkaWRel1cx
andauthored
refactor: replace short-unique-id w/ uid (#894)
Co-authored-by: rEl1cx <[email protected]>
1 parent 0f34ea3 commit 822d8b0

File tree

5 files changed

+26
-18
lines changed

5 files changed

+26
-18
lines changed

packages/core/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@
5050
"@typescript-eslint/type-utils": "^8.19.0",
5151
"@typescript-eslint/types": "^8.19.0",
5252
"@typescript-eslint/utils": "^8.19.0",
53-
"short-unique-id": "^5.2.0",
54-
"ts-pattern": "^5.6.0"
53+
"ts-pattern": "^5.6.0",
54+
"uid": "^2.0.2"
5555
},
5656
"devDependencies": {
5757
"@workspace/configs": "workspace:*",

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@ import * as AST from "@eslint-react/ast";
22
import { O } from "@eslint-react/eff";
33
import { AST_NODE_TYPES } from "@typescript-eslint/types";
44
import type { ESLintUtils, TSESTree } from "@typescript-eslint/utils";
5-
import ShortUniqueId from "short-unique-id";
65
import { match, P } from "ts-pattern";
6+
import { uid } from "uid";
77

88
import type { ERClassComponent } from "./component";
99
import { ERClassComponentFlag } from "./component-flag";
1010

11-
const uid = new ShortUniqueId({ length: 10 });
12-
1311
/**
1412
* Check if a node is a React class component
1513
* @param node The AST node to check
@@ -94,7 +92,7 @@ export function useComponentCollectorLegacy() {
9492
const collect = (node: AST.TSESTreeClass) => {
9593
if (!isClassComponent(node)) return;
9694
const id = AST.getClassIdentifier(node);
97-
const key = uid.rnd();
95+
const key = uid(10);
9896
const flag = isPureComponent(node)
9997
? ERClassComponentFlag.PureComponent
10098
: ERClassComponentFlag.None;

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import type { RuleContext } from "@eslint-react/types";
55
import type { TSESTree } from "@typescript-eslint/types";
66
import { AST_NODE_TYPES } from "@typescript-eslint/types";
77
import type { ESLintUtils } from "@typescript-eslint/utils";
8-
import ShortUniqueId from "short-unique-id";
98
import { match } from "ts-pattern";
9+
import { uid } from "uid";
1010

1111
import { isChildrenOfCreateElement } from "../element";
1212
import { isReactHookCall } from "../hook";
@@ -18,8 +18,6 @@ import { getComponentNameFromIdentifier } from "./component-name";
1818
import { isFunctionOfRenderMethod } from "./component-render-method";
1919
import { hasNoneOrValidComponentName } from "./misc";
2020

21-
const uid = new ShortUniqueId({ length: 10 });
22-
2321
function hasValidHierarchy(node: AST.TSESTreeFunction, context: RuleContext, hint: bigint) {
2422
if (isChildrenOfCreateElement(node, context) || isFunctionOfRenderMethod(node)) {
2523
return false;
@@ -75,7 +73,7 @@ export function useComponentCollector(
7573
hookCalls: TSESTree.CallExpression[],
7674
][] = [];
7775
const getCurrentFunction = () => O.fromNullable(functionStack.at(-1));
78-
const onFunctionEnter = (node: AST.TSESTreeFunction) => functionStack.push([uid.rnd(), node, false, []]);
76+
const onFunctionEnter = (node: AST.TSESTreeFunction) => functionStack.push([uid(10), node, false, []]);
7977
const onFunctionExit = () => {
8078
const [key, fn, isComponent] = functionStack.at(-1) ?? [];
8179
if (!key || !fn || !isComponent) return functionStack.pop();
@@ -119,7 +117,7 @@ export function useComponentCollector(
119117
const initPath = AST.getFunctionInitPath(currentFn);
120118
const id = getFunctionComponentIdentifier(currentFn, context);
121119
const name = O.flatMapNullable(id, getComponentNameFromIdentifier);
122-
const key = uid.rnd();
120+
const key = uid(10);
123121
components.set(key, {
124122
_: key,
125123
id,

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
11
import * as AST from "@eslint-react/ast";
22
import { F, O } from "@eslint-react/eff";
33
import type { ESLintUtils, TSESTree } from "@typescript-eslint/utils";
4-
import ShortUniqueId from "short-unique-id";
4+
import { uid } from "uid";
55

66
import type { ERHook } from "./hook";
77
import { isReactHookName } from "./hook-name";
88
import { isReactHookCall } from "./is";
99

10-
const uid = new ShortUniqueId({ length: 10 });
11-
1210
export function useHookCollector() {
1311
const hooks = new Map<string, ERHook>();
1412
const fStack: [node: AST.TSESTreeFunction, id: O.Option<string>][] = [];
1513
const onFunctionEnter = (node: AST.TSESTreeFunction) => {
1614
const id = AST.getFunctionIdentifier(node);
1715
const name = O.flatMapNullable(id, (id) => id.name);
1816
if (O.isSome(id) && O.isSome(name) && isReactHookName(name.value)) {
19-
const key = uid.rnd();
17+
const key = uid(10);
2018
fStack.push([node, O.some(key)]);
2119
hooks.set(key, {
2220
_: key,

pnpm-lock.yaml

Lines changed: 17 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)