@openrewrite/recipes-react 0.16.0 → 0.18.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 (244) hide show
  1. package/dist/index.d.ts +1 -1
  2. package/dist/index.d.ts.map +1 -1
  3. package/dist/index.js +10 -5
  4. package/dist/index.js.map +1 -1
  5. package/dist/migration/cra-to-vite/add-vite-config.d.ts +7 -0
  6. package/dist/migration/cra-to-vite/add-vite-config.d.ts.map +1 -1
  7. package/dist/migration/cra-to-vite/add-vite-config.js +58 -1
  8. package/dist/migration/cra-to-vite/add-vite-config.js.map +1 -1
  9. package/dist/migration/cra-to-vite/cra-file-gate.d.ts +3 -0
  10. package/dist/migration/cra-to-vite/cra-file-gate.d.ts.map +1 -0
  11. package/dist/migration/cra-to-vite/cra-file-gate.js +10 -0
  12. package/dist/migration/cra-to-vite/cra-file-gate.js.map +1 -0
  13. package/dist/migration/cra-to-vite/cra-runtime.d.ts +2 -0
  14. package/dist/migration/cra-to-vite/cra-runtime.d.ts.map +1 -0
  15. package/dist/migration/cra-to-vite/cra-runtime.js +24 -0
  16. package/dist/migration/cra-to-vite/cra-runtime.js.map +1 -0
  17. package/dist/migration/cra-to-vite/cra-script.d.ts +8 -0
  18. package/dist/migration/cra-to-vite/cra-script.d.ts.map +1 -0
  19. package/dist/migration/cra-to-vite/cra-script.js +82 -0
  20. package/dist/migration/cra-to-vite/cra-script.js.map +1 -0
  21. package/dist/migration/cra-to-vite/cra-to-vite.d.ts +2 -1
  22. package/dist/migration/cra-to-vite/cra-to-vite.d.ts.map +1 -1
  23. package/dist/migration/cra-to-vite/cra-to-vite.js +23 -16
  24. package/dist/migration/cra-to-vite/cra-to-vite.js.map +1 -1
  25. package/dist/migration/cra-to-vite/cra-typecheck.d.ts +4 -0
  26. package/dist/migration/cra-to-vite/cra-typecheck.d.ts.map +1 -0
  27. package/dist/migration/cra-to-vite/cra-typecheck.js +35 -0
  28. package/dist/migration/cra-to-vite/cra-typecheck.js.map +1 -0
  29. package/dist/migration/cra-to-vite/find-cra-config-blockers.d.ts.map +1 -1
  30. package/dist/migration/cra-to-vite/find-cra-config-blockers.js +5 -6
  31. package/dist/migration/cra-to-vite/find-cra-config-blockers.js.map +1 -1
  32. package/dist/migration/cra-to-vite/index.d.ts +8 -2
  33. package/dist/migration/cra-to-vite/index.d.ts.map +1 -1
  34. package/dist/migration/cra-to-vite/index.js +17 -2
  35. package/dist/migration/cra-to-vite/index.js.map +1 -1
  36. package/dist/migration/cra-to-vite/migrate-cra-package.d.ts +17 -0
  37. package/dist/migration/cra-to-vite/migrate-cra-package.d.ts.map +1 -0
  38. package/dist/migration/cra-to-vite/migrate-cra-package.js +181 -0
  39. package/dist/migration/cra-to-vite/migrate-cra-package.js.map +1 -0
  40. package/dist/migration/cra-to-vite/migrate-cra-svg-imports.d.ts +9 -0
  41. package/dist/migration/cra-to-vite/migrate-cra-svg-imports.d.ts.map +1 -0
  42. package/dist/migration/cra-to-vite/migrate-cra-svg-imports.js +66 -0
  43. package/dist/migration/cra-to-vite/migrate-cra-svg-imports.js.map +1 -0
  44. package/dist/migration/cra-to-vite/migrate-cra-tests-to-vitest.d.ts +49 -0
  45. package/dist/migration/cra-to-vite/migrate-cra-tests-to-vitest.d.ts.map +1 -0
  46. package/dist/migration/cra-to-vite/migrate-cra-tests-to-vitest.js +506 -0
  47. package/dist/migration/cra-to-vite/migrate-cra-tests-to-vitest.js.map +1 -0
  48. package/dist/migration/cra-to-vite/move-public-index-html.d.ts +2 -0
  49. package/dist/migration/cra-to-vite/move-public-index-html.d.ts.map +1 -1
  50. package/dist/migration/cra-to-vite/move-public-index-html.js +12 -7
  51. package/dist/migration/cra-to-vite/move-public-index-html.js.map +1 -1
  52. package/dist/migration/cra-to-vite/plan-cra-to-vite.d.ts +30 -0
  53. package/dist/migration/cra-to-vite/plan-cra-to-vite.d.ts.map +1 -0
  54. package/dist/migration/cra-to-vite/plan-cra-to-vite.js +428 -0
  55. package/dist/migration/cra-to-vite/plan-cra-to-vite.js.map +1 -0
  56. package/dist/migration/cra-to-vite/prepare-cra-package.d.ts +15 -0
  57. package/dist/migration/cra-to-vite/prepare-cra-package.d.ts.map +1 -0
  58. package/dist/migration/cra-to-vite/prepare-cra-package.js +100 -0
  59. package/dist/migration/cra-to-vite/prepare-cra-package.js.map +1 -0
  60. package/dist/migration/cra-to-vite/remove-cra-configuration.d.ts +12 -0
  61. package/dist/migration/cra-to-vite/remove-cra-configuration.d.ts.map +1 -0
  62. package/dist/migration/cra-to-vite/remove-cra-configuration.js +85 -0
  63. package/dist/migration/cra-to-vite/remove-cra-configuration.js.map +1 -0
  64. package/dist/migration/cra-to-vite/rename-jsx-files.d.ts +20 -0
  65. package/dist/migration/cra-to-vite/rename-jsx-files.d.ts.map +1 -0
  66. package/dist/migration/cra-to-vite/rename-jsx-files.js +220 -0
  67. package/dist/migration/cra-to-vite/rename-jsx-files.js.map +1 -0
  68. package/dist/migration/cra-to-vite/rename-process-env-react-app.d.ts.map +1 -1
  69. package/dist/migration/cra-to-vite/rename-process-env-react-app.js +6 -3
  70. package/dist/migration/cra-to-vite/rename-process-env-react-app.js.map +1 -1
  71. package/dist/migration/cra-to-vite/rename-react-app-env-vars-in-env-files.js +1 -1
  72. package/dist/migration/cra-to-vite/rename-react-app-env-vars-in-env-files.js.map +1 -1
  73. package/dist/migration/cra-to-vite/replace-react-scripts-dependency.d.ts +11 -4
  74. package/dist/migration/cra-to-vite/replace-react-scripts-dependency.d.ts.map +1 -1
  75. package/dist/migration/cra-to-vite/replace-react-scripts-dependency.js +47 -30
  76. package/dist/migration/cra-to-vite/replace-react-scripts-dependency.js.map +1 -1
  77. package/dist/migration/cra-to-vite/resolved-cra-dependency.d.ts +3 -0
  78. package/dist/migration/cra-to-vite/resolved-cra-dependency.d.ts.map +1 -0
  79. package/dist/migration/cra-to-vite/resolved-cra-dependency.js +42 -0
  80. package/dist/migration/cra-to-vite/resolved-cra-dependency.js.map +1 -0
  81. package/dist/migration/cra-to-vite/scan-cra-projects.d.ts +13 -0
  82. package/dist/migration/cra-to-vite/scan-cra-projects.d.ts.map +1 -1
  83. package/dist/migration/cra-to-vite/scan-cra-projects.js +52 -4
  84. package/dist/migration/cra-to-vite/scan-cra-projects.js.map +1 -1
  85. package/dist/migration/cra-to-vite/shared.d.ts +1 -1
  86. package/dist/migration/cra-to-vite/shared.d.ts.map +1 -1
  87. package/dist/migration/cra-to-vite/shared.js +19 -4
  88. package/dist/migration/cra-to-vite/shared.js.map +1 -1
  89. package/dist/migration/cra-to-vite/svg-component.d.ts +5 -0
  90. package/dist/migration/cra-to-vite/svg-component.d.ts.map +1 -0
  91. package/dist/migration/cra-to-vite/svg-component.js +60 -0
  92. package/dist/migration/cra-to-vite/svg-component.js.map +1 -0
  93. package/dist/migration/cra-to-vite/update-react-app-env-types.d.ts.map +1 -1
  94. package/dist/migration/cra-to-vite/update-react-app-env-types.js +12 -3
  95. package/dist/migration/cra-to-vite/update-react-app-env-types.js.map +1 -1
  96. package/dist/migration/cra-to-vite/update-react-scripts-scripts.d.ts +3 -2
  97. package/dist/migration/cra-to-vite/update-react-scripts-scripts.d.ts.map +1 -1
  98. package/dist/migration/cra-to-vite/update-react-scripts-scripts.js +18 -14
  99. package/dist/migration/cra-to-vite/update-react-scripts-scripts.js.map +1 -1
  100. package/dist/react18/replace-reactdom-render.d.ts.map +1 -1
  101. package/dist/react18/replace-reactdom-render.js +16 -8
  102. package/dist/react18/replace-reactdom-render.js.map +1 -1
  103. package/dist/react18/replace-unmount-component-at-node.d.ts.map +1 -1
  104. package/dist/react18/replace-unmount-component-at-node.js +8 -1
  105. package/dist/react18/replace-unmount-component-at-node.js.map +1 -1
  106. package/dist/react19/find-legacy-context-api.d.ts.map +1 -1
  107. package/dist/react19/find-legacy-context-api.js +21 -38
  108. package/dist/react19/find-legacy-context-api.js.map +1 -1
  109. package/dist/{react18 → react19}/remove-unstable-batched-updates.d.ts +2 -2
  110. package/dist/react19/remove-unstable-batched-updates.d.ts.map +1 -0
  111. package/dist/react19/remove-unstable-batched-updates.js +295 -0
  112. package/dist/react19/remove-unstable-batched-updates.js.map +1 -0
  113. package/dist/react19/replace-default-props.d.ts +1 -0
  114. package/dist/react19/replace-default-props.d.ts.map +1 -1
  115. package/dist/react19/replace-default-props.js +198 -151
  116. package/dist/react19/replace-default-props.js.map +1 -1
  117. package/dist/react19/replace-react-shallow-renderer.d.ts.map +1 -1
  118. package/dist/react19/replace-react-shallow-renderer.js +10 -25
  119. package/dist/react19/replace-react-shallow-renderer.js.map +1 -1
  120. package/dist/refactoring/class-to-functional.d.ts.map +1 -1
  121. package/dist/refactoring/class-to-functional.js +2 -0
  122. package/dist/refactoring/class-to-functional.js.map +1 -1
  123. package/dist/refactoring/create-class-to-es6.d.ts.map +1 -1
  124. package/dist/refactoring/create-class-to-es6.js +14 -49
  125. package/dist/refactoring/create-class-to-es6.js.map +1 -1
  126. package/dist/search/find-function-component-default-props.d.ts.map +1 -1
  127. package/dist/search/find-function-component-default-props.js +6 -15
  128. package/dist/search/find-function-component-default-props.js.map +1 -1
  129. package/dist/search/find-redux-stores.d.ts +12 -0
  130. package/dist/search/find-redux-stores.d.ts.map +1 -0
  131. package/dist/search/find-redux-stores.js +87 -0
  132. package/dist/search/find-redux-stores.js.map +1 -0
  133. package/dist/simplify-object-pattern-property.d.ts.map +1 -1
  134. package/dist/simplify-object-pattern-property.js +14 -21
  135. package/dist/simplify-object-pattern-property.js.map +1 -1
  136. package/dist/simplify-react-imports.d.ts.map +1 -1
  137. package/dist/simplify-react-imports.js +10 -59
  138. package/dist/simplify-react-imports.js.map +1 -1
  139. package/dist/testing/enzyme-constant-strings.d.ts +12 -0
  140. package/dist/testing/enzyme-constant-strings.d.ts.map +1 -0
  141. package/dist/testing/enzyme-constant-strings.js +101 -0
  142. package/dist/testing/enzyme-constant-strings.js.map +1 -0
  143. package/dist/testing/enzyme-host-components.d.ts.map +1 -1
  144. package/dist/testing/enzyme-host-components.js +5 -2
  145. package/dist/testing/enzyme-host-components.js.map +1 -1
  146. package/dist/testing/enzyme-portals.d.ts +38 -0
  147. package/dist/testing/enzyme-portals.d.ts.map +1 -0
  148. package/dist/testing/enzyme-portals.js +226 -0
  149. package/dist/testing/enzyme-portals.js.map +1 -0
  150. package/dist/testing/enzyme-selector-names.d.ts +9 -0
  151. package/dist/testing/enzyme-selector-names.d.ts.map +1 -0
  152. package/dist/testing/enzyme-selector-names.js +50 -0
  153. package/dist/testing/enzyme-selector-names.js.map +1 -0
  154. package/dist/testing/enzyme-to-react-testing-library.d.ts +80 -8
  155. package/dist/testing/enzyme-to-react-testing-library.d.ts.map +1 -1
  156. package/dist/testing/enzyme-to-react-testing-library.js +384 -40
  157. package/dist/testing/enzyme-to-react-testing-library.js.map +1 -1
  158. package/dist/testing/enzyme-usage.d.ts.map +1 -1
  159. package/dist/testing/enzyme-usage.js +27 -4
  160. package/dist/testing/enzyme-usage.js.map +1 -1
  161. package/dist/testing/enzyme-wrapper-scope.d.ts +14 -6
  162. package/dist/testing/enzyme-wrapper-scope.d.ts.map +1 -1
  163. package/dist/testing/enzyme-wrapper-scope.js +143 -16
  164. package/dist/testing/enzyme-wrapper-scope.js.map +1 -1
  165. package/dist/util/java-recipe-proxy.d.ts +18 -0
  166. package/dist/util/java-recipe-proxy.d.ts.map +1 -0
  167. package/dist/util/java-recipe-proxy.js +97 -0
  168. package/dist/util/java-recipe-proxy.js.map +1 -0
  169. package/dist/util/module-imports.d.ts +1 -0
  170. package/dist/util/module-imports.d.ts.map +1 -1
  171. package/dist/util/module-imports.js +5 -1
  172. package/dist/util/module-imports.js.map +1 -1
  173. package/dist/util/reindent.d.ts +5 -0
  174. package/dist/util/reindent.d.ts.map +1 -0
  175. package/dist/util/reindent.js +44 -0
  176. package/dist/util/reindent.js.map +1 -0
  177. package/dist/zustand/redux-stores.d.ts +74 -0
  178. package/dist/zustand/redux-stores.d.ts.map +1 -0
  179. package/dist/zustand/redux-stores.js +402 -0
  180. package/dist/zustand/redux-stores.js.map +1 -0
  181. package/dist/zustand/redux-to-zustand.d.ts +76 -0
  182. package/dist/zustand/redux-to-zustand.d.ts.map +1 -0
  183. package/dist/zustand/redux-to-zustand.js +653 -0
  184. package/dist/zustand/redux-to-zustand.js.map +1 -0
  185. package/dist/zustand/zustand-plan.d.ts +96 -0
  186. package/dist/zustand/zustand-plan.d.ts.map +1 -0
  187. package/dist/zustand/zustand-plan.js +822 -0
  188. package/dist/zustand/zustand-plan.js.map +1 -0
  189. package/package.json +2 -2
  190. package/src/index.ts +9 -4
  191. package/src/migration/cra-to-vite/add-vite-config.ts +53 -1
  192. package/src/migration/cra-to-vite/cra-file-gate.ts +8 -0
  193. package/src/migration/cra-to-vite/cra-runtime.ts +16 -0
  194. package/src/migration/cra-to-vite/cra-script.ts +61 -0
  195. package/src/migration/cra-to-vite/cra-to-vite.ts +25 -36
  196. package/src/migration/cra-to-vite/cra-typecheck.ts +33 -0
  197. package/src/migration/cra-to-vite/find-cra-config-blockers.ts +5 -6
  198. package/src/migration/cra-to-vite/index.ts +10 -2
  199. package/src/migration/cra-to-vite/migrate-cra-package.ts +127 -0
  200. package/src/migration/cra-to-vite/migrate-cra-svg-imports.ts +41 -0
  201. package/src/migration/cra-to-vite/migrate-cra-tests-to-vitest.ts +376 -0
  202. package/src/migration/cra-to-vite/move-public-index-html.ts +12 -7
  203. package/src/migration/cra-to-vite/plan-cra-to-vite.ts +283 -0
  204. package/src/migration/cra-to-vite/prepare-cra-package.ts +86 -0
  205. package/src/migration/cra-to-vite/remove-cra-configuration.ts +64 -0
  206. package/src/migration/cra-to-vite/rename-jsx-files.ts +155 -0
  207. package/src/migration/cra-to-vite/rename-process-env-react-app.ts +6 -3
  208. package/src/migration/cra-to-vite/rename-react-app-env-vars-in-env-files.ts +1 -1
  209. package/src/migration/cra-to-vite/replace-react-scripts-dependency.ts +52 -33
  210. package/src/migration/cra-to-vite/resolved-cra-dependency.ts +41 -0
  211. package/src/migration/cra-to-vite/scan-cra-projects.ts +57 -6
  212. package/src/migration/cra-to-vite/shared.ts +20 -10
  213. package/src/migration/cra-to-vite/svg-component.ts +39 -0
  214. package/src/migration/cra-to-vite/update-react-app-env-types.ts +13 -6
  215. package/src/migration/cra-to-vite/update-react-scripts-scripts.ts +22 -28
  216. package/src/react18/replace-reactdom-render.ts +17 -8
  217. package/src/react18/replace-unmount-component-at-node.ts +13 -2
  218. package/src/react19/find-legacy-context-api.ts +21 -42
  219. package/src/react19/remove-unstable-batched-updates.ts +301 -0
  220. package/src/react19/replace-default-props.ts +212 -159
  221. package/src/react19/replace-react-shallow-renderer.ts +13 -36
  222. package/src/refactoring/class-to-functional.ts +3 -1
  223. package/src/refactoring/create-class-to-es6.ts +12 -63
  224. package/src/search/find-function-component-default-props.ts +8 -10
  225. package/src/search/find-redux-stores.ts +76 -0
  226. package/src/simplify-object-pattern-property.ts +22 -49
  227. package/src/simplify-react-imports.ts +12 -66
  228. package/src/testing/enzyme-constant-strings.ts +95 -0
  229. package/src/testing/enzyme-host-components.ts +6 -2
  230. package/src/testing/enzyme-portals.ts +279 -0
  231. package/src/testing/enzyme-selector-names.ts +59 -0
  232. package/src/testing/enzyme-to-react-testing-library.ts +517 -57
  233. package/src/testing/enzyme-usage.ts +29 -4
  234. package/src/testing/enzyme-wrapper-scope.ts +186 -30
  235. package/src/util/java-recipe-proxy.ts +94 -0
  236. package/src/util/module-imports.ts +6 -1
  237. package/src/util/reindent.ts +35 -0
  238. package/src/zustand/redux-stores.ts +409 -0
  239. package/src/zustand/redux-to-zustand.ts +657 -0
  240. package/src/zustand/zustand-plan.ts +843 -0
  241. package/dist/react18/remove-unstable-batched-updates.d.ts.map +0 -1
  242. package/dist/react18/remove-unstable-batched-updates.js +0 -170
  243. package/dist/react18/remove-unstable-batched-updates.js.map +0 -1
  244. package/src/react18/remove-unstable-batched-updates.ts +0 -192
