@openrewrite/recipes-react 0.20.0 → 0.21.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 (86) hide show
  1. package/dist/i18n/translate-hard-coded-strings.d.ts +67 -0
  2. package/dist/i18n/translate-hard-coded-strings.d.ts.map +1 -0
  3. package/dist/i18n/translate-hard-coded-strings.js +439 -0
  4. package/dist/i18n/translate-hard-coded-strings.js.map +1 -0
  5. package/dist/index.d.ts +2 -0
  6. package/dist/index.d.ts.map +1 -1
  7. package/dist/index.js +10 -1
  8. package/dist/index.js.map +1 -1
  9. package/dist/search/find-enzyme-usage.d.ts +4 -0
  10. package/dist/search/find-enzyme-usage.d.ts.map +1 -1
  11. package/dist/search/find-enzyme-usage.js +30 -3
  12. package/dist/search/find-enzyme-usage.js.map +1 -1
  13. package/dist/search/find-hard-coded-strings.d.ts +110 -0
  14. package/dist/search/find-hard-coded-strings.d.ts.map +1 -0
  15. package/dist/search/find-hard-coded-strings.js +987 -0
  16. package/dist/search/find-hard-coded-strings.js.map +1 -0
  17. package/dist/search/find-missing-use-client-directive.d.ts +2 -0
  18. package/dist/search/find-missing-use-client-directive.d.ts.map +1 -1
  19. package/dist/search/find-missing-use-client-directive.js +1 -0
  20. package/dist/search/find-missing-use-client-directive.js.map +1 -1
  21. package/dist/testing/enzyme-portals.d.ts +3 -5
  22. package/dist/testing/enzyme-portals.d.ts.map +1 -1
  23. package/dist/testing/enzyme-portals.js +8 -39
  24. package/dist/testing/enzyme-portals.js.map +1 -1
  25. package/dist/testing/enzyme-render-blockers.d.ts +10 -0
  26. package/dist/testing/enzyme-render-blockers.d.ts.map +1 -0
  27. package/dist/testing/enzyme-render-blockers.js +119 -0
  28. package/dist/testing/enzyme-render-blockers.js.map +1 -0
  29. package/dist/testing/enzyme-to-react-testing-library.d.ts +9 -2
  30. package/dist/testing/enzyme-to-react-testing-library.d.ts.map +1 -1
  31. package/dist/testing/enzyme-to-react-testing-library.js +80 -17
  32. package/dist/testing/enzyme-to-react-testing-library.js.map +1 -1
  33. package/dist/testing/enzyme-usage.d.ts +2 -0
  34. package/dist/testing/enzyme-usage.d.ts.map +1 -1
  35. package/dist/testing/enzyme-usage.js +29 -3
  36. package/dist/testing/enzyme-usage.js.map +1 -1
  37. package/dist/testing/enzyme-wrapper-scope.d.ts +5 -1
  38. package/dist/testing/enzyme-wrapper-scope.d.ts.map +1 -1
  39. package/dist/testing/enzyme-wrapper-scope.js +12 -3
  40. package/dist/testing/enzyme-wrapper-scope.js.map +1 -1
  41. package/dist/util/json-and-javascript.d.ts +4 -0
  42. package/dist/util/json-and-javascript.d.ts.map +1 -0
  43. package/dist/util/json-and-javascript.js +37 -0
  44. package/dist/util/json-and-javascript.js.map +1 -0
  45. package/dist/util/module-imports.d.ts +36 -0
  46. package/dist/util/module-imports.d.ts.map +1 -1
  47. package/dist/util/module-imports.js +216 -0
  48. package/dist/util/module-imports.js.map +1 -1
  49. package/dist/util/test-source.d.ts +2 -0
  50. package/dist/util/test-source.d.ts.map +1 -0
  51. package/dist/util/test-source.js +5 -0
  52. package/dist/util/test-source.js.map +1 -0
  53. package/dist/zustand/entry-store.d.ts +9 -0
  54. package/dist/zustand/entry-store.d.ts.map +1 -0
  55. package/dist/zustand/entry-store.js +136 -0
  56. package/dist/zustand/entry-store.js.map +1 -0
  57. package/dist/zustand/redux-stores.d.ts +5 -4
  58. package/dist/zustand/redux-stores.d.ts.map +1 -1
  59. package/dist/zustand/redux-stores.js +8 -5
  60. package/dist/zustand/redux-stores.js.map +1 -1
  61. package/dist/zustand/redux-to-zustand.d.ts +11 -2
  62. package/dist/zustand/redux-to-zustand.d.ts.map +1 -1
  63. package/dist/zustand/redux-to-zustand.js +137 -42
  64. package/dist/zustand/redux-to-zustand.js.map +1 -1
  65. package/dist/zustand/zustand-plan.d.ts +7 -5
  66. package/dist/zustand/zustand-plan.d.ts.map +1 -1
  67. package/dist/zustand/zustand-plan.js +87 -33
  68. package/dist/zustand/zustand-plan.js.map +1 -1
  69. package/package.json +1 -1
  70. package/src/i18n/translate-hard-coded-strings.ts +442 -0
  71. package/src/index.ts +7 -0
  72. package/src/search/find-enzyme-usage.ts +30 -6
  73. package/src/search/find-hard-coded-strings.ts +1006 -0
  74. package/src/search/find-missing-use-client-directive.ts +1 -1
  75. package/src/testing/enzyme-portals.ts +4 -44
  76. package/src/testing/enzyme-render-blockers.ts +113 -0
  77. package/src/testing/enzyme-to-react-testing-library.ts +90 -18
  78. package/src/testing/enzyme-usage.ts +35 -2
  79. package/src/testing/enzyme-wrapper-scope.ts +13 -2
  80. package/src/util/json-and-javascript.ts +26 -0
  81. package/src/util/module-imports.ts +223 -0
  82. package/src/util/test-source.ts +2 -0
  83. package/src/zustand/entry-store.ts +131 -0
  84. package/src/zustand/redux-stores.ts +12 -7
  85. package/src/zustand/redux-to-zustand.ts +168 -48
  86. package/src/zustand/zustand-plan.ts +98 -32
