@openrewrite/recipes-react 0.18.0 → 0.19.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.
Files changed (33) hide show
  1. package/dist/react19/no-implicit-ref-callback-return.d.ts.map +1 -1
  2. package/dist/react19/no-implicit-ref-callback-return.js +25 -5
  3. package/dist/react19/no-implicit-ref-callback-return.js.map +1 -1
  4. package/dist/testing/enzyme-host-components.d.ts +2 -1
  5. package/dist/testing/enzyme-host-components.d.ts.map +1 -1
  6. package/dist/testing/enzyme-host-components.js +33 -2
  7. package/dist/testing/enzyme-host-components.js.map +1 -1
  8. package/dist/testing/enzyme-to-react-testing-library.d.ts.map +1 -1
  9. package/dist/testing/enzyme-to-react-testing-library.js +4 -2
  10. package/dist/testing/enzyme-to-react-testing-library.js.map +1 -1
  11. package/dist/testing/enzyme-wrapper-scope.d.ts +1 -0
  12. package/dist/testing/enzyme-wrapper-scope.d.ts.map +1 -1
  13. package/dist/testing/enzyme-wrapper-scope.js +26 -0
  14. package/dist/testing/enzyme-wrapper-scope.js.map +1 -1
  15. package/dist/zustand/redux-stores.d.ts.map +1 -1
  16. package/dist/zustand/redux-stores.js +40 -3
  17. package/dist/zustand/redux-stores.js.map +1 -1
  18. package/dist/zustand/redux-to-zustand.d.ts +1 -1
  19. package/dist/zustand/redux-to-zustand.d.ts.map +1 -1
  20. package/dist/zustand/redux-to-zustand.js +3 -2
  21. package/dist/zustand/redux-to-zustand.js.map +1 -1
  22. package/dist/zustand/zustand-plan.d.ts +1 -0
  23. package/dist/zustand/zustand-plan.d.ts.map +1 -1
  24. package/dist/zustand/zustand-plan.js +15 -4
  25. package/dist/zustand/zustand-plan.js.map +1 -1
  26. package/package.json +1 -1
  27. package/src/react19/no-implicit-ref-callback-return.ts +34 -8
  28. package/src/testing/enzyme-host-components.ts +38 -3
  29. package/src/testing/enzyme-to-react-testing-library.ts +6 -4
  30. package/src/testing/enzyme-wrapper-scope.ts +24 -0
  31. package/src/zustand/redux-stores.ts +35 -3
  32. package/src/zustand/redux-to-zustand.ts +8 -5
  33. package/src/zustand/zustand-plan.ts +17 -6
