Skip to content

Commit

Permalink
refactor(core): replace 'uid' dependency with native bigint id
Browse files Browse the repository at this point in the history
  • Loading branch information
Rel1cx committed Jan 1, 2025
1 parent 7c4f2af commit c0d476d
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 15 deletions.
4 changes: 4 additions & 0 deletions packages/core/docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

# @eslint-react/core

## Namespaces

- [uid](namespaces/uid/README.md)

## Interfaces

- [ERClassComponent](interfaces/ERClassComponent.md)
Expand Down
4 changes: 2 additions & 2 deletions packages/core/docs/functions/useComponentCollector.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
#### listeners.:function\[type\]()

> `readonly` **:function\[type\]**: (`node`) => `number` = `onFunctionEnter`
> `readonly` **:function\[type\]**: (`node`) => `void` = `onFunctionEnter`
##### Parameters

Expand All @@ -74,7 +74,7 @@

##### Returns

`number`
`void`

#### listeners.:function\[type\]:exit()

Expand Down
11 changes: 11 additions & 0 deletions packages/core/docs/namespaces/uid/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[**@eslint-react/core**](../../README.md)

***

[@eslint-react/core](../../README.md) / uid

# uid

## Functions

- [next](functions/next.md)
13 changes: 13 additions & 0 deletions packages/core/docs/namespaces/uid/functions/next.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[**@eslint-react/core**](../../../README.md)

***

[@eslint-react/core](../../../README.md) / [uid](../README.md) / next

# Function: next()

> **next**(): `number`
## Returns

`number`
3 changes: 1 addition & 2 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@
"@typescript-eslint/type-utils": "^8.19.0",
"@typescript-eslint/types": "^8.19.0",
"@typescript-eslint/utils": "^8.19.0",
"ts-pattern": "^5.6.0",
"uid": "^2.0.2"
"ts-pattern": "^5.6.0"
},
"devDependencies": {
"@workspace/configs": "workspace:*",
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/component/component-collector-legacy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { O } from "@eslint-react/eff";
import { AST_NODE_TYPES } from "@typescript-eslint/types";
import type { ESLintUtils, TSESTree } from "@typescript-eslint/utils";
import { match, P } from "ts-pattern";
import { uid } from "uid";

import { uid } from "../utils";
import type { ERClassComponent } from "./component";
import { ERClassComponentFlag } from "./component-flag";

Expand Down Expand Up @@ -92,7 +92,7 @@ export function useComponentCollectorLegacy() {
const collect = (node: AST.TSESTreeClass) => {
if (!isClassComponent(node)) return;
const id = AST.getClassIdentifier(node);
const key = uid(10);
const key = uid.next().toString();
const flag = isPureComponent(node)
? ERClassComponentFlag.PureComponent
: ERClassComponentFlag.None;
Expand Down
11 changes: 7 additions & 4 deletions packages/core/src/component/component-collector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import type { TSESTree } from "@typescript-eslint/types";
import { AST_NODE_TYPES } from "@typescript-eslint/types";
import type { ESLintUtils } from "@typescript-eslint/utils";
import { match } from "ts-pattern";
import { uid } from "uid";

import { isChildrenOfCreateElement } from "../element";
import { isReactHookCall } from "../hook";
import { uid } from "../utils";
import type { ERFunctionComponent } from "./component";
import { DEFAULT_COMPONENT_HINT, ERComponentHint } from "./component-collector-hint";
import { ERFunctionComponentFlag } from "./component-flag";
Expand Down Expand Up @@ -62,7 +62,7 @@ function getComponentFlag(initPath: ERFunctionComponent["initPath"]) {

export function useComponentCollector(
context: RuleContext,
hint: bigint = DEFAULT_COMPONENT_HINT,
hint = DEFAULT_COMPONENT_HINT,
) {
const jsxCtx = { getScope: (node: TSESTree.Node) => context.sourceCode.getScope(node) } as const;
const components = new Map<string, ERFunctionComponent>();
Expand All @@ -73,7 +73,10 @@ export function useComponentCollector(
hookCalls: TSESTree.CallExpression[],
][] = [];
const getCurrentFunction = () => O.fromNullable(functionStack.at(-1));
const onFunctionEnter = (node: AST.TSESTreeFunction) => functionStack.push([uid(10), node, false, []]);
const onFunctionEnter = (node: AST.TSESTreeFunction) => {
const key = uid.next().toString();
functionStack.push([key, node, false, []]);
};
const onFunctionExit = () => {
const [key, fn, isComponent] = functionStack.at(-1) ?? [];
if (!key || !fn || !isComponent) return functionStack.pop();
Expand Down Expand Up @@ -117,7 +120,7 @@ export function useComponentCollector(
const initPath = AST.getFunctionInitPath(currentFn);
const id = getFunctionComponentIdentifier(currentFn, context);
const name = O.flatMapNullable(id, getComponentNameFromIdentifier);
const key = uid(10);
const key = uid.next().toString();
components.set(key, {
_: key,
id,
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/hook/hook-collector.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as AST from "@eslint-react/ast";
import { F, O } from "@eslint-react/eff";
import type { ESLintUtils, TSESTree } from "@typescript-eslint/utils";
import { uid } from "uid";

import { uid } from "../utils";
import type { ERHook } from "./hook";
import { isReactHookName } from "./hook-name";
import { isReactHookCall } from "./is";
Expand All @@ -14,7 +14,7 @@ export function useHookCollector() {
const id = AST.getFunctionIdentifier(node);
const name = O.flatMapNullable(id, (id) => id.name);
if (O.isSome(id) && O.isSome(name) && isReactHookName(name.value)) {
const key = uid(10);
const key = uid.next().toString();
fStack.push([node, O.some(key)]);
hooks.set(key, {
_: key,
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./is-from-react";
export * from "./is-initialized-from-react";
export * from "./is-react-api";
export * as uid from "./uid";
5 changes: 5 additions & 0 deletions packages/core/src/utils/uid.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
let id = 0;

export function next() {
return id++;
}
3 changes: 0 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c0d476d

Please sign in to comment.