@@ -15,7 +15,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
15
15
  });
16
16
  };
17
17
  Object.defineProperty(exports, "__esModule", { value: true });
18
- exports.CONNECT = exports.SELECTOR = exports.ReduxToZustand = exports.AddImmer = exports.AddZustand4 = exports.AddZustand = void 0;
18
+ exports.SELECTOR = exports.ReduxToZustand = exports.AddImmer = exports.AddZustand4 = exports.AddZustand = void 0;
19
+ exports.connectSource = connectSource;
19
20
  const rewrite_1 = require("@openrewrite/rewrite");
20
21
  const javascript_1 = require("@openrewrite/rewrite/javascript");
21
22
  const java_1 = require("@openrewrite/rewrite/java");
@@ -26,6 +27,7 @@ const rewrite_2 = require("@openrewrite/rewrite");
26
27
  const java_recipe_proxy_1 = require("../util/java-recipe-proxy");
27
28
  const module_imports_1 = require("../util/module-imports");
28
29
  const enzyme_to_react_testing_library_1 = require("../testing/enzyme-to-react-testing-library");
30
+ const entry_store_1 = require("./entry-store");
29
31
  const redux_stores_1 = require("./redux-stores");
30
32
  const zustand_plan_1 = require("./zustand-plan");
31
33
  const ZUSTAND = "zustand";
