@openrewrite/recipes-react 0.18.0 → 0.20.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/react19/no-implicit-ref-callback-return.d.ts.map +1 -1
- package/dist/react19/no-implicit-ref-callback-return.js +25 -5
- package/dist/react19/no-implicit-ref-callback-return.js.map +1 -1
- package/dist/search/find-enzyme-usage.d.ts.map +1 -1
- package/dist/search/find-enzyme-usage.js +34 -1
- package/dist/search/find-enzyme-usage.js.map +1 -1
- package/dist/testing/enzyme-host-components.d.ts +2 -1
- package/dist/testing/enzyme-host-components.d.ts.map +1 -1
- package/dist/testing/enzyme-host-components.js +33 -2
- package/dist/testing/enzyme-host-components.js.map +1 -1
- package/dist/testing/enzyme-shared-wrapper.d.ts +26 -0
- package/dist/testing/enzyme-shared-wrapper.d.ts.map +1 -0
- package/dist/testing/enzyme-shared-wrapper.js +213 -0
- package/dist/testing/enzyme-shared-wrapper.js.map +1 -0
- package/dist/testing/enzyme-to-react-testing-library.d.ts.map +1 -1
- package/dist/testing/enzyme-to-react-testing-library.js +280 -2
- package/dist/testing/enzyme-to-react-testing-library.js.map +1 -1
- package/dist/testing/enzyme-usage.d.ts +3 -1
- package/dist/testing/enzyme-usage.d.ts.map +1 -1
- package/dist/testing/enzyme-usage.js +19 -3
- package/dist/testing/enzyme-usage.js.map +1 -1
- package/dist/testing/enzyme-wrapper-scope.d.ts +3 -0
- package/dist/testing/enzyme-wrapper-scope.d.ts.map +1 -1
- package/dist/testing/enzyme-wrapper-scope.js +38 -6
- package/dist/testing/enzyme-wrapper-scope.js.map +1 -1
- package/dist/zustand/redux-stores.d.ts +10 -0
- package/dist/zustand/redux-stores.d.ts.map +1 -1
- package/dist/zustand/redux-stores.js +187 -8
- package/dist/zustand/redux-stores.js.map +1 -1
- package/dist/zustand/redux-to-zustand.d.ts +20 -5
- package/dist/zustand/redux-to-zustand.d.ts.map +1 -1
- package/dist/zustand/redux-to-zustand.js +520 -27
- package/dist/zustand/redux-to-zustand.js.map +1 -1
- package/dist/zustand/zustand-plan.d.ts +28 -4
- package/dist/zustand/zustand-plan.d.ts.map +1 -1
- package/dist/zustand/zustand-plan.js +265 -19
- package/dist/zustand/zustand-plan.js.map +1 -1
- package/package.json +1 -1
- package/src/react19/no-implicit-ref-callback-return.ts +34 -8
- package/src/search/find-enzyme-usage.ts +32 -3
- package/src/testing/enzyme-host-components.ts +38 -3
- package/src/testing/enzyme-shared-wrapper.ts +224 -0
- package/src/testing/enzyme-to-react-testing-library.ts +271 -6
- package/src/testing/enzyme-usage.ts +27 -3
- package/src/testing/enzyme-wrapper-scope.ts +35 -4
- package/src/zustand/redux-stores.ts +195 -8
- package/src/zustand/redux-to-zustand.ts +559 -35
- package/src/zustand/zustand-plan.ts +284 -18
|
@@ -133,6 +133,11 @@ export const SIMULATED_EVENTS: ReadonlySet<string> = new Set(["click", "doubleCl
|
|
|
133
133
|
"touchMove", "touchEnd", "wheel"]);
|
|
134
134
|
// Enzyme also accepts the DOM's lowercase names of these events, and `dblclick` for a double click
|
|
135
135
|
const NATIVE_EVENT_NAMES = new Map([...[...SIMULATED_EVENTS].map(event => [event.toLowerCase(), event] as const), ["dblclick", "doubleClick"]]);
|
|
136
|
+
/** The React event a `simulate` name stands for, where `fireEvent` can dispatch it. */
|
|
137
|
+
export function simulatedEvent(name: string): string | undefined {
|
|
138
|
+
return SIMULATED_EVENTS.has(name) ? name : NATIVE_EVENT_NAMES.get(name);
|
|
139
|
+
}
|
|
140
|
+
|
|
136
141
|
const CLICK_DEFAULT_ACTIONS = new Set(["a", "input", "label", "option", "select", "summary", "textarea"]);
|
|
137
142
|
|
|
138
143
|
export function simpleChain(node: Expression): boolean {
|
|
@@ -213,6 +218,30 @@ export function statementPosition(cursor: Cursor): boolean {
|
|
|
213
218
|
return statement(cursor) !== undefined;
|
|
214
219
|
}
|
|
215
220
|
|
|
221
|
+
const THROW_MATCHERS = new Set(["toThrow", "toThrowError"]);
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Whether a render is the whole body of an arrow function that `expect` takes for `toThrow` or `toThrowError`,
|
|
225
|
+
* plain or negated, as in `expect(() => mount(<Broken />)).toThrow()`. React rethrows an error that no boundary
|
|
226
|
+
* catches from Enzyme's render and from RTL's alike, and the arrow keeps no wrapper.
|
|
227
|
+
*/
|
|
228
|
+
export function throwingPosition(cursor: Cursor): boolean {
|
|
229
|
+
const trees: J[] = [];
|
|
230
|
+
for (let parent = cursor.parentTree(); parent && trees.length < 5; parent = parent.parentTree()) trees.push(parent.value as J);
|
|
231
|
+
const [lambda, arrow, expectCall, ...rest] = trees;
|
|
232
|
+
if (lambda?.kind !== J.Kind.Lambda || (lambda as J.Lambda).body !== cursor.value) return false;
|
|
233
|
+
if (arrow?.kind !== JS.Kind.ArrowFunction || (arrow as JS.ArrowFunction).lambda.parameters.parameters.some(({element}) => element.kind !== J.Kind.Empty)) return false;
|
|
234
|
+
if (expectCall?.kind !== J.Kind.MethodInvocation) return false;
|
|
235
|
+
const expectation = expectCall as J.MethodInvocation;
|
|
236
|
+
if (expectation.name.simpleName !== "expect" || expectation.select || expectation.arguments.elements.length !== 1 ||
|
|
237
|
+
expectation.arguments.elements[0].element !== arrow) return false;
|
|
238
|
+
const [negated, matcher] = rest[0]?.kind === J.Kind.FieldAccess ? rest : [undefined, rest[0]];
|
|
239
|
+
if (negated && ((negated as J.FieldAccess).name.element.simpleName !== "not" || (negated as J.FieldAccess).target !== expectation)) return false;
|
|
240
|
+
if (matcher?.kind !== J.Kind.MethodInvocation) return false;
|
|
241
|
+
const call = matcher as J.MethodInvocation;
|
|
242
|
+
return THROW_MATCHERS.has(call.name.simpleName) && call.select?.element === (negated ?? expectation);
|
|
243
|
+
}
|
|
244
|
+
|
|
216
245
|
/** The argument `mount` renders, which `render` accepts as it is. */
|
|
217
246
|
export function mountedElement(mount: J.MethodInvocation): boolean {
|
|
218
247
|
const [element] = mount.arguments.elements;
|
|
@@ -258,7 +287,7 @@ function stringLiteral(node: J | undefined): node is J.Literal {
|
|
|
258
287
|
return node?.kind === J.Kind.Literal && typeof (node as J.Literal).value === "string";
|
|
259
288
|
}
|
|
260
289
|
|
|
261
|
-
function objectProperties(node: J | undefined): Map<string, Expression> | undefined {
|
|
290
|
+
export function objectProperties(node: J | undefined): Map<string, Expression> | undefined {
|
|
262
291
|
if (!node || !isObjectLiteral(node) || !node.body) return undefined;
|
|
263
292
|
const properties = new Map<string, Expression>();
|
|
264
293
|
for (const statement of node.body.statements) {
|
|
@@ -478,10 +507,12 @@ export function analyzeWrapperScope(block: Cursor, {declaration, name}: WrapperD
|
|
|
478
507
|
const name = terminalArgs[0].value as string;
|
|
479
508
|
const event = SIMULATED_EVENTS.has(name) ? name : NATIVE_EVENT_NAMES.get(name);
|
|
480
509
|
if (!event || !events.has(event)) return false;
|
|
481
|
-
|
|
482
|
-
|
|
510
|
+
// Enzyme assigns an empty mock's properties to the event, which are none
|
|
511
|
+
const init = objectProperties(terminalArgs[1])?.size === 0 ? undefined : terminalArgs[1];
|
|
512
|
+
const nativeEvent = nativeEventInit(event, init);
|
|
513
|
+
if (!nativeEvent && !eventInit(event, init)) return false;
|
|
483
514
|
if (event === "click" && CLICK_DEFAULT_ACTIONS.has(selectorTag(selectorText) ?? "")) return false;
|
|
484
|
-
return record(terminal, {...read, use: "simulate", event, ...nativeEvent ? {nativeEvent} : {eventInit:
|
|
515
|
+
return record(terminal, {...read, use: "simulate", event, ...nativeEvent ? {nativeEvent} : {eventInit: init}});
|
|
485
516
|
}
|
|
486
517
|
return false;
|
|
487
518
|
}
|
|
@@ -159,6 +159,32 @@ function plainParameter(node: J): string | undefined {
|
|
|
159
159
|
return variable && !declarations.typeExpression && !declarations.varargs && !variable.initializer && isIdentifier(variable.name) ? variable.name.simpleName : undefined;
|
|
160
160
|
}
|
|
161
161
|
|
|
162
|
+
/** The object pattern a parameter is, `{a, b: c}`, without a type or a default for the whole. */
|
|
163
|
+
function destructured(node: J): JS.ObjectBindingPattern | undefined {
|
|
164
|
+
if (node.kind !== J.Kind.VariableDeclarations) return undefined;
|
|
165
|
+
const declarations = node as J.VariableDeclarations;
|
|
166
|
+
const variable = declarations.variables.length === 1 ? declarations.variables[0].element : undefined;
|
|
167
|
+
const pattern = variable?.name;
|
|
168
|
+
return variable && !declarations.typeExpression && !variable.initializer && pattern?.kind === JS.Kind.ObjectBindingPattern &&
|
|
169
|
+
!(pattern as JS.ObjectBindingPattern).initializer && !(pattern as JS.ObjectBindingPattern).typeExpression ? pattern as JS.ObjectBindingPattern : undefined;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** The members `{a, b: c}` takes, each bound to a name of its own; otherwise what else the pattern does. */
|
|
173
|
+
function thunkApiMembers(pattern: JS.ObjectBindingPattern): string[] | string {
|
|
174
|
+
const members: string[] = [];
|
|
175
|
+
for (const {element} of pattern.bindings.elements) {
|
|
176
|
+
if (element.kind === J.Kind.Empty) continue;
|
|
177
|
+
if (element.kind !== JS.Kind.BindingElement) return "is a pattern of more than names";
|
|
178
|
+
const binding = element as JS.BindingElement;
|
|
179
|
+
if (binding.initializer) return "pattern has a default";
|
|
180
|
+
if (!isIdentifier(binding.name)) return "pattern has a rest element or a nested pattern";
|
|
181
|
+
const key = binding.propertyName?.element ?? binding.name;
|
|
182
|
+
if (!isIdentifier(key)) return "pattern has a computed key";
|
|
183
|
+
members.push(key.simpleName);
|
|
184
|
+
}
|
|
185
|
+
return members;
|
|
186
|
+
}
|
|
187
|
+
|
|
162
188
|
/**
|
|
163
189
|
* The thunks and cases of `extraReducers` written as `builder => builder.addCase(thunk.pending, reducer)...`, where
|
|
164
190
|
* each thunk is a `createAsyncThunk` of the same source; otherwise why they can't be kept, for the review row.
|
|
@@ -223,9 +249,15 @@ function asyncThunk(name: string, cu: JS.CompilationUnit): SliceThunk | string {
|
|
|
223
249
|
if (!creator) return `\`${name}\`'s payload creator isn't an arrow function`;
|
|
224
250
|
const parameters = parametersOf(creator);
|
|
225
251
|
if (parameters.length > 2) return `\`${name}\`'s payload creator takes more than \`arg\` and \`thunkAPI\``;
|
|
226
|
-
if (parameters.length === 2) {
|
|
252
|
+
if (parameters.length === 2 && destructured(parameters[1])) {
|
|
253
|
+
// `{rejectWithValue, signal}` takes only what stays within the thunk, whatever the payload creator does with it
|
|
254
|
+
const taken = thunkApiMembers(destructured(parameters[1])!);
|
|
255
|
+
if (typeof taken === "string") return `\`${name}\`'s \`thunkAPI\` ${taken}`;
|
|
256
|
+
const other = taken.find(member => !THUNK_API.has(member));
|
|
257
|
+
if (other) return `\`${name}\` takes \`${other}\` from \`thunkAPI\`, beyond ${[...THUNK_API].map(m => `\`${m}\``).join(", ")}`;
|
|
258
|
+
} else if (parameters.length === 2) {
|
|
227
259
|
const api = plainParameter(parameters[1]);
|
|
228
|
-
if (!api) return `\`${name}\`'s \`thunkAPI\` isn't a plain parameter`;
|
|
260
|
+
if (!api) return `\`${name}\`'s \`thunkAPI\` isn't a plain parameter or an object pattern`;
|
|
229
261
|
// Every mention of `thunkAPI` must be a read of a member that stays within the thunk
|
|
230
262
|
let mentions = 0;
|
|
231
263
|
let kept = 0;
|
|
@@ -333,11 +365,11 @@ export function resolveSliceReducer(node: J, cu: JS.CompilationUnit, local: Loca
|
|
|
333
365
|
|
|
334
366
|
export const SUGGESTED_ACTIONS: Readonly<Record<string, string>> = {
|
|
335
367
|
SLICE_STORE: "A store of `createSlice` reducers with the default middleware: each slice can become a zustand store. Check its consumers next.",
|
|
336
|
-
LEGACY_STORE: "
|
|
368
|
+
LEGACY_STORE: "A `createStore` of a reducer, perhaps a preloaded state, and perhaps `applyMiddleware(thunk)` or Redux DevTools, moves whole; move a store with other middleware or enhancers to Redux Toolkit's `configureStore` first, or write the zustand store by hand.",
|
|
337
369
|
OPTIONS_NOT_LITERAL: "Write the `configureStore` options as an object literal, so that the setup can be read.",
|
|
338
370
|
ROOT_REDUCER: "Pass `reducer` as an object of slice reducers; a single root reducer, such as `combineReducers(...)`, can't be split into zustand stores.",
|
|
339
371
|
NON_SLICE_REDUCER: "Rewrite this reducer with `createSlice`, or keep it in Redux; zustand has no place for a hand-written reducer.",
|
|
340
|
-
EXTRA_REDUCERS: "Write `extraReducers` as `builder.addCase(thunk.pending, reducer)` cases of `createAsyncThunk`s declared in the slice's source, whose payload creators use only `rejectWithValue`, `fulfillWithValue`, `signal` and `requestId` of `thunkAPI`, or move what it handles into actions of the zustand store by hand.",
|
|
372
|
+
EXTRA_REDUCERS: "Write `extraReducers` as `builder.addCase(thunk.pending, reducer)` cases of `createAsyncThunk`s declared in the slice's source, whose payload creators take or use only `rejectWithValue`, `fulfillWithValue`, `signal` and `requestId` of `thunkAPI`, or move what it handles into actions of the zustand store by hand.",
|
|
341
373
|
SLICE_OPTIONS_NOT_LITERAL: "Write the `createSlice` options as an object literal, so that the slice can be read.",
|
|
342
374
|
SLICE_REDUCERS_NOT_LITERAL: "Write `reducers` as an object of reducer functions; the builder callback form can't be read.",
|
|
343
375
|
MIDDLEWARE: "Custom middleware has no zustand equivalent: move it into the store's actions or zustand middleware by hand.",
|
|
@@ -367,15 +399,167 @@ export function storeFunctions(cu: JS.CompilationUnit): {legacy: Set<string>, co
|
|
|
367
399
|
};
|
|
368
400
|
}
|
|
369
401
|
|
|
402
|
+
/**
|
|
403
|
+
* How a store can move to zustand whole: a `configureStore` of an object of reducers it combines (`combined`) or of a
|
|
404
|
+
* reducer of its own (`reducer`), or a legacy `createStore` of a reducer and perhaps a preloaded state (`legacy`).
|
|
405
|
+
*/
|
|
406
|
+
export type ReducerStoreKind = "combined" | "reducer" | "legacy" | "legacyThunk";
|
|
407
|
+
|
|
408
|
+
const REDUX_THUNK = "redux-thunk";
|
|
409
|
+
const DEVTOOLS = ["redux-devtools-extension", "@redux-devtools/extension"];
|
|
410
|
+
|
|
411
|
+
/**
|
|
412
|
+
* What a legacy store's enhancer does, when it is one the conversion reproduces: whether it applies redux-thunk, and
|
|
413
|
+
* the names of the top-level `const`s it is written with. Otherwise why it isn't one.
|
|
414
|
+
*/
|
|
415
|
+
export type LegacyEnhancer = {thunk: boolean, consts: Set<string>} | {unsupported: string};
|
|
416
|
+
|
|
417
|
+
/**
|
|
418
|
+
* Reads a legacy store's enhancer: `applyMiddleware` of redux-thunk alone, perhaps spread from a `const` array, inside
|
|
419
|
+
* `compose`, Redux DevTools' compose or `composeWithDevTools`, or DevTools' own enhancer. DevTools only shows the store
|
|
420
|
+
* in the browser extension, so dropping it changes nothing the app does.
|
|
421
|
+
*/
|
|
422
|
+
export function legacyEnhancer(expression: J, cu: JS.CompilationUnit): LegacyEnhancer {
|
|
423
|
+
const bindings = importBindings(cu);
|
|
424
|
+
const imported = (name: string, modules: string[], member: string) => bindings.some(b => b.local === name && modules.includes(b.module) && b.name === member);
|
|
425
|
+
const thunk = (name: string) => imported(name, [REDUX_THUNK], "default") || imported(name, [REDUX_THUNK], "thunk");
|
|
426
|
+
const consts = new Set<string>();
|
|
427
|
+
const constant = (name: string): J | undefined => {
|
|
428
|
+
const declarations = cu.statements.map(st => st.element).flatMap(statement => {
|
|
429
|
+
if (statement.kind !== J.Kind.VariableDeclarations) return [];
|
|
430
|
+
const variables = statement as J.VariableDeclarations;
|
|
431
|
+
return variables.variables.map(v => v.element).filter(v => isIdentifier(v.name) && v.name.simpleName === name)
|
|
432
|
+
.map(v => ({constant: variables.variables.length === 1 && variables.modifiers.some(m => m.keyword === "const"), initializer: v.initializer?.element}));
|
|
433
|
+
});
|
|
434
|
+
if (declarations.length !== 1 || !declarations[0].constant || !declarations[0].initializer) return undefined;
|
|
435
|
+
consts.add(name);
|
|
436
|
+
return declarations[0].initializer;
|
|
437
|
+
};
|
|
438
|
+
const unwrap = (node: J): J => node.kind === J.Kind.Parentheses ? unwrap((node as J.Parentheses<J>).tree.element) : node;
|
|
439
|
+
const window = (node: J, member: string) => {
|
|
440
|
+
const n = unwrap(node);
|
|
441
|
+
return n.kind === J.Kind.FieldAccess && isIdentifier((n as J.FieldAccess).target) && ((n as J.FieldAccess).target as J.Identifier).simpleName === "window" &&
|
|
442
|
+
(n as J.FieldAccess).name.element.simpleName === member;
|
|
443
|
+
};
|
|
444
|
+
const binary = (node: J, operator: string) => {
|
|
445
|
+
const n = unwrap(node);
|
|
446
|
+
return n.kind === J.Kind.Binary && (n as J.Binary).operator.element === operator ? n as J.Binary : undefined;
|
|
447
|
+
};
|
|
448
|
+
// `window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose`, perhaps guarded by `typeof window !== "undefined" &&`
|
|
449
|
+
const devtoolsCompose = (node: J): boolean => {
|
|
450
|
+
const or = binary(node, "Or");
|
|
451
|
+
if (!or || !isIdentifier(unwrap(or.right)) || !imported((unwrap(or.right) as J.Identifier).simpleName, [REDUX], "compose")) return false;
|
|
452
|
+
const guarded = binary(or.left, "And");
|
|
453
|
+
if (guarded) return guarded.left.kind === JS.Kind.Binary && (guarded.left as JS.Binary).left.kind === JS.Kind.TypeOf && window(guarded.right, "__REDUX_DEVTOOLS_EXTENSION_COMPOSE__");
|
|
454
|
+
return window(or.left, "__REDUX_DEVTOOLS_EXTENSION_COMPOSE__");
|
|
455
|
+
};
|
|
456
|
+
const composer = (callee: J): boolean => {
|
|
457
|
+
if (!isIdentifier(callee)) return false;
|
|
458
|
+
const name = callee.simpleName;
|
|
459
|
+
if (imported(name, [REDUX], "compose") || imported(name, DEVTOOLS, "composeWithDevTools")) return true;
|
|
460
|
+
const initializer = constant(name);
|
|
461
|
+
return initializer !== undefined && devtoolsCompose(initializer);
|
|
462
|
+
};
|
|
463
|
+
const middleware = (node: J): string | undefined => {
|
|
464
|
+
if (isIdentifier(node)) return thunk(node.simpleName) ? undefined : `the middleware \`${node.simpleName}\``;
|
|
465
|
+
if (node.kind === JS.Kind.Spread && isIdentifier((node as JS.Spread).expression)) {
|
|
466
|
+
const name = ((node as JS.Spread).expression as J.Identifier).simpleName;
|
|
467
|
+
const array = constant(name);
|
|
468
|
+
if (array?.kind !== J.Kind.NewArray) return `middleware spread from \`${name}\``;
|
|
469
|
+
for (const element of (array as J.NewArray).initializer?.elements.map(e => e.element).filter(e => e.kind !== J.Kind.Empty) ?? []) {
|
|
470
|
+
const why = middleware(element);
|
|
471
|
+
if (why) return why;
|
|
472
|
+
}
|
|
473
|
+
return undefined;
|
|
474
|
+
}
|
|
475
|
+
return "middleware other than redux-thunk";
|
|
476
|
+
};
|
|
477
|
+
let thunked = false;
|
|
478
|
+
const enhancer = (node: J): string | undefined => {
|
|
479
|
+
node = unwrap(node);
|
|
480
|
+
if (isIdentifier(node)) {
|
|
481
|
+
const initializer = constant(node.simpleName);
|
|
482
|
+
return initializer ? enhancer(initializer) : `an enhancer \`${node.simpleName}\``;
|
|
483
|
+
}
|
|
484
|
+
// `window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()`
|
|
485
|
+
const and = binary(node, "And");
|
|
486
|
+
if (and && window(and.left, "__REDUX_DEVTOOLS_EXTENSION__")) {
|
|
487
|
+
const call = unwrap(and.right);
|
|
488
|
+
return call.kind === J.Kind.MethodInvocation && (call as J.MethodInvocation).name.simpleName === "__REDUX_DEVTOOLS_EXTENSION__" ? undefined : "an enhancer other than Redux DevTools";
|
|
489
|
+
}
|
|
490
|
+
if (node.kind !== J.Kind.MethodInvocation || (node as J.MethodInvocation).select) return "an enhancer other than applyMiddleware and Redux DevTools";
|
|
491
|
+
const call = node as J.MethodInvocation;
|
|
492
|
+
const args = call.arguments.elements.map(e => e.element).filter(e => e.kind !== J.Kind.Empty);
|
|
493
|
+
const name = call.name.simpleName;
|
|
494
|
+
if (imported(name, [REDUX], "applyMiddleware")) {
|
|
495
|
+
for (const arg of args) {
|
|
496
|
+
const why = middleware(arg);
|
|
497
|
+
if (why) return why;
|
|
498
|
+
}
|
|
499
|
+
thunked ||= args.length > 0;
|
|
500
|
+
return undefined;
|
|
501
|
+
}
|
|
502
|
+
if (imported(name, DEVTOOLS, "devToolsEnhancer") || imported(name, DEVTOOLS, "devToolsEnhancerLogOnlyInProduction")) return undefined;
|
|
503
|
+
if (composer(call.name)) {
|
|
504
|
+
for (const arg of args) {
|
|
505
|
+
const why = enhancer(arg);
|
|
506
|
+
if (why) return why;
|
|
507
|
+
}
|
|
508
|
+
return undefined;
|
|
509
|
+
}
|
|
510
|
+
return `an enhancer \`${name}\``;
|
|
511
|
+
};
|
|
512
|
+
const why = enhancer(expression);
|
|
513
|
+
return why ? {unsupported: why} : {thunk: thunked, consts};
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
/** Whether `expression` is a function or a call of `applyMiddleware` or `compose`, which `createStore` takes as an enhancer. */
|
|
517
|
+
function isFunction(expression: J): boolean {
|
|
518
|
+
return expression.kind === J.Kind.Lambda || expression.kind === JS.Kind.ArrowFunction ||
|
|
519
|
+
(expression.kind === J.Kind.MethodInvocation && ["applyMiddleware", "compose"].includes((expression as J.MethodInvocation).name.simpleName));
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
/**
|
|
523
|
+
* Whether `expression`, passed after the reducer to `createStore`, is surely not a function, so Redux takes it as the
|
|
524
|
+
* preloaded state: a literal, an object or array literal, or a top-level `const` the source declares once as one.
|
|
525
|
+
*/
|
|
526
|
+
export function preloadedValue(expression: J, cu: JS.CompilationUnit): boolean {
|
|
527
|
+
const literal = (node: J | undefined): boolean => node !== undefined && (node.kind === J.Kind.Literal || node.kind === J.Kind.NewArray ||
|
|
528
|
+
(node.kind === J.Kind.NewClass && !(node as J.NewClass).class));
|
|
529
|
+
if (literal(expression)) return true;
|
|
530
|
+
if (expression.kind !== J.Kind.Identifier) return false;
|
|
531
|
+
const name = (expression as J.Identifier).simpleName;
|
|
532
|
+
const declarations = cu.statements.map(s => s.element).flatMap(statement => {
|
|
533
|
+
if (statement.kind !== J.Kind.VariableDeclarations) return [];
|
|
534
|
+
const variables = statement as J.VariableDeclarations;
|
|
535
|
+
return variables.variables.map(v => v.element).filter(v => v.name.kind === J.Kind.Identifier && (v.name as J.Identifier).simpleName === name)
|
|
536
|
+
.map(v => ({constant: variables.modifiers.some(m => m.keyword === "const"), initializer: v.initializer?.element}));
|
|
537
|
+
});
|
|
538
|
+
return declarations.length === 1 && declarations[0].constant && literal(declarations[0].initializer);
|
|
539
|
+
}
|
|
540
|
+
|
|
370
541
|
/**
|
|
371
542
|
* Why the store `call` creates can't move to zustand, or `[]` for a candidate, with the slices of its reducers by key.
|
|
372
543
|
* `undefined` when `call` creates no store.
|
|
373
544
|
*/
|
|
374
545
|
export function storeFindings(call: J.MethodInvocation, cu: JS.CompilationUnit, modules: ReadonlyMap<string, SliceModule>,
|
|
375
|
-
functions = storeFunctions(cu)): {api: string, findings: Finding[], slices: Map<string, SliceFacts
|
|
546
|
+
functions = storeFunctions(cu)): {api: string, findings: Finding[], slices: Map<string, SliceFacts>, reducerStore?: ReducerStoreKind} | undefined {
|
|
376
547
|
if (call.select) return undefined;
|
|
377
548
|
const api = call.name.simpleName;
|
|
378
|
-
if (functions.legacy.has(api))
|
|
549
|
+
if (functions.legacy.has(api)) {
|
|
550
|
+
// Redux takes a function after the reducer as an enhancer, and anything else as the preloaded state
|
|
551
|
+
const args = call.arguments.elements.map(e => e.element).filter(e => e.kind !== J.Kind.Empty);
|
|
552
|
+
// With three arguments, Redux always takes the second as the preloaded state and the third as the enhancer
|
|
553
|
+
const preloaded = args.length === 3 || (args.length === 2 && preloadedValue(args[1], cu));
|
|
554
|
+
const read = args.length === 3 || (args.length === 2 && !preloaded) ? legacyEnhancer(args[args.length - 1], cu) : undefined;
|
|
555
|
+
if (args.length === 1 || args.length === 2 && preloaded || read && !("unsupported" in read)) {
|
|
556
|
+
const thunk = read !== undefined && !("unsupported" in read) && read.thunk;
|
|
557
|
+
return {api, findings: [{reason: "LEGACY_STORE", detail: api}], slices: new Map(), reducerStore: thunk ? "legacyThunk" : "legacy"};
|
|
558
|
+
}
|
|
559
|
+
const detail = args.length === 2 && !isFunction(args[1]) && read && "unsupported" in read && read.unsupported.startsWith("an enhancer `") ?
|
|
560
|
+
`${api} with a second argument that may be an enhancer` : `${api} with ${read && "unsupported" in read ? read.unsupported : "an enhancer"}`;
|
|
561
|
+
return {api, findings: [{reason: "LEGACY_STORE", detail}], slices: new Map()};
|
|
562
|
+
}
|
|
379
563
|
if (!functions.configure.has(api)) return undefined;
|
|
380
564
|
const slices = new Map<string, SliceFacts>();
|
|
381
565
|
const options = objectProperties(call.arguments.elements[0]?.element);
|
|
@@ -384,8 +568,11 @@ export function storeFindings(call: J.MethodInvocation, cu: JS.CompilationUnit,
|
|
|
384
568
|
for (const key of options.keys()) {
|
|
385
569
|
if (key !== "reducer") findings.push({reason: BLOCKING_OPTIONS[key] ?? "UNKNOWN_OPTION", detail: key});
|
|
386
570
|
}
|
|
571
|
+
// A store of its `reducer` alone can also move to zustand whole, driven by that reducer
|
|
572
|
+
const reducer = options.get("reducer");
|
|
573
|
+
const reducerStore = options.size !== 1 || !reducer ? undefined : reducer.kind === J.Kind.NewClass && !(reducer as J.NewClass).class ? "combined" : "reducer";
|
|
387
574
|
const reducers = objectProperties(options.get("reducer"));
|
|
388
|
-
if (!reducers) return {api, findings: [...findings, {reason: "ROOT_REDUCER", detail: ""}], slices};
|
|
575
|
+
if (!reducers) return {api, findings: [...findings, {reason: "ROOT_REDUCER", detail: ""}], slices, reducerStore};
|
|
389
576
|
const local = localSlices(cu);
|
|
390
577
|
for (const [key, value] of reducers) {
|
|
391
578
|
const slice = resolveSliceReducer(value, cu, local, modules);
|
|
@@ -393,7 +580,7 @@ export function storeFindings(call: J.MethodInvocation, cu: JS.CompilationUnit,
|
|
|
393
580
|
else if (slice.blocker) findings.push(slice.blocker);
|
|
394
581
|
else slices.set(key, slice);
|
|
395
582
|
}
|
|
396
|
-
return {api, findings, slices};
|
|
583
|
+
return {api, findings, slices, reducerStore};
|
|
397
584
|
}
|
|
398
585
|
|
|
399
586
|
/** The variable or function a store is created in, such as `store` in `export const store = configureStore(...)`. */
|