@openrewrite/recipes-react 0.19.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/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-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 +276 -0
- 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 +2 -0
- package/dist/testing/enzyme-wrapper-scope.d.ts.map +1 -1
- package/dist/testing/enzyme-wrapper-scope.js +12 -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 +147 -5
- 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 +518 -26
- package/dist/zustand/redux-to-zustand.js.map +1 -1
- package/dist/zustand/zustand-plan.d.ts +27 -4
- package/dist/zustand/zustand-plan.d.ts.map +1 -1
- package/dist/zustand/zustand-plan.js +250 -15
- package/dist/zustand/zustand-plan.js.map +1 -1
- package/package.json +1 -1
- package/src/search/find-enzyme-usage.ts +32 -3
- package/src/testing/enzyme-shared-wrapper.ts +224 -0
- package/src/testing/enzyme-to-react-testing-library.ts +265 -2
- package/src/testing/enzyme-usage.ts +27 -3
- package/src/testing/enzyme-wrapper-scope.ts +11 -4
- package/src/zustand/redux-stores.ts +160 -5
- package/src/zustand/redux-to-zustand.ts +555 -34
- package/src/zustand/zustand-plan.ts +267 -12
|
@@ -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 {
|
|
@@ -282,7 +287,7 @@ function stringLiteral(node: J | undefined): node is J.Literal {
|
|
|
282
287
|
return node?.kind === J.Kind.Literal && typeof (node as J.Literal).value === "string";
|
|
283
288
|
}
|
|
284
289
|
|
|
285
|
-
function objectProperties(node: J | undefined): Map<string, Expression> | undefined {
|
|
290
|
+
export function objectProperties(node: J | undefined): Map<string, Expression> | undefined {
|
|
286
291
|
if (!node || !isObjectLiteral(node) || !node.body) return undefined;
|
|
287
292
|
const properties = new Map<string, Expression>();
|
|
288
293
|
for (const statement of node.body.statements) {
|
|
@@ -502,10 +507,12 @@ export function analyzeWrapperScope(block: Cursor, {declaration, name}: WrapperD
|
|
|
502
507
|
const name = terminalArgs[0].value as string;
|
|
503
508
|
const event = SIMULATED_EVENTS.has(name) ? name : NATIVE_EVENT_NAMES.get(name);
|
|
504
509
|
if (!event || !events.has(event)) return false;
|
|
505
|
-
|
|
506
|
-
|
|
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;
|
|
507
514
|
if (event === "click" && CLICK_DEFAULT_ACTIONS.has(selectorTag(selectorText) ?? "")) return false;
|
|
508
|
-
return record(terminal, {...read, use: "simulate", event, ...nativeEvent ? {nativeEvent} : {eventInit:
|
|
515
|
+
return record(terminal, {...read, use: "simulate", event, ...nativeEvent ? {nativeEvent} : {eventInit: init}});
|
|
509
516
|
}
|
|
510
517
|
return false;
|
|
511
518
|
}
|
|
@@ -365,7 +365,7 @@ export function resolveSliceReducer(node: J, cu: JS.CompilationUnit, local: Loca
|
|
|
365
365
|
|
|
366
366
|
export const SUGGESTED_ACTIONS: Readonly<Record<string, string>> = {
|
|
367
367
|
SLICE_STORE: "A store of `createSlice` reducers with the default middleware: each slice can become a zustand store. Check its consumers next.",
|
|
368
|
-
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.",
|
|
369
369
|
OPTIONS_NOT_LITERAL: "Write the `configureStore` options as an object literal, so that the setup can be read.",
|
|
370
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.",
|
|
371
371
|
NON_SLICE_REDUCER: "Rewrite this reducer with `createSlice`, or keep it in Redux; zustand has no place for a hand-written reducer.",
|
|
@@ -399,15 +399,167 @@ export function storeFunctions(cu: JS.CompilationUnit): {legacy: Set<string>, co
|
|
|
399
399
|
};
|
|
400
400
|
}
|
|
401
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
|
+
|
|
402
541
|
/**
|
|
403
542
|
* Why the store `call` creates can't move to zustand, or `[]` for a candidate, with the slices of its reducers by key.
|
|
404
543
|
* `undefined` when `call` creates no store.
|
|
405
544
|
*/
|
|
406
545
|
export function storeFindings(call: J.MethodInvocation, cu: JS.CompilationUnit, modules: ReadonlyMap<string, SliceModule>,
|
|
407
|
-
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 {
|
|
408
547
|
if (call.select) return undefined;
|
|
409
548
|
const api = call.name.simpleName;
|
|
410
|
-
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
|
+
}
|
|
411
563
|
if (!functions.configure.has(api)) return undefined;
|
|
412
564
|
const slices = new Map<string, SliceFacts>();
|
|
413
565
|
const options = objectProperties(call.arguments.elements[0]?.element);
|
|
@@ -416,8 +568,11 @@ export function storeFindings(call: J.MethodInvocation, cu: JS.CompilationUnit,
|
|
|
416
568
|
for (const key of options.keys()) {
|
|
417
569
|
if (key !== "reducer") findings.push({reason: BLOCKING_OPTIONS[key] ?? "UNKNOWN_OPTION", detail: key});
|
|
418
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";
|
|
419
574
|
const reducers = objectProperties(options.get("reducer"));
|
|
420
|
-
if (!reducers) return {api, findings: [...findings, {reason: "ROOT_REDUCER", detail: ""}], slices};
|
|
575
|
+
if (!reducers) return {api, findings: [...findings, {reason: "ROOT_REDUCER", detail: ""}], slices, reducerStore};
|
|
421
576
|
const local = localSlices(cu);
|
|
422
577
|
for (const [key, value] of reducers) {
|
|
423
578
|
const slice = resolveSliceReducer(value, cu, local, modules);
|
|
@@ -425,7 +580,7 @@ export function storeFindings(call: J.MethodInvocation, cu: JS.CompilationUnit,
|
|
|
425
580
|
else if (slice.blocker) findings.push(slice.blocker);
|
|
426
581
|
else slices.set(key, slice);
|
|
427
582
|
}
|
|
428
|
-
return {api, findings, slices};
|
|
583
|
+
return {api, findings, slices, reducerStore};
|
|
429
584
|
}
|
|
430
585
|
|
|
431
586
|
/** The variable or function a store is created in, such as `store` in `export const store = configureStore(...)`. */
|