@@ -36,6 +38,12 @@ const ZUSTAND_5 = {
36
38
  const ZUSTAND_4 = Object.assign(Object.assign({}, ZUSTAND_5), { version: "^4.5.7", onlyIfResolving: ["react@>=16.8.0 <18"] });
37
39
  const IMMER_10 = Object.assign(Object.assign({}, ZUSTAND_5), { packageName: IMMER, version: "^10.1.3", onlyIfResolving: ["react@>=16.8.0"] });
38
40
  const LOCKFILE = /(^|\/)(package-lock\.json|npm-shrinkwrap\.json|yarn\.lock|pnpm-lock\.yaml|bun\.lock)$/;
41
+ function resolverOf(scanned) {
42
+ return (from, specifier) => (0, module_imports_1.resolveModule)(from, specifier, scanned.sources, scanned.configs);
43
+ }
44
+ function followsImport(module) {
45
+ return module === zustand_plan_1.REACT_REDUX || module.startsWith(".") || (0, module_imports_1.isAliasSpecifier)(module);
46
+ }
39
47
  class ZustandDependency extends java_recipe_proxy_1.JavaDependencyRecipe {
40
48
  constructor(addition, forecast) {
41
49
  super("org.openrewrite.javascript.AddDependency", addition);
@@ -215,17 +223,19 @@ class ReduxToZustand extends rewrite_1.ScanningRecipe {
215
223
  super(options);
216
224
  this.name = "org.openrewrite.react.migrate.redux-to-zustand";
217
225
  this.displayName = "Migrate Redux Toolkit slices to zustand";
218
- this.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`, replaced with its children, 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. A store of its `reducer` alone whose slices can't move one by one, such as one with a reducer that is no slice's, thunks that reach the rest of the store or dispatches whose result is used, moves whole: its reducer runs on one zustand store whose `dispatch` runs thunks as Redux Toolkit's default middleware does, its selectors run on that store, through a hook of the store's source that keeps each selection per state where zustand 5 would run the selector on each read, and the store keeps its `getState` and `dispatch`, and its `subscribe`, whose listeners its `dispatch` calls after every action as Redux does, where a source subscribes to it. A legacy `createStore` of a reducer, and perhaps a preloaded state, moves whole too, its `dispatch` taking plain actions only, as Redux's does without middleware, or thunks as well where its enhancer is `applyMiddleware(thunk)`, perhaps inside `compose` or Redux DevTools, which is dropped. Components connected with `connect`, with no `mergeProps` and no option but `forwardRef`, move with a store that moves whole: its source declares a `connect` for the zustand store that passes the same props, copies the same statics and skips the same re-renders as react-redux's, and the store batches its updates as react-redux does. Adds `zustand`, and `immer` where slices move one by one, where they are missing. Stores with any other use, such as `connect` with `mergeProps`, other middleware or enhancers, other uses of the store object such as `persistStore(store)`, selectors with their own equality function or several stores, stay in Redux and are recorded for review.";
226
+ this.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`, replaced with its children, 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. A store of its `reducer` alone whose slices can't move one by one, such as one with a reducer that is no slice's, thunks that reach the rest of the store or dispatches whose result is used, moves whole: its reducer runs on one zustand store whose `dispatch` runs thunks as Redux Toolkit's default middleware does, its selectors run on that store, through a hook of the store's source that keeps each selection per state where zustand 5 would run the selector on each read, and the store keeps its `getState` and `dispatch`, and its `subscribe`, whose listeners its `dispatch` calls after every action as Redux does, where a source subscribes to it. A legacy `createStore` of a reducer, and perhaps a preloaded state, moves whole too, its `dispatch` taking plain actions only, as Redux's does without middleware, or thunks as well where its enhancer is `applyMiddleware(thunk)`, perhaps inside `compose` or Redux DevTools, which is dropped. Components connected with `connect`, with no `mergeProps` and no option but `forwardRef`, move with a store that moves whole: its source declares a `connect` for the zustand store that passes the same props, copies the same statics and skips the same re-renders as react-redux's, and the store batches its updates as react-redux does. A store that moves whole but is created, and not exported, at the top level of an application's entry source, such as the one that renders the `Provider`, moves to an `appStore` module beside that source, with the imports and top-level `const`s and `let`s only it uses, and the source imports back what it still uses. A store's source may export it in its declaration, as its default export or in an export list such as `export {store}`, and other sources may import it, and its slices, by relative paths or by aliases such as `@/store` that the `paths` of the nearest `tsconfig.json` or `jsconfig.json` declare. Adds `zustand`, and `immer` where slices move one by one, where they are missing. Stores with any other use, such as `connect` with `mergeProps`, other middleware or enhancers, other uses of the store object such as `persistStore(store)`, selectors with their own equality function or several stores, stay in Redux and are recorded for review.";
219
227
  this.forecast = { refused: new Map(), withoutImmer: new Set() };
220
228
  this.storeSources = [];
221
229
  this.steps = ADDS_DEPENDENCIES ? { zustand: new AddZustand(this.forecast), zustand4: new AddZustand4(this.forecast), immer: new AddImmer(this.forecast) } : undefined;
222
230
  this.stores = new rewrite_1.DataTable(redux_stores_1.REDUX_STORES_TABLE, "Redux stores", "The Redux stores of the repository and whether each one's setup can move to zustand, with one row for each reason it can't.", redux_stores_1.ReduxStoreRow);
231
+ this.reported = new WeakMap();
232
+ this.splits = new WeakMap();
223
233
  this.plans = new WeakMap();
224
234
  (_a = this.wholeStores) !== null && _a !== void 0 ? _a : (this.wholeStores = true);
225
235
  }
226
236
  initialValue() {
227
237
  this.storeSources.length = 0;
228
- return { sources: new Map(), modules: new Map(), sliceSources: new Map(), packages: new Map() };
238
+ return { sources: new Map(), modules: new Map(), sliceSources: new Map(), packages: new Map(), configs: new Map() };
229
239
  }
230
240
  scanner(scanned) {
231
241
  return __awaiter(this, void 0, void 0, function* () {
@@ -261,8 +271,9 @@ class ReduxToZustand extends rewrite_1.ScanningRecipe {
261
271
  if (/(^|\/)package\.json$/.test(doc.sourcePath)) {
262
272
  const declared = (0, javascript_1.findNodeResolutionResult)(doc) ? undefined : yield declaredDependencies(doc);
263
273
  const get = (name) => declared ? declared[name] : version(doc, name);
264
- scanned.packages.set(packageRoot(doc.sourcePath), { react: get("react"), reactDom: get("react-dom"), reactNative: get("react-native"), zustand: get(ZUSTAND), immer: get(IMMER) });
274
+ scanned.packages.set(packageRoot(doc.sourcePath), { react: get("react"), reactDom: get("react-dom"), reactNative: get("react-native"), zustand: get(ZUSTAND), immer: get(IMMER), redux: get("redux"), toolkit: get(redux_stores_1.RTK), reactRedux: get("react-redux") });
265
275
  }
276
+ yield (0, module_imports_1.addPathsConfig)(scanned.configs, doc);
266
277
  return doc;
267
278
  });
268
279
  }
@@ -292,10 +303,13 @@ class ReduxToZustand extends rewrite_1.ScanningRecipe {
292
303
  return cached;
293
304
  }
294
305
  const result = (() => __awaiter(this, void 0, void 0, function* () {
306
+ var _a, _b;
307
+ const splits = yield this.split(scanned);
295
308
  for (const cu of this.storeSources) {
296
- const facts = scanned.sources.get(cu.sourcePath);
309
+ const source = (_b = (_a = splits.get(cu.sourcePath)) === null || _a === void 0 ? void 0 : _a.module) !== null && _b !== void 0 ? _b : cu;
310
+ const facts = scanned.sources.get(source.sourcePath);
297
311
  if (facts)
298
- facts.stores = yield (0, zustand_plan_1.storeFacts)(cu, scanned.modules);
312
+ facts.stores = yield (0, zustand_plan_1.storeFacts)(source, scanned.modules, resolverOf(scanned));
299
313
  }
300
314
  return (0, zustand_plan_1.planPackages)(scanned.sources, scanned.sliceSources, path => packageOf(scanned.packages, path), (storePath, immer) => this.dependencies(scanned, storePath, forecast, immer), this.wholeStores !== false, storePath => {
301
315
  var _a;
@@ -305,13 +319,59 @@ class ReduxToZustand extends rewrite_1.ScanningRecipe {
305
319
  var _a, _b, _c;
306
320
  const facts = scanned.packages.get((_a = packageOf(scanned.packages, storePath)) !== null && _a !== void 0 ? _a : "");
307
321
  return (facts === null || facts === void 0 ? void 0 : facts.zustand) !== undefined ? ((_b = major(facts.zustand)) !== null && _b !== void 0 ? _b : 0) >= 5 : ((_c = major(facts === null || facts === void 0 ? void 0 : facts.react)) !== null && _c !== void 0 ? _c : 0) >= 18;
308
- });
322
+ }, resolverOf(scanned));
309
323
  }))();
310
324
  if (!forecast)
311
325
  this.plans.set(scanned, result);
312
326
  return result;
313
327
  });
314
328
  }
