@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
@@ -15,12 +15,17 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
15
15
  });
16
16
  };
17
17
  Object.defineProperty(exports, "__esModule", { value: true });
18
- exports.EnzymeToReactTestingLibrary = void 0;
18
+ exports.EnzymeSnapshotsToReactTestingLibrary = exports.EnzymeToReactTestingLibrary = exports.AddTestingLibraryDom18 = exports.AddTestingLibraryReact18 = exports.AddTestingLibraryReact = void 0;
19
+ exports.lockfileScopes = lockfileScopes;
20
+ exports.rtlLanded = rtlLanded;
19
21
  exports.supportsResolvingGate = supportsResolvingGate;
20
22
  exports.rewriteVersion = rewriteVersion;
23
+ exports.injectsRootStyles = injectsRootStyles;
24
+ exports.parsesOptionalChaining = parsesOptionalChaining;
21
25
  exports.reactMajor = reactMajor;
22
26
  exports.simulatesEvents = simulatesEvents;
23
27
  const rewrite_1 = require("@openrewrite/rewrite");
28
+ const text_1 = require("@openrewrite/rewrite/text");
24
29
  const javascript_1 = require("@openrewrite/rewrite/javascript");
25
30
  const java_1 = require("@openrewrite/rewrite/java");
26
31
  const mutative_1 = require("mutative");
@@ -29,15 +34,152 @@ const fs_1 = require("fs");
29
34
  const path_1 = require("path");
30
35
  const semver_1 = require("semver");
31
36
  const find_enzyme_usage_1 = require("../search/find-enzyme-usage");
37
+ const java_recipe_proxy_1 = require("../util/java-recipe-proxy");
38
+ const module_imports_1 = require("../util/module-imports");
32
39
  const enzyme_usage_1 = require("./enzyme-usage");
33
40
  const enzyme_host_components_1 = require("./enzyme-host-components");
41
+ const enzyme_portals_1 = require("./enzyme-portals");
42
+ const enzyme_constant_strings_1 = require("./enzyme-constant-strings");
43
+ const enzyme_selector_names_1 = require("./enzyme-selector-names");
34
44
  const enzyme_wrapper_scope_1 = require("./enzyme-wrapper-scope");
35
45
  const RTL = "@testing-library/react";
36
- const REACT_RANGE = ">=16.9.0 <18";
46
+ const REACT_RANGE = ">=15.5.0 <18";
47
+ const REACT_18_RANGE = ">=18.0.0 <19";
48
+ const resolvingReact = (range) => [`react@${range}`, `react-dom@${range}`];
37
49
  const RTL_ADDITION = {
38
50
  packageName: RTL, version: "^12.1.5", scope: "devDependencies",
39
- onlyIfDeclaring: ["enzyme"], onlyIfResolving: [`react@${REACT_RANGE}`, `react-dom@${REACT_RANGE}`]
51
+ onlyIfDeclaring: ["enzyme"], onlyIfResolving: resolvingReact(REACT_RANGE)
40
52
  };
