Skip to content

Commit 18ec90a

Browse files
committed
refactor(eff): rename getOrUpdate to getOrElseUpdate
1 parent cc53f72 commit 18ec90a

File tree

8 files changed

+123
-32
lines changed

8 files changed

+123
-32
lines changed

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { ESLintUtils, TSESTree } from "@typescript-eslint/utils";
33
import type { Scope } from "@typescript-eslint/utils/ts-eslint";
44
import * as AST from "@eslint-react/ast";
55
import * as ER from "@eslint-react/core";
6-
import { constVoid, getOrUpdate } from "@eslint-react/eff";
6+
import { constVoid, getOrElseUpdate } from "@eslint-react/eff";
77
import { getSettingsFromContext } from "@eslint-react/shared";
88
import * as VAR from "@eslint-react/var";
99
import { AST_NODE_TYPES as T } from "@typescript-eslint/types";
@@ -130,8 +130,8 @@ export function useNoDirectSetStateInUseEffect<Ctx extends RuleContext>(
130130
}
131131
default: {
132132
const vd = AST.findParentNode(node, isVariableDeclaratorFromHookCall);
133-
if (vd == null) getOrUpdate(indSetStateCalls, pEntry.node, () => []).push(node);
134-
else getOrUpdate(indSetStateCallsInUseMemoOrCallback, vd.init, () => []).push(node);
133+
if (vd == null) getOrElseUpdate(indSetStateCalls, pEntry.node, () => []).push(node);
134+
else getOrElseUpdate(indSetStateCallsInUseMemoOrCallback, vd.init, () => []).push(node);
135135
}
136136
}
137137
})
@@ -166,7 +166,7 @@ export function useNoDirectSetStateInUseEffect<Ctx extends RuleContext>(
166166
}
167167
const vd = AST.findParentNode(parent, isVariableDeclaratorFromHookCall);
168168
if (vd != null) {
169-
getOrUpdate(indSetStateCallsInUseEffectArg0, vd.init, () => []).push(node);
169+
getOrElseUpdate(indSetStateCallsInUseEffectArg0, vd.init, () => []).push(node);
170170
}
171171
break;
172172
}
@@ -180,14 +180,14 @@ export function useNoDirectSetStateInUseEffect<Ctx extends RuleContext>(
180180
if (isUseCallbackCall(node.parent)) {
181181
const vd = AST.findParentNode(node.parent, isVariableDeclaratorFromHookCall);
182182
if (vd != null) {
183-
getOrUpdate(indSetStateCallsInUseEffectArg0, vd.init, () => []).push(node);
183+
getOrElseUpdate(indSetStateCallsInUseEffectArg0, vd.init, () => []).push(node);
184184
}
185185
break;
186186
}
187187
// const [state, setState] = useState();
188188
// useEffect(setState);
189189
if (isUseEffectLikeCall(node.parent)) {
190-
getOrUpdate(indSetStateCallsInUseEffectSetup, node.parent, () => []).push(node);
190+
getOrElseUpdate(indSetStateCallsInUseEffectSetup, node.parent, () => []).push(node);
191191
}
192192
}
193193
}

