@openrewrite/recipes-react 0.14.0 → 0.16.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 (238) hide show
  1. package/dist/index.d.ts +12 -4
  2. package/dist/index.d.ts.map +1 -1
  3. package/dist/index.js +70 -17
  4. package/dist/index.js.map +1 -1
  5. package/dist/migration/cra-to-vite/add-vite-config.d.ts +9 -0
  6. package/dist/migration/cra-to-vite/add-vite-config.d.ts.map +1 -0
  7. package/dist/migration/cra-to-vite/add-vite-config.js +49 -0
  8. package/dist/migration/cra-to-vite/add-vite-config.js.map +1 -0
  9. package/dist/migration/cra-to-vite/cra-blockers-table.d.ts +10 -0
  10. package/dist/migration/cra-to-vite/cra-blockers-table.d.ts.map +1 -0
  11. package/dist/migration/cra-to-vite/cra-blockers-table.js +31 -0
  12. package/dist/migration/cra-to-vite/cra-blockers-table.js.map +1 -0
  13. package/dist/migration/cra-to-vite/cra-to-vite.d.ts +14 -0
  14. package/dist/migration/cra-to-vite/cra-to-vite.d.ts.map +1 -0
  15. package/dist/migration/cra-to-vite/cra-to-vite.js +73 -0
  16. package/dist/migration/cra-to-vite/cra-to-vite.js.map +1 -0
  17. package/dist/migration/cra-to-vite/find-cra-config-blockers.d.ts +14 -0
  18. package/dist/migration/cra-to-vite/find-cra-config-blockers.d.ts.map +1 -0
  19. package/dist/migration/cra-to-vite/find-cra-config-blockers.js +177 -0
  20. package/dist/migration/cra-to-vite/find-cra-config-blockers.js.map +1 -0
  21. package/dist/migration/cra-to-vite/find-cra-env-file-blockers.d.ts +10 -0
  22. package/dist/migration/cra-to-vite/find-cra-env-file-blockers.d.ts.map +1 -0
  23. package/dist/migration/cra-to-vite/find-cra-env-file-blockers.js +67 -0
  24. package/dist/migration/cra-to-vite/find-cra-env-file-blockers.js.map +1 -0
  25. package/dist/migration/cra-to-vite/find-cra-source-blockers.d.ts +10 -0
  26. package/dist/migration/cra-to-vite/find-cra-source-blockers.d.ts.map +1 -0
  27. package/dist/migration/cra-to-vite/find-cra-source-blockers.js +168 -0
  28. package/dist/migration/cra-to-vite/find-cra-source-blockers.js.map +1 -0
  29. package/dist/migration/cra-to-vite/find-cra-to-vite-blockers.d.ts +8 -0
  30. package/dist/migration/cra-to-vite/find-cra-to-vite-blockers.d.ts.map +1 -0
  31. package/dist/migration/cra-to-vite/find-cra-to-vite-blockers.js +31 -0
  32. package/dist/migration/cra-to-vite/find-cra-to-vite-blockers.js.map +1 -0
  33. package/dist/migration/cra-to-vite/index.d.ts +16 -0
  34. package/dist/migration/cra-to-vite/index.d.ts.map +1 -0
  35. package/dist/migration/cra-to-vite/index.js +35 -0
  36. package/dist/migration/cra-to-vite/index.js.map +1 -0
  37. package/dist/migration/cra-to-vite/move-public-index-html.d.ts +11 -0
  38. package/dist/migration/cra-to-vite/move-public-index-html.d.ts.map +1 -0
  39. package/dist/migration/cra-to-vite/move-public-index-html.js +124 -0
  40. package/dist/migration/cra-to-vite/move-public-index-html.js.map +1 -0
  41. package/dist/migration/cra-to-vite/rename-process-env-react-app.d.ts +9 -0
  42. package/dist/migration/cra-to-vite/rename-process-env-react-app.d.ts.map +1 -0
  43. package/dist/migration/cra-to-vite/rename-process-env-react-app.js +92 -0
  44. package/dist/migration/cra-to-vite/rename-process-env-react-app.js.map +1 -0
  45. package/dist/migration/cra-to-vite/rename-react-app-env-vars-in-env-files.d.ts +9 -0
  46. package/dist/migration/cra-to-vite/rename-react-app-env-vars-in-env-files.d.ts.map +1 -0
  47. package/dist/migration/cra-to-vite/rename-react-app-env-vars-in-env-files.js +65 -0
  48. package/dist/migration/cra-to-vite/rename-react-app-env-vars-in-env-files.js.map +1 -0
  49. package/dist/migration/cra-to-vite/replace-react-scripts-dependency.d.ts +14 -0
  50. package/dist/migration/cra-to-vite/replace-react-scripts-dependency.d.ts.map +1 -0
  51. package/dist/migration/cra-to-vite/replace-react-scripts-dependency.js +78 -0
  52. package/dist/migration/cra-to-vite/replace-react-scripts-dependency.js.map +1 -0
  53. package/dist/migration/cra-to-vite/scan-cra-projects.d.ts +21 -0
  54. package/dist/migration/cra-to-vite/scan-cra-projects.d.ts.map +1 -0
  55. package/dist/migration/cra-to-vite/scan-cra-projects.js +87 -0
  56. package/dist/migration/cra-to-vite/scan-cra-projects.js.map +1 -0
  57. package/dist/migration/cra-to-vite/shared.d.ts +24 -0
  58. package/dist/migration/cra-to-vite/shared.d.ts.map +1 -0
  59. package/dist/migration/cra-to-vite/shared.js +105 -0
  60. package/dist/migration/cra-to-vite/shared.js.map +1 -0
  61. package/dist/migration/cra-to-vite/update-react-app-env-types.d.ts +9 -0
  62. package/dist/migration/cra-to-vite/update-react-app-env-types.d.ts.map +1 -0
  63. package/dist/migration/cra-to-vite/update-react-app-env-types.js +47 -0
  64. package/dist/migration/cra-to-vite/update-react-app-env-types.js.map +1 -0
  65. package/dist/migration/cra-to-vite/update-react-scripts-scripts.d.ts +9 -0
  66. package/dist/migration/cra-to-vite/update-react-scripts-scripts.d.ts.map +1 -0
  67. package/dist/migration/cra-to-vite/update-react-scripts-scripts.js +62 -0
  68. package/dist/migration/cra-to-vite/update-react-scripts-scripts.js.map +1 -0
  69. package/dist/migration/cra-to-vite/update-tsconfig-for-vite.d.ts +10 -0
  70. package/dist/migration/cra-to-vite/update-tsconfig-for-vite.d.ts.map +1 -0
  71. package/dist/migration/cra-to-vite/update-tsconfig-for-vite.js +44 -0
  72. package/dist/migration/cra-to-vite/update-tsconfig-for-vite.js.map +1 -0
  73. package/dist/react17/migrate-deprecated-lifecycles.d.ts +8 -0
  74. package/dist/react17/migrate-deprecated-lifecycles.d.ts.map +1 -0
  75. package/dist/react17/migrate-deprecated-lifecycles.js +258 -0
  76. package/dist/react17/migrate-deprecated-lifecycles.js.map +1 -0
  77. package/dist/react17/use-new-jsx-transform.d.ts +8 -0
  78. package/dist/react17/use-new-jsx-transform.d.ts.map +1 -0
  79. package/dist/react17/use-new-jsx-transform.js +121 -0
  80. package/dist/react17/use-new-jsx-transform.js.map +1 -0
  81. package/dist/react18/add-children-to-props.d.ts +8 -0
  82. package/dist/react18/add-children-to-props.d.ts.map +1 -0
  83. package/dist/react18/add-children-to-props.js +277 -0
  84. package/dist/react18/add-children-to-props.js.map +1 -0
  85. package/dist/react18/replace-reactdom-render.d.ts.map +1 -1
  86. package/dist/react18/replace-reactdom-render.js +109 -12
  87. package/dist/react18/replace-reactdom-render.js.map +1 -1
  88. package/dist/react18/replace-unmount-component-at-node.d.ts.map +1 -1
  89. package/dist/react18/replace-unmount-component-at-node.js +62 -20
  90. package/dist/react18/replace-unmount-component-at-node.js.map +1 -1
  91. package/dist/react19/replace-default-props.d.ts.map +1 -1
  92. package/dist/react19/replace-default-props.js +10 -13
  93. package/dist/react19/replace-default-props.js.map +1 -1
  94. package/dist/react19/replace-react-shallow-renderer.d.ts.map +1 -1
  95. package/dist/react19/replace-react-shallow-renderer.js +1 -6
  96. package/dist/react19/replace-react-shallow-renderer.js.map +1 -1
  97. package/dist/refactoring/class-to-functional.d.ts.map +1 -1
  98. package/dist/refactoring/class-to-functional.js +81 -7
  99. package/dist/refactoring/class-to-functional.js.map +1 -1
  100. package/dist/refactoring/create-class-to-es6.d.ts.map +1 -1
  101. package/dist/refactoring/create-class-to-es6.js +4 -7
  102. package/dist/refactoring/create-class-to-es6.js.map +1 -1
  103. package/dist/refactoring/create-element-to-jsx.d.ts.map +1 -1
  104. package/dist/refactoring/create-element-to-jsx.js +3 -4
  105. package/dist/refactoring/create-element-to-jsx.js.map +1 -1
  106. package/dist/refactoring/fix-async-effect.d.ts +8 -0
  107. package/dist/refactoring/fix-async-effect.d.ts.map +1 -0
  108. package/dist/refactoring/fix-async-effect.js +71 -0
  109. package/dist/refactoring/fix-async-effect.js.map +1 -0
  110. package/dist/refactoring/prop-types-to-typescript.d.ts +8 -0
  111. package/dist/refactoring/prop-types-to-typescript.d.ts.map +1 -0
  112. package/dist/refactoring/prop-types-to-typescript.js +341 -0
  113. package/dist/refactoring/prop-types-to-typescript.js.map +1 -0
  114. package/dist/refactoring/pure-render-mixin.d.ts.map +1 -1
  115. package/dist/refactoring/pure-render-mixin.js +3 -6
  116. package/dist/refactoring/pure-render-mixin.js.map +1 -1
  117. package/dist/search/find-deprecated-lifecycles.d.ts +12 -0
  118. package/dist/search/find-deprecated-lifecycles.d.ts.map +1 -0
  119. package/dist/search/find-deprecated-lifecycles.js +86 -0
  120. package/dist/search/find-deprecated-lifecycles.js.map +1 -0
  121. package/dist/search/find-effects-without-cleanup.d.ts +9 -0
  122. package/dist/search/find-effects-without-cleanup.d.ts.map +1 -0
  123. package/dist/search/find-effects-without-cleanup.js +133 -0
  124. package/dist/search/find-effects-without-cleanup.js.map +1 -0
  125. package/dist/search/find-enzyme-usage.d.ts +9 -0
  126. package/dist/search/find-enzyme-usage.d.ts.map +1 -0
  127. package/dist/search/find-enzyme-usage.js +155 -0
  128. package/dist/search/find-enzyme-usage.js.map +1 -0
  129. package/dist/search/find-function-component-default-props.d.ts +11 -0
  130. package/dist/search/find-function-component-default-props.d.ts.map +1 -0
  131. package/dist/search/find-function-component-default-props.js +96 -0
  132. package/dist/search/find-function-component-default-props.js.map +1 -0
  133. package/dist/search/find-legacy-react-apis.d.ts +8 -0
  134. package/dist/search/find-legacy-react-apis.d.ts.map +1 -0
  135. package/dist/search/find-legacy-react-apis.js +41 -0
  136. package/dist/search/find-legacy-react-apis.js.map +1 -0
  137. package/dist/search/find-missing-use-client-directive.d.ts +9 -0
  138. package/dist/search/find-missing-use-client-directive.d.ts.map +1 -0
  139. package/dist/search/find-missing-use-client-directive.js +127 -0
  140. package/dist/search/find-missing-use-client-directive.js.map +1 -0
  141. package/dist/search/find-reactdom-root-apis.d.ts +14 -0
  142. package/dist/search/find-reactdom-root-apis.d.ts.map +1 -0
  143. package/dist/search/find-reactdom-root-apis.js +122 -0
  144. package/dist/search/find-reactdom-root-apis.js.map +1 -0
  145. package/dist/testing/enzyme-host-components.d.ts +14 -0
  146. package/dist/testing/enzyme-host-components.d.ts.map +1 -0
  147. package/dist/testing/enzyme-host-components.js +245 -0
  148. package/dist/testing/enzyme-host-components.js.map +1 -0
  149. package/dist/testing/enzyme-to-react-testing-library.d.ts +41 -0
  150. package/dist/testing/enzyme-to-react-testing-library.d.ts.map +1 -0
  151. package/dist/testing/enzyme-to-react-testing-library.js +572 -0
  152. package/dist/testing/enzyme-to-react-testing-library.js.map +1 -0
  153. package/dist/testing/enzyme-usage.d.ts +22 -0
  154. package/dist/testing/enzyme-usage.d.ts.map +1 -0
  155. package/dist/testing/enzyme-usage.js +107 -0
  156. package/dist/testing/enzyme-usage.js.map +1 -0
  157. package/dist/testing/enzyme-wrapper-scope.d.ts +51 -0
  158. package/dist/testing/enzyme-wrapper-scope.d.ts.map +1 -0
  159. package/dist/testing/enzyme-wrapper-scope.js +420 -0
  160. package/dist/testing/enzyme-wrapper-scope.js.map +1 -0
  161. package/dist/util/module-imports.d.ts +12 -0
  162. package/dist/util/module-imports.d.ts.map +1 -0
  163. package/dist/util/module-imports.js +56 -0
  164. package/dist/util/module-imports.js.map +1 -0
  165. package/dist/util/react-class.d.ts +7 -0
  166. package/dist/util/react-class.d.ts.map +1 -0
  167. package/dist/util/react-class.js +42 -0
  168. package/dist/util/react-class.js.map +1 -0
  169. package/dist/util/tree-search.d.ts +6 -0
  170. package/dist/util/tree-search.d.ts.map +1 -0
  171. package/dist/util/tree-search.js +51 -0
  172. package/dist/util/tree-search.js.map +1 -0
  173. package/package.json +7 -4
  174. package/src/index.ts +53 -12
  175. package/src/migration/cra-to-vite/add-vite-config.ts +47 -0
  176. package/src/migration/cra-to-vite/cra-blockers-table.ts +27 -0
  177. package/src/migration/cra-to-vite/cra-to-vite.ts +76 -0
  178. package/src/migration/cra-to-vite/find-cra-config-blockers.ts +173 -0
  179. package/src/migration/cra-to-vite/find-cra-env-file-blockers.ts +60 -0
  180. package/src/migration/cra-to-vite/find-cra-source-blockers.ts +138 -0
  181. package/src/migration/cra-to-vite/find-cra-to-vite-blockers.ts +21 -0
  182. package/src/migration/cra-to-vite/index.ts +26 -0
  183. package/src/migration/cra-to-vite/move-public-index-html.ts +132 -0
  184. package/src/migration/cra-to-vite/rename-process-env-react-app.ts +93 -0
  185. package/src/migration/cra-to-vite/rename-react-app-env-vars-in-env-files.ts +61 -0
  186. package/src/migration/cra-to-vite/replace-react-scripts-dependency.ts +71 -0
  187. package/src/migration/cra-to-vite/scan-cra-projects.ts +86 -0
  188. package/src/migration/cra-to-vite/shared.ts +123 -0
  189. package/src/migration/cra-to-vite/update-react-app-env-types.ts +40 -0
  190. package/src/migration/cra-to-vite/update-react-scripts-scripts.ts +64 -0
  191. package/src/migration/cra-to-vite/update-tsconfig-for-vite.ts +40 -0
  192. package/src/react17/migrate-deprecated-lifecycles.ts +265 -0
  193. package/src/react17/use-new-jsx-transform.ts +108 -0
  194. package/src/react18/add-children-to-props.ts +235 -0
  195. package/src/react18/replace-reactdom-render.ts +109 -20
  196. package/src/react18/replace-unmount-component-at-node.ts +78 -32
  197. package/src/react19/replace-default-props.ts +11 -15
  198. package/src/react19/replace-react-shallow-renderer.ts +2 -7
  199. package/src/refactoring/class-to-functional.ts +101 -13
  200. package/src/refactoring/create-class-to-es6.ts +6 -9
  201. package/src/refactoring/create-element-to-jsx.ts +4 -6
  202. package/src/refactoring/fix-async-effect.ts +88 -0
  203. package/src/refactoring/prop-types-to-typescript.ts +341 -0
  204. package/src/refactoring/pure-render-mixin.ts +4 -7
  205. package/src/search/find-deprecated-lifecycles.ts +64 -0
  206. package/src/search/find-effects-without-cleanup.ts +109 -0
  207. package/src/search/find-enzyme-usage.ts +117 -0
  208. package/src/search/find-function-component-default-props.ts +68 -0
  209. package/src/search/find-legacy-react-apis.ts +41 -0
  210. package/src/search/find-missing-use-client-directive.ts +99 -0
  211. package/src/search/find-reactdom-root-apis.ts +99 -0
  212. package/src/testing/enzyme-host-components.ts +283 -0
  213. package/src/testing/enzyme-to-react-testing-library.ts +622 -0
  214. package/src/testing/enzyme-usage.ts +94 -0
  215. package/src/testing/enzyme-wrapper-scope.ts +456 -0
  216. package/src/util/module-imports.ts +55 -0
  217. package/src/util/react-class.ts +35 -0
  218. package/src/util/tree-search.ts +32 -0
  219. package/dist/react16/upgrade-to-react-16.d.ts +0 -12
  220. package/dist/react16/upgrade-to-react-16.d.ts.map +0 -1
  221. package/dist/react16/upgrade-to-react-16.js +0 -66
  222. package/dist/react16/upgrade-to-react-16.js.map +0 -1
  223. package/dist/react17/upgrade-to-react-17.d.ts +0 -12
  224. package/dist/react17/upgrade-to-react-17.d.ts.map +0 -1
  225. package/dist/react17/upgrade-to-react-17.js +0 -62
  226. package/dist/react17/upgrade-to-react-17.js.map +0 -1
  227. package/dist/react18/upgrade-to-react-18.d.ts +0 -12
  228. package/dist/react18/upgrade-to-react-18.d.ts.map +0 -1
  229. package/dist/react18/upgrade-to-react-18.js +0 -64
  230. package/dist/react18/upgrade-to-react-18.js.map +0 -1
  231. package/dist/react19/upgrade-to-react-19.d.ts +0 -8
  232. package/dist/react19/upgrade-to-react-19.d.ts.map +0 -1
  233. package/dist/react19/upgrade-to-react-19.js +0 -68
  234. package/dist/react19/upgrade-to-react-19.js.map +0 -1
  235. package/src/react16/upgrade-to-react-16.ts +0 -59
  236. package/src/react17/upgrade-to-react-17.ts +0 -52
  237. package/src/react18/upgrade-to-react-18.ts +0 -55
  238. package/src/react19/upgrade-to-react-19.ts +0 -75