53
+ const RTL_18_ADDITION = Object.assign(Object.assign({}, RTL_ADDITION), { version: "^16.3.0", onlyIfResolving: resolvingReact(REACT_18_RANGE) });
54
+ const DOM = "@testing-library/dom";
55
+ const DOM_18_ADDITION = Object.assign(Object.assign({}, RTL_18_ADDITION), { packageName: DOM, version: "^10.4.0" });
56
+ function packageRoot(manifestPath) {
57
+ return manifestPath.slice(0, -"package.json".length);
58
+ }
59
+ function lockfileScopes(manifests) {
60
+ var _a, _b;
61
+ const scopes = new Map();
62
+ for (const manifest of manifests) {
63
+ const root = packageRoot(manifest.sourcePath);
64
+ if (!scopes.has(manifest.sourcePath))
65
+ scopes.set(manifest.sourcePath, root);
66
+ for (const member of (_b = (_a = (0, javascript_1.findNodeResolutionResult)(manifest)) === null || _a === void 0 ? void 0 : _a.workspacePackagePaths) !== null && _b !== void 0 ? _b : []) {
67
+ scopes.set(member.replace(/^\.\//, ""), root);
68
+ }
69
+ }
70
+ return scopes;
71
+ }
72
+ class EnzymeTestDependency extends java_recipe_proxy_1.JavaDependencyRecipe {
73
+ constructor(addition, forecast) {
74
+ super("org.openrewrite.javascript.AddDependency", addition);
75
+ this.addition = addition;
76
+ this.forecast = forecast;
77
+ this.landed = new Set();
78
+ this.manifests = [];
79
+ this.held = [];
80
+ }
81
+ initialValue() {
82
+ this.landed.clear();
83
+ this.manifests.length = 0;
84
+ this.held.length = 0;
85
+ return super.initialValue();
86
+ }
87
+ candidates() {
88
+ return new Set(this.manifests.map(manifest => packageRoot(manifest.sourcePath)));
89
+ }
90
+ scanned(manifest) {
91
+ if (manifest.kind !== json_1.Json.Kind.Document || !/(^|\/)package\.json$/.test(manifest.sourcePath))
92
+ return true;
93
+ if (!this.admits(manifest))
94
+ return false;
95
+ if (this.addition.onlyIfDeclaring.some(name => declares(manifest, name)))
96
+ this.manifests.push(manifest);
97
+ this.held.push(manifest);
98
+ return false;
99
+ }
100
+ admits(_manifest) {
101
+ return true;
102
+ }
103
+ generate(acc, ctx) {
104
+ const _super = Object.create(null, {
105
+ generate: { get: () => super.generate }
106
+ });
107
+ return __awaiter(this, void 0, void 0, function* () {
108
+ var _a, _b;
109
+ const converting = yield ((_b = (_a = this.forecast).converting) === null || _b === void 0 ? void 0 : _b.call(_a));
110
+ const scopes = lockfileScopes(this.held);
111
+ const forwarded = new Set(this.held.filter(manifest => {
112
+ var _a;
113
+ const scope = (_a = scopes.get(manifest.sourcePath)) !== null && _a !== void 0 ? _a : packageRoot(manifest.sourcePath);
114
+ return !converting || [...converting].some(source => source.startsWith(scope));
115
+ }));
116
+ const scanner = yield this.java.scanner(acc);
117
+ for (const manifest of forwarded)
118
+ yield scanner.visit(manifest, ctx);
119
+ const kept = this.manifests.filter(manifest => forwarded.has(manifest));
120
+ this.manifests.length = 0;
121
+ this.manifests.push(...kept);
122
+ const generated = yield _super.generate.call(this, acc, ctx);
123
+ const editor = yield this.java.editorWithData(acc);
124
+ for (const manifest of this.manifests) {
125
+ const edited = yield editor.visit(manifest, ctx);
126
+ if (edited && edited !== manifest && declares(edited, this.addition.packageName))
127
+ this.landed.add(manifest.sourcePath.slice(0, -"package.json".length));
128
+ }
129
+ return generated;
130
+ });
131
+ }
132
+ }
133
+ class AddTestingLibraryReact extends EnzymeTestDependency {
134
+ constructor(forecast = {}) {
135
+ super(RTL_ADDITION, forecast);
136
+ this.name = "org.openrewrite.react.migrate.add-testing-library-react";
137
+ this.displayName = "Add `@testing-library/react` where Enzyme tests are migrated";
138
+ this.description = "Adds `@testing-library/react` 12 as a dev dependency to packages that declare `enzyme` and resolve React and React DOM 15.5 to 17.";
139
+ }
140
+ }
141
+ exports.AddTestingLibraryReact = AddTestingLibraryReact;
142
+ class AddTestingLibraryReact18 extends EnzymeTestDependency {
143
+ constructor(forecast = {}) {
144
+ super(RTL_18_ADDITION, forecast);
145
+ this.name = "org.openrewrite.react.migrate.add-testing-library-react-18";
146
+ this.displayName = "Add `@testing-library/react` 16 where Enzyme tests on React 18 are migrated";
147
+ this.description = "Adds `@testing-library/react` 16 as a dev dependency to packages that declare `enzyme` and resolve React and React DOM 18.";
148
+ }
149
+ }
150
+ exports.AddTestingLibraryReact18 = AddTestingLibraryReact18;
151
+ class AddTestingLibraryDom18 extends EnzymeTestDependency {
152
+ constructor(forecast = {}) {
153
+ super(DOM_18_ADDITION, forecast);
154
+ this.name = "org.openrewrite.react.migrate.add-testing-library-dom-18";
155
+ this.displayName = "Add `@testing-library/dom` where Enzyme tests on React 18 are migrated";
156
+ this.description = "Adds `@testing-library/dom` 10, the peer of `@testing-library/react` 16, as a dev dependency to packages that declare `enzyme` and resolve React and React DOM 18.";
157
+ }
158
+ admits(manifest) {
159
+ var _a, _b;
160
+ const rtl = declaration(manifest, RTL);
161
+ if (rtl === undefined || !declares(manifest, enzyme_usage_1.ENZYME) || declares(manifest, DOM))
162
+ return true;
163
+ const resolved = (_b = (_a = rtl.resolved) === null || _a === void 0 ? void 0 : _a.version.match(/^(\d+)\./)) === null || _b === void 0 ? void 0 : _b[1];
164
+ if (resolved !== undefined)
165
+ return Number(resolved) === 16;
166
+ const version = declaredVersion(rtl.versionConstraint);
167
+ return version !== undefined && version !== "unsupported" && version.major === 16;
168
+ }
169
+ }
170
+ exports.AddTestingLibraryDom18 = AddTestingLibraryDom18;
171
+ function rtlLanded(rtl, rtl18, dom18) {
172
+ return new Set([...rtl, ...[...rtl18].filter(root => dom18.has(root))]);
173
+ }
174
+ function declaration(manifest, name) {
175
+ var _a, _b, _c, _d;
176
+ const resolution = (0, javascript_1.findNodeResolutionResult)(manifest);
177
+ return [...(_a = resolution === null || resolution === void 0 ? void 0 : resolution.dependencies) !== null && _a !== void 0 ? _a : [], ...(_b = resolution === null || resolution === void 0 ? void 0 : resolution.devDependencies) !== null && _b !== void 0 ? _b : [],
178
+ ...(_c = resolution === null || resolution === void 0 ? void 0 : resolution.optionalDependencies) !== null && _c !== void 0 ? _c : [], ...(_d = resolution === null || resolution === void 0 ? void 0 : resolution.peerDependencies) !== null && _d !== void 0 ? _d : []].find(dep => dep.name === name);
179
+ }
180
+ function declares(manifest, name) {
181
+ return declaration(manifest, name) !== undefined;
182
+ }
41
183
  const ADDS_RTL = (() => {
42
184
  try {
43
185
  return supportsResolvingGate(rewriteVersion(require.resolve("@openrewrite/rewrite"), require.cache));
@@ -74,37 +216,90 @@ function documentAt(cursor) {
74
216
  return "document";
75
217
  return scope.declares("globalThis", "value") ? undefined : "globalThis.document";
76
218
  }
219
+ function formattedBy(cu, style) {
220
+ return __awaiter(this, void 0, void 0, function* () {
221
+ const detector = javascript_1.Autodetect.detector();
222
+ yield detector.sampleJavaScript(cu);
223
+ const detected = detector.getTabsAndIndentsStyle();
224
+ const useTabs = style.config.useTabs === true;
225
+ const tabWidth = typeof style.config.tabWidth === "number" ? style.config.tabWidth : 2;
226
+ return detected.useTabCharacter === useTabs && (useTabs || detected.indentSize === tabWidth);
227
+ });
228
+ }
77
229
  function renderOptions(document) {
78
230
  return `{container: ${document}.createElement('div')}`;
79
231
  }
232
+ const ROOT_STYLES = new Map([["@ant-design/icons", ">=5.1.1"]]);
233
+ function injectsRootStyles(packages, sourcePath) {
234
+ return [...packages.entries()].some(([root, declarations]) => sourcePath.startsWith(root) && declarations.rootStyles);
235
+ }
236
+ function parsesOptionalChaining(packages, sourcePath, typescript) {
237
+ return [...packages.entries()].every(([root, { optionalChaining }]) => !sourcePath.startsWith(root) ||
238
+ (typescript ? optionalChaining.ts : optionalChaining.js));
239
+ }
240
+ const below = (version, major, minor) => typeof version === "object" &&
241
+ (version.major < major || version.major === major && version.minor !== undefined && version.minor < minor);
242
+ const CONTAINER_READS = new Set(["text", "rootHtml", "snapshot"]);
243
+ const SNAPSHOT_FILE = /^(.*?)__snapshots__\/([^/]+)\.snap$/;
244
+ const NULL_SNAPSHOT = /^exports\[`.*`\] = `null`;$/m;
80
245
  const READS = {
81
246
  mount: { simulate: true, html: true, props: () => true },
82
247
  shallow: { simulate: false, html: false, props: enzyme_host_components_1.propsRenderHostElements }
83
248
  };
84
- function reactMajor(packages, sourcePath, addsRtl = false) {
85
- return compatibility(packages, sourcePath, addsRtl).react;
249
+ function reactMajor(packages, sourcePath, landed = new Set()) {
250
+ return compatibility(packages, sourcePath, landed).react;
86
251
  }
87
- function simulatesEvents(packages, sourcePath, addsRtl = false) {
88
- const { react, rtl } = compatibility(packages, sourcePath, addsRtl);
89
- return react === 17 && rtl >= 11;
252
+ const HOVER_EVENTS = new Set(["mouseEnter", "mouseLeave", "mouseOver", "mouseOut"]);
253
+ function simulatesEvents(packages, sourcePath, landed = new Set()) {
254
+ const { react, rtl } = compatibility(packages, sourcePath, landed);
255
+ if (react >= 17 && rtl >= 11)
256
+ return { events: enzyme_wrapper_scope_1.SIMULATED_EVENTS, attached: false };
257
+ if ((react === 15 || react === 16) && rtl >= 10)
258
+ return { events: new Set([...enzyme_wrapper_scope_1.SIMULATED_EVENTS].filter(event => !HOVER_EVENTS.has(event))), attached: true };
259
+ return { events: new Set(), attached: false };
90
260
  }
91
- function compatibility(packages, sourcePath, addsRtl) {
261
+ function compatibility(packages, sourcePath, landed) {
92
262
  var _a;
93
263
  const roots = [...packages.keys()].filter(root => sourcePath.startsWith(root)).sort((a, b) => b.length - a.length);
94
264
  const declared = (name, from = roots) => from.map(root => packages.get(root)[name]).find(version => version !== undefined);
95
265
  const supportedReact = (version) => typeof version === "object" &&
96
- (version.major === 17 || (version.major === 16 && version.minor !== undefined && version.minor >= 9));
266
+ ([16, 17, 18].includes(version.major) || version.major === 15 && version.minor !== undefined && version.minor >= 5);
97
267
  const react = declared("react");
98
268
  const rtl = declared("rtl");
99
269
  const enzyme = roots.findIndex(root => packages.get(root).enzyme);
100
- const added = addsRtl && rtl === undefined && enzyme >= 0 && packages.get(roots[enzyme]).lockfileRegenerated &&
101
- supportedReact(declared("react", roots.slice(enzyme))) && supportedReact(declared("reactDom", roots.slice(enzyme)));
102
- const rtlRoot = added ? enzyme : roots.findIndex(root => packages.get(root).rtl !== undefined);
103
- const reactDom = (_a = declared("reactDom", roots.slice(Math.max(rtlRoot, 0)))) !== null && _a !== void 0 ? _a : declared("reactDom");
104
- const rtlMajor = added ? 12 : typeof rtl === "object" && rtl.major >= 10 && rtl.major <= 12 ? rtl.major : 0;
105
- const compatible = supportedReact(react) && supportedReact(reactDom) && react.major === reactDom.major && rtlMajor > 0;
270
+ const enzymeRoots = enzyme >= 0 ? roots.slice(enzyme) : providerRoots(packages, roots);
271
+ const enzymeReact = enzymeRoots.length ? declared("react", enzymeRoots) : undefined;
272
+ const added = rtl === undefined && enzymeRoots.length > 0 && landed.has(enzymeRoots[0]) &&
273
+ supportedReact(enzymeReact) && supportedReact(declared("reactDom", enzymeRoots));
274
+ const rtlRoot = roots.findIndex(root => packages.get(root).rtl !== undefined);
275
+ const reactDom = added ? declared("reactDom", enzymeRoots) : (_a = declared("reactDom", roots.slice(Math.max(rtlRoot, 0)))) !== null && _a !== void 0 ? _a : declared("reactDom");
276
+ const rtlMajor = added ? (enzymeReact.major >= 18 ? 16 : 12) : typeof rtl === "object" && rtl.major >= 10 && rtl.major <= 16 ? rtl.major : 0;
277
+ const compatible = supportedReact(react) && supportedReact(reactDom) && react.major === reactDom.major &&
278
+ rtlMajor > 0 && rtlSupports(rtlMajor, react.major);
106
279
  return { react: compatible ? react.major : 0, rtl: rtlMajor };
107
280
  }
281
+ function providerRoots(packages, roots) {
282
+ var _a;
283
+ const workspace = (root) => { var _a; return (_a = [...packages.entries()].find(([, declarations]) => declarations.members.has(root))) === null || _a === void 0 ? void 0 : _a[0]; };
284
+ const provider = (_a = [...packages.entries()].find(([path, declarations]) => declarations.enzyme && declarations.name !== undefined &&
285
+ workspace(path) !== undefined && roots.some(root => packages.get(root).dependencies.has(declarations.name) &&
286
+ (root === workspace(path) || workspace(root) === workspace(path))))) === null || _a === void 0 ? void 0 : _a[0];
287
+ if (provider === undefined)
288
+ return [];
289
+ return [...packages.keys()].filter(root => provider.startsWith(root)).sort((a, b) => b.length - a.length);
290
+ }
291
+ function rtlSupports(rtl, react) {
292
+ if (react === 15)
293
+ return rtl === 12;
294
+ return react <= 17 ? rtl <= 12 : rtl === 16;
295
+ }
296
+ const BABEL_6 = ["babel-preset-es2015", "babel-preset-es2016", "babel-preset-es2017", "babel-preset-latest", "babel-preset-stage-0", "babel-preset-stage-1"];
297
+ function optionalChainingOf(declares, version) {
298
+ const babel6 = !declares("@babel/core") && (BABEL_6.some(declares) || below(version("babel-core"), 7, 0) ||
299
+ below(version("babel-preset-env"), 2, 0) || below(version("babel-cli"), 7, 0));
300
+ const js = !babel6 && !below(version("@babel/core"), 7, 8) && !below(version("react-scripts"), 3, 3);
301
+ return { js, ts: js && !below(version("typescript"), 3, 7) };
302
+ }
108
303
  function declaredVersion(version) {
109
304
  var _a;
110
305
  if (version === undefined)
@@ -119,18 +314,32 @@ class EnzymeToReactTestingLibrary extends rewrite_1.ScanningRecipe {
119
314
  super(options);
120
315
  this.name = "org.openrewrite.react.migrate.enzyme-to-react-testing-library";
121
316
  this.displayName = "Migrate Enzyme to React Testing Library";
122
- this.description = "Migrates Enzyme `mount` wrappers whose every use is a supported DOM read or event (`find(selector).length`, `.exists()`, `.text()`, `.html()`, `.hasClass()`, positions through `.at(n)`, `.first()`, `.last()`, the wrapper's `text()` and `exists(selector)`, found nodes as a `toHaveLength` subject, `setProps` as a rerender of the cloned element, and on React 17 `simulate` with common mouse, keyboard, focus, form, and touch events, including `nativeEvent` mocks) and bare `mount` statements to React Testing Library container queries, `rerender`, and `fireEvent` in packages using React 16.9+/17 with RTL 10 to 12, adding RTL 12 where it is missing. Converts `shallow` wrappers the same way, without `simulate` or `html()`, where the component is imported from a source in the repository that creates host elements only and runs no effects, so a full render produces the DOM the shallow render does. Selectors are limited to HTML or SVG tag, class, ID, and pass-through attribute compounds and their descendant combinations. Reports remaining Enzyme usages for manual review. Requires an existing DOM test environment; shared wrappers, props, snapshots, and shallow rendering of other components are left for review.";
317
+ this.description = "Migrates Enzyme `mount` wrappers whose every use is a supported DOM read or event (`find(selector).length`, `.exists()`, `.text()`, `.html()`, `.hasClass()`, positions through `.at(n)`, `.first()`, `.last()`, the wrapper's `text()` and `exists(selector)`, found nodes as a `toHaveLength` subject, `setProps` as a rerender of the cloned element, and `simulate` with common mouse, keyboard, focus, form, and touch events, including `nativeEvent` mocks, on React 17 and 18 and, without the hover events and in a render attached to the document, on React 15.5 and 16) and bare `mount` statements to React Testing Library container queries, `rerender`, and `fireEvent` in packages using React 16 or 17 with RTL 10 to 12, React 15.5+ with RTL 12, or React 18 with RTL 16, adding RTL 12, or RTL 16 with its peer `@testing-library/dom` 10, where it is missing. On React 15, whose markup differs, `html()` and DOM nodes are left for review. Converts `shallow` wrappers the same way, without `simulate` or `html()`, where the component is imported from a source in the repository that creates host elements only and runs no effects, so a full render produces the DOM the shallow render does. Selectors are limited to HTML or SVG tag, class, ID, and pass-through attribute compounds and their descendant combinations. Reports remaining Enzyme usages for manual review. Requires an existing DOM test environment; shared wrappers, props, snapshots, and shallow rendering of other components are left for review.";
318
+ this.forecast = {};
319
+ this.tests = [];
320
+ this.dependencySteps = ADDS_RTL ? { rtl: new AddTestingLibraryReact(this.forecast), rtl18: new AddTestingLibraryReact18(this.forecast),
321
+ dom18: new AddTestingLibraryDom18(this.forecast) } : undefined;
322
+ this.snapshots = false;
123
323
  this.usages = new rewrite_1.DataTable(enzyme_usage_1.ENZYME_USAGE_TABLE, "Enzyme migration inventory", "Remaining Enzyme usages and migration actions. Module rows include references whose call types may be unavailable; they are not counts of test cases.", enzyme_usage_1.EnzymeUsageRow);
124
324
  }
125
325
  initialValue() {
126
- return { packages: new Map(), components: new Map() };
326
+ this.tests.length = 0;
327
+ return { packages: new Map(), components: new Map(), modules: new Map(), names: new Map(), nullSnapshots: new Set() };
127
328
  }
128
329
  scanner(scanned) {
129
330
  return __awaiter(this, void 0, void 0, function* () {
331
+ const steps = this.dependencySteps;
332
+ const tests = this.tests;
333
+ let converting;
334
+ this.forecast.converting = steps && (() => converting !== null && converting !== void 0 ? converting : (converting = this.dryRun(scanned, rtlLanded(steps.rtl.candidates(), steps.rtl18.candidates(), steps.dom18.candidates()))));
130
335
  const sources = new class extends javascript_1.JavaScriptVisitor {
131
336
  visitJsCompilationUnit(cu) {
132
337
  return __awaiter(this, void 0, void 0, function* () {
133
338
  scanned.components.set(cu.sourcePath, (0, enzyme_host_components_1.hostComponentExports)(cu));
339
+ scanned.modules.set(cu.sourcePath, (0, enzyme_portals_1.moduleFacts)(cu));
340
+ scanned.names.set(cu.sourcePath, (0, enzyme_selector_names_1.selectorNames)(cu));
341
+ if ((0, module_imports_1.importBindings)(cu).some(({ module }) => module === enzyme_usage_1.ENZYME))
342
+ tests.push(cu);
134
343
  return cu;
135
344
  });
136
345
  }
@@ -138,7 +347,7 @@ class EnzymeToReactTestingLibrary extends rewrite_1.ScanningRecipe {
138
347
  const manifests = new class extends json_1.JsonVisitor {
139
348
  visitDocument(doc, ctx) {
140
349
  return __awaiter(this, void 0, void 0, function* () {
141
- var _a, _b, _c, _d;
350
+ var _a, _b, _c, _d, _e, _f;
142
351
  if (!/(^|\/)package\.json$/.test(doc.sourcePath))
143
352
  return doc;
144
353
  const resolution = (0, javascript_1.findNodeResolutionResult)(doc);
@@ -158,27 +367,70 @@ class EnzymeToReactTestingLibrary extends rewrite_1.ScanningRecipe {
158
367
  reactDom: declaredVersion(version("react-dom")),
159
368
  rtl: declaredVersion(version(RTL)),
160
369
  enzyme: dependencies.some(dep => dep.name === "enzyme"),
161
- lockfileRegenerated: (resolution === null || resolution === void 0 ? void 0 : resolution.packageManager) !== "Pnpm" && (resolution === null || resolution === void 0 ? void 0 : resolution.packageManager) !== "Bun"
370
+ name: resolution === null || resolution === void 0 ? void 0 : resolution.name,
371
+ members: new Set(((_e = resolution === null || resolution === void 0 ? void 0 : resolution.workspacePackagePaths) !== null && _e !== void 0 ? _e : []).map(member => member.replace(/^\.\//, "").slice(0, -"package.json".length))),
372
+ dependencies: new Set(dependencies.map(dep => dep.name)),
373
+ rootStyles: ((_f = resolution === null || resolution === void 0 ? void 0 : resolution.resolvedDependencies) !== null && _f !== void 0 ? _f : []).some(dep => {
374
+ const range = ROOT_STYLES.get(dep.name);
375
+ return range !== undefined && (0, semver_1.valid)(dep.version) !== null && (0, semver_1.satisfies)(dep.version, range);
376
+ }),
377
+ optionalChaining: optionalChainingOf(name => dependencies.some(dep => dep.name === name), name => declaredVersion(version(name)))
162
378
  });
163
379
  return doc;
164
380
  });
165
381
  }
166
382
  }();
167
- return new class extends rewrite_1.TreeVisitor {
383
+ const scan = new class extends rewrite_1.TreeVisitor {
384
+ isAcceptable(sourceFile) {
385
+ return __awaiter(this, void 0, void 0, function* () {
386
+ return (0, text_1.isPlainText)(sourceFile) && SNAPSHOT_FILE.test(sourceFile.sourcePath) || (yield manifests.isAcceptable(sourceFile)) || (yield sources.isAcceptable(sourceFile));
387
+ });
388
+ }
168
389
  visit(tree, ctx, parent) {
169
390
  return __awaiter(this, void 0, void 0, function* () {
391
+ if ((0, text_1.isPlainText)(tree)) {
392
+ const snapshot = SNAPSHOT_FILE.exec(tree.sourcePath);
393
+ if (snapshot && NULL_SNAPSHOT.test(tree.text))
394
+ scanned.nullSnapshots.add(snapshot[1] + snapshot[2]);
395
+ }
170
396
  if (tree.kind === json_1.Json.Kind.Document)
171
397
  return yield manifests.visit(tree, ctx, parent);
172
398
  return tree.kind === javascript_1.JS.Kind.CompilationUnit ? yield sources.visit(tree, ctx, parent) : tree;
173
399
  });
174
400
  }
175
401
  }();
402
+ return (0, rewrite_1.check)((0, rewrite_1.or)((0, java_recipe_proxy_1.treesOfType)("org.openrewrite.json.tree.Json"), (0, java_recipe_proxy_1.treesOfType)("org.openrewrite.javascript.tree.JS"), (0, rewrite_1.and)((0, java_recipe_proxy_1.treesOfType)("org.openrewrite.text.PlainText"), new rewrite_1.RecipeRef("org.openrewrite.FindSourceFiles", { filePattern: "**/__snapshots__/*.snap" }))), scan);
403
+ });
404
+ }
405
+ editor() {
406
+ const _super = Object.create(null, {
407
+ editor: { get: () => super.editor }
408
+ });
409
+ return __awaiter(this, void 0, void 0, function* () {
410
+ return (0, rewrite_1.check)((0, java_recipe_proxy_1.treesOfType)("org.openrewrite.javascript.tree.JS"), _super.editor.call(this));
176
411
  });
177
412
  }
178
413
  editorWithData(scanned) {
414
+ return __awaiter(this, void 0, void 0, function* () {
415
+ return this.conversionEditor(scanned);
416
+ });
417
+ }
418
+ dryRun(scanned, landed) {
419
+ return __awaiter(this, void 0, void 0, function* () {
420
+ const dry = { landed, converted: new Set() };
421
+ const editor = yield this.conversionEditor(scanned, dry);
422
+ const ctx = new rewrite_1.ExecutionContext();
423
+ for (const cu of this.tests)
424
+ yield editor.visit(cu, ctx);
425
+ this.tests.length = 0;
426
+ return dry.converted;
427
+ });
428
+ }
429
+ conversionEditor(scanned, dry) {
179
430
  return __awaiter(this, void 0, void 0, function* () {
180
431
  const jsx = (0, javascript_1.capture)({ name: "element", constraint: node => node.kind !== javascript_1.JS.Kind.Spread });
181
- const selector = (0, javascript_1.capture)({ name: "selector", constraint: node => (0, enzyme_wrapper_scope_1.selectorLiteral)(node) });
432
+ const hostNames = (names) => (0, enzyme_selector_names_1.hostOnlyNames)(names, scanned.names);
433
+ const selector = (0, javascript_1.capture)({ name: "selector", constraint: node => (0, enzyme_wrapper_scope_1.selectorLiteral)(node, hostNames) });
182
434
  const after = { context: [`import {render} from "${RTL}";`], dependencies: { [RTL]: "12.1.5", "@types/react": "16.14.62" } };
183
435
  const patterns = (api) => {
184
436
  const before = { lenientTypeMatching: false, context: [`import {${api}} from "enzyme";`], dependencies: { "@types/enzyme": "3.10.19", "@types/react": "16.14.62" } };
@@ -204,6 +456,10 @@ class EnzymeToReactTestingLibrary extends rewrite_1.ScanningRecipe {
204
456
  mount: (0, javascript_1.rewrite)(() => ({
205
457
  before: from.render,
206
458
  after: (0, javascript_1.template) `render(${jsx}, ${options})`.configure(after)
459
+ })),
460
+ attachedMount: (0, javascript_1.rewrite)(() => ({
461
+ before: from.render,
462
+ after: (0, javascript_1.template) `render(${jsx})`.configure(after)
207
463
  }))
208
464
  };
209
465
  };
@@ -212,13 +468,18 @@ class EnzymeToReactTestingLibrary extends rewrite_1.ScanningRecipe {
212
468
  "globalThis.document": { mount: renderRules("globalThis.document", "mount"), shallow: renderRules("globalThis.document", "shallow") }
213
469
  };
214
470
  const usages = this.usages;
471
+ const steps = this.dependencySteps;
215
472
  const untaggedPositions = this.untaggedSelectorPositions !== false;
473
+ const snapshots = this.snapshots;
216
474
  return (0, rewrite_1.check)((0, rewrite_1.and)((0, rewrite_1.not)((0, javascript_1.isVendoredOrBundled)()), (0, rewrite_1.or)((0, javascript_1.usesMethod)("enzyme mount(..)"), (0, javascript_1.usesMethod)("enzyme shallow(..)"))), new class extends javascript_1.JavaScriptVisitor {
217
475
  constructor() {
218
476
  super(...arguments);
219
477
  this.enabled = false;
220
478
  this.reactMajor = 0;
221
- this.events = false;
479
+ this.events = { events: new Set(), attached: false };
480
+ this.rootStyles = false;
481
+ this.optionalChaining = true;
482
+ this.nullSnapshots = false;
222
483
  this.typescript = false;
223
484
  this.sourcePath = "";
224
485
  this.mounts = new Map();
@@ -233,6 +494,7 @@ class EnzymeToReactTestingLibrary extends rewrite_1.ScanningRecipe {
233
494
  this.hoists = new Map();
234
495
  this.hoistedMounts = new Map();
235
496
  this.names = new Set();
497
+ this.portals = new Set();
236
498
  this.rows = [];
237
499
  this.prettier = false;
238
500
  this.printWidth = 80;
@@ -249,19 +511,40 @@ class EnzymeToReactTestingLibrary extends rewrite_1.ScanningRecipe {
249
511
  this.rows.length = 0;
250
512
  this.names = (0, javascript_1.namesUsedIn)(cu);
251
513
  const prettierStyle = (0, rewrite_1.findMarker)(cu, javascript_1.StyleKind.PrettierStyle);
252
- this.prettier = prettierStyle !== undefined;
514
+ this.prettier = prettierStyle !== undefined && !prettierStyle.ignored && (yield formattedBy(cu, prettierStyle));
253
515
  this.printWidth = typeof (prettierStyle === null || prettierStyle === void 0 ? void 0 : prettierStyle.config.printWidth) === "number" ? prettierStyle.config.printWidth : 80;
254
- this.reactMajor = reactMajor(scanned.packages, cu.sourcePath, ADDS_RTL);
255
- this.events = simulatesEvents(scanned.packages, cu.sourcePath, ADDS_RTL);
516
+ const landed = dry ? dry.landed : steps ? rtlLanded(steps.rtl.landed, steps.rtl18.landed, steps.dom18.landed) : undefined;
517
+ this.reactMajor = reactMajor(scanned.packages, cu.sourcePath, landed);
518
+ this.events = simulatesEvents(scanned.packages, cu.sourcePath, landed);
519
+ this.rootStyles = injectsRootStyles(scanned.packages, cu.sourcePath);
520
+ this.nullSnapshots = scanned.nullSnapshots.has(cu.sourcePath);
256
521
  this.typescript = (0, javascript_1.isTypeScript)(cu);
522
+ this.optionalChaining = parsesOptionalChaining(scanned.packages, cu.sourcePath, this.typescript);
257
523
  this.enabled = this.reactMajor > 0 && (0, javascript_1.moduleBindings)(this).moduleSystem === "esm";
258
524
  if (!this.enabled)
259
525
  return cu;
260
526
  this.imports = (0, enzyme_host_components_1.relativeImports)(cu, scanned.components);
261
527
  this.importedNames = (0, enzyme_host_components_1.importedNames)(cu);
262
- const visited = yield _super.visitJsCompilationUnit.call(this, cu, ctx);
263
- this.rows.forEach(row => usages.insertRow(ctx, row));
264
- return visited;
528
+ const foreign = prettierStyle !== undefined && !this.prettier;
529
+ const visiting = foreign ? (0, mutative_1.create)(cu, draft => {
530
+ draft.markers.markers = draft.markers.markers.filter(marker => marker.kind !== javascript_1.StyleKind.PrettierStyle);
531
+ }) : cu;
532
+ if (foreign)
533
+ this.cursor = new rewrite_1.Cursor(visiting, this.cursor.parent);
534
+ this.cu = cu;
535
+ this.portals.clear();
536
+ const visited = yield _super.visitJsCompilationUnit.call(this, visiting, ctx);
537
+ if (dry) {
538
+ if (this.rows.some(row => row.status === "CONVERTED"))
539
+ dry.converted.add(cu.sourcePath);
540
+ }
541
+ else
542
+ this.rows.forEach(row => usages.insertRow(ctx, row));
543
+ if (!foreign || !visited)
544
+ return visited;
545
+ return visited === visiting ? cu : (0, mutative_1.create)(visited, draft => {
546
+ draft.markers.markers.push(prettierStyle);
547
+ });
265
548
  });
266
549
  }
267
550
  visitBlock(block, ctx) {
@@ -277,7 +560,9 @@ class EnzymeToReactTestingLibrary extends rewrite_1.ScanningRecipe {
277
560
  continue;
278
561
  const api = candidate.render;
279
562
  const reads = READS[api];
280
- const scope = (0, enzyme_wrapper_scope_1.analyzeWrapperScope)(this.cursor, candidate, { events: this.events && reads.simulate, html: reads.html, untaggedPositions });
563
+ const markup = this.reactMajor >= 16;
564
+ const scope = (0, enzyme_wrapper_scope_1.analyzeWrapperScope)(this.cursor, candidate, { events: reads.simulate ? this.events.events : new Set(), html: reads.html && markup, untaggedPositions, hostNames, snapshots: snapshots && markup, optionalChaining: this.optionalChaining,
565
+ constant: node => this.constantString(node) });
281
566
  if (!scope)
282
567
  continue;
283
568
  if ([...scope.uses.values()].some(use => use.use === "setProps" && !reads.props(use.props, this.importedNames)))
@@ -286,7 +571,10 @@ class EnzymeToReactTestingLibrary extends rewrite_1.ScanningRecipe {
286
571
  const document = documentAt(mount);
287
572
  if (!document || !(yield renderPatterns[api].render.match(candidate.mount, mount)))
288
573
  continue;
289
- this.mounts.set(candidate.mount.id, { document, api });
574
+ const attached = this.events.attached && [...scope.uses.values()].some(use => use.use === "simulate");
575
+ if (!attached && this.rootStyles && [...scope.uses.values()].some(use => CONTAINER_READS.has(use.use)))
576
+ continue;
577
+ this.mounts.set(candidate.mount.id, { document, api, attached });
290
578
  apis.push(api);
291
579
  this.declarations.add(candidate.declaration.id);
292
580
  scope.uses.forEach((use, id) => this.uses.set(id, use));
@@ -415,14 +703,14 @@ class EnzymeToReactTestingLibrary extends rewrite_1.ScanningRecipe {
415
703
  if (mounted) {
416
704
  const hoisted = this.hoistedMounts.get(method.id);
417
705
  const result = hoisted ?
418
- yield this.applyTemplate(javascript_1.Template.builder().code(`render(${hoisted}, ${renderOptions(mounted.document)})`).build().configure(after), visited) :
419
- yield rules[mounted.document][mounted.api].mount.tryOn(this.cursor, visited, { visitor: this });
706
+ yield this.applyTemplate(javascript_1.Template.builder().code(mounted.attached ? `render(${hoisted})` : `render(${hoisted}, ${renderOptions(mounted.document)})`).build().configure(after), visited) :
707
+ yield rules[mounted.document][mounted.api][mounted.attached ? "attachedMount" : "mount"].tryOn(this.cursor, visited, { visitor: this });
420
708
  if (!result)
421
709
  return visited;
422
710
  this.pending.delete(method.id);
423
711
  return (0, enzyme_wrapper_scope_1.withRenderOptionsOnElementLine)(result, ctx);
424
712
  }
425
- const statementOf = (0, enzyme_wrapper_scope_1.mountedElement)(visited) && (0, enzyme_wrapper_scope_1.statementPosition)(this.cursor) ? this.renderOf(visited) : undefined;
713
+ const statementOf = (0, enzyme_wrapper_scope_1.mountedElement)(visited) && (0, enzyme_wrapper_scope_1.statementPosition)(this.cursor) && !(0, enzyme_wrapper_scope_1.passesRef)(visited) ? this.renderOf(visited, false) : undefined;
426
714
  const statement = statementOf && (yield this.tryInline(statementOf, "mount", visited, statementOf, (0, enzyme_usage_1.enclosingScope)(this.cursor), "MOUNT_STATEMENT", ctx));
427
715
  if (statement)
428
716
  return statement;
@@ -496,13 +784,25 @@ class EnzymeToReactTestingLibrary extends rewrite_1.ScanningRecipe {
496
784
  case "text":
497
785
  return code.param(read.wrapper).code(".container.textContent" + (read.unasserted && this.typescript ? "!" : "")).build();
498
786
  case "selectorText":
499
- if (read.unasserted)
787
+ if (read.unasserted || !this.optionalChaining)
500
788
  return element(true).code(".textContent" + (this.typescript ? "!" : "")).build();
501
789
  return element(false).code("?.textContent").build();
502
- case "html":
503
- return element(!!read.unasserted).code(read.unasserted ? ".outerHTML" : "?.outerHTML").build();
790
+ case "html": {
791
+ const throwing = !!read.unasserted || !this.optionalChaining;
792
+ return element(throwing).code(throwing ? ".outerHTML" : "?.outerHTML").build();
793
+ }
794
+ case "rootHtml": {
795
+ const nullable = read.snapshot && this.nullSnapshots;
796
+ if (nullable)
797
+ code.param(read.wrapper).code(".container.childNodes.length ? ");
798
+ return code.param(read.wrapper).code(".container.innerHTML" + (nullable ? " : null" : "")).build();
799
+ }
800
+ case "node":
801
+ return element(true).build();
504
802
  case "hasClass":
505
803
  return element(true).code(".classList.contains(").param(read.className).code(")").build();
804
+ case "snapshot":
805
+ return code.code("expect(").param(read.wrapper).code(".asFragment())").build();
506
806
  case "setProps":
507
807
  code.param(read.wrapper).code(`.rerender(${read.reassigned ? `(${read.element} = ` : ""}cloneElement(${read.element}, `);
508
808
  return code.param(read.props).code(read.reassigned ? ")))" : "))").build().configure({
@@ -532,23 +832,56 @@ class EnzymeToReactTestingLibrary extends rewrite_1.ScanningRecipe {
532
832
  });
533
833
  }
534
834
  }
535
- renderOf(node) {
835
+ renderOf(node, read = true) {
536
836
  var _a;
537
837
  if ((node === null || node === void 0 ? void 0 : node.kind) !== java_1.J.Kind.MethodInvocation)
538
838
  return undefined;
539
839
  const call = node;
540
840
  const api = (0, enzyme_usage_1.enzymeMethod)(call);
541
841
  if (api === "mount")
542
- return api;
842
+ return read && this.rendersPortal(call) ? undefined : api;
543
843
  const element = (_a = call.arguments.elements[0]) === null || _a === void 0 ? void 0 : _a.element;
544
844
  return api === "shallow" && call.arguments.elements.length === 1 && (0, enzyme_host_components_1.rendersHostElements)(element, this.imports, scanned.components, this.importedNames) ? api : undefined;
545
845
  }
846
+ rendersPortal(call) {
847
+ var _a;
848
+ const element = (_a = call.arguments.elements[0]) === null || _a === void 0 ? void 0 : _a.element;
849
+ const { roots, declares, resolve } = this.moduleResolution();
850
+ const portal = !!element && !!this.cu && (0, enzyme_portals_1.rendersPortal)(element, this.cu, {
851
+ sourcePath: this.sourcePath,
852
+ modules: scanned.modules,
853
+ resolve,
854
+ declares,
855
+ packagePortals: () => [...scanned.modules].some(([path, facts]) => {
856
+ var _a;
857
+ return facts.portal &&
858
+ path.startsWith((_a = roots[0]) !== null && _a !== void 0 ? _a : "") && !/(^|\/)__tests?__\/|\.(test|spec)\.[cm]?[jt]sx?$/.test(path);
859
+ })
860
+ });
861
+ if (portal && !this.portals.has(call.id)) {
862
+ this.portals.add(call.id);
863
+ this.rows.push({ sourcePath: this.sourcePath, scope: (0, enzyme_usage_1.enclosingScope)(this.cursor), api: "mount", status: "REVIEW", reason: "PORTAL",
864
+ suggestedAction: "A component it renders may render into a portal, which RTL renders outside the container: query `screen` or `baseElement` for that content." });
865
+ }
866
+ return portal;
867
+ }
868
+ moduleResolution() {
869
+ const roots = [...scanned.packages.keys()].filter(root => this.sourcePath.startsWith(root)).sort((a, b) => b.length - a.length);
870
+ const declares = (name) => roots.some(root => scanned.packages.get(root).dependencies.has(name));
871
+ return { roots, declares, resolve: (0, enzyme_portals_1.resolver)(scanned.modules, [...new Set([...roots, ""])], declares) };
872
+ }
873
+ constantString(node) {
874
+ const parts = (0, enzyme_constant_strings_1.stringParts)(node);
875
+ return parts && (0, enzyme_constant_strings_1.evaluate)(parts, this.sourcePath, scanned.modules, this.moduleResolution().resolve);
876
+ }
546
877
  laidOutByPrettier(node) {
547
878
  if (this.prettier)
548
879
  this.reformat.add(node.id);
549
880
  }
550
881
  converted(api, scope, reason) {
551
882
  (0, javascript_1.maybeRemoveImport)(this, enzyme_usage_1.ENZYME);
883
+ if (snapshots)
884
+ (0, javascript_1.maybeRemoveImport)(this, "enzyme-to-json");
552
885
  this.rows.push({ sourcePath: this.sourcePath, scope, api, status: "CONVERTED", reason,
553
886
  suggestedAction: "Run the migrated test and review whether an accessible RTL query would better express its intent." });
554
887
  }
@@ -557,7 +890,8 @@ class EnzymeToReactTestingLibrary extends rewrite_1.ScanningRecipe {
557
890
  }
558
891
  recipeList() {
559
892
  return __awaiter(this, void 0, void 0, function* () {
560
- return [...ADDS_RTL ? [yield (0, javascript_1.addDependency)(RTL_ADDITION)] : [], new find_enzyme_usage_1.FindEnzymeUsage()];
893
+ const steps = this.dependencySteps;
894
+ return [...steps ? [steps.rtl, steps.rtl18, steps.dom18] : [], new find_enzyme_usage_1.FindEnzymeUsage()];
561
895
  });
562
896
  }
563
897
  }
@@ -569,4 +903,14 @@ __decorate([
569
903
  required: false
570
904
  })
571
905
  ], EnzymeToReactTestingLibrary.prototype, "untaggedSelectorPositions", void 0);
906
+ class EnzymeSnapshotsToReactTestingLibrary extends EnzymeToReactTestingLibrary {
907
+ constructor() {
908
+ super(...arguments);
909
+ this.name = "org.openrewrite.react.migrate.enzyme-snapshots-to-react-testing-library";
910
+ this.displayName = "Migrate Enzyme to React Testing Library, snapshots included";
911
+ this.description = "Migrates Enzyme wrappers as `org.openrewrite.react.migrate.enzyme-to-react-testing-library` does, and also converts `expect(wrapper).toMatchSnapshot()` and `expect(toJson(wrapper)).toMatchSnapshot()` of those wrappers to `expect(wrapper.asFragment()).toMatchSnapshot()`, removing the `enzyme-to-json` import where nothing else uses it. RTL snapshots the rendered DOM where Enzyme's serializer recorded a component tree, so the stored snapshots of converted tests no longer match: run the tests with `--updateSnapshot` once and review the new snapshots. Inline snapshots and snapshots of shallow renders that are not converted are left for review.";
912
+ this.snapshots = true;
913
+ }
914
+ }
915
+ exports.EnzymeSnapshotsToReactTestingLibrary = EnzymeSnapshotsToReactTestingLibrary;
572
916
  //# sourceMappingURL=enzyme-to-react-testing-library.js.map