packages/plugins/eslint-plugin-react-x/src/rules/no-unstable-context-value.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { RuleContext, RuleFeature } from "@eslint-react/kit";
22
import type { RuleListener } from "@typescript-eslint/utils/ts-eslint";
33
import * as AST from "@eslint-react/ast";
44
import * as ER from "@eslint-react/core";
5-
import { getOrUpdate } from "@eslint-react/eff";
5+
import { getOrElseUpdate } from "@eslint-react/eff";
66
import * as JSX from "@eslint-react/jsx";
77
import { getSettingsFromContext } from "@eslint-react/shared";
88
import * as VAR from "@eslint-react/var";
@@ -67,7 +67,7 @@ export function create(context: RuleContext<MessageID, []>): RuleListener {
6767
if (ER.isReactHookCall(construction.node)) {
6868
return;
6969
}
70-
getOrUpdate(constructions, functionEntry.node, () => []).push(construction);
70+
getOrElseUpdate(constructions, functionEntry.node, () => []).push(construction);
7171
},
7272
"Program:exit"(program) {
7373
const components = ctx.getAllComponents(program).values();

packages/plugins/eslint-plugin-react-x/src/rules/no-unstable-default-props.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { RuleListener } from "@typescript-eslint/utils/ts-eslint";
33
import type { CamelCase } from "string-ts";
44
import * as AST from "@eslint-react/ast";
55
import * as ER from "@eslint-react/core";
6-
import { getOrUpdate } from "@eslint-react/eff";
6+
import { getOrElseUpdate } from "@eslint-react/eff";
77
import { SEL } from "@eslint-react/kit";
88
import * as VAR from "@eslint-react/var";
99
import { AST_NODE_TYPES as T } from "@typescript-eslint/types";
@@ -92,7 +92,7 @@ export function create(context: RuleContext<MessageID, []>): RuleListener {
9292
[SEL.OBJECT_DESTRUCTURING_VARIABLE_DECLARATOR](node: SEL.ObjectDestructuringVariableDeclarator) {
9393
const functionEntry = ctx.getCurrentEntry();
9494
if (functionEntry == null) return;
95-
getOrUpdate(
95+
getOrElseUpdate(
9696
declarators,
9797
functionEntry.node,
9898
() => [],

packages/utilities/eff/docs/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
- [constVoid](functions/constVoid.md)
2929
- [flip](functions/flip.md)
3030
- [flow](functions/flow.md)
31-
- [getOrUpdate](functions/getOrUpdate.md)
31+
- [getOrElse](functions/getOrElse.md)
32+
- [getOrElseUpdate](functions/getOrElseUpdate.md)
3233
- [identity](functions/identity.md)
3334
- [isArray](functions/isArray.md)
3435
- [isFunction](functions/isFunction.md)
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
[**@eslint-react/eff**](../README.md)
2+
3+
***
4+
5+
[@eslint-react/eff](../README.md) / getOrElse
6+
7+
# Function: getOrElse()
8+
9+
## Call Signature
10+
11+
> **getOrElse**\<`K`, `V`\>(`map`, `key`, `callback`): `V`
12+
13+
Retrieves a value from a Map or WeakMap if the key exists, or computes a new value if it doesn't.
14+
15+
### Type Parameters
16+
17+
#### K
18+
19+
`K` *extends* `WeakKey`
20+
21+
#### V
22+
23+
`V`
24+
25+
### Parameters
26+
27+
#### map
28+
29+
[`WeakMap`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/WeakMap)\<`K`, `V`\>
30+
31+
The Map or WeakMap to get from
32+
33+
#### key
34+
35+
`K`
36+
37+
The key to look up in the Map or WeakMap
38+
39+
#### callback
40+
41+
() => `V`
42+
43+
The function to call to generate a new value if the key doesn't exist
44+
45+
### Returns
46+
47+
`V`
48+
49+
## Call Signature
50+
51+
> **getOrElse**\<`K`, `V`\>(`map`, `key`, `callback`): `V`
52+
53+
Retrieves a value from a Map or WeakMap if the key exists, or computes a new value if it doesn't.
54+
55+
### Type Parameters
56+
57+
#### K
58+
59+
`K`
60+
61+
#### V
62+
63+
`V`
64+
65+
### Parameters
66+
67+
#### map
68+
69+
[`Map`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Map)\<`K`, `V`\>
70+
71+
The Map or WeakMap to get from
72+
73+
#### key
74+
75+
`K`
76+
77+
The key to look up in the Map or WeakMap
78+
79+
#### callback
80+
81+
() => `V`
82+
83+
The function to call to generate a new value if the key doesn't exist
84+
85+
### Returns
86+
87+
`V`

packages/utilities/eff/docs/functions/getOrUpdate.md renamed to packages/utilities/eff/docs/functions/getOrElseUpdate.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
***
44

5-
[@eslint-react/eff](../README.md) / getOrUpdate
5+
[@eslint-react/eff](../README.md) / getOrElseUpdate
66

7-
# Function: getOrUpdate()
7+
# Function: getOrElseUpdate()
88

99
## Call Signature
1010

11-
> **getOrUpdate**\<`K`, `V`\>(`map`, `key`, `callback`): `V`
11+
> **getOrElseUpdate**\<`K`, `V`\>(`map`, `key`, `callback`): `V`
1212
1313
Retrieves a value from a Map or WeakMap if the key exists, or computes and stores a new value if it doesn't.
1414

@@ -50,7 +50,7 @@ The existing value for the key, or the newly computed value
5050

5151
## Call Signature
5252

53-
> **getOrUpdate**\<`K`, `V`\>(`map`, `key`, `callback`): `V`
53+
> **getOrElseUpdate**\<`K`, `V`\>(`map`, `key`, `callback`): `V`
5454
5555
Retrieves a value from a Map or WeakMap if the key exists, or computes and stores a new value if it doesn't.
5656

packages/utilities/eff/src/index.ts

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,28 +1187,31 @@ export function zipWith<T, U, V>(
11871187

11881188
// #region Map & Set
11891189

1190+
/**
1191+
* Retrieves a value from a Map or WeakMap if the key exists, or computes a new value if it doesn't.
1192+
* @param map - The Map or WeakMap to get from
1193+
* @param key - The key to look up in the Map or WeakMap
1194+
* @param callback - The function to call to generate a new value if the key doesn't exist
1195+
*/
1196+
export function getOrElse<K extends WeakKey, V>(map: WeakMap<K, V>, key: K, callback: () => V): V;
1197+
export function getOrElse<K, V>(map: Map<K, V>, key: K, callback: () => V): V;
1198+
export function getOrElse<K extends WeakKey, V>(map: Map<K, V> | WeakMap<K, V>, key: K, callback: () => V): V {
1199+
if (map.has(key)) {
1200+
return map.get(key)!;
1201+
}
1202+
return callback();
1203+
}
1204+
11901205
/**
11911206
* Retrieves a value from a Map or WeakMap if the key exists, or computes and stores a new value if it doesn't.
11921207
* @param map - The Map or WeakMap to get from or update
11931208
* @param key - The key to look up in the Map or WeakMap
11941209
* @param callback - The function to call to generate a new value if the key doesn't exist
11951210
* @returns The existing value for the key, or the newly computed value
11961211
*/
1197-
export function getOrUpdate<K extends WeakKey, V>(
1198-
map: WeakMap<K, V>,
1199-
key: K,
1200-
callback: () => V,
1201-
): V;
1202-
export function getOrUpdate<K, V>(
1203-
map: Map<K, V>,
1204-
key: K,
1205-
callback: () => V,
1206-
): V;
1207-
export function getOrUpdate<K extends WeakKey, V>(
1208-
map: Map<K, V> | WeakMap<K, V>,
1209-
key: K,
1210-
callback: () => V,
1211-
): V {
1212+
export function getOrElseUpdate<K extends WeakKey, V>(map: WeakMap<K, V>, key: K, callback: () => V): V;
1213+
export function getOrElseUpdate<K, V>(map: Map<K, V>, key: K, callback: () => V): V;
1214+
export function getOrElseUpdate<K extends WeakKey, V>(map: Map<K, V> | WeakMap<K, V>, key: K, callback: () => V): V {
12121215
if (map.has(key)) {
12131216
return map.get(key)!;
12141217
}

packages/utilities/kit/src/JsxConfig/JsxConfig.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { RuleContext } from "../types";
2-
import { getOrUpdate } from "@eslint-react/eff";
2+
import { getOrElseUpdate } from "@eslint-react/eff";
33
import { type CompilerOptions, JsxEmit } from "typescript";
44
import * as RE from "../RE";
55

@@ -49,7 +49,7 @@ const cache = new WeakMap<RuleContext["sourceCode"], JsxConfig>();
4949
* @returns JsxConfig
5050
*/
5151
export function getFromAnnotation(context: RuleContext) {
52-
return getOrUpdate(
52+
return getOrElseUpdate(
5353
cache,
5454
context.sourceCode,
5555
() => {

0 commit comments

Comments
 (0)