@@ -0,0 +1,117 @@
1
+ import {and, check, not, or, DataTable, ExecutionContext, foundSearchResult, Recipe, RecipeRef, TreeVisitor} from "@openrewrite/rewrite";
2
+ import {isVendoredOrBundled, JavaScriptVisitor, JS, usesMethod, usesType} from "@openrewrite/rewrite/javascript";
3
+ import {isIdentifier, J, Type, TypedTree} from "@openrewrite/rewrite/java";
4
+ import {enclosingScope, ENZYME_GUIDE, ENZYME_USAGE_TABLE, enzymeMethod, enzymeModule, enzymeWrapper, EnzymeUsageRow, migrationReason} from "../testing/enzyme-usage";
5
+
6
+ /**
7
+ * Inventories imports/setup even when types are unavailable; calls and wrapper fields are
8
+ * attributed to Enzyme by declaring type, not by names such as `find` or `state`.
9
+ *
10
+ * @see https://testing-library.com/docs/react-testing-library/migrate-from-enzyme/#how-to-migrate-from-enzyme-to-react-testing-library
11
+ */
12
+ export class FindEnzymeUsage extends Recipe {
13
+ readonly name = "org.openrewrite.react.search.find-enzyme-usage";
14
+ readonly displayName = "Find Enzyme usage";
15
+ readonly description = "Finds `enzyme` imports, re-exports, setup modules, typed calls, and wrapper fields, and reports review steps for migrating to React Testing Library. Module references are reported even when call types are unavailable.";
16
+
17
+ private readonly usages = new DataTable<EnzymeUsageRow>(
18
+ ENZYME_USAGE_TABLE, "Enzyme migration inventory",
19
+ "Remaining Enzyme usages and migration actions. Module rows include references whose call types may be unavailable; they are not counts of test cases.",
20
+ EnzymeUsageRow
21
+ );
22
+
23
+ async editor(): Promise<TreeVisitor<any, ExecutionContext>> {
24
+ const usages = this.usages;
25
+ // Keep untyped imports (including escaped module names), and attributed uses
26
+ // through helpers. The JVM evaluates these gates before serializing an LST to JS.
27
+ const relevant = or(
28
+ new RecipeRef("org.openrewrite.text.Find", {find: String.raw`enzyme|\\`, regex: true, caseSensitive: true}),
29
+ usesMethod("enzyme *(..)"), usesType("enzyme.*", true), usesType("enzyme..*", true)
30
+ );
31
+ return check(and(not(isVendoredOrBundled()), relevant), new class extends JavaScriptVisitor<ExecutionContext> {
32
+ private sourcePath = "";
33
+
34
+ override async visitJsCompilationUnit(cu: JS.CompilationUnit, ctx: ExecutionContext): Promise<J | undefined> {
35
+ this.sourcePath = cu.sourcePath;
36
+ return super.visitJsCompilationUnit(cu, ctx);
37
+ }
38
+
39
+ override async visitImportDeclaration(imp: JS.Import, ctx: ExecutionContext): Promise<J | undefined> {
40
+ const module = imp.moduleSpecifier?.element;
41
+ const moduleName = module?.kind === J.Kind.Literal ? (module as J.Literal).value : undefined;
42
+ const result = await super.visitImportDeclaration(imp, ctx) as JS.Import;
43
+ if (!enzymeModule(moduleName)) return result;
44
+ return this.report(result, moduleName, "MODULE_REFERENCE",
45
+ "Review this import and its consumers; unresolved or indirect calls require manual inspection. Keep it while Enzyme consumers remain.", ctx);
46
+ }
47
+
48
+ override async visitExportDeclaration(exp: JS.ExportDeclaration, ctx: ExecutionContext): Promise<J | undefined> {
49
+ const result = await super.visitExportDeclaration(exp, ctx) as JS.ExportDeclaration;
50
+ const module = exp.moduleSpecifier?.element;
51
+ const moduleName = module?.kind === J.Kind.Literal ? (module as J.Literal).value : undefined;
52
+ if (!enzymeModule(moduleName)) return result;
53
+ return this.report(result, moduleName, "HELPER_REEXPORT",
54
+ "Trace downstream helper consumers before migrating or removing this Enzyme re-export.", ctx);
55
+ }
56
+
57
+ override async visitMethodInvocation(method: J.MethodInvocation, ctx: ExecutionContext): Promise<J | undefined> {
58
+ const result = await super.visitMethodInvocation(method, ctx) as J.MethodInvocation;
59
+ const args = method.arguments.elements.map(arg => arg.element);
60
+ const moduleName = args[0]?.kind === J.Kind.Literal ? (args[0] as J.Literal).value : undefined;
61
+ // A local function called require is not a module loader.
62
+ if (!method.select && method.name.simpleName === "require" && args.length === 1 && enzymeModule(moduleName)) {
63
+ const variable = method.name.fieldType;
64
+ const unresolved = !variable && (!method.methodType || method.methodType.declaringType.kind === Type.Kind.Unknown);
65
+ const nodeRequire = variable && Type.isOfTypeWithName(variable.type, false, name => name === "NodeJS.Require");
66
+ if (unresolved || nodeRequire) {
67
+ return this.report(result, moduleName, "MODULE_REFERENCE",
68
+ "Review this module load and its consumers; CommonJS and dynamic imports may need manual migration.", ctx);
69
+ }
70
+ }
71
+ const api = enzymeMethod(method);
72
+ if (!api) return result;
73
+ const {reason, suggestedAction} = migrationReason(api);
74
+ return this.report(result, api, reason, suggestedAction, ctx);
75
+ }
76
+
77
+ override async visitFunctionCall(call: JS.FunctionCall, ctx: ExecutionContext): Promise<J | undefined> {
78
+ const result = await super.visitFunctionCall(call, ctx) as JS.FunctionCall;
79
+ const callee = call.function?.element;
80
+ const args = call.arguments.elements;
81
+ const module = args[0]?.element;
82
+ const name = module?.kind === J.Kind.Literal ? (module as J.Literal).value : undefined;
83
+ if (!callee || !isIdentifier(callee) || callee.simpleName !== "import" ||
84
+ args.length !== 1 || !enzymeModule(name)) return result;
85
+ return this.report(result, name, "MODULE_REFERENCE",
86
+ "Trace the consumers of this dynamic Enzyme import before migrating them.", ctx);
87
+ }
88
+
89
+ override async visitFieldAccess(field: J.FieldAccess, ctx: ExecutionContext): Promise<J | undefined> {
90
+ const result = await super.visitFieldAccess(field, ctx) as J.FieldAccess;
91
+ if (!enzymeWrapper(TypedTree.getType(field.target))) return result;
92
+ const api = field.name.element.simpleName;
93
+ return this.report(result, api, "WRAPPER_FIELD",
94
+ "Preserve assertion cardinality and wrapper semantics when replacing this Enzyme field.", ctx);
95
+ }
96
+
97
+ override async visitIdentifier(identifier: J.Identifier, ctx: ExecutionContext): Promise<J | undefined> {
98
+ const result = await super.visitIdentifier(identifier, ctx) as J.Identifier;
99
+ if (!identifier.fieldType || !enzymeWrapper(identifier.type)) return result;
100
+ const parent = this.cursor.parentTree()?.value as J | undefined;
101
+ if (parent?.kind === J.Kind.NamedVariable &&
102
+ (parent as J.VariableDeclarations.NamedVariable).name === identifier) return result;
103
+ if (parent?.kind === J.Kind.MethodInvocation &&
104
+ (parent as J.MethodInvocation).select?.element === identifier) return result;
105
+ if (parent?.kind === J.Kind.FieldAccess && (parent as J.FieldAccess).target === identifier) return result;
106
+ return this.report(result, identifier.simpleName, "WRAPPER_ESCAPE",
107
+ "Trace this wrapper alias, argument, assignment, or return value before migrating its connected scope.", ctx);
108
+ }
109
+
110
+ private report<T extends J>(node: T, api: string, reason: string, suggestedAction: string, ctx: ExecutionContext): T {
111
+ usages.insertRow(ctx, {sourcePath: this.sourcePath, scope: enclosingScope(this.cursor), api,
112
+ status: "REVIEW", reason, suggestedAction});
113
+ return foundSearchResult(node, `Enzyme ${api}: ${reason}. See ${ENZYME_GUIDE}`);
114
+ }
115
+ }());
116
+ }
117
+ }
@@ -0,0 +1,68 @@
1
+ import {Column, DataTable, ExecutionContext, foundSearchResult, Recipe, TreeVisitor, check, not} from "@openrewrite/rewrite";
2
+ import {isVendoredOrBundled, JavaScriptVisitor, JS} from "@openrewrite/rewrite/javascript";
3
+ import {J} from "@openrewrite/rewrite/java";
4
+ import {extendsReactComponentByName} from "../util/react-class";
5
+
6
+ class DefaultPropsRow {
7
+ @Column({displayName: "File path", description: "The file where `defaultProps` is assigned"})
8
+ filePath!: string;
9
+
10
+ @Column({displayName: "Component", description: "The component whose `defaultProps` is assigned"})
11
+ component!: string;
12
+
13
+ @Column({displayName: "Upgrade step", description: "The step recipe that migrates it"})
14
+ upgradeStep!: string;
15
+ }
16
+
17
+ export const DEFAULT_PROPS_STEP = 'org.openrewrite.react.migrate.react-18-to-19';
18
+ export const DEFAULT_PROPS_MIGRATOR = 'org.openrewrite.react.19.replace-default-props';
19
+
20
+ /**
21
+ * Finds `Component.defaultProps = ...` on a component that is not a class declared in the same file. React 19 removed
22
+ * `defaultProps` for function components only; a class component keeps it. The receiver must be capitalized, so
23
+ * `Object.keys(schema.defaultProps)` is not reported.
24
+ *
25
+ * @see https://react.dev/blog/2024/04/25/react-19-upgrade-guide#removed-proptypes-and-defaultprops
26
+ */
27
+ export class FindFunctionComponentDefaultProps extends Recipe {
28
+ readonly name = "org.openrewrite.react.search.find-function-component-default-props";
29
+ readonly displayName = "Find `defaultProps` on function components";
30
+ readonly description = "Finds assignments to `defaultProps` on a function component, which React 19 removed, and records the step recipe that migrates them in a data table.";
31
+
32
+ private dataTable = new DataTable<DefaultPropsRow>(
33
+ "org.openrewrite.react.search.FunctionComponentDefaultPropsTable",
34
+ "Function component defaultProps usages",
35
+ "Assignments to `defaultProps` on function components, with the step recipe that migrates them.",
36
+ DefaultPropsRow
37
+ );
38
+
39
+ async editor(): Promise<TreeVisitor<any, ExecutionContext>> {
40
+ const dataTable = this.dataTable;
41
+
42
+ return check(not(isVendoredOrBundled()), new class extends JavaScriptVisitor<ExecutionContext> {
43
+ private filePath = "";
44
+ private classComponents = new Set<string>();
45
+
46
+ override async visitJsCompilationUnit(cu: JS.CompilationUnit, ctx: ExecutionContext): Promise<J | undefined> {
47
+ this.filePath = cu.sourcePath ?? "";
48
+ const classes = this.classComponents = new Set<string>();
49
+ await new class extends JavaScriptVisitor<ExecutionContext> {
50
+ protected override async visitClassDeclaration(decl: J.ClassDeclaration, c: ExecutionContext): Promise<J | undefined> {
51
+ if (extendsReactComponentByName(decl)) classes.add(decl.name.simpleName);
52
+ return super.visitClassDeclaration(decl, c);
53
+ }
54
+ }().visit(cu, ctx);
55
+ return super.visitJsCompilationUnit(cu, ctx);
56
+ }
57
+
58
+ override async visitFieldAccess(fieldAccess: J.FieldAccess, ctx: ExecutionContext): Promise<J | undefined> {
59
+ const fa = await super.visitFieldAccess(fieldAccess, ctx) as J.FieldAccess;
60
+ if (fa.name.element.simpleName !== 'defaultProps' || fa.target.kind !== J.Kind.Identifier) return fa;
61
+ const receiver = (fa.target as J.Identifier).simpleName;
62
+ if (!/^[A-Z]/.test(receiver) || this.classComponents.has(receiver)) return fa;
63
+ dataTable.insertRow(ctx, {filePath: this.filePath, component: receiver, upgradeStep: DEFAULT_PROPS_STEP});
64
+ return foundSearchResult(fa, 'defaultProps') as J.FieldAccess;
65
+ }
66
+ }());
67
+ }
68
+ }
@@ -0,0 +1,41 @@
1
+ import {Recipe} from "@openrewrite/rewrite";
2
+ import {FindDeprecatedReactDomAPIs} from "../react19/find-deprecated-reactdom-apis";
3
+ import {FindLegacyContextAPI} from "../react19/find-legacy-context-api";
4
+ import {FindServerRenderingUsage} from "./find-server-rendering-usage";
5
+ import {FindReactDomRootApis} from "./find-reactdom-root-apis";
6
+ import {FindDeprecatedLifecycles} from "./find-deprecated-lifecycles";
7
+ import {FindFunctionComponentDefaultProps} from "./find-function-component-default-props";
8
+
9
+ /**
10
+ * Finds the legacy React APIs that block an upgrade to React 19, by running the finders that each cover one family
11
+ * of them. Every finder keeps its own marker and data table, so a call is reported once, by the finder that owns it:
12
+ *
13
+ * - `FindDeprecatedReactDomAPIs`: `findDOMNode`, `unmountComponentAtNode`, `createFactory`, `renderToNodeStream`.
14
+ * - `FindLegacyContextAPI`: `contextTypes`, `childContextTypes` and `getChildContext`.
15
+ * - `FindServerRenderingUsage`: server rendering entry points.
16
+ * - `FindReactDomRootApis`: `ReactDOM.render` and `ReactDOM.hydrate`, matched by import.
17
+ * - `FindDeprecatedLifecycles`: `componentWillMount`, `componentWillReceiveProps`, `componentWillUpdate`.
18
+ * - `FindFunctionComponentDefaultProps`: `defaultProps` on function components.
19
+ *
20
+ * It does not report string refs, `Context.Provider`, the `act` import, `unstable_batchedUpdates` or
21
+ * `react-test-renderer/shallow`; the step recipes migrate those without a finder.
22
+ *
23
+ * To see which React version a repository is on, use the `DevCenterReactStarter` card in DevCenter, which reads
24
+ * the resolved versions.
25
+ */
26
+ export class FindLegacyReactApis extends Recipe {
27
+ readonly name = "org.openrewrite.react.search.find-legacy-react-apis";
28
+ readonly displayName = "Find legacy React API usage";
29
+ readonly description = "Finds the removed and deprecated React APIs that block an upgrade to React 19 (`ReactDOM.render` and `hydrate`, `findDOMNode`, legacy context, `componentWill*` lifecycles, function component `defaultProps` and more) by running one finder for each family, each with its own data table. For where a repository sits on the upgrade path by version, see the `DevCenterReactStarter` card in DevCenter.";
30
+
31
+ async recipeList(): Promise<Recipe[]> {
32
+ return [
33
+ new FindReactDomRootApis(),
34
+ new FindDeprecatedReactDomAPIs(),
35
+ new FindLegacyContextAPI(),
36
+ new FindServerRenderingUsage(),
37
+ new FindDeprecatedLifecycles(),
38
+ new FindFunctionComponentDefaultProps(),
39
+ ];
40
+ }
41
+ }
@@ -0,0 +1,99 @@
1
+ import {Column, DataTable, ExecutionContext, foundSearchResult, Recipe, TreeVisitor, check, not} from "@openrewrite/rewrite";
2
+ import {JavaScriptVisitor, JS, isVendoredOrBundled} from "@openrewrite/rewrite/javascript";
3
+ import {J} from "@openrewrite/rewrite/java";
4
+ import {extendsReactComponentByName} from "../util/react-class";
5
+
6
+ class MissingDirectiveRow {
7
+ @Column({displayName: "File path", description: "The component file with no `'use client'` directive"})
8
+ filePath!: string;
9
+
10
+ @Column({displayName: "Reasons", description: "The client-only features the file uses, comma separated"})
11
+ reasons!: string;
12
+ }
13
+
14
+ const CLIENT_HOOKS = new Set([
15
+ 'useState', 'useEffect', 'useLayoutEffect', 'useReducer', 'useRef', 'useContext', 'useCallback', 'useMemo',
16
+ 'useTransition', 'useDeferredValue', 'useImperativeHandle', 'useSyncExternalStore', 'useInsertionEffect',
17
+ 'useActionState', 'useFormState', 'useOptimistic',
18
+ ]);
19
+ const BROWSER_GLOBALS = new Set(['window', 'document', 'localStorage', 'sessionStorage', 'navigator']);
20
+
21
+ /** Directives are the string-literal statements at the top of a file. */
22
+ function hasDirective(cu: JS.CompilationUnit, directive: string): boolean {
23
+ for (const statement of cu.statements) {
24
+ let node: any = statement.element;
25
+ if (node.kind === JS.Kind.ExpressionStatement) node = node.expression;
26
+ if (node.kind !== J.Kind.Literal) return false;
27
+ if ((node as J.Literal).value === directive) return true;
28
+ }
29
+ return false;
30
+ }
31
+
32
+ /**
33
+ * Finds component files that use client-only features but have no `'use client'` directive.
34
+ *
35
+ * In the Next.js App Router and other React Server Components setups, a file that uses state,
36
+ * effects, class components or browser globals fails unless it, or a file above it, is marked as a
37
+ * client boundary. This is a find-only recipe: the right boundary is usually the component that
38
+ * owns the interaction, which a recipe cannot tell. Only `.tsx` and `.jsx` files are considered.
39
+ */
40
+ export class FindMissingUseClientDirective extends Recipe {
41
+ readonly name = "org.openrewrite.react.search.find-missing-use-client-directive";
42
+ readonly displayName = "Find client components missing a `'use client'` directive";
43
+ readonly description = "Finds `.tsx` and `.jsx` files that use hooks, class components or browser globals and have no `'use client'` directive, to review before adopting React Server Components. Files with `'use server'` are skipped.";
44
+
45
+ private dataTable = new DataTable<MissingDirectiveRow>(
46
+ "org.openrewrite.react.search.MissingUseClientDirectiveTable",
47
+ "Files missing a 'use client' directive",
48
+ "Component files that use client-only features and have no 'use client' directive.",
49
+ MissingDirectiveRow
50
+ );
51
+
52
+ async editor(): Promise<TreeVisitor<any, ExecutionContext>> {
53
+ const dataTable = this.dataTable;
54
+
55
+ return check(not(isVendoredOrBundled()), new class extends JavaScriptVisitor<ExecutionContext> {
56
+ private reasons = new Set<string>();
57
+ private first?: J;
58
+
59
+ override async visitJsCompilationUnit(cu: JS.CompilationUnit, ctx: ExecutionContext): Promise<J | undefined> {
60
+ const path = cu.sourcePath ?? "";
61
+ if (!/\.[jt]sx$/.test(path) || hasDirective(cu, 'use client') || hasDirective(cu, 'use server')) return cu;
62
+ this.reasons = new Set();
63
+ this.first = undefined;
64
+ const result = await super.visitJsCompilationUnit(cu, ctx) as JS.CompilationUnit;
65
+ if (this.reasons.size > 0) {
66
+ dataTable.insertRow(ctx, {filePath: path, reasons: [...this.reasons].join(', ')});
67
+ }
68
+ return result;
69
+ }
70
+
71
+ /** Records a reason and marks only the file's first occurrence. */
72
+ private note<T extends J>(tree: T, reason: string): T {
73
+ this.reasons.add(reason);
74
+ if (this.first) return tree;
75
+ this.first = tree;
76
+ return foundSearchResult(tree, "Missing 'use client': " + reason) as T;
77
+ }
78
+
79
+ override async visitMethodInvocation(method: J.MethodInvocation, ctx: ExecutionContext): Promise<J | undefined> {
80
+ const m = await super.visitMethodInvocation(method, ctx) as J.MethodInvocation;
81
+ const select = m.select?.element;
82
+ const reactReceiver = select?.kind === J.Kind.Identifier && (select as J.Identifier).simpleName === 'React';
83
+ return CLIENT_HOOKS.has(m.name.simpleName) && (!select || reactReceiver) ? this.note(m, m.name.simpleName) : m;
84
+ }
85
+
86
+ override async visitClassDeclaration(decl: J.ClassDeclaration, ctx: ExecutionContext): Promise<J | undefined> {
87
+ const c = await super.visitClassDeclaration(decl, ctx) as J.ClassDeclaration;
88
+ return extendsReactComponentByName(c) ? this.note(c, 'class component') : c;
89
+ }
90
+
91
+ override async visitFieldAccess(fieldAccess: J.FieldAccess, ctx: ExecutionContext): Promise<J | undefined> {
92
+ const fa = await super.visitFieldAccess(fieldAccess, ctx) as J.FieldAccess;
93
+ const target = fa.target;
94
+ return target.kind === J.Kind.Identifier && BROWSER_GLOBALS.has((target as J.Identifier).simpleName)
95
+ ? this.note(fa, (target as J.Identifier).simpleName) : fa;
96
+ }
97
+ }());
98
+ }
99
+ }
@@ -0,0 +1,99 @@
1
+ import {Column, DataTable, ExecutionContext, foundSearchResult, Recipe, TreeVisitor, check, not} from "@openrewrite/rewrite";
2
+ import {isVendoredOrBundled, JavaScriptVisitor, JS, moduleBindings, resolve} from "@openrewrite/rewrite/javascript";
3
+ import {J} from "@openrewrite/rewrite/java";
4
+
5
+ class ReactDomRootApiRow {
6
+ @Column({displayName: "File path", description: "The file where the API is used"})
7
+ filePath!: string;
8
+
9
+ @Column({displayName: "API", description: "`ReactDOM.render` or `ReactDOM.hydrate`"})
10
+ api!: string;
11
+
12
+ @Column({displayName: "Upgrade step", description: "The step recipe that migrates the API"})
13
+ upgradeStep!: string;
14
+ }
15
+
16
+ /** The `react-dom` root APIs React 19 removed, with the step recipe that migrates each. */
17
+ export const REACTDOM_ROOT_APIS = new Map<string, { api: string; upgradeStep: string; migratedBy: string }>([
18
+ ['render', {api: 'ReactDOM.render', upgradeStep: 'org.openrewrite.react.migrate.react-17-to-18', migratedBy: 'org.openrewrite.react.18.replace-reactdom-render'}],
19
+ ['hydrate', {api: 'ReactDOM.hydrate', upgradeStep: 'org.openrewrite.react.migrate.react-18-to-19', migratedBy: 'org.openrewrite.react.19.replace-reactdom-hydrate'}],
20
+ ]);
21
+
22
+ /**
23
+ * Finds calls of `ReactDOM.render` and `ReactDOM.hydrate`, which React 19 removed.
24
+ *
25
+ * A call is matched by where its callee was imported from, not by how it is spelled: `ReactDOM.render` through a
26
+ * default, namespace or `require` binding of `react-dom` under any local name, and `render` or `hydrate` imported by
27
+ * name, renamed or not. A local function that happens to be called `render` is not reported.
28
+ *
29
+ * @see https://react.dev/blog/2024/04/25/react-19-upgrade-guide#removed-reactdom-render
30
+ */
31
+ export class FindReactDomRootApis extends Recipe {
32
+ readonly name = "org.openrewrite.react.search.find-reactdom-root-apis";
33
+ readonly displayName = "Find `ReactDOM.render` and `ReactDOM.hydrate`";
34
+ readonly description = "Finds calls of `ReactDOM.render` and `ReactDOM.hydrate` however `react-dom` was imported, and records the step recipe that migrates each in a data table.";
35
+
36
+ private dataTable = new DataTable<ReactDomRootApiRow>(
37
+ "org.openrewrite.react.search.ReactDomRootApisTable",
38
+ "ReactDOM root API usages",
39
+ "Calls of `ReactDOM.render` and `ReactDOM.hydrate`, with the step recipe that migrates each.",
40
+ ReactDomRootApiRow
41
+ );
42
+
43
+ async editor(): Promise<TreeVisitor<any, ExecutionContext>> {
44
+ const dataTable = this.dataTable;
45
+
46
+ return check(not(isVendoredOrBundled()), new class extends JavaScriptVisitor<ExecutionContext> {
47
+ private filePath = "";
48
+ /** Local name to the `react-dom` member it was imported by name, for `import {render as r} from 'react-dom'`. */
49
+ private named = new Map<string, string>();
50
+
51
+ override async visitJsCompilationUnit(cu: JS.CompilationUnit, ctx: ExecutionContext): Promise<J | undefined> {
52
+ this.filePath = cu.sourcePath ?? "";
53
+ this.named.clear();
54
+ return super.visitJsCompilationUnit(cu, ctx);
55
+ }
56
+
57
+ protected override async visitImportSpecifier(spec: JS.ImportSpecifier, ctx: ExecutionContext): Promise<J | undefined> {
58
+ const imp = this.cursor.firstEnclosing((n: any): n is JS.Import => n?.kind === JS.Kind.Import);
59
+ const module = (imp?.moduleSpecifier?.element as J.Literal | undefined)?.value;
60
+ if (module === 'react-dom') {
61
+ const specifier = spec.specifier as J;
62
+ if (specifier.kind === JS.Kind.Alias) {
63
+ const alias = specifier as JS.Alias;
64
+ const imported = alias.propertyName.element as J;
65
+ if (imported.kind === J.Kind.Identifier && alias.alias.kind === J.Kind.Identifier) {
66
+ this.named.set((alias.alias as J.Identifier).simpleName, (imported as J.Identifier).simpleName);
67
+ }
68
+ } else if (specifier.kind === J.Kind.Identifier) {
69
+ this.named.set((specifier as J.Identifier).simpleName, (specifier as J.Identifier).simpleName);
70
+ }
71
+ }
72
+ return super.visitImportSpecifier(spec, ctx);
73
+ }
74
+
75
+ override async visitMethodInvocation(method: J.MethodInvocation, ctx: ExecutionContext): Promise<J | undefined> {
76
+ const m = await super.visitMethodInvocation(method, ctx) as J.MethodInvocation;
77
+ const select = m.select?.element as J | undefined;
78
+ let member: string | undefined;
79
+ if (select) {
80
+ if (select.kind === J.Kind.Identifier && moduleBindings(this).moduleOf((select as J.Identifier).simpleName) === 'react-dom') {
81
+ member = m.name.simpleName;
82
+ }
83
+ } else if (this.named.has(m.name.simpleName) && this.boundByTheImport(m.name)) {
84
+ member = this.named.get(m.name.simpleName);
85
+ }
86
+ const found = member ? REACTDOM_ROOT_APIS.get(member) : undefined;
87
+ if (!found) return m;
88
+ dataTable.insertRow(ctx, {filePath: this.filePath, api: found.api, upgradeStep: found.upgradeStep});
89
+ return foundSearchResult(m, found.api) as J.MethodInvocation;
90
+ }
91
+
92
+ /** Whether the name still reads the import, rather than a parameter or local declaration that shadows it. */
93
+ private boundByTheImport(name: J.Identifier): boolean {
94
+ const owner = resolve(this.cursor, name);
95
+ return owner === undefined || owner.kind === JS.Kind.CompilationUnit;
96
+ }
97
+ }());
98
+ }
99
+ }