Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions packages/core/src/component/component-collector-legacy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import { unit } from "@eslint-react/eff";
import type { ESLintUtils, TSESTree } from "@typescript-eslint/utils";
import type { ClassComponent } from "./component-semantic-node";

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

const idGen = new IdGenerator("class_component_");

export declare namespace useComponentCollectorLegacy {
type ReturnType = {
ctx: {
Expand Down Expand Up @@ -35,7 +37,7 @@ export function useComponentCollectorLegacy(): useComponentCollectorLegacy.Retur
return;
}
const id = AST.getClassId(node);
const key = getId();
const key = idGen.next();
const flag = isPureComponent(node)
? ComponentFlag.PureComponent
: ComponentFlag.None;
Expand Down
8 changes: 5 additions & 3 deletions packages/core/src/component/component-collector.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as AST from "@eslint-react/ast";
import { unit } from "@eslint-react/eff";
import { type RuleContext } from "@eslint-react/shared";
import { getId } from "@eslint-react/shared";
import { IdGenerator } from "@eslint-react/shared";
import type { TSESTree } from "@typescript-eslint/types";
import { AST_NODE_TYPES as T } from "@typescript-eslint/types";
import type { ESLintUtils } from "@typescript-eslint/utils";
Expand All @@ -16,6 +16,8 @@ import { getFunctionComponentId } from "./component-id";
import { getComponentFlagFromInitPath } from "./component-init-path";
import { getComponentNameFromId, hasNoneOrLooseComponentName } from "./component-name";

const idGen = new IdGenerator("function_component_");

type FunctionEntry = {
key: string;
node: AST.TSESTreeFunction;
Expand Down Expand Up @@ -60,7 +62,7 @@ export function useComponentCollector(

const getCurrentEntry = () => functionEntries.at(-1);
const onFunctionEnter = (node: AST.TSESTreeFunction) => {
const key = getId();
const key = idGen.next();
functionEntries.push({ key, node, hookCalls: [], isComponent: false });
};
const onFunctionExit = () => {
Expand Down Expand Up @@ -106,8 +108,8 @@ export function useComponentCollector(
if (!isComponent) return;
const initPath = AST.getFunctionInitPath(entry.node);
const id = getFunctionComponentId(context, entry.node);
const key = entry.key;
const name = getComponentNameFromId(id);
const key = getId();
components.set(key, {
id,
key,
Expand Down
6 changes: 4 additions & 2 deletions packages/core/src/hook/hook-collector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import * as AST from "@eslint-react/ast";
import type { ESLintUtils, TSESTree } from "@typescript-eslint/utils";
import type { Hook } from "./hook-semantic-node";

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

const idGen = new IdGenerator("hook_");

type FunctionEntry = {
key: string;
node: AST.TSESTreeFunction;
Expand All @@ -26,7 +28,7 @@ export function useHookCollector(): useHookCollector.ReturnType {
const functionEntries: FunctionEntry[] = [];
const onFunctionEnter = (node: AST.TSESTreeFunction) => {
const id = AST.getFunctionId(node);
const key = getId();
const key = idGen.next();
const name = id?.name;
if (name != null && isReactHookName(name)) {
functionEntries.push({ key, node, isHook: true });
Expand Down
4 changes: 4 additions & 0 deletions packages/shared/docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

# @eslint-react/shared

## Classes

- [IdGenerator](classes/IdGenerator.md)

## Interfaces

- [CompatibleConfig](interfaces/CompatibleConfig.md)
Expand Down
41 changes: 41 additions & 0 deletions packages/shared/docs/classes/IdGenerator.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
[**@eslint-react/shared**](../README.md)

***

[@eslint-react/shared](../README.md) / IdGenerator

# Class: IdGenerator

A generator for unique ids.

## Constructors

### Constructor

> **new IdGenerator**(`prefix`): `IdGenerator`

#### Parameters

##### prefix

`string` = `"id_"`

Optional. A prefix of generated ids.

#### Returns

`IdGenerator`

## Methods

### next()

> **next**(): `string`

Generates id.

#### Returns

`string`

A generated id.
28 changes: 25 additions & 3 deletions packages/shared/src/_id.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,28 @@
let id = 0n;
// ------------------------------------------------------------------------------
// Public Interface
// ------------------------------------------------------------------------------

/**
* @internal
* A generator for unique ids.
*/
export const getId = () => (id++).toString();
class IdGenerator {
private n: bigint;
private prefix: string;
/**
* @param prefix Optional. A prefix of generated ids.
*/
constructor(prefix: string = "id_") {
this.prefix = prefix;
this.n = 0n;
}
/**
* Generates an id.
* @returns A generated id.
*/
next() {
this.n = 1n + this.n;
return this.prefix + this.n.toString(16);
}
}

export { IdGenerator };