@@ -0,0 +1,822 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.hookName = exports.REACT_REDUX = void 0;
13
+ exports.parentTree = parentTree;
14
+ exports.isStatement = isStatement;
15
+ exports.isReactDependencies = isReactDependencies;
16
+ exports.argumentsOf = argumentsOf;
17
+ exports.lambdaOf = lambdaOf;
18
+ exports.selectorRead = selectorRead;
19
+ exports.parameterName = parameterName;
20
+ exports.getStateRead = getStateRead;
21
+ exports.dispatchedAction = dispatchedAction;
22
+ exports.walk = walk;
23
+ exports.sourceFacts = sourceFacts;
24
+ exports.storeFacts = storeFacts;
25
+ exports.isDispatcherDeclaration = isDispatcherDeclaration;
26
+ exports.providerChild = providerChild;
27
+ exports.plan = plan;
28
+ exports.planPackages = planPackages;
29
+ exports.sliceSource = sliceSource;
30
+ exports.actionsExport = actionsExport;
31
+ exports.isReducerExport = isReducerExport;
32
+ exports.relativeSpecifier = relativeSpecifier;
33
+ const rewrite_1 = require("@openrewrite/rewrite");
34
+ const javascript_1 = require("@openrewrite/rewrite/javascript");
35
+ const java_1 = require("@openrewrite/rewrite/java");
36
+ const path_1 = require("path");
37
+ const module_imports_1 = require("../util/module-imports");
38
+ const redux_stores_1 = require("./redux-stores");
39
+ exports.REACT_REDUX = "react-redux";
40
+ function parentTree(cursor) {
41
+ for (let c = cursor.parent; c; c = c.parent) {
42
+ const value = c.value;
43
+ if (value && typeof value === "object" && "kind" in value && typeof value.kind === "string" && !value.kind.endsWith("RightPadded") &&
44
+ !value.kind.endsWith("LeftPadded") && !value.kind.endsWith("Container"))
45
+ return value;
46
+ }
47
+ return undefined;
48
+ }
49
+ function ancestors(cursor) {
50
+ const trees = [];
51
+ for (let c = cursor; c; c = c.parent) {
52
+ const value = c.value;
53
+ if (value && typeof value === "object" && "kind" in value && typeof value.kind === "string" && !/Padded$|Container$/.test(value.kind))
54
+ trees.push(value);
55
+ }
56
+ return trees;
57
+ }
58
+ const STATEMENT_PARENTS = new Set([java_1.J.Kind.Block, java_1.J.Kind.If, java_1.J.Kind.IfElse, java_1.J.Kind.WhileLoop, java_1.J.Kind.DoWhileLoop, java_1.J.Kind.ForLoop,
59
+ java_1.J.Kind.ForEachLoop, java_1.J.Kind.Case, javascript_1.JS.Kind.CompilationUnit]);
60
+ function isStatement(node, parent) {
61
+ if (!parent)
62
+ return false;
63
+ if (parent.kind === javascript_1.JS.Kind.ExpressionStatement)
64
+ return true;
65
+ if (parent.kind === java_1.J.Kind.Lambda && parent.body === node)
66
+ return false;
67
+ return STATEMENT_PARENTS.has(parent.kind);
68
+ }
69
+ const DEPENDENCY_HOOKS = new Set(["useEffect", "useLayoutEffect", "useInsertionEffect", "useCallback", "useMemo", "useImperativeHandle"]);
70
+ function isReactDependencies(array, call, react) {
71
+ if (array.kind !== java_1.J.Kind.NewArray || (call === null || call === void 0 ? void 0 : call.kind) !== java_1.J.Kind.MethodInvocation)
72
+ return false;
73
+ const invocation = call;
74
+ const args = argumentsOf(invocation);
75
+ const name = invocation.name.simpleName;
76
+ if (args.length < 2 || args[args.length - 1] !== array || !DEPENDENCY_HOOKS.has(name))
77
+ return false;
78
+ if (!invocation.select)
79
+ return react.some(binding => binding.module === "react" && binding.local === name && binding.name === name);
80
+ const select = invocation.select.element;
81
+ return (0, java_1.isIdentifier)(select) && react.some(binding => binding.module === "react" && binding.local === select.simpleName && binding.name === "default");
82
+ }
83
+ function argumentsOf(call) {
84
+ return call.arguments.elements.map(arg => arg.element).filter(arg => arg.kind !== java_1.J.Kind.Empty);
85
+ }
86
+ function lambdaOf(node) {
87
+ if ((node === null || node === void 0 ? void 0 : node.kind) === javascript_1.JS.Kind.ArrowFunction)
88
+ return node.lambda;
89
+ return (node === null || node === void 0 ? void 0 : node.kind) === java_1.J.Kind.Lambda ? node : undefined;
90
+ }
91
+ function selectorRead(node) {
92
+ const lambda = lambdaOf(node);
93
+ if (!lambda || (node.kind === javascript_1.JS.Kind.ArrowFunction && (node.modifiers.length > 0 || node.returnTypeExpression)))
94
+ return undefined;
95
+ const parameters = lambda.parameters.parameters.map(p => p.element).filter(p => p.kind !== java_1.J.Kind.Empty);
96
+ const parameter = parameters.length === 1 ? parameterName(parameters[0]) : undefined;
97
+ if (!parameter)
98
+ return undefined;
99
+ let expression = lambda.body;
100
+ let read;
101
+ for (;;) {
102
+ if (expression.kind === java_1.J.Kind.FieldAccess) {
103
+ const access = expression;
104
+ if ((0, java_1.isIdentifier)(access.target) && access.target.simpleName === parameter) {
105
+ read = access;
106
+ break;
107
+ }
108
+ expression = access.target;
109
+ }
110
+ else if (expression.kind === java_1.J.Kind.ArrayAccess) {
111
+ const access = expression;
112
+ if (access.dimension.index.element.kind !== java_1.J.Kind.Literal)
113
+ return undefined;
114
+ expression = access.indexed;
115
+ }
116
+ else
117
+ return undefined;
118
+ }
119
+ return { key: read.name.element.simpleName, read, lambda };
120
+ }
121
+ function parameterName(node) {
122
+ if ((0, java_1.isIdentifier)(node))
123
+ return node.simpleName;
124
+ if (node.kind !== java_1.J.Kind.VariableDeclarations)
125
+ return undefined;
126
+ const declarations = node;
127
+ const variable = declarations.variables.length === 1 ? declarations.variables[0].element : undefined;
128
+ return variable && !declarations.typeExpression && !declarations.varargs && !variable.initializer && (0, java_1.isIdentifier)(variable.name) ? variable.name.simpleName : undefined;
129
+ }
130
+ function getStateRead(call, parent) {
131
+ if (call.name.simpleName !== "getState" || argumentsOf(call).length > 0)
132
+ return undefined;
133
+ if ((parent === null || parent === void 0 ? void 0 : parent.kind) !== java_1.J.Kind.FieldAccess || parent.target !== call)
134
+ return undefined;
135
+ return { key: parent.name.element.simpleName, read: parent };
136
+ }
137
+ function dispatchedAction(call) {
138
+ const args = argumentsOf(call);
139
+ if (args.length !== 1 || args[0].kind !== java_1.J.Kind.MethodInvocation)
140
+ return undefined;
141
+ const action = args[0];
142
+ return action.select ? undefined : action;
143
+ }
144
+ function declaredNames(cu) {
145
+ const names = new Map();
146
+ const count = (name) => { var _a; return names.set(name, ((_a = names.get(name)) !== null && _a !== void 0 ? _a : 0) + 1); };
147
+ walk(cu, node => {
148
+ if (node.kind === javascript_1.JS.Kind.Import)
149
+ return false;
150
+ if (node.kind === java_1.J.Kind.NamedVariable && (0, java_1.isIdentifier)(node.name))
151
+ count(node.name.simpleName);
152
+ if (node.kind === javascript_1.JS.Kind.BindingElement && (0, java_1.isIdentifier)(node.name))
153
+ count(node.name.simpleName);
154
+ if (node.kind === java_1.J.Kind.MethodDeclaration)
155
+ count(node.name.simpleName);
156
+ if (node.kind === java_1.J.Kind.ClassDeclaration)
157
+ count(node.name.simpleName);
158
+ if (node.kind === java_1.J.Kind.LambdaParameters) {
159
+ for (const { element } of node.parameters)
160
+ if ((0, java_1.isIdentifier)(element))
161
+ count(element.simpleName);
162
+ }
163
+ return true;
164
+ });
165
+ return names;
166
+ }
167
+ function walk(node, visit) {
168
+ if (!node || typeof node !== "object")
169
+ return;
170
+ if (Array.isArray(node)) {
171
+ for (const element of node)
172
+ walk(element, visit);
173
+ return;
174
+ }
175
+ const value = node;
176
+ if (typeof value.kind === "string" && /\.tree\.JS?X?\$/.test(value.kind) && !visit(value))
177
+ return;
178
+ for (const [key, child] of Object.entries(value)) {
179
+ if (SKIPPED.has(key))
180
+ continue;
181
+ if (child && typeof child === "object")
182
+ walk(child, visit);
183
+ }
184
+ }
185
+ const SKIPPED = new Set(["markers", "prefix", "type", "methodType", "fieldType", "variableType", "constructorType", "javaType", "before", "after"]);
186
+ function sourceFacts(cu) {
187
+ return __awaiter(this, void 0, void 0, function* () {
188
+ var _a, _b, _c, _d, _e;
189
+ const bindings = (0, module_imports_1.importBindings)(cu);
190
+ const declared = declaredNames(cu);
191
+ const facts = {
192
+ path: cu.sourcePath, typescript: /\.(?:ts|tsx|mts|cts)$/.test(cu.sourcePath), test: redux_stores_1.TEST_SOURCE.test(cu.sourcePath),
193
+ bindings, declared: new Set(declared.keys()), uses: [], unfollowed: [], stores: []
194
+ };
195
+ const followed = new Map(bindings.filter(binding => binding.module === exports.REACT_REDUX || (0, module_imports_1.isRelativeSpecifier)(binding.module))
196
+ .map(binding => [binding.local, binding]));
197
+ for (const { element } of cu.statements) {
198
+ if (element.kind === javascript_1.JS.Kind.Import) {
199
+ const imp = element;
200
+ const specifier = (_a = imp.moduleSpecifier) === null || _a === void 0 ? void 0 : _a.element;
201
+ const module = (specifier === null || specifier === void 0 ? void 0 : specifier.kind) === java_1.J.Kind.Literal ? String(specifier.value) : "";
202
+ if (((_c = (_b = imp.importClause) === null || _b === void 0 ? void 0 : _b.namedBindings) === null || _c === void 0 ? void 0 : _c.kind) === javascript_1.JS.Kind.Alias)
203
+ facts.unfollowed.push({ module, detail: "namespace import" });
204
+ if (!imp.importClause && module)
205
+ facts.unfollowed.push({ module, detail: "import for side effects" });
206
+ }
207
+ if (element.kind === javascript_1.JS.Kind.ExportDeclaration) {
208
+ const specifier = (_d = element.moduleSpecifier) === null || _d === void 0 ? void 0 : _d.element;
209
+ if ((specifier === null || specifier === void 0 ? void 0 : specifier.kind) === java_1.J.Kind.Literal)
210
+ facts.unfollowed.push({ module: String(specifier.value), detail: "re-export" });
211
+ }
212
+ }
213
+ const dispatchers = new Set();
214
+ const collector = new class extends javascript_1.JavaScriptVisitor {
215
+ visitMethodInvocation(call, ctx) {
216
+ const _super = Object.create(null, {
217
+ visitMethodInvocation: { get: () => super.visitMethodInvocation }
218
+ });
219
+ return __awaiter(this, void 0, void 0, function* () {
220
+ const first = argumentsOf(call)[0];
221
+ if ((first === null || first === void 0 ? void 0 : first.kind) === java_1.J.Kind.Literal && typeof first.value === "string" &&
222
+ (!call.select && call.name.simpleName === "require" ||
223
+ ["mock", "doMock", "unmock", "requireActual"].includes(call.name.simpleName) && call.select)) {
224
+ facts.unfollowed.push({ module: String(first.value), detail: call.name.simpleName });
225
+ }
226
+ return _super.visitMethodInvocation.call(this, call, ctx);
227
+ });
228
+ }
229
+ visitIdentifier(identifier, ctx) {
230
+ return __awaiter(this, void 0, void 0, function* () {
231
+ const binding = followed.get(identifier.simpleName);
232
+ if (!binding && dispatchers.has(identifier.simpleName)) {
233
+ const use = dispatcherUse(identifier, this.cursor, followed, bindings);
234
+ if (use)
235
+ facts.uses.push(use);
236
+ }
237
+ if (binding) {
238
+ const use = classify(identifier, binding, this.cursor, followed, dispatchers, declared);
239
+ if (use)
240
+ facts.uses.push(use);
241
+ }
242
+ return identifier;
243
+ });
244
+ }
245
+ }();
246
+ walkDispatchers(cu, followed, dispatchers);
247
+ yield collector.visit(cu, new rewrite_1.ExecutionContext());
248
+ for (const name of dispatchers) {
249
+ if (((_e = declared.get(name)) !== null && _e !== void 0 ? _e : 0) > dispatcherCount(cu, followed, name))
250
+ facts.uses.push({ local: name, kind: "other", detail: `\`${name}\` is declared otherwise too` });
251
+ }
252
+ for (const [local] of followed) {
253
+ if (declared.has(local))
254
+ facts.uses.push({ local, kind: "other", detail: `\`${local}\` is shadowed` });
255
+ }
256
+ return facts;
257
+ });
258
+ }
259
+ function storeFacts(cu, modules) {
260
+ return __awaiter(this, void 0, void 0, function* () {
261
+ const stores = [];
262
+ const functions = (0, redux_stores_1.storeFunctions)(cu);
263
+ if (functions.legacy.size === 0 && functions.configure.size === 0)
264
+ return stores;
265
+ yield new class extends javascript_1.JavaScriptVisitor {
266
+ visitMethodInvocation(call, ctx) {
267
+ const _super = Object.create(null, {
268
+ visitMethodInvocation: { get: () => super.visitMethodInvocation }
269
+ });
270
+ return __awaiter(this, void 0, void 0, function* () {
271
+ const store = (0, redux_stores_1.storeFindings)(call, cu, modules, functions);
272
+ if (store) {
273
+ stores.push({ path: cu.sourcePath, api: store.api, store: storeNameAt(this.cursor), findings: store.findings,
274
+ slices: store.slices, exportedAs: exportedStore(cu, this.cursor) });
275
+ }
276
+ return _super.visitMethodInvocation.call(this, call, ctx);
277
+ });
278
+ }
279
+ }().visit(cu, new rewrite_1.ExecutionContext());
280
+ return stores;
281
+ });
282
+ }
283
+ function isDispatcherDeclaration(variable, followed) {
284
+ var _a;
285
+ const init = (_a = variable.initializer) === null || _a === void 0 ? void 0 : _a.element;
286
+ if (!(0, java_1.isIdentifier)(variable.name) || (init === null || init === void 0 ? void 0 : init.kind) !== java_1.J.Kind.MethodInvocation)
287
+ return false;
288
+ const call = init;
289
+ const binding = followed.get(call.name.simpleName);
290
+ return !call.select && (binding === null || binding === void 0 ? void 0 : binding.module) === exports.REACT_REDUX && binding.name === "useDispatch" && argumentsOf(call).length === 0 && !call.typeParameters;
291
+ }
292
+ function walkDispatchers(cu, followed, dispatchers) {
293
+ walk(cu, node => {
294
+ if (node.kind === java_1.J.Kind.VariableDeclarations) {
295
+ const declarations = node;
296
+ for (const { element } of declarations.variables) {
297
+ if (isDispatcherDeclaration(element, followed) && declarations.variables.length === 1)
298
+ dispatchers.add(element.name.simpleName);
299
+ }
300
+ }
301
+ return true;
302
+ });
303
+ }
304
+ function dispatcherCount(cu, followed, name) {
305
+ let count = 0;
306
+ walk(cu, node => {
307
+ if (node.kind === java_1.J.Kind.NamedVariable && isDispatcherDeclaration(node, followed) &&
308
+ node.name.simpleName === name)
309
+ count++;
310
+ return true;
311
+ });
312
+ return count;
313
+ }
314
+ function classify(identifier, binding, cursor, followed, dispatchers, declared) {
315
+ var _a, _b;
316
+ const trees = ancestors(cursor);
317
+ if (trees.some(tree => tree.kind === javascript_1.JS.Kind.Import))
318
+ return undefined;
319
+ const parent = parentTree(cursor);
320
+ const local = identifier.simpleName;
321
+ const other = (detail) => ({ local, kind: "other", detail });
322
+ if ((parent === null || parent === void 0 ? void 0 : parent.kind) === java_1.J.Kind.FieldAccess && parent.name.element === identifier)
323
+ return undefined;
324
+ if ((parent === null || parent === void 0 ? void 0 : parent.kind) === java_1.J.Kind.MethodInvocation && parent.name === identifier && parent.select)
325
+ return undefined;
326
+ if ((parent === null || parent === void 0 ? void 0 : parent.kind) === javascript_1.JS.Kind.JsxAttribute && parent.key === identifier)
327
+ return undefined;
328
+ if ((parent === null || parent === void 0 ? void 0 : parent.kind) === javascript_1.JS.Kind.PropertyAssignment && parent.name.element === identifier &&
329
+ parent.initializer)
330
+ return undefined;
331
+ if ((parent === null || parent === void 0 ? void 0 : parent.kind) === java_1.J.Kind.MethodDeclaration && parent.name === identifier)
332
+ return undefined;
333
+ if ((parent === null || parent === void 0 ? void 0 : parent.kind) === javascript_1.JS.Kind.JsxTag) {
334
+ const tag = parent;
335
+ if (((_a = tag.closingName) === null || _a === void 0 ? void 0 : _a.element) === identifier)
336
+ return undefined;
337
+ if (tag.openName.element !== identifier)
338
+ return other("JSX");
339
+ if (binding.module !== exports.REACT_REDUX || binding.name !== "Provider")
340
+ return undefined;
341
+ return providerUse(tag, followed, local);
342
+ }
343
+ if (binding.module === exports.REACT_REDUX) {
344
+ if ((parent === null || parent === void 0 ? void 0 : parent.kind) !== java_1.J.Kind.MethodInvocation || parent.name !== identifier)
345
+ return other(`\`${binding.name}\` from react-redux`);
346
+ const call = parent;
347
+ if (binding.name === "useSelector") {
348
+ const args = argumentsOf(call);
349
+ const read = args.length === 1 && !call.typeParameters ? selectorRead(args[0]) : undefined;
350
+ return read ? { local, kind: "selector", key: read.key } : Object.assign(Object.assign({}, other("a selector that isn't a read of one slice's state")), { reason: "SELECTOR" });
351
+ }
352
+ if (binding.name === "useDispatch") {
353
+ const variable = parentTree(cursorOf(cursor, call));
354
+ return (variable === null || variable === void 0 ? void 0 : variable.kind) === java_1.J.Kind.NamedVariable && dispatchers.has(variable.name.simpleName)
355
+ ? { local, kind: "dispatcher" } : other("`useDispatch` not held in a variable of its own");
356
+ }
357
+ return other(`\`${binding.name}\` from react-redux`);
358
+ }
359
+ if ((parent === null || parent === void 0 ? void 0 : parent.kind) === java_1.J.Kind.MethodInvocation && parent.name === identifier) {
360
+ const action = parent;
361
+ const dispatch = parentTree(cursorOf(cursor, action));
362
+ if ((dispatch === null || dispatch === void 0 ? void 0 : dispatch.kind) !== java_1.J.Kind.MethodInvocation || dispatchedAction(dispatch) !== action)
363
+ return other("called outside a dispatch");
364
+ const call = dispatch;
365
+ const via = call.select ? (0, java_1.isIdentifier)(call.select.element) && call.name.simpleName === "dispatch" ? call.select.element.simpleName : undefined :
366
+ dispatchers.has(call.name.simpleName) ? call.name.simpleName : undefined;
367
+ if (!via)
368
+ return other("passed to a call that isn't a dispatch");
369
+ if (!isStatement(call, parentTree(cursorOf(cursor, call))))
370
+ return Object.assign(Object.assign({}, other("the dispatch's result is used")), { reason: "DISPATCH" });
371
+ return { local, kind: "dispatched", via };
372
+ }
373
+ if ((parent === null || parent === void 0 ? void 0 : parent.kind) === java_1.J.Kind.MethodInvocation && ((_b = parent.select) === null || _b === void 0 ? void 0 : _b.element) === identifier) {
374
+ const call = parent;
375
+ const outer = parentTree(cursorOf(cursor, call));
376
+ const read = getStateRead(call, outer);
377
+ if (read)
378
+ return { local, kind: "getState", key: read.key };
379
+ if (call.name.simpleName === "dispatch" && dispatchedAction(call)) {
380
+ return isStatement(call, outer) ? { local, kind: "storeDispatch" } : Object.assign(Object.assign({}, other("the dispatch's result is used")), { reason: "DISPATCH" });
381
+ }
382
+ return other(`\`${local}.${call.name.simpleName}\``);
383
+ }
384
+ if ((parent === null || parent === void 0 ? void 0 : parent.kind) === javascript_1.JS.Kind.JsxEmbeddedExpression) {
385
+ const attribute = trees[2];
386
+ const tag = trees[3];
387
+ if ((attribute === null || attribute === void 0 ? void 0 : attribute.kind) === javascript_1.JS.Kind.JsxAttribute && (tag === null || tag === void 0 ? void 0 : tag.kind) === javascript_1.JS.Kind.JsxTag && (0, java_1.isIdentifier)(attribute.key) &&
388
+ attribute.key.simpleName === "store") {
389
+ const name = tag.openName.element;
390
+ const provider = (0, java_1.isIdentifier)(name) ? followed.get(name.simpleName) : undefined;
391
+ if ((provider === null || provider === void 0 ? void 0 : provider.module) === exports.REACT_REDUX && provider.name === "Provider")
392
+ return { local, kind: "provider" };
393
+ }
394
+ }
395
+ return other("referenced");
396
+ }
397
+ function dispatcherUse(identifier, cursor, followed, bindings) {
398
+ const parent = parentTree(cursor);
399
+ const local = identifier.simpleName;
400
+ if (parent && isReactDependencies(parent, parentTree(cursorOf(cursor, parent)), bindings))
401
+ return undefined;
402
+ if ((parent === null || parent === void 0 ? void 0 : parent.kind) === java_1.J.Kind.NamedVariable && parent.name === identifier)
403
+ return undefined;
404
+ if ((parent === null || parent === void 0 ? void 0 : parent.kind) === java_1.J.Kind.FieldAccess && parent.name.element === identifier)
405
+ return undefined;
406
+ if ((parent === null || parent === void 0 ? void 0 : parent.kind) === java_1.J.Kind.MethodInvocation && parent.name === identifier && !parent.select) {
407
+ const call = parent;
408
+ const action = dispatchedAction(call);
409
+ const binding = action && followed.get(action.name.simpleName);
410
+ if (binding && binding.module !== exports.REACT_REDUX)
411
+ return undefined;
412
+ return { local, kind: "other", detail: "dispatches what isn't an imported action creator", reason: "DISPATCH" };
413
+ }
414
+ return { local, kind: "other", detail: "the dispatcher is used other than to dispatch", reason: "DISPATCH" };
415
+ }
416
+ function providerUse(tag, followed, local) {
417
+ var _a;
418
+ const other = (detail) => ({ local, kind: "other", detail });
419
+ const attributes = tag.attributes.map(a => a.element);
420
+ if (attributes.length !== 1 || attributes[0].kind !== javascript_1.JS.Kind.JsxAttribute)
421
+ return other("`Provider` with props other than `store`");
422
+ const value = (_a = attributes[0].value) === null || _a === void 0 ? void 0 : _a.element;
423
+ const expression = (value === null || value === void 0 ? void 0 : value.kind) === javascript_1.JS.Kind.JsxEmbeddedExpression ? value.expression.element : undefined;
424
+ if (!(0, java_1.isIdentifier)(expression) || !followed.has(expression.simpleName))
425
+ return other("`Provider` of a store that isn't imported");
426
+ if (!providerChild(tag))
427
+ return other("`Provider` without exactly one element child");
428
+ return { local, kind: "providerTag" };
429
+ }
430
+ function providerChild(tag) {
431
+ var _a;
432
+ const children = ((_a = tag.children) !== null && _a !== void 0 ? _a : []).filter(child => {
433
+ var _a;
434
+ return !(child.kind === java_1.J.Kind.Literal && String((_a = child.value) !== null && _a !== void 0 ? _a : "").trim() === "") &&
435
+ child.kind !== java_1.J.Kind.Empty;
436
+ });
437
+ return children.length === 1 && children[0].kind === javascript_1.JS.Kind.JsxTag ? children[0] : undefined;
438
+ }
439
+ function cursorOf(cursor, node) {
440
+ for (let c = cursor; c; c = c.parent)
441
+ if (c.value === node)
442
+ return c;
443
+ return cursor;
444
+ }
445
+ function storeNameAt(cursor) {
446
+ for (let c = cursor.parent; c; c = c.parent) {
447
+ const node = c.value;
448
+ if ((node === null || node === void 0 ? void 0 : node.kind) === java_1.J.Kind.NamedVariable && (0, java_1.isIdentifier)(node.name)) {
449
+ return node.name.simpleName;
450
+ }
451
+ if ((node === null || node === void 0 ? void 0 : node.kind) === java_1.J.Kind.MethodDeclaration)
452
+ return node.name.simpleName;
453
+ }
454
+ return "<module>";
455
+ }
456
+ function exportedStore(cu, cursor) {
457
+ var _a;
458
+ const statement = cu.statements.map(s => s.element).find(s => ancestors(cursor).includes(s));
459
+ if (!statement)
460
+ return undefined;
461
+ if (statement.kind === javascript_1.JS.Kind.ExportAssignment) {
462
+ return statement.expression.element === cursor.value ? "default" : undefined;
463
+ }
464
+ if (statement.kind !== java_1.J.Kind.VariableDeclarations)
465
+ return undefined;
466
+ const declarations = statement;
467
+ const variable = declarations.variables.length === 1 ? declarations.variables[0].element : undefined;
468
+ if (!variable || ((_a = variable.initializer) === null || _a === void 0 ? void 0 : _a.element) !== cursor.value || !(0, java_1.isIdentifier)(variable.name))
469
+ return undefined;
470
+ const name = variable.name.simpleName;
471
+ if (declarations.modifiers.some(m => m.keyword === "export"))
472
+ return name;
473
+ const exported = cu.statements.map(s => s.element).find(s => s.kind === javascript_1.JS.Kind.ExportAssignment &&
474
+ (0, java_1.isIdentifier)(s.expression.element) && s.expression.element.simpleName === name);
475
+ return exported ? "default" : undefined;
476
+ }
477
+ const hookName = (key) => `use${key.charAt(0).toUpperCase()}${key.slice(1)}Store`;
478
+ exports.hookName = hookName;
479
+ function plan(facts, sliceSources, dependencies, scope = () => true) {
480
+ var _a;
481
+ const stores = [...facts.values()].filter(source => scope(source.path)).flatMap(source => source.stores);
482
+ const result = { slices: new Map(), sources: new Set(), blocked: new Map() };
483
+ if (stores.length === 0)
484
+ return result;
485
+ if (stores.length > 1) {
486
+ for (const store of stores)
487
+ result.blocked.set(store, [...store.findings, { reason: "MULTIPLE_STORES", detail: `${stores.length} stores` }]);
488
+ return result;
489
+ }
490
+ const store = stores[0];
491
+ const findings = [...store.findings];
492
+ const block = (reason, detail) => findings.push({ reason, detail });
493
+ const held = new Map();
494
+ if (findings.length === 0) {
495
+ if (!store.exportedAs)
496
+ block("STORE_NOT_MODULE_LEVEL", store.store);
497
+ const storeSource = sliceSources.get(store.path);
498
+ if (storeSource && !storeSource.storeOnly)
499
+ block("STORE_SOURCE", "the store's source declares more than the store");
500
+ const dependency = dependencies(store.path);
501
+ if (dependency)
502
+ findings.push(dependency);
503
+ for (const [key, slice] of store.slices) {
504
+ const source = sliceSources.get(slice.path);
505
+ const reducers = source === null || source === void 0 ? void 0 : source.slices.get(slice.variable);
506
+ if (!source || !reducers) {
507
+ block("SLICE_SOURCE", key);
508
+ continue;
509
+ }
510
+ if (reducers.blocker) {
511
+ findings.push(reducers.blocker);
512
+ held.set(key, slice.path);
513
+ continue;
514
+ }
515
+ const hook = (0, exports.hookName)(key);
516
+ const free = (...names) => { var _a; return (_a = names.find(name => !source.names.has(name) && name !== hook)) !== null && _a !== void 0 ? _a : names[0]; };
517
+ const planned = Object.assign(Object.assign({}, slice), { key, hook, reducers: free(`${key}Reducers`, `${key}SliceReducers`), action: free(`${key}Action`, `${key}SliceAction`), actions: reducers.actions, unknown: reducers.unknown, thunkVariables: new Map([...reducers.thunks].map(thunk => [thunk, free(`${thunk}Thunk`, `${thunk}AsyncThunk`)])), caseObject: free(`${key}Cases`, `${key}SliceCases`), dispatch: free(`${key}Dispatch`, `${key}SliceDispatch`) });
518
+ if (!/^[A-Za-z_$][\w$]*$/.test(key))
519
+ block("SLICE_KEY", key);
520
+ const thunkNames = planned.thunkVariables.size > 0 ? [planned.caseObject, planned.dispatch, ...planned.thunkVariables.values()] : [];
521
+ for (const name of [hook, planned.reducers, planned.action, "create", "produce", "freeze", ...thunkNames]) {
522
+ if (source.names.has(name))
523
+ block("NAME_TAKEN", `${name} in ${slice.path}`);
524
+ }
525
+ if ([...result.slices.values()].some(other => other.path === slice.path))
526
+ block("SLICE_SOURCE", `${key} shares its source with another slice`);
527
+ if (!scope(slice.path))
528
+ block("SLICE_SOURCE", `${key} is declared in another package, ${slice.path}`);
529
+ result.slices.set(key, planned);
530
+ }
531
+ }
532
+ const slicePaths = new Map([...result.slices.values()].map(slice => [slice.path, slice]));
533
+ const sources = new Set([store.path, ...slicePaths.keys()]);
534
+ for (const source of facts.values()) {
535
+ const resolve = (module) => (0, module_imports_1.resolveRelative)(source.path, module, facts);
536
+ const touches = (module) => {
537
+ const path = resolve(module);
538
+ return path === store.path || (path !== undefined && slicePaths.has(path));
539
+ };
540
+ if (!scope(source.path)) {
541
+ for (const binding of source.bindings) {
542
+ const path = binding.module === exports.REACT_REDUX ? undefined : resolve(binding.module);
543
+ if (path === store.path)
544
+ block("STORE_USE", `${binding.local} in ${source.path}, of another package`);
545
+ else if (path !== undefined && slicePaths.has(path))
546
+ block("SLICE_USE", `${binding.local} in ${source.path}, of another package`);
547
+ }
548
+ for (const { module, detail } of source.unfollowed) {
549
+ if (touches(module))
550
+ block("UNFOLLOWED_IMPORT", `${detail} of ${module} in ${source.path}, of another package`);
551
+ }
552
+ continue;
553
+ }
554
+ for (const { module, detail } of source.unfollowed) {
555
+ if (module === exports.REACT_REDUX || touches(module))
556
+ block("UNFOLLOWED_IMPORT", `${detail} of ${module} in ${source.path}`);
557
+ }
558
+ for (const binding of source.bindings) {
559
+ const path = binding.module === exports.REACT_REDUX ? undefined : resolve(binding.module);
560
+ const slice = path !== undefined ? slicePaths.get(path) : undefined;
561
+ if (slice === null || slice === void 0 ? void 0 : slice.unknown.has(binding.name))
562
+ block("UNKNOWN_ACTION", `${binding.local} in ${source.path}: \`${binding.name}\` is no reducer of the slice`);
563
+ }
564
+ let changes = false;
565
+ for (const use of source.uses) {
566
+ const binding = source.bindings.find(b => b.local === use.local);
567
+ const path = binding && binding.module !== exports.REACT_REDUX ? resolve(binding.module) : undefined;
568
+ const isStore = path === store.path && (binding === null || binding === void 0 ? void 0 : binding.name) === store.exportedAs;
569
+ const slice = path !== undefined ? slicePaths.get(path) : undefined;
570
+ const isReactRedux = (binding === null || binding === void 0 ? void 0 : binding.module) === exports.REACT_REDUX || !binding;
571
+ if (!isReactRedux && !isStore && !slice && path !== store.path && use.kind !== "dispatched")
572
+ continue;
573
+ if (path !== undefined && [...held.values()].includes(path) || (use.kind === "selector" || use.kind === "getState") && held.has(use.key))
574
+ continue;
575
+ if (source.path === store.path && slice && use.kind === "other")
576
+ continue;
577
+ changes = true;
578
+ const where = `${use.local} in ${source.path}`;
579
+ if (use.kind === "other") {
580
+ block((_a = use.reason) !== null && _a !== void 0 ? _a : (isStore ? "STORE_USE" : slice ? "SLICE_USE" : !binding ? "DISPATCH" : isReactRedux ? "REACT_REDUX_USE" : "STORE_USE"), `${where}: ${use.detail}`);
581
+ }
582
+ else if (use.kind === "selector" || use.kind === "getState") {
583
+ if (!result.slices.has(use.key))
584
+ block("SELECTOR", `${where} reads \`${use.key}\`, which is no slice of the store`);
585
+ }
586
+ else if (use.kind === "dispatched") {
587
+ if (!slice || !binding || !slice.actions.has(binding.name) && !slice.unknown.has(binding.name) && !slice.thunkVariables.has(binding.name))
588
+ block("DISPATCH", `${where} dispatches what isn't an action creator of a slice`);
589
+ const via = source.bindings.find(b => b.local === use.via);
590
+ if (via && !(resolve(via.module) === store.path && via.name === store.exportedAs))
591
+ block("DISPATCH", `${where} is dispatched through \`${use.via}\``);
592
+ }
593
+ else if ((use.kind === "provider" || use.kind === "storeDispatch") && !isStore) {
594
+ block("STORE_USE", `${where} isn't the store`);
595
+ }
596
+ if (slice && use.kind !== "dispatched" && use.kind !== "other")
597
+ block("SLICE_USE", `${where}: ${use.kind}`);
598
+ if (path === store.path && !isStore && source.path !== store.path)
599
+ block("STORE_USE", `${where} imports more of the store's source`);
600
+ }
601
+ for (const binding of source.bindings) {
602
+ if (source.path === store.path || binding.module === exports.REACT_REDUX || resolve(binding.module) !== store.path)
603
+ continue;
604
+ changes = true;
605
+ if (binding.name !== store.exportedAs)
606
+ block("STORE_USE", `${binding.local} in ${source.path} imports more of the store's source`);
607
+ }
608
+ if (changes && source.path !== store.path) {
609
+ if (source.typescript)
610
+ block("TYPESCRIPT", source.path);
611
+ for (const slice of result.slices.values()) {
612
+ if (source.bindings.some(b => b.local === slice.hook) || source.declared.has(slice.hook)) {
613
+ block("NAME_TAKEN", `${slice.hook} in ${source.path}`);
614
+ }
615
+ }
616
+ sources.add(source.path);
617
+ }
618
+ }
619
+ if ([store.path, ...slicePaths.keys()].some(path => /\.(?:ts|tsx|mts|cts)$/.test(path)))
620
+ block("TYPESCRIPT", store.path);
621
+ if (findings.length > 0) {
622
+ result.blocked.set(store, dedupe(findings));
623
+ result.slices.clear();
624
+ return result;
625
+ }
626
+ result.store = store;
627
+ result.sources = sources;
628
+ return result;
629
+ }
630
+ function planPackages(facts, sliceSources, packageOf, dependencies) {
631
+ const root = (path) => { var _a; return (_a = packageOf(path)) !== null && _a !== void 0 ? _a : ""; };
632
+ const roots = new Set([...facts.values()].filter(source => source.stores.length > 0).map(source => root(source.path)));
633
+ return [...roots].map(packageRoot => plan(facts, sliceSources, dependencies, path => root(path) === packageRoot));
634
+ }
635
+ function dedupe(findings) {
636
+ const seen = new Set();
637
+ return findings.filter(f => !seen.has(f.reason + "\0" + f.detail) && seen.add(f.reason + "\0" + f.detail));
638
+ }
639
+ function sliceSource(cu) {
640
+ var _a;
641
+ const createSlice = new Set((0, module_imports_1.importBindings)(cu).filter(b => b.module === redux_stores_1.RTK && b.name === "createSlice").map(b => b.local));
642
+ const functions = (0, redux_stores_1.storeFunctions)(cu);
643
+ if (createSlice.size === 0 && functions.configure.size === 0)
644
+ return undefined;
645
+ const names = new Set([...declaredNames(cu).keys(), ...(0, module_imports_1.importBindings)(cu).map(b => b.local)]);
646
+ const result = { path: cu.sourcePath, names, slices: new Map(), storeOnly: false };
647
+ const local = (0, redux_stores_1.localSlices)(cu);
648
+ const statements = cu.statements.map(s => s.element);
649
+ result.storeOnly = statements.every(s => s.kind === javascript_1.JS.Kind.Import || s.kind === javascript_1.JS.Kind.ExportAssignment ||
650
+ s.kind === java_1.J.Kind.VariableDeclarations && s.variables.every(v => {
651
+ var _a;
652
+ return ((_a = v.element.initializer) === null || _a === void 0 ? void 0 : _a.element.kind) === java_1.J.Kind.MethodInvocation &&
653
+ functions.configure.has(v.element.initializer.element.name.simpleName);
654
+ }));
655
+ for (const [variable, facts] of local.localSlices) {
656
+ const slice = { actions: new Set(), unknown: new Set(), thunks: new Set((_a = facts.thunks) === null || _a === void 0 ? void 0 : _a.keys()), blocker: facts.blocker };
657
+ result.slices.set(variable, slice);
658
+ if (slice.blocker)
659
+ continue;
660
+ slice.blocker = sliceBlocker(cu, variable, slice.actions, slice.unknown, slice.thunks, local.localReducers);
661
+ }
662
+ return result;
663
+ }
664
+ function sliceBlocker(cu, variable, actions, unknown, thunks, reducerVariables) {
665
+ var _a, _b, _c, _d, _e, _f;
666
+ const block = (reason, detail) => ({ reason, detail: `${variable}: ${detail}` });
667
+ const statements = cu.statements.map(s => s.element);
668
+ const declaration = statements.find(s => s.kind === java_1.J.Kind.VariableDeclarations &&
669
+ s.variables.some(v => (0, java_1.isIdentifier)(v.element.name) && v.element.name.simpleName === variable));
670
+ if (!declaration || declaration.variables.length !== 1)
671
+ return block("SLICE_SOURCE", "declared with other variables");
672
+ const call = declaration.variables[0].element.initializer.element;
673
+ const options = (0, redux_stores_1.objectProperties)(argumentsOf(call)[0]);
674
+ for (const key of options.keys()) {
675
+ if (!["name", "initialState", "reducers", "extraReducers"].includes(key))
676
+ return block("SLICE_OPTION", key);
677
+ }
678
+ const name = options.get("name");
679
+ if ((name === null || name === void 0 ? void 0 : name.kind) !== java_1.J.Kind.Literal || typeof name.value !== "string")
680
+ return block("SLICE_NAME", "the name isn't a string literal");
681
+ const initial = options.get("initialState");
682
+ if (!initial || !isInitialState(initial, statements))
683
+ return block("INITIAL_STATE", "the initial state may be a function");
684
+ const reducers = (_c = (_b = (_a = options.get("reducers")) === null || _a === void 0 ? void 0 : _a.body) === null || _b === void 0 ? void 0 : _b.statements.map(s => s.element)) !== null && _c !== void 0 ? _c : [];
685
+ const reducerNames = new Set();
686
+ for (const reducer of reducers) {
687
+ let key;
688
+ let value;
689
+ if (reducer.kind === java_1.J.Kind.MethodDeclaration) {
690
+ key = reducer.name.simpleName;
691
+ value = reducer;
692
+ }
693
+ else {
694
+ const property = reducer;
695
+ key = (0, java_1.isIdentifier)(property.name.element) ? property.name.element.simpleName : undefined;
696
+ value = (_d = property.initializer) !== null && _d !== void 0 ? _d : property.name.element;
697
+ }
698
+ if (!key)
699
+ return block("REDUCER", "a reducer with a computed name");
700
+ if (!isReducerFunction(value, statements))
701
+ return block("REDUCER", `\`${key}\` isn't a function, such as one with \`prepare\``);
702
+ if (usesThis(value))
703
+ return block("REDUCER", `\`${key}\` uses \`this\``);
704
+ reducerNames.add(key);
705
+ }
706
+ const rest = [];
707
+ for (const statement of statements) {
708
+ if (statement === declaration)
709
+ continue;
710
+ const actionExport = actionsExport(statement, variable);
711
+ if (!actionExport) {
712
+ rest.push(statement);
713
+ continue;
714
+ }
715
+ for (const element of actionExport) {
716
+ if (!element.plain)
717
+ return block("ACTIONS_EXPORT", "an action creator exported under another name or with a default");
718
+ (reducerNames.has(element.name) ? actions : unknown).add(element.name);
719
+ }
720
+ }
721
+ const used = (node) => node &&
722
+ [...(0, javascript_1.namesReferencedWithin)(node)].find(n => n === variable || actions.has(n) || unknown.has(n) || thunks.has(n));
723
+ const use = (name) => block("SLICE_USE", `\`${name}\` is used in its own source`);
724
+ for (const reducer of reducers) {
725
+ const name = used(reducer.kind === java_1.J.Kind.MethodDeclaration ? reducer.body : reducer.initializer);
726
+ if (name)
727
+ return use(name);
728
+ }
729
+ for (const statement of rest) {
730
+ if (isReducerExport(statement, variable, reducerVariables))
731
+ continue;
732
+ const thunk = statement.kind === java_1.J.Kind.VariableDeclarations ? (_e = statement.variables[0]) === null || _e === void 0 ? void 0 : _e.element : undefined;
733
+ const declared = thunk && (0, java_1.isIdentifier)(thunk.name) && thunks.has(thunk.name.simpleName) ? thunk : undefined;
734
+ const name = used(declared ? (_f = declared.initializer) === null || _f === void 0 ? void 0 : _f.element : statement);
735
+ if (name)
736
+ return use(name);
737
+ }
738
+ return undefined;
739
+ }
740
+ function isInitialState(node, statements) {
741
+ if ([java_1.J.Kind.Literal, java_1.J.Kind.NewClass, java_1.J.Kind.NewArray].includes(node.kind))
742
+ return node.kind !== java_1.J.Kind.NewClass || !node.class;
743
+ if (!(0, java_1.isIdentifier)(node))
744
+ return false;
745
+ const init = constInitializer(node.simpleName, statements);
746
+ return init !== undefined && init.kind !== javascript_1.JS.Kind.ArrowFunction && init.kind !== java_1.J.Kind.Lambda && isInitialState(init, statements);
747
+ }
748
+ function isReducerFunction(node, statements) {
749
+ if (node.kind === java_1.J.Kind.MethodDeclaration || node.kind === javascript_1.JS.Kind.ArrowFunction || node.kind === java_1.J.Kind.Lambda)
750
+ return true;
751
+ if (!(0, java_1.isIdentifier)(node))
752
+ return false;
753
+ const init = constInitializer(node.simpleName, statements);
754
+ if (init)
755
+ return init.kind === javascript_1.JS.Kind.ArrowFunction || init.kind === java_1.J.Kind.Lambda;
756
+ return statements.some(s => s.kind === java_1.J.Kind.MethodDeclaration && s.name.simpleName === node.simpleName);
757
+ }
758
+ function constInitializer(name, statements) {
759
+ var _a;
760
+ for (const statement of statements) {
761
+ if (statement.kind !== java_1.J.Kind.VariableDeclarations)
762
+ continue;
763
+ const declarations = statement;
764
+ if (!declarations.modifiers.some(m => m.keyword === "const"))
765
+ continue;
766
+ for (const { element } of declarations.variables) {
767
+ if ((0, java_1.isIdentifier)(element.name) && element.name.simpleName === name)
768
+ return (_a = element.initializer) === null || _a === void 0 ? void 0 : _a.element;
769
+ }
770
+ }
771
+ return undefined;
772
+ }
773
+ function usesThis(node) {
774
+ let found = false;
775
+ walk(node, tree => {
776
+ if ((0, java_1.isIdentifier)(tree) && tree.simpleName === "this")
777
+ found = true;
778
+ return !found;
779
+ });
780
+ return found;
781
+ }
782
+ function actionsExport(statement, variable) {
783
+ var _a;
784
+ if (statement.kind !== java_1.J.Kind.VariableDeclarations)
785
+ return undefined;
786
+ const declarations = statement;
787
+ if (declarations.variables.length !== 1)
788
+ return undefined;
789
+ const declared = declarations.variables[0].element;
790
+ const init = (_a = declared.initializer) === null || _a === void 0 ? void 0 : _a.element;
791
+ if ((init === null || init === void 0 ? void 0 : init.kind) !== java_1.J.Kind.FieldAccess || init.name.element.simpleName !== "actions" ||
792
+ !(0, java_1.isIdentifier)(init.target) || init.target.simpleName !== variable)
793
+ return undefined;
794
+ if (declared.name.kind !== javascript_1.JS.Kind.ObjectBindingPattern || !declarations.modifiers.some(m => m.keyword === "export"))
795
+ return [{ name: "", plain: false }];
796
+ return declared.name.bindings.elements.map(({ element }) => {
797
+ const binding = element;
798
+ return binding.kind === javascript_1.JS.Kind.BindingElement && !binding.propertyName && !binding.initializer && (0, java_1.isIdentifier)(binding.name)
799
+ ? { name: binding.name.simpleName, plain: true } : { name: "", plain: false };
800
+ });
801
+ }
802
+ function isReducerExport(statement, variable, reducerVariables) {
803
+ var _a, _b;
804
+ const isReducer = (node) => (node === null || node === void 0 ? void 0 : node.kind) === java_1.J.Kind.FieldAccess && node.name.element.simpleName === "reducer" &&
805
+ (0, java_1.isIdentifier)(node.target) && node.target.simpleName === variable;
806
+ if (statement.kind === javascript_1.JS.Kind.ExportAssignment) {
807
+ const expression = statement.expression.element;
808
+ return isReducer(expression) || (0, java_1.isIdentifier)(expression) && ((_a = reducerVariables.get(expression.simpleName)) === null || _a === void 0 ? void 0 : _a.variable) === variable;
809
+ }
810
+ if (statement.kind !== java_1.J.Kind.VariableDeclarations)
811
+ return false;
812
+ const variables = statement.variables;
813
+ return variables.length === 1 && isReducer((_b = variables[0].element.initializer) === null || _b === void 0 ? void 0 : _b.element);
814
+ }
815
+ function relativeSpecifier(from, target) {
816
+ const withoutExtension = target.replace(/\.(?:js|jsx|mjs|cjs)$/, "");
817
+ let specifier = path_1.posix.relative(path_1.posix.dirname(from), withoutExtension);
818
+ if (!specifier.startsWith("."))
819
+ specifier = `./${specifier}`;
820
+ return specifier;
821
+ }
822
+ //# sourceMappingURL=zustand-plan.js.map