@@ -192,17 +192,19 @@ const ADDS_DEPENDENCIES = (() => {
192
192
  * - `dispatch(increment())` and `store.dispatch(increment())`, whose result is unused, become `increment()`, and
193
193
  * `const dispatch = useDispatch()` goes;
194
194
  * - `store.getState().counter` becomes `useCounterStore.getState()`;
195
- * - `<Provider store={store}>` is replaced with its one child, and the store's source is removed.
195
+ * - `<Provider store={store}>` is replaced with its one child, and the store's source is removed;
196
+ * - a `createAsyncThunk` of the slice's source whose stages `extraReducers` handles stays RTK's, and runs its actions
197
+ * through those cases on the slice's zustand store; a dispatch of it becomes a call.
196
198
  *
197
- * Any other use, such as `connect`, a thunk, a selector that builds a value, or a dispatch whose result is used, keeps
198
- * the store in Redux, recorded in the `ReduxStores` table with the reason.
199
+ * Any other use, such as `connect`, a thunk that reaches the rest of the store, a selector that builds a value, or a
200
+ * dispatch whose result is used, keeps the store in Redux, recorded in the `ReduxStores` table with the reason.
199
201
  *
200
202
  * @see https://zustand.docs.pmnd.rs/guides/migrating-to-zustand
201
203
  */
202
204
  export class ReduxToZustand extends ScanningRecipe<Scanned> {
203
205
  readonly name = "org.openrewrite.react.migrate.redux-to-zustand";
204
206
  readonly displayName = "Migrate Redux Toolkit slices to zustand";
205
- readonly description = "Moves a repository's one Redux Toolkit store to zustand when it is a `configureStore` of `createSlice` reducers with no other option, its sources are JavaScript, and every use of it converts: each slice becomes a zustand store whose action creators keep their names and run the slice's reducers through immer's `produce`, `useSelector` reads of one slice become the slice's zustand hook, `dispatch` and `store.dispatch` of the slices' action creators become calls of them, `store.getState()` reads become the slice store's `getState()`, and the react-redux `Provider` and the store's source are removed. Adds `zustand` and `immer` where they are missing. Stores with any other use, such as `connect`, thunks, selectors that build values or several stores, stay in Redux and are recorded for review.";
207
+ readonly description = "Moves a repository's one Redux Toolkit store to zustand when it is a `configureStore` of `createSlice` reducers with no other option, its sources are JavaScript, and every use of it converts: each slice becomes a zustand store whose action creators keep their names and run the slice's reducers through immer's `produce`, `useSelector` reads of one slice become the slice's zustand hook, `dispatch` and `store.dispatch` of the slices' action creators become calls of them, `store.getState()` reads become the slice store's `getState()`, and the react-redux `Provider` and the store's source are removed. A slice's own `createAsyncThunk`s, whose stages its `extraReducers` handles, keep running on Redux Toolkit and send their actions to the slice's zustand store. Adds `zustand` and `immer` where they are missing. Stores with any other use, such as `connect`, thunks that reach the rest of the store, selectors that build values or several stores, stay in Redux and are recorded for review.";
206
208
 
207
209
  private readonly forecast: ZustandForecast = {refused: new Map()};
208
210
  private readonly storeSources: JS.CompilationUnit[] = [];
@@ -351,6 +353,7 @@ const CONVERSION_ACTIONS: Readonly<Record<string, string>> = {
351
353
  NAME_TAKEN: "Rename what already uses the name the conversion would give the zustand store or its helpers.",
352
354
  SLICE_OPTION: "Remove the `createSlice` option, or convert the slice by hand.",
353
355
  SLICE_NAME: "Write the slice's `name` as a string literal.",
356
+ SHARED_ACTION_TYPE: "Give the slices, or the thunk, different names: in Redux an action reaches the reducers and cases of every slice that handles its type, and in zustand only its own slice's.",
354
357
  INITIAL_STATE: "Write the initial state as a value, or a `const` holding one, rather than a function.",
355
358
  REDUCER: "Write each reducer as a function; `prepare` callbacks and reducers that use `this` are converted by hand.",
356
359
  ACTIONS_EXPORT: "Export the action creators as `export const {a, b} = slice.actions`, under their own names.",
@@ -358,7 +361,7 @@ const CONVERSION_ACTIONS: Readonly<Record<string, string>> = {
358
361
  SLICE_USE: "Use the slice only through its action creators and the store; other uses of the slice object are converted by hand.",
359
362
  STORE_USE: "Use the store only in `<Provider store>`, `store.getState().key` and `store.dispatch(action())`.",
360
363
  REACT_REDUX_USE: "Replace `connect`, `useStore` and other react-redux APIs with `useSelector` and `useDispatch`, or convert by hand.",
361
- DISPATCH: "Dispatch only the slices' action creators, as a statement of its own; thunks and dispatches whose result is used are converted by hand.",
364
+ DISPATCH: "Dispatch only the slices' action creators and their own thunks, as a statement of its own; other thunks and dispatches whose result is used are converted by hand.",
362
365
  SELECTOR: "Read one slice's state in each selector, such as `state => state.counter.value`; selectors that build a value are converted by hand.",
363
366
  UNFOLLOWED_IMPORT: "Import the store, its slices and react-redux with named or default imports; namespace imports, re-exports and module mocks are converted by hand.",
364
367
  TYPESCRIPT: "TypeScript sources are not converted yet.",
@@ -520,6 +520,7 @@ export function plan(facts: ReadonlyMap<string, SourceFacts>, sliceSources: Read
520
520
  const block = (reason: string, detail: string) => findings.push({reason, detail});
521
521
  // Slices that stay in Redux for a reason of their own, already recorded: their uses add nothing to it
522
522
  const held = new Map<string, string>();
523
+ const handled = new Map<string, string>();
523
524
  if (findings.length === 0) {
524
525
  if (!store.exportedAs) block("STORE_NOT_MODULE_LEVEL", store.store);
525
526
  const storeSource = sliceSources.get(store.path);
@@ -554,6 +555,12 @@ export function plan(facts: ReadonlyMap<string, SourceFacts>, sliceSources: Read
554
555
  // The other package's own uses of the slice aren't planned here
555
556
  if (!scope(slice.path)) block("SLICE_SOURCE", `${key} is declared in another package, ${slice.path}`);
556
557
  result.slices.set(key, planned);
558
+ // In Redux an action reaches every slice, so a type two slices handle, as when they share a name, runs both
559
+ for (const type of reducers.types) {
560
+ const other = handled.get(type);
561
+ if (other) block("SHARED_ACTION_TYPE", `\`${type}\` is handled by both \`${other}\` and \`${key}\``);
562
+ else handled.set(type, key);
563
+ }
557
564
  }
558
565
  }
559
566
  const slicePaths = new Map([...result.slices.values()].map(slice => [slice.path, slice]));
@@ -664,7 +671,8 @@ export interface SliceSource {
664
671
  /** The names the source declares or imports, which generated names must not take. */
665
672
  names: Set<string>;
666
673
  /** Each slice by variable, with the reducers it exports actions for, or why it can't be rewritten. */
667
- slices: Map<string, {actions: Set<string>, unknown: Set<string>, thunks: Set<string>, blocker?: Finding}>;
674
+ /** Each slice's action creators, names without a reducer, its thunks, the action types its reducers handle, and why it can't convert. */
675
+ slices: Map<string, {actions: Set<string>, unknown: Set<string>, thunks: Set<string>, types: Set<string>, blocker?: Finding}>;
668
676
  /** Whether the source holds only imports and the store, so that it can be removed. */
669
677
  storeOnly: boolean;
670
678
  }
@@ -683,20 +691,22 @@ export function sliceSource(cu: JS.CompilationUnit): SliceSource | undefined {
683
691
  s.kind === J.Kind.VariableDeclarations && (s as J.VariableDeclarations).variables.every(v => v.element.initializer?.element.kind === J.Kind.MethodInvocation &&
684
692
  functions.configure.has((v.element.initializer.element as J.MethodInvocation).name.simpleName)));
685
693
  for (const [variable, facts] of local.localSlices) {
686
- const slice = {actions: new Set<string>(), unknown: new Set<string>(), thunks: new Set(facts.thunks?.keys()), blocker: facts.blocker} as
687
- {actions: Set<string>, unknown: Set<string>, thunks: Set<string>, blocker?: Finding};
694
+ const slice = {actions: new Set<string>(), unknown: new Set<string>(), thunks: new Set(facts.thunks?.keys()), types: new Set<string>(), blocker: facts.blocker} as
695
+ {actions: Set<string>, unknown: Set<string>, thunks: Set<string>, types: Set<string>, blocker?: Finding};
688
696
  result.slices.set(variable, slice);
689
697
  if (slice.blocker) continue;
690
- slice.blocker = sliceBlocker(cu, variable, slice.actions, slice.unknown, slice.thunks, local.localReducers);
698
+ slice.blocker = sliceBlocker(cu, variable, slice.actions, slice.unknown, slice.thunks, slice.types, local.localReducers);
699
+ // A case handles its thunk's stage, as a reducer handles `name/key`
700
+ for (const {thunk, stage} of facts.cases ?? []) slice.types.add(`${facts.thunks!.get(thunk)!.typePrefix}/${stage}`);
691
701
  }
692
702
  return result;
693
703
  }
694
704
 
695
705
  /**
696
706
  * Why the slice in `variable` can't be rewritten, filling `actions` with the reducers it exports action creators for,
697
- * and `unknown` with the names it exports from `slice.actions` that no reducer has.
707
+ * `unknown` with the names it exports from `slice.actions` that no reducer has, and `types` with the action type each reducer handles.
698
708
  */
699
- function sliceBlocker(cu: JS.CompilationUnit, variable: string, actions: Set<string>, unknown: Set<string>, thunks: ReadonlySet<string>,
709
+ function sliceBlocker(cu: JS.CompilationUnit, variable: string, actions: Set<string>, unknown: Set<string>, thunks: ReadonlySet<string>, types: Set<string>,
700
710
  reducerVariables: ReadonlyMap<string, SliceFacts>): Finding | undefined {
701
711
  const block = (reason: string, detail: string): Finding => ({reason, detail: `${variable}: ${detail}`});
702
712
  const statements = cu.statements.map(s => s.element);
@@ -730,6 +740,7 @@ function sliceBlocker(cu: JS.CompilationUnit, variable: string, actions: Set<str
730
740
  if (!isReducerFunction(value!, statements)) return block("REDUCER", `\`${key}\` isn't a function, such as one with \`prepare\``);
731
741
  if (usesThis(value!)) return block("REDUCER", `\`${key}\` uses \`this\``);
732
742
  reducerNames.add(key);
743
+ types.add(`${(name as J.Literal).value}/${key}`);
733
744
  }
734
745
  // Every reference to the slice must be `slice.actions` destructured into exports, or `slice.reducer`
735
746
  // The exported names are collected first, as a statement above their export, such as a thunk, may use them