329
+ split(scanned) {
330
+ return __awaiter(this, void 0, void 0, function* () {
331
+ const known = this.splits.get(scanned);
332
+ if (known)
333
+ return known;
334
+ const splits = new Map();
335
+ this.splits.set(scanned, splits);
336
+ const paths = [...scanned.sources.keys()];
337
+ const created = new Map();
338
+ for (const cu of this.storeSources)
339
+ created.set(cu, yield (0, zustand_plan_1.storeFacts)(cu, scanned.modules, resolverOf(scanned)));
340
+ const storesIn = (root) => [...created].filter(([cu]) => packageOf(scanned.packages, cu.sourcePath) === root).reduce((n, [, stores]) => n + stores.length, 0);
341
+ for (const [cu, stores] of created) {
342
+ if (/\.(?:ts|tsx|mts|cts)$/.test(cu.sourcePath) || redux_stores_1.TEST_SOURCE.test(cu.sourcePath))
343
+ continue;
344
+ if (stores.length !== 1 || stores[0].exportedAs || storesIn(packageOf(scanned.packages, cu.sourcePath)) !== 1)
345
+ continue;
346
+ const directory = cu.sourcePath.includes("/") ? cu.sourcePath.slice(0, cu.sourcePath.lastIndexOf("/") + 1) : "";
347
+ const base = directory + entry_store_1.ENTRY_STORE_MODULE;
348
+ if (paths.some(path => path.startsWith(base + ".") || path.startsWith(base + "/")))
349
+ continue;
350
+ const split = yield (0, entry_store_1.splitEntryStore)(cu, stores[0].store, base + ".js", new rewrite_1.ExecutionContext());
351
+ if (!split)
352
+ continue;
353
+ splits.set(cu.sourcePath, Object.assign(Object.assign({}, split), { modulePath: base + ".js" }));
354
+ paths.push(base + ".js");
355
+ for (const source of [split.entry, split.module]) {
356
+ const { reducers, slices } = (0, redux_stores_1.localSlices)(source);
357
+ scanned.modules.set(source.sourcePath, { reducers, slices });
358
+ scanned.sources.set(source.sourcePath, yield (0, zustand_plan_1.sourceFacts)(source));
359
+ const slice = (0, zustand_plan_1.sliceSource)(source);
360
+ if (slice)
361
+ scanned.sliceSources.set(source.sourcePath, slice);
362
+ else
363
+ scanned.sliceSources.delete(source.sourcePath);
364
+ }
365
+ }
366
+ return splits;
367
+ });
368
+ }
369
+ generate(scanned, ctx) {
370
+ return __awaiter(this, void 0, void 0, function* () {
371
+ const plans = yield this.planned(scanned, true);
372
+ return [...(yield this.split(scanned)).values()].filter(split => plans.some(p => { var _a; return ((_a = p.store) === null || _a === void 0 ? void 0 : _a.path) === split.modulePath && p.reducerStore; })).map(split => split.module);
373
+ });
374
+ }
315
375
  dependencies(scanned, storePath, forecast, needsImmer = true) {
316
376
  var _a, _b, _c, _d, _e, _f, _g, _h, _j;
317
377
  const root = packageOf(scanned.packages, storePath);
@@ -338,38 +398,45 @@ class ReduxToZustand extends rewrite_1.ScanningRecipe {
338
398
  return __awaiter(this, void 0, void 0, function* () {
339
399
  const recipe = this;
340
400
  const stores = this.stores;
341
- const reported = new Set();
401
+ let reported = this.reported.get(scanned);
402
+ if (!reported)
403
+ this.reported.set(scanned, reported = new Set());
342
404
  return (0, rewrite_2.check)((0, rewrite_2.not)((0, javascript_1.isVendoredOrBundled)()), new class extends javascript_1.JavaScriptVisitor {
343
405
  visitJsCompilationUnit(cu, ctx) {
344
406
  return __awaiter(this, void 0, void 0, function* () {
345
- var _a, _b, _c, _d, _e;
407
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
346
408
  const plans = yield recipe.planned(scanned);
347
409
  const facts = scanned.sources.get(cu.sourcePath);
348
- for (const store of (_a = facts === null || facts === void 0 ? void 0 : facts.stores) !== null && _a !== void 0 ? _a : []) {
410
+ const splits = yield recipe.split(scanned);
411
+ const split = splits.get(cu.sourcePath);
412
+ const entryOf = (path) => { var _a, _b; return (_b = (_a = [...splits].find(([, s]) => s.modulePath === path)) === null || _a === void 0 ? void 0 : _a[0]) !== null && _b !== void 0 ? _b : path; };
413
+ for (const store of [...(_a = facts === null || facts === void 0 ? void 0 : facts.stores) !== null && _a !== void 0 ? _a : [], ...split ? (_c = (_b = scanned.sources.get(split.modulePath)) === null || _b === void 0 ? void 0 : _b.stores) !== null && _c !== void 0 ? _c : [] : []]) {
349
414
  const key = `${store.path}\0${store.store}\0${store.api}`;
350
415
  if (reported.has(key))
351
416
  continue;
352
417
  reported.add(key);
353
- const p = (_b = plans.find(candidate => candidate.store === store)) !== null && _b !== void 0 ? _b : plans.find(candidate => candidate.blocked.has(store));
354
- const findings = (p === null || p === void 0 ? void 0 : p.store) === store ? [] : (_c = p === null || p === void 0 ? void 0 : p.blocked.get(store)) !== null && _c !== void 0 ? _c : store.findings;
418
+ const p = (_d = plans.find(candidate => candidate.store === store)) !== null && _d !== void 0 ? _d : plans.find(candidate => candidate.blocked.has(store));
419
+ const findings = (p === null || p === void 0 ? void 0 : p.store) === store ? [] : (_e = p === null || p === void 0 ? void 0 : p.blocked.get(store)) !== null && _e !== void 0 ? _e : store.findings;
355
420
  const rows = findings.length === 0 ? [{ reason: (p === null || p === void 0 ? void 0 : p.reducerStore) ? "REDUCER_STORE" : "SLICE_STORE", detail: "" }] : findings;
356
- for (const { reason, detail } of rows) {
421
+ for (const { reason, detail, action } of rows) {
357
422
  stores.insertRow(ctx, {
358
- sourcePath: store.path, store: store.store, api: store.api,
423
+ sourcePath: entryOf(store.path), store: store.store, api: store.api,
359
424
  status: (p === null || p === void 0 ? void 0 : p.store) === store ? "CONVERTED" : "REVIEW", reason, detail,
360
- testSource: redux_stores_1.TEST_SOURCE.test(store.path), suggestedAction: (_e = (_d = redux_stores_1.SUGGESTED_ACTIONS[reason]) !== null && _d !== void 0 ? _d : CONVERSION_ACTIONS[reason]) !== null && _e !== void 0 ? _e : ""
425
+ testSource: redux_stores_1.TEST_SOURCE.test(entryOf(store.path)), suggestedAction: (_g = (_f = action !== null && action !== void 0 ? action : redux_stores_1.SUGGESTED_ACTIONS[reason]) !== null && _f !== void 0 ? _f : CONVERSION_ACTIONS[reason]) !== null && _g !== void 0 ? _g : ""
361
426
  });
362
427
  }
363
428
  }
364
429
  const p = plans.find(candidate => candidate.store && candidate.sources.has(cu.sourcePath));
430
+ const generated = [...splits.values()].some(s => s.modulePath === cu.sourcePath);
365
431
  if (!(p === null || p === void 0 ? void 0 : p.store))
366
- return cu;
432
+ return generated ? undefined : cu;
367
433
  if (p.reducerStore)
368
- return cu.sourcePath === p.store.path ? convertReducerStore(cu, p, ctx) : convertReducerConsumer(cu, p, scanned, ctx);
434
+ return cu.sourcePath === p.store.path ? convertReducerStore(cu, p, scanned.packages.get((_h = packageOf(scanned.packages, cu.sourcePath)) !== null && _h !== void 0 ? _h : ""), ctx) : convertReducerConsumer((_j = split === null || split === void 0 ? void 0 : split.entry) !== null && _j !== void 0 ? _j : cu, p, scanned, ctx);
369
435
  if (cu.sourcePath === p.store.path)
370
436
  return undefined;
371
437
  const slice = [...p.slices.values()].find(s => s.path === cu.sourcePath);
372
- const converted = slice ? yield convertSlice(this, cu, slice, scanned, ctx) : cu;
438
+ const source = (_k = split === null || split === void 0 ? void 0 : split.entry) !== null && _k !== void 0 ? _k : cu;
439
+ const converted = slice ? yield convertSlice(this, source, slice, scanned, ctx) : source;
373
440
  return convertConsumer(converted, p, scanned, ctx);
374
441
  });
375
442
  }
@@ -394,7 +461,7 @@ __decorate([
394
461
  ], ReduxToZustand.prototype, "wholeStores", void 0);
395
462
  const CONVERSION_ACTIONS = {
396
463
  MULTIPLE_STORES: "Convert by hand, or one store at a time: the uses of react-redux can't be told apart by store.",
397
- STORE_NOT_MODULE_LEVEL: "Create the store once at the top level of its module; a zustand store is a module-level singleton.",
464
+ STORE_NOT_MODULE_LEVEL: "Create the store once at the top level of its module, and export it; a zustand store is a module-level singleton. A store its source creates at the top level moves to a module of its own where only imports and top-level `const`s and `let`s it uses move with it, nothing that stays uses them, and no `appStore` module is beside the source already.",
398
465
  STORE_SOURCE: "Move what else the store's source declares, such as types or helpers, to another module.",
399
466
  SLICE_SOURCE: "Declare each slice in a source of its own, held in a `const` of its own.",
400
467
  SLICE_KEY: "Use a reducer key that is a JavaScript identifier, which names the zustand hook.",
@@ -607,10 +674,10 @@ function withPrefix(node, prefix) {
607
674
  function convertConsumer(cu, p, scanned, ctx) {
608
675
  return __awaiter(this, void 0, void 0, function* () {
609
676
  const bindings = (0, module_imports_1.importBindings)(cu);
610
- const followed = new Map(bindings.filter(b => b.module === zustand_plan_1.REACT_REDUX || b.module.startsWith(".")).map(b => [b.local, b]));
677
+ const followed = new Map(bindings.filter(b => followsImport(b.module)).map(b => [b.local, b]));
611
678
  const isStore = (name) => {
612
679
  const binding = followed.get(name);
613
- return !!binding && binding.module !== zustand_plan_1.REACT_REDUX && (0, module_imports_1.resolveRelative)(cu.sourcePath, binding.module, scanned.sources) === p.store.path &&
680
+ return !!binding && binding.module !== zustand_plan_1.REACT_REDUX && resolverOf(scanned)(cu.sourcePath, binding.module) === p.store.path &&
614
681
  binding.name === p.store.exportedAs;
615
682
  };
616
683
  const reactRedux = (name, member) => { var _a, _b; return ((_a = followed.get(name)) === null || _a === void 0 ? void 0 : _a.module) === zustand_plan_1.REACT_REDUX && ((_b = followed.get(name)) === null || _b === void 0 ? void 0 : _b.name) === member; };
@@ -840,7 +907,16 @@ exports.SELECTOR = `export function SELECTOR_HOOK(selector) {
840
907
  };
841
908
  return useSyncExternalStore(HOOK.subscribe, select, select);
842
909
  }`;
843
- exports.CONNECT = `export function connect(mapStateToProps, mapDispatchToProps, mergeProps, options) {
910
+ const STORE_UPDATE_ERROR = {
911
+ swallowed: `if (!update) throw error;`,
912
+ rethrown: `if (update) cache.error = error;
913
+ throw error;`
914
+ };
915
+ function connectSource(hook, reactRedux) {
916
+ var _a;
917
+ return CONNECT.split("HOOK").join(hook).replace("STORE_UPDATE_ERROR", STORE_UPDATE_ERROR[((_a = major(reactRedux)) !== null && _a !== void 0 ? _a : 8) >= 8 ? "swallowed" : "rethrown"]);
918
+ }
919
+ const CONNECT = `export function connect(mapStateToProps, mapDispatchToProps, mergeProps, options) {
844
920
  const forwardsRef = Boolean(options && options.forwardRef);
845
921
  const selectState = mapStateToProps ? state => state : () => undefined;
846
922
  const dependsOnOwnProps = map => map.dependsOnOwnProps !== undefined && map.dependsOnOwnProps !== null ? Boolean(map.dependsOnOwnProps) : map.length !== 1;
@@ -903,17 +979,34 @@ exports.CONNECT = `export function connect(mapStateToProps, mapDispatchToProps,
903
979
  }
904
980
  }
905
981
  const ownProps = cache.ownProps;
906
- if (!cache.stateProps || (mapState && (cache.state !== state || (mapState.dependsOnOwnProps() && cache.stateOwnProps !== ownProps)))) {
907
- cache.stateProps = mapState ? mapState.map(state, ownProps) : {};
982
+ // Own props compare shallowly with those of the last render, whether that render finished or not
983
+ const ownPropsChanged = !cache.seenOwnProps || !shallowEqual(cache.seenOwnProps, ownProps);
984
+ cache.seenOwnProps = ownProps;
985
+ const propsChanged = Boolean(mapState) && mapState.dependsOnOwnProps() && !(cache.stateOwnProps && shallowEqual(cache.stateOwnProps, ownProps));
986
+ if (!cache.stateProps || (mapState && (cache.state !== state || propsChanged))) {
987
+ // The state and props count as mapped before the map runs, so a render React retries after the map
988
+ // throws keeps the props from before
989
+ const update = cache.stateProps !== undefined && cache.state !== state && !propsChanged;
908
990
  cache.state = state;
909
991
  cache.stateOwnProps = ownProps;
992
+ cache.error = undefined;
993
+ try {
994
+ cache.stateProps = mapState ? mapState.map(state, ownProps) : {};
995
+ cache.kept = false;
996
+ } catch (error) {
997
+ // The props passed last stay, own props too, until the map returns again
998
+ cache.kept = true;
999
+ STORE_UPDATE_ERROR
1000
+ }
1001
+ } else if (cache.error) {
1002
+ throw cache.error;
910
1003
  }
911
1004
  if (!cache.dispatchProps || (mapDispatch && mapDispatch.dependsOnOwnProps() && cache.dispatchOwnProps !== ownProps)) {
912
1005
  cache.dispatchProps = mapDispatch ? mapDispatch.map(HOOK.dispatch, ownProps) : mapDispatchToProps ? bound(mapDispatchToProps) : {dispatch: HOOK.dispatch};
913
1006
  cache.dispatchOwnProps = ownProps;
914
1007
  }
915
1008
  const merged = {...ownProps, ...cache.stateProps, ...cache.dispatchProps};
916
- if (!cache.merged || !shallowEqual(cache.merged, merged)) cache.merged = merged;
1009
+ if (!cache.merged || (!cache.kept || ownPropsChanged) && !shallowEqual(cache.merged, merged)) cache.merged = merged;
917
1010
  if (!cache.element || cache.rendered !== cache.merged || cache.ref !== props.reactReduxForwardedRef) {
918
1011
  cache.rendered = cache.merged;
919
1012
  cache.ref = props.reactReduxForwardedRef;
@@ -932,7 +1025,14 @@ exports.CONNECT = `export function connect(mapStateToProps, mapDispatchToProps,
932
1025
  return hoistStatics(Forwarded, WrappedComponent);
933
1026
  };
934
1027
  }`;
935
- function convertReducerStore(cu, p, ctx) {
1028
+ function typedByString(facts, fromToolkit) {
1029
+ var _a;
1030
+ const redux = fromToolkit ? undefined : major(facts === null || facts === void 0 ? void 0 : facts.redux);
1031
+ if (redux !== undefined)
1032
+ return redux >= 5;
1033
+ return ((_a = major(facts === null || facts === void 0 ? void 0 : facts.toolkit)) !== null && _a !== void 0 ? _a : 0) >= 2;
1034
+ }
1035
+ function convertReducerStore(cu, p, facts, ctx) {
936
1036
  return __awaiter(this, void 0, void 0, function* () {
937
1037
  var _a;
938
1038
  const store = p.store;
@@ -946,6 +1046,9 @@ function convertReducerStore(cu, p, ctx) {
946
1046
  const combined = store.reducerStore === "combined";
947
1047
  const legacy = store.reducerStore === "legacy" || store.reducerStore === "legacyThunk";
948
1048
  const thunk = store.reducerStore === "legacyThunk";
1049
+ const checks = `if (typeof action !== "object" || action === null || ![Object.prototype, null].includes(Object.getPrototypeOf(action))) throw new Error("Actions must be plain objects.");
1050
+ if (action.type === undefined) throw new Error("Actions may not have an undefined 'type' property.");` +
1051
+ (typedByString(facts, (0, redux_stores_1.importedAs)(cu, redux_stores_1.RTK, "configureStore", "createStore", "legacy_createStore").has(store.api)) ? `\n if (typeof action.type !== "string") throw new Error("Action 'type' property must be a string.");` : "");
949
1052
  const update = `${hook}.setState(${reducer}(${hook}.getState(), action), true);`;
950
1053
  const notified = listeners ? `${update}\n for (const listener of [...${listeners}.values()]) listener();` : update;
951
1054
  const setState = renderer ? listeners ? `unstable_batchedUpdates(() => {\n ${notified}\n });` : `unstable_batchedUpdates(() => ${update.slice(0, -1)});` : notified;
@@ -964,23 +1067,15 @@ function convertReducerStore(cu, p, ctx) {
964
1067
  yield (0, javascript_1.template) `export const ${(0, javascript_1.raw)(hook)} = create(() => ${(0, javascript_1.raw)(reducer)}(${found.preloaded}, {type: "@@redux/INIT"}));`.apply(statement, cursor) :
965
1068
  yield (0, javascript_1.template) `export const ${(0, javascript_1.raw)(hook)} = create(() => ${(0, javascript_1.raw)(reducer)}(undefined, {type: "@@redux/INIT"}));`.apply(statement, cursor),
966
1069
  ...listeners ? [yield (0, javascript_1.template) `const ${(0, javascript_1.raw)(listeners)} = new Map();`.apply(statement, cursor)] : [],
967
- thunk ?
1070
+ legacy && !thunk ?
968
1071
  yield (0, javascript_1.template) `${(0, javascript_1.raw)(hook)}.dispatch = action => {
969
- if (typeof action === "function") return action(${(0, javascript_1.raw)(hook)}.dispatch, ${(0, javascript_1.raw)(hook)}.getState, undefined);
970
- if (typeof action !== "object" || action === null || ![Object.prototype, null].includes(Object.getPrototypeOf(action))) throw new Error("Actions must be plain objects.");
971
- if (action.type === undefined) throw new Error("Actions may not have an undefined 'type' property.");
972
- ${(0, javascript_1.raw)(setState)}
973
- return action;
974
- };`.apply(statement, cursor) :
975
- legacy ?
976
- yield (0, javascript_1.template) `${(0, javascript_1.raw)(hook)}.dispatch = action => {
977
- if (typeof action !== "object" || action === null || ![Object.prototype, null].includes(Object.getPrototypeOf(action))) throw new Error("Actions must be plain objects.");
978
- if (action.type === undefined) throw new Error("Actions may not have an undefined 'type' property.");
1072
+ ${(0, javascript_1.raw)(checks)}
979
1073
  ${(0, javascript_1.raw)(setState)}
980
1074
  return action;
981
1075
  };`.apply(statement, cursor) :
982
- yield (0, javascript_1.template) `${(0, javascript_1.raw)(hook)}.dispatch = action => {
1076
+ yield (0, javascript_1.template) `${(0, javascript_1.raw)(hook)}.dispatch = action => {
983
1077
  if (typeof action === "function") return action(${(0, javascript_1.raw)(hook)}.dispatch, ${(0, javascript_1.raw)(hook)}.getState, undefined);
1078
+ ${(0, javascript_1.raw)(checks)}
984
1079
  ${(0, javascript_1.raw)(setState)}
985
1080
  return action;
986
1081
  };`.apply(statement, cursor),
@@ -988,7 +1083,7 @@ function convertReducerStore(cu, p, ctx) {
988
1083
  yield (0, javascript_1.template) `export default {${(0, javascript_1.raw)(members)}};`.apply(statement, cursor) :
989
1084
  yield (0, javascript_1.template) `${(0, javascript_1.raw)(statement.modifiers.some(m => m.keyword === "export") ? "export " : "")}const ${(0, javascript_1.raw)(store.store)} = {${(0, javascript_1.raw)(members)}};`.apply(statement, cursor),
990
1085
  ...selectorHook ? [yield (0, javascript_1.template) `${(0, javascript_1.raw)(exports.SELECTOR.split("SELECTOR_HOOK").join(selectorHook).split("HOOK").join(hook))}`.apply(statement, cursor)] : [],
991
- ...renderer ? [yield (0, javascript_1.template) `${(0, javascript_1.raw)(exports.CONNECT.split("HOOK").join(hook))}`.apply(statement, cursor)] : []
1086
+ ...renderer ? [yield (0, javascript_1.template) `${(0, javascript_1.raw)(connectSource(hook, facts === null || facts === void 0 ? void 0 : facts.reactRedux))}`.apply(statement, cursor)] : []
992
1087
  ];
993
1088
  const statements = [...cu.statements.slice(0, found.index)];
994
1089
  for (let i = 0; i < generated.length; i++) {
@@ -1051,9 +1146,9 @@ function convertReducerConsumer(cu, p, scanned, ctx) {
1051
1146
  const { hook, selectorHook } = p.reducerStore;
1052
1147
  const source = scanned.sources.get(cu.sourcePath);
1053
1148
  const reads = (selector) => !!(0, zustand_plan_1.selectorRead)(selector) ||
1054
- (0, java_1.isIdentifier)(selector) && !!source && (0, zustand_plan_1.readSelector)(source, selector.simpleName, scanned.sources);
1149
+ (0, java_1.isIdentifier)(selector) && !!source && (0, zustand_plan_1.readSelector)(source, selector.simpleName, scanned.sources, resolverOf(scanned));
1055
1150
  const bindings = (0, module_imports_1.importBindings)(cu);
1056
- const followed = new Map(bindings.filter(b => b.module === zustand_plan_1.REACT_REDUX || b.module.startsWith(".")).map(b => [b.local, b]));
1151
+ const followed = new Map(bindings.filter(b => followsImport(b.module)).map(b => [b.local, b]));
1057
1152
  const reactRedux = (name, member) => { var _a, _b; return ((_a = followed.get(name)) === null || _a === void 0 ? void 0 : _a.module) === zustand_plan_1.REACT_REDUX && ((_b = followed.get(name)) === null || _b === void 0 ? void 0 : _b.name) === member; };
1058
1153
  const removed = new Set();
1059
1154
  const uses = new Set();
@@ -1129,7 +1224,7 @@ function convertReducerConsumer(cu, p, scanned, ctx) {
1129
1224
  }
1130
1225
  if (removed.has("Provider")) {
1131
1226
  for (const binding of bindings) {
1132
- if (binding.module !== zustand_plan_1.REACT_REDUX && (0, module_imports_1.resolveRelative)(cu.sourcePath, binding.module, scanned.sources) === p.store.path)
1227
+ if (binding.module !== zustand_plan_1.REACT_REDUX && resolverOf(scanned)(cu.sourcePath, binding.module) === p.store.path)
1133
1228
  (0, javascript_1.maybeRemoveImport)(this, binding.module, binding.name);
1134
1229
  }
1135
1230
  }