@openrewrite/recipes-react 0.20.0 → 0.22.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 (276) hide show
  1. package/dist/i18n/translate-hard-coded-strings.d.ts +67 -0
  2. package/dist/i18n/translate-hard-coded-strings.d.ts.map +1 -0
  3. package/dist/i18n/translate-hard-coded-strings.js +439 -0
  4. package/dist/i18n/translate-hard-coded-strings.js.map +1 -0
  5. package/dist/index.d.ts +3 -0
  6. package/dist/index.d.ts.map +1 -1
  7. package/dist/index.js +37 -1
  8. package/dist/index.js.map +1 -1
  9. package/dist/mui/adapt-v4-theme.d.ts +8 -0
  10. package/dist/mui/adapt-v4-theme.d.ts.map +1 -0
  11. package/dist/mui/adapt-v4-theme.js +65 -0
  12. package/dist/mui/adapt-v4-theme.js.map +1 -0
  13. package/dist/mui/add-component-prop.d.ts +18 -0
  14. package/dist/mui/add-component-prop.d.ts.map +1 -0
  15. package/dist/mui/add-component-prop.js +53 -0
  16. package/dist/mui/add-component-prop.js.map +1 -0
  17. package/dist/mui/add-emotion-prepend.d.ts +8 -0
  18. package/dist/mui/add-emotion-prepend.d.ts.map +1 -0
  19. package/dist/mui/add-emotion-prepend.js +71 -0
  20. package/dist/mui/add-emotion-prepend.js.map +1 -0
  21. package/dist/mui/change-module-specifier.d.ts +14 -0
  22. package/dist/mui/change-module-specifier.d.ts.map +1 -0
  23. package/dist/mui/change-module-specifier.js +95 -0
  24. package/dist/mui/change-module-specifier.js.map +1 -0
  25. package/dist/mui/convert-mui-imports.d.ts +14 -0
  26. package/dist/mui/convert-mui-imports.d.ts.map +1 -0
  27. package/dist/mui/convert-mui-imports.js +195 -0
  28. package/dist/mui/convert-mui-imports.js.map +1 -0
  29. package/dist/mui/convert-prop-to-sx.d.ts +22 -0
  30. package/dist/mui/convert-prop-to-sx.d.ts.map +1 -0
  31. package/dist/mui/convert-prop-to-sx.js +99 -0
  32. package/dist/mui/convert-prop-to-sx.js.map +1 -0
  33. package/dist/mui/index.d.ts +32 -0
  34. package/dist/mui/index.d.ts.map +1 -0
  35. package/dist/mui/index.js +67 -0
  36. package/dist/mui/index.js.map +1 -0
  37. package/dist/mui/jsx-attributes.d.ts +27 -0
  38. package/dist/mui/jsx-attributes.d.ts.map +1 -0
  39. package/dist/mui/jsx-attributes.js +140 -0
  40. package/dist/mui/jsx-attributes.js.map +1 -0
  41. package/dist/mui/mark-manual-migration.d.ts +16 -0
  42. package/dist/mui/mark-manual-migration.d.ts.map +1 -0
  43. package/dist/mui/mark-manual-migration.js +73 -0
  44. package/dist/mui/mark-manual-migration.js.map +1 -0
  45. package/dist/mui/mark-palette-mode-usage.d.ts +12 -0
  46. package/dist/mui/mark-palette-mode-usage.d.ts.map +1 -0
  47. package/dist/mui/mark-palette-mode-usage.js +50 -0
  48. package/dist/mui/mark-palette-mode-usage.js.map +1 -0
  49. package/dist/mui/move-component-prop-to-slot.d.ts +22 -0
  50. package/dist/mui/move-component-prop-to-slot.d.ts.map +1 -0
  51. package/dist/mui/move-component-prop-to-slot.js +87 -0
  52. package/dist/mui/move-component-prop-to-slot.js.map +1 -0
  53. package/dist/mui/move-primary-text-to-children.d.ts +12 -0
  54. package/dist/mui/move-primary-text-to-children.d.ts.map +1 -0
  55. package/dist/mui/move-primary-text-to-children.js +52 -0
  56. package/dist/mui/move-primary-text-to-children.js.map +1 -0
  57. package/dist/mui/move-system-props-to-sx.d.ts +16 -0
  58. package/dist/mui/move-system-props-to-sx.d.ts.map +1 -0
  59. package/dist/mui/move-system-props-to-sx.js +122 -0
  60. package/dist/mui/move-system-props-to-sx.js.map +1 -0
  61. package/dist/mui/mui-components.d.ts +8 -0
  62. package/dist/mui/mui-components.d.ts.map +1 -0
  63. package/dist/mui/mui-components.js +41 -0
  64. package/dist/mui/mui-components.js.map +1 -0
  65. package/dist/mui/mui-tag-visitor.d.ts +5 -0
  66. package/dist/mui/mui-tag-visitor.d.ts.map +1 -0
  67. package/dist/mui/mui-tag-visitor.js +54 -0
  68. package/dist/mui/mui-tag-visitor.js.map +1 -0
  69. package/dist/mui/pre-v5-recipe.d.ts +14 -0
  70. package/dist/mui/pre-v5-recipe.d.ts.map +1 -0
  71. package/dist/mui/pre-v5-recipe.js +88 -0
  72. package/dist/mui/pre-v5-recipe.js.map +1 -0
  73. package/dist/mui/remove-component-prop.d.ts +18 -0
  74. package/dist/mui/remove-component-prop.d.ts.map +1 -0
  75. package/dist/mui/remove-component-prop.js +55 -0
  76. package/dist/mui/remove-component-prop.js.map +1 -0
  77. package/dist/mui/remove-root-ref.d.ts +8 -0
  78. package/dist/mui/remove-root-ref.d.ts.map +1 -0
  79. package/dist/mui/remove-root-ref.js +61 -0
  80. package/dist/mui/remove-root-ref.js.map +1 -0
  81. package/dist/mui/remove-spacing-px-suffix.d.ts +8 -0
  82. package/dist/mui/remove-spacing-px-suffix.d.ts.map +1 -0
  83. package/dist/mui/remove-spacing-px-suffix.js +62 -0
  84. package/dist/mui/remove-spacing-px-suffix.js.map +1 -0
  85. package/dist/mui/rename-component-prop.d.ts +20 -0
  86. package/dist/mui/rename-component-prop.d.ts.map +1 -0
  87. package/dist/mui/rename-component-prop.js +66 -0
  88. package/dist/mui/rename-component-prop.js.map +1 -0
  89. package/dist/mui/rename-joy-css-variables.d.ts +8 -0
  90. package/dist/mui/rename-joy-css-variables.d.ts.map +1 -0
  91. package/dist/mui/rename-joy-css-variables.js +51 -0
  92. package/dist/mui/rename-joy-css-variables.js.map +1 -0
  93. package/dist/mui/rename-theme-palette-type.d.ts +8 -0
  94. package/dist/mui/rename-theme-palette-type.d.ts.map +1 -0
  95. package/dist/mui/rename-theme-palette-type.js +61 -0
  96. package/dist/mui/rename-theme-palette-type.js.map +1 -0
  97. package/dist/mui/replace-box-border-radius.d.ts +10 -0
  98. package/dist/mui/replace-box-border-radius.d.ts.map +1 -0
  99. package/dist/mui/replace-box-border-radius.js +50 -0
  100. package/dist/mui/replace-box-border-radius.js.map +1 -0
  101. package/dist/mui/replace-breakpoints-width.d.ts +8 -0
  102. package/dist/mui/replace-breakpoints-width.d.ts.map +1 -0
  103. package/dist/mui/replace-breakpoints-width.js +46 -0
  104. package/dist/mui/replace-breakpoints-width.js.map +1 -0
  105. package/dist/mui/replace-component-class.d.ts +18 -0
  106. package/dist/mui/replace-component-class.d.ts.map +1 -0
  107. package/dist/mui/replace-component-class.js +126 -0
  108. package/dist/mui/replace-component-class.js.map +1 -0
  109. package/dist/mui/replace-components-with-slots.d.ts +14 -0
  110. package/dist/mui/replace-components-with-slots.d.ts.map +1 -0
  111. package/dist/mui/replace-components-with-slots.js +69 -0
  112. package/dist/mui/replace-components-with-slots.js.map +1 -0
  113. package/dist/mui/replace-import-paths.d.ts +14 -0
  114. package/dist/mui/replace-import-paths.d.ts.map +1 -0
  115. package/dist/mui/replace-import-paths.js +101 -0
  116. package/dist/mui/replace-import-paths.js.map +1 -0
  117. package/dist/mui/replace-in-string-literals.d.ts +15 -0
  118. package/dist/mui/replace-in-string-literals.d.ts.map +1 -0
  119. package/dist/mui/replace-in-string-literals.js +62 -0
  120. package/dist/mui/replace-in-string-literals.js.map +1 -0
  121. package/dist/mui/replace-list-item-button-prop.d.ts +12 -0
  122. package/dist/mui/replace-list-item-button-prop.d.ts.map +1 -0
  123. package/dist/mui/replace-list-item-button-prop.js +80 -0
  124. package/dist/mui/replace-list-item-button-prop.js.map +1 -0
  125. package/dist/mui/replace-tabs-scroll-buttons.d.ts +8 -0
  126. package/dist/mui/replace-tabs-scroll-buttons.d.ts.map +1 -0
  127. package/dist/mui/replace-tabs-scroll-buttons.js +41 -0
  128. package/dist/mui/replace-tabs-scroll-buttons.js.map +1 -0
  129. package/dist/mui/replace-theme-color-functions.d.ts +8 -0
  130. package/dist/mui/replace-theme-color-functions.d.ts.map +1 -0
  131. package/dist/mui/replace-theme-color-functions.js +72 -0
  132. package/dist/mui/replace-theme-color-functions.js.map +1 -0
  133. package/dist/mui/replace-theme-spacing-unit.d.ts +8 -0
  134. package/dist/mui/replace-theme-spacing-unit.d.ts.map +1 -0
  135. package/dist/mui/replace-theme-spacing-unit.js +77 -0
  136. package/dist/mui/replace-theme-spacing-unit.js.map +1 -0
  137. package/dist/mui/replace-typography-round.d.ts +8 -0
  138. package/dist/mui/replace-typography-round.d.ts.map +1 -0
  139. package/dist/mui/replace-typography-round.js +44 -0
  140. package/dist/mui/replace-typography-round.js.map +1 -0
  141. package/dist/mui/shift-hidden-down-props.d.ts +10 -0
  142. package/dist/mui/shift-hidden-down-props.d.ts.map +1 -0
  143. package/dist/mui/shift-hidden-down-props.js +49 -0
  144. package/dist/mui/shift-hidden-down-props.js.map +1 -0
  145. package/dist/mui/theme-access.d.ts +5 -0
  146. package/dist/mui/theme-access.d.ts.map +1 -0
  147. package/dist/mui/theme-access.js +21 -0
  148. package/dist/mui/theme-access.js.map +1 -0
  149. package/dist/mui/update-theme-breakpoints.d.ts +10 -0
  150. package/dist/mui/update-theme-breakpoints.d.ts.map +1 -0
  151. package/dist/mui/update-theme-breakpoints.js +58 -0
  152. package/dist/mui/update-theme-breakpoints.js.map +1 -0
  153. package/dist/mui/upgrade-grid-props.d.ts +16 -0
  154. package/dist/mui/upgrade-grid-props.d.ts.map +1 -0
  155. package/dist/mui/upgrade-grid-props.js +90 -0
  156. package/dist/mui/upgrade-grid-props.js.map +1 -0
  157. package/dist/search/find-enzyme-usage.d.ts +8 -0
  158. package/dist/search/find-enzyme-usage.d.ts.map +1 -1
  159. package/dist/search/find-enzyme-usage.js +119 -8
  160. package/dist/search/find-enzyme-usage.js.map +1 -1
  161. package/dist/search/find-hard-coded-strings.d.ts +110 -0
  162. package/dist/search/find-hard-coded-strings.d.ts.map +1 -0
  163. package/dist/search/find-hard-coded-strings.js +987 -0
  164. package/dist/search/find-hard-coded-strings.js.map +1 -0
  165. package/dist/search/find-missing-use-client-directive.d.ts +2 -0
  166. package/dist/search/find-missing-use-client-directive.d.ts.map +1 -1
  167. package/dist/search/find-missing-use-client-directive.js +1 -0
  168. package/dist/search/find-missing-use-client-directive.js.map +1 -1
  169. package/dist/testing/enzyme-host-components.d.ts +12 -0
  170. package/dist/testing/enzyme-host-components.d.ts.map +1 -1
  171. package/dist/testing/enzyme-host-components.js +100 -0
  172. package/dist/testing/enzyme-host-components.js.map +1 -1
  173. package/dist/testing/enzyme-portals.d.ts +3 -5
  174. package/dist/testing/enzyme-portals.d.ts.map +1 -1
  175. package/dist/testing/enzyme-portals.js +8 -39
  176. package/dist/testing/enzyme-portals.js.map +1 -1
  177. package/dist/testing/enzyme-render-blockers.d.ts +19 -0
  178. package/dist/testing/enzyme-render-blockers.d.ts.map +1 -0
  179. package/dist/testing/enzyme-render-blockers.js +275 -0
  180. package/dist/testing/enzyme-render-blockers.js.map +1 -0
  181. package/dist/testing/enzyme-to-react-testing-library.d.ts +17 -3
  182. package/dist/testing/enzyme-to-react-testing-library.d.ts.map +1 -1
  183. package/dist/testing/enzyme-to-react-testing-library.js +237 -27
  184. package/dist/testing/enzyme-to-react-testing-library.js.map +1 -1
  185. package/dist/testing/enzyme-usage.d.ts +9 -0
  186. package/dist/testing/enzyme-usage.d.ts.map +1 -1
  187. package/dist/testing/enzyme-usage.js +36 -3
  188. package/dist/testing/enzyme-usage.js.map +1 -1
  189. package/dist/testing/enzyme-wrapper-scope.d.ts +8 -1
  190. package/dist/testing/enzyme-wrapper-scope.d.ts.map +1 -1
  191. package/dist/testing/enzyme-wrapper-scope.js +26 -5
  192. package/dist/testing/enzyme-wrapper-scope.js.map +1 -1
  193. package/dist/util/json-and-javascript.d.ts +4 -0
  194. package/dist/util/json-and-javascript.d.ts.map +1 -0
  195. package/dist/util/json-and-javascript.js +37 -0
  196. package/dist/util/json-and-javascript.js.map +1 -0
  197. package/dist/util/module-imports.d.ts +36 -0
  198. package/dist/util/module-imports.d.ts.map +1 -1
  199. package/dist/util/module-imports.js +216 -0
  200. package/dist/util/module-imports.js.map +1 -1
  201. package/dist/util/test-source.d.ts +2 -0
  202. package/dist/util/test-source.d.ts.map +1 -0
  203. package/dist/util/test-source.js +5 -0
  204. package/dist/util/test-source.js.map +1 -0
  205. package/dist/zustand/entry-store.d.ts +9 -0
  206. package/dist/zustand/entry-store.d.ts.map +1 -0
  207. package/dist/zustand/entry-store.js +136 -0
  208. package/dist/zustand/entry-store.js.map +1 -0
  209. package/dist/zustand/redux-stores.d.ts +7 -4
  210. package/dist/zustand/redux-stores.d.ts.map +1 -1
  211. package/dist/zustand/redux-stores.js +128 -11
  212. package/dist/zustand/redux-stores.js.map +1 -1
  213. package/dist/zustand/redux-to-zustand.d.ts +11 -2
  214. package/dist/zustand/redux-to-zustand.d.ts.map +1 -1
  215. package/dist/zustand/redux-to-zustand.js +147 -46
  216. package/dist/zustand/redux-to-zustand.js.map +1 -1
  217. package/dist/zustand/zustand-plan.d.ts +14 -5
  218. package/dist/zustand/zustand-plan.d.ts.map +1 -1
  219. package/dist/zustand/zustand-plan.js +226 -52
  220. package/dist/zustand/zustand-plan.js.map +1 -1
  221. package/package.json +1 -1
  222. package/src/i18n/translate-hard-coded-strings.ts +442 -0
  223. package/src/index.ts +17 -0
  224. package/src/mui/adapt-v4-theme.ts +43 -0
  225. package/src/mui/add-component-prop.ts +43 -0
  226. package/src/mui/add-emotion-prepend.ts +48 -0
  227. package/src/mui/change-module-specifier.ts +71 -0
  228. package/src/mui/convert-mui-imports.ts +184 -0
  229. package/src/mui/convert-prop-to-sx.ts +89 -0
  230. package/src/mui/index.ts +31 -0
  231. package/src/mui/jsx-attributes.ts +133 -0
  232. package/src/mui/mark-manual-migration.ts +53 -0
  233. package/src/mui/mark-palette-mode-usage.ts +36 -0
  234. package/src/mui/move-component-prop-to-slot.ts +84 -0
  235. package/src/mui/move-primary-text-to-children.ts +41 -0
  236. package/src/mui/move-system-props-to-sx.ts +118 -0
  237. package/src/mui/mui-components.ts +40 -0
  238. package/src/mui/mui-tag-visitor.ts +36 -0
  239. package/src/mui/pre-v5-recipe.ts +82 -0
  240. package/src/mui/remove-component-prop.ts +43 -0
  241. package/src/mui/remove-root-ref.ts +41 -0
  242. package/src/mui/remove-spacing-px-suffix.ts +43 -0
  243. package/src/mui/rename-component-prop.ts +55 -0
  244. package/src/mui/rename-joy-css-variables.ts +41 -0
  245. package/src/mui/rename-theme-palette-type.ts +42 -0
  246. package/src/mui/replace-box-border-radius.ts +42 -0
  247. package/src/mui/replace-breakpoints-width.ts +28 -0
  248. package/src/mui/replace-component-class.ts +97 -0
  249. package/src/mui/replace-components-with-slots.ts +61 -0
  250. package/src/mui/replace-import-paths.ts +100 -0
  251. package/src/mui/replace-in-string-literals.ts +44 -0
  252. package/src/mui/replace-list-item-button-prop.ts +59 -0
  253. package/src/mui/replace-tabs-scroll-buttons.ts +31 -0
  254. package/src/mui/replace-theme-color-functions.ts +55 -0
  255. package/src/mui/replace-theme-spacing-unit.ts +59 -0
  256. package/src/mui/replace-typography-round.ts +29 -0
  257. package/src/mui/shift-hidden-down-props.ts +44 -0
  258. package/src/mui/theme-access.ts +18 -0
  259. package/src/mui/update-theme-breakpoints.ts +47 -0
  260. package/src/mui/upgrade-grid-props.ts +82 -0
  261. package/src/search/find-enzyme-usage.ts +109 -12
  262. package/src/search/find-hard-coded-strings.ts +1006 -0
  263. package/src/search/find-missing-use-client-directive.ts +1 -1
  264. package/src/testing/enzyme-host-components.ts +110 -0
  265. package/src/testing/enzyme-portals.ts +4 -44
  266. package/src/testing/enzyme-render-blockers.ts +278 -0
  267. package/src/testing/enzyme-to-react-testing-library.ts +240 -34
  268. package/src/testing/enzyme-usage.ts +48 -2
  269. package/src/testing/enzyme-wrapper-scope.ts +35 -3
  270. package/src/util/json-and-javascript.ts +26 -0
  271. package/src/util/module-imports.ts +223 -0
  272. package/src/util/test-source.ts +2 -0
  273. package/src/zustand/entry-store.ts +131 -0
  274. package/src/zustand/redux-stores.ts +131 -12
  275. package/src/zustand/redux-to-zustand.ts +179 -52
  276. package/src/zustand/zustand-plan.ts +256 -49
