Skip to content

Commit 41f3935

Browse files
committed
refactor: remove the Chunk module because it's actually slower than MutList and Array
1 parent 3a43b90 commit 41f3935

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

98 files changed

+115
-8471
lines changed

packages/plugins/eslint-plugin-react-hooks-extra/src/rules/no-direct-set-state-in-use-effect.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { TSESTreeFunction } from "@eslint-react/ast";
22
import { getNestedIdentifiers, is, isFunction, isIIFE, NodeType } from "@eslint-react/ast";
33
import { isReactHookCallWithNameLoose, isUseEffectCall, isUseStateCall } from "@eslint-react/core";
44
import { parseESLintSettings } from "@eslint-react/shared";
5-
import { Chunk, F, MutList, MutRef, O } from "@eslint-react/tools";
5+
import { F, MutList, MutRef, O } from "@eslint-react/tools";
66
import { findVariable, getVariableNode } from "@eslint-react/var";
77
import type { ESLintUtils, TSESTree } from "@typescript-eslint/utils";
88
import { getStaticValue } from "@typescript-eslint/utils/ast-utils";
@@ -32,10 +32,11 @@ export default createRule<[], MessageID>({
3232
},
3333
name: RULE_NAME,
3434
create(context) {
35-
const settings = parseESLintSettings(context.settings)["react-x"];
36-
const { useEffect: useEffectAlias = [], useState: useStateAlias = [] } = settings?.additionalHooks ?? {};
35+
const settings = parseESLintSettings(context.settings)["react-x"] ?? {};
36+
const { useEffect: useEffectAlias = [], useState: useStateAlias = [] } = settings.additionalHooks ?? {};
3737
function isUseEffectCallWithAlias(node: TSESTree.CallExpression) {
38-
return isUseEffectCall(node, context) || useEffectAlias.some(F.flip(isReactHookCallWithNameLoose)(node));
38+
return isUseEffectCall(node, context)
39+
|| useEffectAlias.some(F.flip(isReactHookCallWithNameLoose)(node));
3940
}
4041
function isUseStateCallWithAlias(node: TSESTree.CallExpression) {
4142
return isUseStateCall(node, context) || useStateAlias.some(F.flip(isReactHookCallWithNameLoose)(node));
@@ -124,9 +125,9 @@ export default createRule<[], MessageID>({
124125
}
125126
const functionStack = MutList.make<[node: TSESTreeFunction, kind: FunctionKind]>();
126127
const effectFunctionRef = MutRef.make<TSESTreeFunction | null>(null);
127-
const effectFunctionIdentifiers = MutRef.make(Chunk.empty<TSESTree.Identifier>());
128-
const indirectFunctionCalls = MutRef.make(Chunk.empty<TSESTree.CallExpression>());
129-
const indirectSetStateCalls = new WeakMap<TSESTreeFunction, Chunk.Chunk<TSESTree.CallExpression>>();
128+
const effectFunctionIdentifiers: TSESTree.Identifier[] = [];
129+
const indirectFunctionCalls: TSESTree.CallExpression[] = [];
130+
const indirectSetStateCalls = new WeakMap<TSESTreeFunction, TSESTree.CallExpression[]>();
130131
const onEffectFunctionEnter = (node: TSESTreeFunction) => {
131132
MutRef.set(effectFunctionRef, node);
132133
};
@@ -156,8 +157,8 @@ export default createRule<[], MessageID>({
156157
.with("setState", () => {
157158
if (!parentFn) return;
158159
if (parentFn !== effectFn && parentFnKind !== "immediate") {
159-
const calls = indirectSetStateCalls.get(parentFn) ?? Chunk.empty<TSESTree.CallExpression>();
160-
indirectSetStateCalls.set(parentFn, Chunk.append(calls, node));
160+
const calls = indirectSetStateCalls.get(parentFn) ?? [];
161+
indirectSetStateCalls.set(parentFn, [...calls, node]);
161162
return;
162163
}
163164
context.report({
@@ -168,12 +169,12 @@ export default createRule<[], MessageID>({
168169
.with("useEffect", () => {
169170
if (node.arguments.every(isFunction)) return;
170171
const identifiers = getNestedIdentifiers(node);
171-
MutRef.update(effectFunctionIdentifiers, Chunk.appendAll(Chunk.unsafeFromArray(identifiers)));
172+
effectFunctionIdentifiers.push(...identifiers);
172173
})
173174
.with("other", () => {
174175
const isInEffectFunction = effectFn === parentFn;
175176
if (!isInEffectFunction) return;
176-
MutRef.update(indirectFunctionCalls, Chunk.append(node));
177+
indirectFunctionCalls.push(node);
177178
})
178179
.otherwise(F.constVoid);
179180
},
@@ -183,12 +184,11 @@ export default createRule<[], MessageID>({
183184
findVariable(id, initialScope),
184185
O.flatMap(getVariableNode(0)),
185186
O.filter(isFunction),
186-
O.flatMapNullable((fn) => indirectSetStateCalls.get(fn as TSESTreeFunction)),
187-
O.map(Chunk.toReadonlyArray),
187+
O.flatMapNullable((fn) => indirectSetStateCalls.get(fn)),
188188
O.getOrElse(() => []),
189189
);
190190
};
191-
for (const { callee } of Chunk.toReadonlyArray(MutRef.get(indirectFunctionCalls))) {
191+
for (const { callee } of indirectFunctionCalls) {
192192
if (!("name" in callee)) continue;
193193
const { name } = callee;
194194
const setStateCalls = getSetStateCalls(name, context.sourceCode.getScope(callee));
@@ -200,7 +200,7 @@ export default createRule<[], MessageID>({
200200
});
201201
}
202202
}
203-
for (const id of Chunk.toReadonlyArray(MutRef.get(effectFunctionIdentifiers))) {
203+
for (const id of effectFunctionIdentifiers) {
204204
const setStateCalls = getSetStateCalls(id.name, context.sourceCode.getScope(id));
205205
for (const setStateCall of setStateCalls) {
206206
context.report({

packages/plugins/eslint-plugin-react-hooks-extra/src/rules/no-direct-set-state-in-use-layout-effect.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { TSESTreeFunction } from "@eslint-react/ast";
22
import { getNestedIdentifiers, is, isFunction, isIIFE, NodeType } from "@eslint-react/ast";
33
import { isReactHookCallWithNameLoose, isUseLayoutEffectCall, isUseStateCall } from "@eslint-react/core";
44
import { parseESLintSettings } from "@eslint-react/shared";
5-
import { Chunk, F, MutList, MutRef, O } from "@eslint-react/tools";
5+
import { F, MutList, MutRef, O } from "@eslint-react/tools";
66
import { findVariable, getVariableNode } from "@eslint-react/var";
77
import type { ESLintUtils, TSESTree } from "@typescript-eslint/utils";
88
import { getStaticValue } from "@typescript-eslint/utils/ast-utils";
@@ -126,9 +126,9 @@ export default createRule<[], MessageID>({
126126
}
127127
const functionStack = MutList.make<[node: TSESTreeFunction, kind: FunctionKind]>();
128128
const effectFunctionRef = MutRef.make<TSESTreeFunction | null>(null);
129-
const effectFunctionIdentifiers = MutRef.make(Chunk.empty<TSESTree.Identifier>());
130-
const indirectFunctionCalls = MutRef.make(Chunk.empty<TSESTree.CallExpression>());
131-
const indirectSetStateCalls = new WeakMap<TSESTreeFunction, Chunk.Chunk<TSESTree.CallExpression>>();
129+
const effectFunctionIdentifiers: TSESTree.Identifier[] = [];
130+
const indirectFunctionCalls: TSESTree.CallExpression[] = [];
131+
const indirectSetStateCalls = new WeakMap<TSESTreeFunction, TSESTree.CallExpression[]>();
132132
const onEffectFunctionEnter = (node: TSESTreeFunction) => {
133133
MutRef.set(effectFunctionRef, node);
134134
};
@@ -158,8 +158,8 @@ export default createRule<[], MessageID>({
158158
.with("setState", () => {
159159
if (!parentFn) return;
160160
if (parentFn !== effectFn && parentFnKind !== "immediate") {
161-
const calls = indirectSetStateCalls.get(parentFn) ?? Chunk.empty<TSESTree.CallExpression>();
162-
indirectSetStateCalls.set(parentFn, Chunk.append(calls, node));
161+
const calls = indirectSetStateCalls.get(parentFn) ?? [];
162+
indirectSetStateCalls.set(parentFn, [...calls, node]);
163163
return;
164164
}
165165
context.report({
@@ -170,12 +170,12 @@ export default createRule<[], MessageID>({
170170
.with("useLayoutEffect", () => {
171171
if (node.arguments.every(isFunction)) return;
172172
const identifiers = getNestedIdentifiers(node);
173-
MutRef.update(effectFunctionIdentifiers, Chunk.appendAll(Chunk.unsafeFromArray(identifiers)));
173+
effectFunctionIdentifiers.push(...identifiers);
174174
})
175175
.with("other", () => {
176176
const isInEffectFunction = effectFn === parentFn;
177177
if (!isInEffectFunction) return;
178-
MutRef.update(indirectFunctionCalls, Chunk.append(node));
178+
indirectFunctionCalls.push(node);
179179
})
180180
.otherwise(F.constVoid);
181181
},
@@ -185,12 +185,11 @@ export default createRule<[], MessageID>({
185185
findVariable(id, initialScope),
186186
O.flatMap(getVariableNode(0)),
187187
O.filter(isFunction),
188-
O.flatMapNullable((fn) => indirectSetStateCalls.get(fn as TSESTreeFunction)),
189-
O.map(Chunk.toReadonlyArray),
188+
O.flatMapNullable((fn) => indirectSetStateCalls.get(fn)),
190189
O.getOrElse(() => []),
191190
);
192191
};
193-
for (const { callee } of Chunk.toReadonlyArray(MutRef.get(indirectFunctionCalls))) {
192+
for (const { callee } of indirectFunctionCalls) {
194193
if (!("name" in callee)) continue;
195194
const { name } = callee;
196195
const setStateCalls = getSetStateCalls(name, context.sourceCode.getScope(callee));
@@ -202,7 +201,7 @@ export default createRule<[], MessageID>({
202201
});
203202
}
204203
}
205-
for (const id of Chunk.toReadonlyArray(MutRef.get(effectFunctionIdentifiers))) {
204+
for (const id of effectFunctionIdentifiers) {
206205
const setStateCalls = getSetStateCalls(id.name, context.sourceCode.getScope(id));
207206
for (const setStateCall of setStateCalls) {
208207
context.report({

packages/tools/docs/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
## Namespaces
88

9-
- [Chunk](namespaces/Chunk/README.md)
109
- [Data](namespaces/Data/README.md)
1110
- [E](namespaces/E/README.md)
1211
- [F](namespaces/F/README.md)

packages/tools/docs/namespaces/Chunk/README.md

Lines changed: 0 additions & 151 deletions
This file was deleted.

packages/tools/docs/namespaces/Chunk/functions/append.md

Lines changed: 0 additions & 73 deletions
This file was deleted.

0 commit comments

Comments
 (0)