@@ -0,0 +1,29 @@
1
+ import {ExecutionContext, Recipe, TreeVisitor, check, not} from "@openrewrite/rewrite";
2
+ import {JavaScriptVisitor, isVendoredOrBundled, template} from "@openrewrite/rewrite/javascript";
3
+ import {J} from "@openrewrite/rewrite/java";
4
+ import {endsWithName} from "./theme-access";
5
+
6
+ /**
7
+ * Replaces `theme.typography.round(x)`, which v5 removed, with its definition.
8
+ *
9
+ * Before: `theme.typography.round(16 / 14)`
10
+ * After: `Math.round(16 / 14 * 1e5) / 1e5`
11
+ *
12
+ * @see https://mui.com/material-ui/migration/v5-component-changes/#theme
13
+ */
14
+ export class ReplaceTypographyRound extends Recipe {
15
+ readonly name = "org.openrewrite.react.mui.replace-typography-round";
16
+ readonly displayName: string = "Replace `theme.typography.round()`";
17
+ readonly description: string = "Replaces `theme.typography.round(x)` with `Math.round(x * 1e5) / 1e5`.";
18
+
19
+ async editor(): Promise<TreeVisitor<any, ExecutionContext>> {
20
+ return check(not(isVendoredOrBundled()), new class extends JavaScriptVisitor<ExecutionContext> {
21
+ override async visitMethodInvocation(invocation: J.MethodInvocation, ctx: ExecutionContext): Promise<J | undefined> {
22
+ const visited = await super.visitMethodInvocation(invocation, ctx) as J.MethodInvocation;
23
+ if (visited.name.simpleName !== "round" || visited.arguments.elements.length !== 1 || !endsWithName(visited.select?.element, "typography")) return visited;
24
+ const replaced = await template`Math.round(${visited.arguments.elements[0].element} * 1e5) / 1e5`.apply(visited, this.cursor);
25
+ return replaced ? {...replaced, prefix: visited.prefix} as J : visited;
26
+ }
27
+ }());
28
+ }
29
+ }
@@ -0,0 +1,44 @@
1
+ import {ExecutionContext, TreeVisitor} from "@openrewrite/rewrite";
2
+ import {PreV5Recipe} from "./pre-v5-recipe";
3
+ import {J} from "@openrewrite/rewrite/java";
4
+ import {attributeName} from "./mui-components";
5
+ import {muiTagEditor} from "./mui-tag-visitor";
6
+
7
+ const SHIFT: Record<string, string> = {xsDown: "smDown", smDown: "mdDown", mdDown: "lgDown", lgDown: "xlDown"};
8
+
9
+ /**
10
+ * Shifts the `...Down` props of `Hidden` one breakpoint up, since v5 reads them as "below" where v4 read "up to".
11
+ *
12
+ * Before: `<Hidden xsDown />`, `<Hidden lgDown />`
13
+ * After: `<Hidden smDown />`, `<Hidden xlDown />`
14
+ *
15
+ * Every prop of a tag is shifted once, in one step, so that `smDown` that was `xsDown` is not shifted again.
16
+ *
17
+ * @see https://mui.com/material-ui/migration/v5-component-changes/#hidden
18
+ */
19
+ export class ShiftHiddenDownProps extends PreV5Recipe {
20
+ readonly name = "org.openrewrite.react.mui.shift-hidden-down-props";
21
+ readonly displayName: string = "Shift the `Hidden` `...Down` props one breakpoint up";
22
+ readonly description: string = "Renames `xsDown` to `smDown`, `smDown` to `mdDown`, `mdDown` to `lgDown` and `lgDown` to `xlDown` on `Hidden`, each prop once.";
23
+
24
+ /** The tags this recipe shifted, which the instance keeps across cycles, since a second cycle would shift them again. */
25
+ private readonly shifted = new Set<string>();
26
+
27
+ protected async v4Editor(): Promise<TreeVisitor<any, ExecutionContext>> {
28
+ const shifted = this.shifted;
29
+ return muiTagEditor("Hidden", undefined, async tag => {
30
+ if (shifted.has(tag.id)) return tag;
31
+ let changed = false;
32
+ const attributes = tag.attributes.map(a => {
33
+ const name = attributeName(a.element);
34
+ if (!name || !(name in SHIFT) || a.element.kind !== "org.openrewrite.javascript.tree.JSX$Attribute") return a;
35
+ changed = true;
36
+ const attribute = a.element as any;
37
+ return {...a, element: {...attribute, key: {...(attribute.key as J.Identifier), simpleName: SHIFT[name]}}};
38
+ });
39
+ if (!changed) return tag;
40
+ shifted.add(tag.id);
41
+ return {...tag, attributes} as typeof tag;
42
+ });
43
+ }
44
+ }
@@ -0,0 +1,18 @@
1
+ import {isIdentifier, J} from "@openrewrite/rewrite/java";
2
+
3
+ /** Whether an expression is `name` or ends with `.name`, such as `theme.breakpoints` for `breakpoints`. */
4
+ export function endsWithName(expression: J | undefined, name: string): boolean {
5
+ if (!expression) return false;
6
+ if (isIdentifier(expression)) return expression.simpleName === name;
7
+ return expression.kind === J.Kind.FieldAccess && (expression as J.FieldAccess).name.element.simpleName === name;
8
+ }
9
+
10
+ /** The text of a plain string literal, or undefined. */
11
+ export const stringValue = (e: J | undefined): string | undefined =>
12
+ e?.kind === J.Kind.Literal && typeof (e as J.Literal).value === "string" ? (e as J.Literal).value as string : undefined;
13
+
14
+ /** The literal with its text replaced, in the quotes it already has. */
15
+ export function withString(literal: J.Literal, text: string): J.Literal {
16
+ const quote = literal.valueSource?.charAt(0) ?? "'";
17
+ return {...literal, value: text, valueSource: `${quote}${text}${quote}`} as J.Literal;
18
+ }
@@ -0,0 +1,47 @@
1
+ import {ExecutionContext, TreeVisitor, check, not} from "@openrewrite/rewrite";
2
+ import {JavaScriptVisitor, isVendoredOrBundled} from "@openrewrite/rewrite/javascript";
3
+ import {PreV5Recipe} from "./pre-v5-recipe";
4
+ import {J} from "@openrewrite/rewrite/java";
5
+ import {endsWithName, stringValue, withString} from "./theme-access";
6
+
7
+ const NEXT = new Map([["xs", "sm"], ["sm", "md"], ["md", "lg"], ["lg", "xl"], ["xl", "xl"]]);
8
+
9
+ /**
10
+ * Moves the breakpoint values of `theme.breakpoints.down()` and the end of `between()` up one step, since v5 reads
11
+ * them as exclusive.
12
+ *
13
+ * Before: `theme.breakpoints.down('sm')`, `theme.breakpoints.between('sm', 'md')`
14
+ * After: `theme.breakpoints.down('md')`, `theme.breakpoints.between('sm', 'lg')`
15
+ *
16
+ * It is not idempotent: run it once.
17
+ *
18
+ * @see https://mui.com/material-ui/migration/v5-component-changes/#theme
19
+ */
20
+ export class UpdateThemeBreakpoints extends PreV5Recipe {
21
+ readonly name = "org.openrewrite.react.mui.update-theme-breakpoints";
22
+ readonly displayName: string = "Update theme breakpoint values for v5";
23
+ readonly description: string = "Moves the breakpoint passed to `theme.breakpoints.down()`, and the second one passed to `between()`, up one step to match the v5 logic. Not idempotent: run it once.";
24
+
25
+ /** The literals shifted so far in a run, which the recipe instance keeps across cycles, since a second cycle would shift them again. */
26
+ private readonly shifted = new Set<string>();
27
+
28
+ protected async v4Editor(): Promise<TreeVisitor<any, ExecutionContext>> {
29
+ const shifted = this.shifted;
30
+ return check(not(isVendoredOrBundled()), new class extends JavaScriptVisitor<ExecutionContext> {
31
+ override async visitMethodInvocation(invocation: J.MethodInvocation, ctx: ExecutionContext): Promise<J | undefined> {
32
+ const visited = await super.visitMethodInvocation(invocation, ctx) as J.MethodInvocation;
33
+ const name = visited.name.simpleName;
34
+ if ((name !== "down" && name !== "between") || !endsWithName(visited.select?.element, "breakpoints")) return visited;
35
+ const elements = visited.arguments.elements.map((a, i) => {
36
+ const value = stringValue(a.element);
37
+ const next = value && NEXT.get(value);
38
+ if (!next || !(name === "down" || i === 1) || shifted.has(a.element.id)) return a;
39
+ shifted.add(a.element.id);
40
+ return {...a, element: withString(a.element as J.Literal, next)};
41
+ });
42
+ if (elements.every((e, i) => e === visited.arguments.elements[i])) return visited;
43
+ return {...visited, arguments: {...visited.arguments, elements}} as J.MethodInvocation;
44
+ }
45
+ }());
46
+ }
47
+ }
@@ -0,0 +1,82 @@
1
+ import {Cursor, ExecutionContext, Option, Recipe, TreeVisitor} from "@openrewrite/rewrite";
2
+ import {JSX} from "@openrewrite/rewrite/javascript";
3
+ import {J} from "@openrewrite/rewrite/java";
4
+ import {
5
+ appendProperty, attributeExpression, bare, hasSpread, newAttribute, newObject, newProperty, sourceExpression, withAttributes
6
+ } from "./jsx-attributes";
7
+ import {attributeName} from "./mui-components";
8
+ import {muiTagEditor} from "./mui-tag-visitor";
9
+
10
+ const DEFAULT_BREAKPOINTS = ["xs", "sm", "md", "lg", "xl"];
11
+
12
+ /**
13
+ * Moves the breakpoint props of the responsive `Grid` into its `size` and `offset` props, and drops `item` and
14
+ * `zeroMinWidth`.
15
+ *
16
+ * Before: `<Grid item xs={12} sm={6} xsOffset={2} />`
17
+ * After: `<Grid size={{ xs: 12, sm: 6 }} offset={2} />`
18
+ *
19
+ * A prop without a value, such as a bare `xs`, becomes `"grow"`. A tag with a spread attribute is left alone.
20
+ *
21
+ * @see https://mui.com/material-ui/migration/upgrade-to-grid-v2/
22
+ */
23
+ export class UpgradeGridProps extends Recipe {
24
+ readonly name = "org.openrewrite.react.mui.upgrade-grid-props";
25
+ readonly displayName: string = "Upgrade the props of Grid to `size` and `offset`";
26
+ readonly description: string = "Moves the breakpoint props of the responsive Grid, `xs={12}` and `xsOffset={2}`, into its `size` and `offset` props, and removes `item` and `zeroMinWidth`. Tags with a spread attribute are left alone.";
27
+
28
+ @Option({displayName: "Component name", description: "The Grid component, `Grid` or `Grid2`.", example: "Grid"})
29
+ componentName!: string;
30
+
31
+ @Option({displayName: "Breakpoints", description: "The breakpoints of the theme, separated by commas.", example: "xs,sm,md,lg,xl", required: false})
32
+ breakpoints?: string;
33
+
34
+ @Option({displayName: "Package name", description: "A package that re-exports Material UI.", example: "@org/ui", required: false})
35
+ packageName?: string;
36
+
37
+ constructor(options?: { componentName?: string; breakpoints?: string; packageName?: string }) {
38
+ super(options);
39
+ }
40
+
41
+ async editor(): Promise<TreeVisitor<any, ExecutionContext>> {
42
+ const component = this.componentName;
43
+ const breakpoints = this.breakpoints ? this.breakpoints.split(",").map(s => s.trim()) : DEFAULT_BREAKPOINTS;
44
+
45
+ return muiTagEditor(component, this.packageName, async (visited, cursor) => {
46
+ if (hasSpread(visited)) return visited;
47
+ const attributes = visited.attributes.map(a => a.element as JSX.Attribute);
48
+ const sizes = attributes.filter(a => breakpoints.includes(attributeName(a) ?? ""));
49
+ const offsets = attributes.filter(a => breakpoints.includes((attributeName(a) ?? "").replace(/Offset$/, "")) && attributeName(a)!.endsWith("Offset"));
50
+ const obsolete = attributes.filter(a => ["item", "zeroMinWidth"].includes(attributeName(a) ?? ""));
51
+ if (!sizes.length && !offsets.length && !obsolete.length) return visited;
52
+
53
+ const created = new Map<JSX.Attribute, JSX.Attribute>();
54
+ for (const [group, name, suffix] of [[sizes, "size", ""], [offsets, "offset", "Offset"]] as const) {
55
+ if (!group.length) continue;
56
+ const entries: [string, J][] = [];
57
+ for (const attribute of group) {
58
+ const value = attribute.value ? attributeExpression(attribute) : undefined;
59
+ const isTrue = value?.kind === J.Kind.Literal && (value as J.Literal).value === true;
60
+ entries.push([attributeName(attribute)!.slice(0, attributeName(attribute)!.length - suffix.length),
61
+ !value || isTrue ? await sourceExpression(cursor, visited, '"grow"') : bare(value)]);
62
+ }
63
+ created.set(group[0], await build(cursor, visited, name, entries));
64
+ }
65
+ const removed = new Set<unknown>([...sizes, ...offsets, ...obsolete]);
66
+ return withAttributes(visited, visited.attributes.flatMap(a => {
67
+ const attribute = a.element as JSX.Attribute;
68
+ const replacement = created.get(attribute);
69
+ if (replacement) return [{...a, element: {...replacement, prefix: attribute.prefix}}];
70
+ return removed.has(attribute) ? [] : [a];
71
+ }));
72
+ });
73
+ }
74
+ }
75
+
76
+ /** `name={value}` for a lone `xs`, and `name={{ xs: a, md: b }}` otherwise. */
77
+ async function build(cursor: Cursor, tag: JSX.Tag, name: string, entries: [string, J][]): Promise<JSX.Attribute> {
78
+ if (entries.length === 1 && entries[0][0] === "xs") return newAttribute(cursor, tag, name, entries[0][1]);
79
+ let object = await newObject(cursor, tag, entries[0][0], entries[0][1]);
80
+ for (const [key, value] of entries.slice(1)) object = appendProperty(object, await newProperty(cursor, tag, key, value));
81
+ return newAttribute(cursor, tag, name, object);
82
+ }
@@ -1,10 +1,32 @@
1
1
  import {and, check, not, or, DataTable, ExecutionContext, foundSearchResult, Recipe, RecipeRef, TreeVisitor} from "@openrewrite/rewrite";
2
2
  import {isObjectLiteral, isVendoredOrBundled, JavaScriptVisitor, JS, usesMethod, usesType} from "@openrewrite/rewrite/javascript";
3
3
  import {isIdentifier, J, Type, TypedTree} from "@openrewrite/rewrite/java";
4
- import {enclosingScope, ENZYME_GUIDE, ENZYME_USAGE_TABLE, enzymeMethod, enzymeModule, enzymeWrapper, EnzymeUsageRow, EventKind, migrationReason, SelectorKind} from "../testing/enzyme-usage";
4
+ import {enclosingScope, ENZYME_GUIDE, ENZYME_USAGE_TABLE, enzymeMethod, enzymeModule, enzymeWrapper, EnzymeUsageRow, EventKind, FILE_BLOCKER, migrationReason, RENDER_BLOCKERS, SELECTOR_VALUES, SelectorKind, unreadSelector} from "../testing/enzyme-usage";
5
5
  import {domSelector, objectProperties, simulatedEvent} from "../testing/enzyme-wrapper-scope";
6
6
 
7
- function eventKind(api: string, [name, mock]: J[]): EventKind | undefined {
7
+ const LENGTH_ACTION = "Use the `length` of the `querySelectorAll` or `getAllBy*` result. For a component selector, count the elements the component renders.";
8
+
9
+ /** What to do with a wrapper that leaves the reads the converter follows, by where it goes. */
10
+ function escapeAction(identifier: J.Identifier, parent: J | undefined): string {
11
+ const callee = parent?.kind === J.Kind.MethodInvocation ? (parent as J.MethodInvocation).name.simpleName :
12
+ parent?.kind === JS.Kind.FunctionCall ? undefined : null;
13
+ if (callee === "expect") {
14
+ return "`expect(wrapper)` asserts on the Enzyme wrapper itself. Assert on what it renders instead, " +
15
+ "such as `expect(container).toMatchSnapshot()` or `expect(container.querySelector(selector)).not.toBeNull()`.";
16
+ }
17
+ if (callee !== null) {
18
+ return "The wrapper is passed to a function. Change the function to take RTL's render result or the element it queries, " +
19
+ "and convert the reads inside it with this test's.";
20
+ }
21
+ if (parent?.kind === J.Kind.Return) return "A function returns the wrapper. Return RTL's render result instead, and convert its callers' reads with it.";
22
+ if (parent?.kind === J.Kind.NamedVariable && (parent as J.VariableDeclarations.NamedVariable).initializer?.element === identifier ||
23
+ parent?.kind === J.Kind.Assignment && (parent as J.Assignment).assignment.element === identifier) {
24
+ return "The wrapper is copied to another name. Read it through one name, so the recipe follows every read of it.";
25
+ }
26
+ return "Trace this wrapper alias, argument, assignment, or return value before migrating its connected scope.";
27
+ }
28
+
29
+ export function eventKind(api: string, [name, mock]: J[]): EventKind | undefined {
8
30
  if (api !== "simulate" || name?.kind !== J.Kind.Literal || typeof (name as J.Literal).value !== "string") return undefined;
9
31
  const event = simulatedEvent((name as J.Literal).value as string);
10
32
  if (!event) return "other";
@@ -17,14 +39,18 @@ function eventKind(api: string, [name, mock]: J[]): EventKind | undefined {
17
39
  return ["mouseEnter", "mouseLeave", "mouseOver", "mouseOut"].includes(event) ? "hover" : "event";
18
40
  }
19
41
 
20
- function selectorKind(api: string, selector: J | undefined): SelectorKind | undefined {
42
+ /** What a selector string matches by. */
43
+ export function stringSelectorKind(value: string): SelectorKind {
44
+ // Enzyme reads a selector starting with a capital as a component's display name
45
+ return /^[A-Z]/.test(value) ? "component" : domSelector(value) ? "dom" : "enzyme";
46
+ }
47
+
48
+ export function selectorKind(api: string, selector: J | undefined): SelectorKind | undefined {
21
49
  if (api === "findWhere" || api === "filterWhere") return "predicate";
22
50
  if (api !== "find" || !selector) return undefined;
23
51
  if (selector.kind === J.Kind.Literal) {
24
52
  const value = (selector as J.Literal).value;
25
- if (typeof value !== "string") return undefined;
26
- // Enzyme reads a selector starting with a capital as a component's display name
27
- return /^[A-Z]/.test(value) ? "component" : domSelector(value) ? "dom" : "enzyme";
53
+ return typeof value === "string" ? stringSelectorKind(value) : undefined;
28
54
  }
29
55
  if (isObjectLiteral(selector)) return "props";
30
56
  const name = isIdentifier(selector) ? selector.simpleName :
@@ -32,6 +58,68 @@ function selectorKind(api: string, selector: J | undefined): SelectorKind | unde
32
58
  return name && /^[A-Z]/.test(name) && TypedTree.getType(selector) !== Type.Primitive.String ? "component" : undefined;
33
59
  }
34
60
 
61
+ const IMPORT_REVIEW = "Review this import and its consumers; unresolved or indirect calls require manual inspection. Keep it while Enzyme consumers remain.";
62
+
63
+ function moduleOf(imp: JS.Import): string | undefined {
64
+ const module = imp.moduleSpecifier?.element;
65
+ return module?.kind === J.Kind.Literal ? (module as J.Literal).value as string : undefined;
66
+ }
67
+
68
+ /** The local names an import binds: its default, named and type-only specifiers, and a namespace. */
69
+ export function importedLocals(imp: JS.Import): string[] {
70
+ const clause = imp.importClause;
71
+ if (!clause) return [];
72
+ const names = clause.name ? [clause.name.element.simpleName] : [];
73
+ const bindings = clause.namedBindings;
74
+ if (bindings?.kind === JS.Kind.Alias && isIdentifier((bindings as JS.Alias).alias)) names.push(((bindings as JS.Alias).alias as J.Identifier).simpleName);
75
+ if (bindings?.kind === JS.Kind.NamedImports) {
76
+ for (const {element: imported} of (bindings as JS.NamedImports).elements.elements) {
77
+ if (imported.kind !== JS.Kind.ImportSpecifier) continue;
78
+ const named = (imported as JS.ImportSpecifier).specifier;
79
+ if (isIdentifier(named)) names.push(named.simpleName);
80
+ else if (named.kind === JS.Kind.Alias && isIdentifier((named as JS.Alias).alias)) names.push(((named as JS.Alias).alias as J.Identifier).simpleName);
81
+ }
82
+ }
83
+ return names;
84
+ }
85
+
86
+ /** How often the source refers to each of `names` outside its imports, leaving out member names such as `wrapper.mount()`. */
87
+ async function referencesTo(cu: JS.CompilationUnit, names: ReadonlySet<string>, ctx: ExecutionContext): Promise<Map<string, number>> {
88
+ const counts = new Map<string, number>();
89
+ if (names.size === 0) return counts;
90
+ await new class extends JavaScriptVisitor<ExecutionContext> {
91
+ override async visitImportDeclaration(imp: JS.Import): Promise<J | undefined> {
92
+ return imp;
93
+ }
94
+
95
+ override async visitIdentifier(identifier: J.Identifier, p: ExecutionContext): Promise<J | undefined> {
96
+ if (names.has(identifier.simpleName)) {
97
+ const parent = this.cursor.parentTree()?.value as J | undefined;
98
+ const member = parent?.kind === J.Kind.FieldAccess && (parent as J.FieldAccess).name.element === identifier ||
99
+ parent?.kind === J.Kind.MethodInvocation && (parent as J.MethodInvocation).name === identifier && (parent as J.MethodInvocation).select !== undefined;
100
+ if (!member) counts.set(identifier.simpleName, (counts.get(identifier.simpleName) ?? 0) + 1);
101
+ }
102
+ return super.visitIdentifier(identifier, p);
103
+ }
104
+ }().visit(cu, ctx);
105
+ return counts;
106
+ }
107
+
108
+ /** What to do with an Enzyme import: which of its names the source still uses, and how often, or that it uses none. */
109
+ export function importAction(locals: readonly string[], references: ReadonlyMap<string, number>): string {
110
+ if (locals.length === 0) return IMPORT_REVIEW;
111
+ const quoted = (names: string[]) => names.map(name => `\`${name}\``).join(", ");
112
+ const used = locals.filter(name => (references.get(name) ?? 0) > 0);
113
+ const unused = locals.filter(name => !used.includes(name));
114
+ if (used.length === 0) return `This file uses none of the names this import binds, ${quoted(unused)}: remove the import.`;
115
+ const kept = used.map(name => {
116
+ const count = references.get(name)!;
117
+ return `\`${name}\` (${count} ${count === 1 ? "use" : "uses"})`;
118
+ }).join(", ");
119
+ const rest = unused.length === 0 ? "" : ` This file doesn't use ${quoted(unused)}, so remove ${unused.length === 1 ? "it" : "them"} from the import.`;
120
+ return `Kept for ${kept} in this file. Remove a name from this import once no use of it remains.${rest}`;
121
+ }
122
+
35
123
  /**
36
124
  * Inventories imports/setup even when types are unavailable; calls and wrapper fields are
37
125
  * attributed to Enzyme by declaring type, not by names such as `find` or `state`.
@@ -59,9 +147,12 @@ export class FindEnzymeUsage extends Recipe {
59
147
  );
60
148
  return check(and(not(isVendoredOrBundled()), relevant), new class extends JavaScriptVisitor<ExecutionContext> {
61
149
  private sourcePath = "";
150
+ private references = new Map<string, number>();
62
151
 
63
152
  override async visitJsCompilationUnit(cu: JS.CompilationUnit, ctx: ExecutionContext): Promise<J | undefined> {
64
153
  this.sourcePath = cu.sourcePath;
154
+ this.references = await referencesTo(cu, new Set(cu.statements.flatMap(({element}) => element.kind === JS.Kind.Import &&
155
+ enzymeModule(moduleOf(element as JS.Import)) ? importedLocals(element as JS.Import) : [])), ctx);
65
156
  return super.visitJsCompilationUnit(cu, ctx);
66
157
  }
67
158
 
@@ -70,8 +161,7 @@ export class FindEnzymeUsage extends Recipe {
70
161
  const moduleName = module?.kind === J.Kind.Literal ? (module as J.Literal).value : undefined;
71
162
  const result = await super.visitImportDeclaration(imp, ctx) as JS.Import;
72
163
  if (!enzymeModule(moduleName)) return result;
73
- return this.report(result, moduleName, "MODULE_REFERENCE",
74
- "Review this import and its consumers; unresolved or indirect calls require manual inspection. Keep it while Enzyme consumers remain.", ctx);
164
+ return this.report(result, moduleName, "MODULE_REFERENCE", importAction(importedLocals(imp), this.references), ctx);
75
165
  }
76
166
 
77
167
  override async visitExportDeclaration(exp: JS.ExportDeclaration, ctx: ExecutionContext): Promise<J | undefined> {
@@ -99,7 +189,15 @@ export class FindEnzymeUsage extends Recipe {
99
189
  }
100
190
  const api = enzymeMethod(method);
101
191
  if (!api) return result;
102
- const {reason, suggestedAction} = migrationReason(api, selectorKind(api, args[0]), eventKind(api, args));
192
+ // The converter, run before this, read the string a selector other than a literal holds, where it could
193
+ const read = api === "find" ? SELECTOR_VALUES.get(this.sourcePath)?.get(method.id) : undefined;
194
+ const selector = read && "value" in read ? stringSelectorKind(read.value) : selectorKind(api, args[0]);
195
+ const migration = migrationReason(api, selector, eventKind(api, args));
196
+ const {reason} = migration;
197
+ const suggestedAction = read && "unread" in read ? unreadSelector(read.unread) : migration.suggestedAction;
198
+ // The converter, run before this, says what kept a render it left on Enzyme
199
+ const blocker = reason === "RENDER_REVIEW" || reason === "SHALLOW_RENDERING" ? (RENDER_BLOCKERS.get(this.sourcePath)?.get(method.id) ?? RENDER_BLOCKERS.get(this.sourcePath)?.get(FILE_BLOCKER)) : undefined;
200
+ if (blocker) return this.report(result, api, reason, blocker, ctx);
103
201
  return this.report(result, api, reason, suggestedAction, ctx);
104
202
  }
105
203
 
@@ -119,7 +217,7 @@ export class FindEnzymeUsage extends Recipe {
119
217
  const result = await super.visitFieldAccess(field, ctx) as J.FieldAccess;
120
218
  if (!enzymeWrapper(TypedTree.getType(field.target))) return result;
121
219
  const api = field.name.element.simpleName;
122
- return this.report(result, api, "WRAPPER_FIELD",
220
+ return this.report(result, api, "WRAPPER_FIELD", api === "length" ? LENGTH_ACTION :
123
221
  "Preserve assertion cardinality and wrapper semantics when replacing this Enzyme field.", ctx);
124
222
  }
125
223
 
@@ -132,8 +230,7 @@ export class FindEnzymeUsage extends Recipe {
132
230
  if (parent?.kind === J.Kind.MethodInvocation &&
133
231
  (parent as J.MethodInvocation).select?.element === identifier) return result;
134
232
  if (parent?.kind === J.Kind.FieldAccess && (parent as J.FieldAccess).target === identifier) return result;
135
- return this.report(result, identifier.simpleName, "WRAPPER_ESCAPE",
136
- "Trace this wrapper alias, argument, assignment, or return value before migrating its connected scope.", ctx);
233
+ return this.report(result, identifier.simpleName, "WRAPPER_ESCAPE", escapeAction(identifier, parent), ctx);
137
234
  }
138
235
 
139
236
  private report<T extends J>(node: T, api: string, reason: string, suggestedAction: string, ctx: ExecutionContext): T {