@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,42 @@
1
+ import {ExecutionContext, Recipe, TreeVisitor, check, not} from "@openrewrite/rewrite";
2
+ import {JavaScriptVisitor, JS, isVendoredOrBundled} from "@openrewrite/rewrite/javascript";
3
+ import {isIdentifier, J} from "@openrewrite/rewrite/java";
4
+ import {isObjectLiteral, objectProperties, propertyKey} from "./jsx-attributes";
5
+ import {endsWithName} from "./theme-access";
6
+
7
+ /**
8
+ * Renames the palette's `type` to `mode`, in a theme's `palette` object and in reads of `theme.palette.type`.
9
+ *
10
+ * Before: `createTheme({ palette: { type: 'dark' } })`, `theme.palette.type === 'dark'`
11
+ * After: `createTheme({ palette: { mode: 'dark' } })`, `theme.palette.mode === 'dark'`
12
+ *
13
+ * @see https://mui.com/material-ui/migration/v5-component-changes/#theme
14
+ */
15
+ export class RenameThemePaletteType extends Recipe {
16
+ readonly name = "org.openrewrite.react.mui.rename-theme-palette-type";
17
+ readonly displayName: string = "Rename the theme palette `type` to `mode`";
18
+ readonly description: string = "Renames `type` to `mode` in the `palette` object of a theme and in `palette.type` reads.";
19
+
20
+ async editor(): Promise<TreeVisitor<any, ExecutionContext>> {
21
+ return check(not(isVendoredOrBundled()), new class extends JavaScriptVisitor<ExecutionContext> {
22
+ override async visitFieldAccess(fieldAccess: J.FieldAccess, ctx: ExecutionContext): Promise<J | undefined> {
23
+ const visited = await super.visitFieldAccess(fieldAccess, ctx) as J.FieldAccess;
24
+ return visited.name.element.simpleName === "type" && endsWithName(visited.target, "palette")
25
+ ? {...visited, name: {...visited.name, element: {...visited.name.element, simpleName: "mode"}}} as J.FieldAccess : visited;
26
+ }
27
+
28
+ override async visitPropertyAssignment(property: JS.PropertyAssignment, ctx: ExecutionContext): Promise<J | undefined> {
29
+ const visited = await super.visitPropertyAssignment(property, ctx) as JS.PropertyAssignment;
30
+ const object = visited.initializer;
31
+ if (propertyKey(visited) !== "palette" || !isObjectLiteral(object)) return visited;
32
+ const statements = objectProperties(object).map(s => {
33
+ const name = s.element.kind === JS.Kind.PropertyAssignment && s.element.name.element;
34
+ return name && isIdentifier(name) && name.simpleName === "type"
35
+ ? {...s, element: {...s.element, name: {...s.element.name, element: {...name, simpleName: "mode"}}}} : s;
36
+ });
37
+ return statements.every((s, i) => s === objectProperties(object)[i]) ? visited
38
+ : {...visited, initializer: {...object, body: {...object.body!, statements}}} as JS.PropertyAssignment;
39
+ }
40
+ }());
41
+ }
42
+ }
@@ -0,0 +1,42 @@
1
+ import {ExecutionContext, TreeVisitor} from "@openrewrite/rewrite";
2
+ import {JS, JSX} from "@openrewrite/rewrite/javascript";
3
+ import {PreV5Recipe} from "./pre-v5-recipe";
4
+ import {J} from "@openrewrite/rewrite/java";
5
+ import {attributeExpression, findAttribute, newAttribute, replaceAttribute} from "./jsx-attributes";
6
+ import {attributeString} from "./mui-components";
7
+ import {muiTagEditor} from "./mui-tag-visitor";
8
+
9
+ /**
10
+ * Updates the `borderRadius` of `Box` for the sizing of v5, where a number is a multiple of the theme's radius.
11
+ *
12
+ * Before: `<Box borderRadius="borderRadius" />`, `<Box borderRadius={16} />`
13
+ * After: `<Box borderRadius={1} />`, `<Box borderRadius="16px" />`
14
+ *
15
+ * @see https://mui.com/material-ui/migration/v5-component-changes/#box
16
+ */
17
+ export class ReplaceBoxBorderRadius extends PreV5Recipe {
18
+ readonly name = "org.openrewrite.react.mui.replace-box-border-radius";
19
+ readonly displayName: string = "Update the Box `borderRadius` values";
20
+ readonly description: string = "Replaces `borderRadius=\"borderRadius\"` with `borderRadius={1}`, and a number `borderRadius={16}` with the pixel string `\"16px\"`.";
21
+
22
+ /** The props this recipe wrote, which the instance keeps across cycles, since a second cycle would read `{1}` as 1px. */
23
+ private readonly written = new Set<string>();
24
+
25
+ protected async v4Editor(): Promise<TreeVisitor<any, ExecutionContext>> {
26
+ const written = this.written;
27
+ return muiTagEditor("Box", undefined, async (tag, cursor) => {
28
+ const attribute = findAttribute(tag, "borderRadius");
29
+ if (!attribute?.value || written.has(attribute.id)) return tag;
30
+ if (attributeString(attribute) === "borderRadius" && attribute.value.element.kind === J.Kind.Literal) {
31
+ const created = await newAttribute(cursor, tag, "borderRadius", "{1}");
32
+ written.add(created.id);
33
+ return replaceAttribute(tag, attribute, created);
34
+ }
35
+ const value = attribute.value.element.kind === JS.Kind.JsxEmbeddedExpression ? attributeExpression(attribute) : undefined;
36
+ if (value?.kind !== J.Kind.Literal || typeof (value as J.Literal).value !== "number") return tag;
37
+ const created = await newAttribute(cursor, tag, "borderRadius", `"${(value as J.Literal).value}px"`);
38
+ written.add(created.id);
39
+ return replaceAttribute(tag, attribute, created);
40
+ });
41
+ }
42
+ }
@@ -0,0 +1,28 @@
1
+ import {ExecutionContext, Recipe, TreeVisitor, check, not} from "@openrewrite/rewrite";
2
+ import {JavaScriptVisitor, isVendoredOrBundled, raw, template} from "@openrewrite/rewrite/javascript";
3
+ import {J} from "@openrewrite/rewrite/java";
4
+ import {endsWithName, stringValue} from "./theme-access";
5
+
6
+ /**
7
+ * Replaces `theme.breakpoints.width('md')` with `theme.breakpoints.values.md`.
8
+ *
9
+ * @see https://mui.com/material-ui/migration/v5-component-changes/#theme
10
+ */
11
+ export class ReplaceBreakpointsWidth extends Recipe {
12
+ readonly name = "org.openrewrite.react.mui.replace-breakpoints-width";
13
+ readonly displayName: string = "Replace `theme.breakpoints.width()` with `theme.breakpoints.values`";
14
+ readonly description: string = "Replaces `theme.breakpoints.width('md')` with `theme.breakpoints.values.md`, for a breakpoint written as a string literal.";
15
+
16
+ async editor(): Promise<TreeVisitor<any, ExecutionContext>> {
17
+ return check(not(isVendoredOrBundled()), new class extends JavaScriptVisitor<ExecutionContext> {
18
+ override async visitMethodInvocation(invocation: J.MethodInvocation, ctx: ExecutionContext): Promise<J | undefined> {
19
+ const visited = await super.visitMethodInvocation(invocation, ctx) as J.MethodInvocation;
20
+ const select = visited.select?.element;
21
+ const key = visited.arguments.elements.length === 1 ? stringValue(visited.arguments.elements[0].element) : undefined;
22
+ if (visited.name.simpleName !== "width" || !key || !/^[A-Za-z_$][\w$]*$/.test(key) || !endsWithName(select, "breakpoints")) return visited;
23
+ const replaced = await template`${select!}.values.${raw(key)}`.apply(visited, this.cursor);
24
+ return replaced ? {...replaced, prefix: visited.prefix} as J : visited;
25
+ }
26
+ }());
27
+ }
28
+ }
@@ -0,0 +1,97 @@
1
+ import {ExecutionContext, Option, Recipe, TreeVisitor, check, not} from "@openrewrite/rewrite";
2
+ import {JavaScriptVisitor, JS, JSX, isVendoredOrBundled} from "@openrewrite/rewrite/javascript";
3
+ import {isIdentifier, J} from "@openrewrite/rewrite/java";
4
+ import {attributeObject, findAttribute, objectProperties, propertyKey} from "./jsx-attributes";
5
+ import {escapeRegExp, muiComponents} from "./mui-components";
6
+ import {packagePattern} from "./mui-tag-visitor";
7
+
8
+ const lowerFirst = (s: string) => s.charAt(0).toLowerCase() + s.slice(1);
9
+
10
+ /**
11
+ * Replaces a class key of a Material UI component: the key of its `classes` prop, a `xClasses.key` member, and
12
+ * `.MuiComponent-key` in a style selector.
13
+ *
14
+ * Before: `<Avatar classes={{ circle: c }} />`, `avatarClasses.circle`, `'& .MuiAvatar-circle'`
15
+ * After: `<Avatar classes={{ circular: c }} />`, `avatarClasses.circular`, `'& .MuiAvatar-circular'`
16
+ *
17
+ * With several `newClasses`, a style selector and the `classes` prop's key cannot name them all in one place, so
18
+ * only a selector string is rewritten, to `.MuiButton-text.MuiButton-colorInherit`; `classes` keys and members
19
+ * stay as they are.
20
+ *
21
+ * @see https://mui.com/material-ui/migration/migrating-from-deprecated-apis/
22
+ */
23
+ export class ReplaceComponentClass extends Recipe {
24
+ readonly name = "org.openrewrite.react.mui.replace-component-class";
25
+ readonly displayName: string = "Replace a Material UI component class";
26
+ readonly description: string = "Replaces a class of a Material UI component in its `classes` prop, in `xClasses` members and in `.MuiComponent-class` selectors. With several new classes only selectors are rewritten.";
27
+
28
+ @Option({displayName: "Component name", description: "The Material UI component.", example: "Avatar"})
29
+ componentName!: string;
30
+
31
+ @Option({displayName: "Old class", description: "The class key that is replaced.", example: "circle"})
32
+ oldClass!: string;
33
+
34
+ @Option({displayName: "New classes", description: "The class key that replaces it, or several separated by commas that a selector gets one after the other.", example: "circular"})
35
+ newClasses!: string;
36
+
37
+ @Option({displayName: "Package name", description: "A package that re-exports Material UI.", example: "@org/ui", required: false})
38
+ packageName?: string;
39
+
40
+ constructor(options?: { componentName?: string; oldClass?: string; newClasses?: string; packageName?: string }) {
41
+ super(options);
42
+ }
43
+
44
+ async editor(): Promise<TreeVisitor<any, ExecutionContext>> {
45
+ const {componentName, oldClass} = this;
46
+ const news = this.newClasses.split(",").map(s => s.trim());
47
+ const single = news.length === 1 ? news[0] : undefined;
48
+ const pattern = packagePattern(this.packageName);
49
+ const selector = new RegExp(`\\.Mui${componentName}-${escapeRegExp(oldClass)}(?![A-Za-z0-9_-])`, "g");
50
+ const replacement = news.map(n => `.Mui${componentName}-${n}`).join("");
51
+ const classesName = `${lowerFirst(componentName)}Classes`;
52
+
53
+ return check(not(isVendoredOrBundled()), new class extends JavaScriptVisitor<ExecutionContext> {
54
+ private components = new Map<string, string>();
55
+ private classesImported = false;
56
+
57
+ override async visitJsCompilationUnit(cu: JS.CompilationUnit, ctx: ExecutionContext): Promise<J | undefined> {
58
+ this.components = muiComponents(cu, pattern);
59
+ this.classesImported = [...this.components.entries()].some(([local, name]) => local === classesName && name === classesName);
60
+ return super.visitJsCompilationUnit(cu, ctx);
61
+ }
62
+
63
+ override async visitJsxTag(tag: JSX.Tag, ctx: ExecutionContext): Promise<J | undefined> {
64
+ const visited = await super.visitJsxTag(tag, ctx) as JSX.Tag;
65
+ const name = visited.openName.element;
66
+ if (!single || !isIdentifier(name) || this.components.get(name.simpleName) !== componentName) return visited;
67
+ const classes = findAttribute(visited, "classes");
68
+ const object = classes && attributeObject(classes);
69
+ if (!classes || !object) return visited;
70
+ const statements = objectProperties(object).map(s => {
71
+ const key = s.element.kind === JS.Kind.PropertyAssignment && s.element.name.element;
72
+ return key && isIdentifier(key) && propertyKey(s.element) === oldClass
73
+ ? {...s, element: {...s.element, name: {...s.element.name, element: {...key, simpleName: single}}}} : s;
74
+ });
75
+ if (statements.every((s, i) => s === objectProperties(object)[i])) return visited;
76
+ const embedded = classes.value!.element as JSX.EmbeddedExpression;
77
+ const updated = {...classes, value: {...classes.value!, element: {...embedded, expression: {...embedded.expression, element: {...object, body: {...object.body!, statements}}}}}};
78
+ return {...visited, attributes: visited.attributes.map(a => a.element === classes ? {...a, element: updated} : a)} as JSX.Tag;
79
+ }
80
+
81
+ override async visitFieldAccess(fieldAccess: J.FieldAccess, ctx: ExecutionContext): Promise<J | undefined> {
82
+ const visited = await super.visitFieldAccess(fieldAccess, ctx) as J.FieldAccess;
83
+ const target = visited.target;
84
+ if (!single || !this.classesImported || !isIdentifier(target) || target.simpleName !== classesName || visited.name.element.simpleName !== oldClass) return visited;
85
+ return {...visited, name: {...visited.name, element: {...visited.name.element, simpleName: single}}} as J.FieldAccess;
86
+ }
87
+
88
+ override async visitLiteral(literal: J.Literal, ctx: ExecutionContext): Promise<J | undefined> {
89
+ const visited = await super.visitLiteral(literal, ctx) as J.Literal;
90
+ if (typeof visited.value !== "string" || !visited.valueSource) return visited;
91
+ selector.lastIndex = 0;
92
+ if (!selector.test(visited.value)) return visited;
93
+ return {...visited, value: visited.value.replace(selector, replacement), valueSource: visited.valueSource.replace(selector, replacement)} as J.Literal;
94
+ }
95
+ }());
96
+ }
97
+ }
@@ -0,0 +1,61 @@
1
+ import {ExecutionContext, Option, Recipe, TreeVisitor} from "@openrewrite/rewrite";
2
+ import {JS, JSX} from "@openrewrite/rewrite/javascript";
3
+ import {isIdentifier, J} from "@openrewrite/rewrite/java";
4
+ import {attributeObject, findAttribute, objectProperties, propertyKey} from "./jsx-attributes";
5
+ import {muiTagEditor} from "./mui-tag-visitor";
6
+
7
+ const lowerFirst = (s: string) => s.charAt(0).toLowerCase() + s.slice(1);
8
+
9
+ /**
10
+ * Renames the `components` and `componentsProps` props of a component to `slots` and `slotProps`, and the keys of
11
+ * `components` to camelCase.
12
+ *
13
+ * Before: `<Alert components={{ CloseButton: B }} componentsProps={{ closeButton: p }} />`
14
+ * After: `<Alert slots={{ closeButton: B }} slotProps={{ closeButton: p }} />`
15
+ *
16
+ * A tag that already sets `slots` or `slotProps` keeps the old prop, since merging two objects is not safe to guess.
17
+ *
18
+ * @see https://mui.com/material-ui/migration/migrating-from-deprecated-apis/
19
+ */
20
+ export class ReplaceComponentsWithSlots extends Recipe {
21
+ readonly name = "org.openrewrite.react.mui.replace-components-with-slots";
22
+ readonly displayName: string = "Replace `components` and `componentsProps` with slots";
23
+ readonly description: string = "Renames the deprecated `components` and `componentsProps` props of a Material UI component to `slots` and `slotProps`, with camelCase slot names. Tags that already set the new prop are left alone.";
24
+
25
+ @Option({displayName: "Component name", description: "The Material UI component.", example: "Alert"})
26
+ componentName!: string;
27
+
28
+ @Option({displayName: "Package name", description: "A package that re-exports Material UI.", example: "@org/ui", required: false})
29
+ packageName?: string;
30
+
31
+ constructor(options?: { componentName?: string; packageName?: string }) {
32
+ super(options);
33
+ }
34
+
35
+ async editor(): Promise<TreeVisitor<any, ExecutionContext>> {
36
+ return muiTagEditor(this.componentName, this.packageName, async tag => {
37
+ let result = tag;
38
+ for (const [from, to, camel] of [["components", "slots", true], ["componentsProps", "slotProps", false]] as const) {
39
+ const attribute = findAttribute(result, from);
40
+ if (!attribute || findAttribute(result, to)) continue;
41
+ const object = attributeObject(attribute);
42
+ // Keys can only be renamed in an object literal; anything else is passed through as it is.
43
+ if (camel && !object) continue;
44
+ const key = {...(attribute.key as J.Identifier), simpleName: to};
45
+ let value = attribute.value;
46
+ if (camel && object) {
47
+ const statements = objectProperties(object).map(s => {
48
+ const name = s.element.kind === JS.Kind.PropertyAssignment && s.element.name.element;
49
+ return name && isIdentifier(name) && propertyKey(s.element)
50
+ ? {...s, element: {...s.element, name: {...s.element.name, element: {...name, simpleName: lowerFirst(name.simpleName)}}}} : s;
51
+ });
52
+ const embedded = value!.element as JSX.EmbeddedExpression;
53
+ value = {...value!, element: {...embedded, expression: {...embedded.expression, element: {...object, body: {...object.body!, statements}}}}} as typeof value;
54
+ }
55
+ const renamed = {...attribute, key, value} as JSX.Attribute;
56
+ result = {...result, attributes: result.attributes.map(a => a.element === attribute ? {...a, element: renamed} : a)} as JSX.Tag;
57
+ }
58
+ return result;
59
+ });
60
+ }
61
+ }
@@ -0,0 +1,100 @@
1
+ import {ExecutionContext, Recipe, TreeVisitor} from "@openrewrite/rewrite";
2
+ import {literalEditor} from "./replace-in-string-literals";
3
+
4
+ const capitalize = (s: string): string => s ? s[0].toUpperCase() + s.slice(1) : s;
5
+
6
+ /**
7
+ * Points the `material-ui/svg-icons/<set>/<icon-name>` imports of v0.x at `@material-ui/icons/<IconName>`.
8
+ *
9
+ * Before: `import AccessAlarm from 'material-ui/svg-icons/device/access-alarm';`
10
+ * After: `import AccessAlarm from '@material-ui/icons/AccessAlarm';`
11
+ *
12
+ * @see https://github.com/mui/material-ui/tree/master/packages/mui-codemod/src/v1.0.0/svg-icon-imports
13
+ */
14
+ export class ReplaceSvgIconImports extends Recipe {
15
+ readonly name = "org.openrewrite.react.mui.replace-svg-icon-imports";
16
+ readonly displayName: string = "Replace `material-ui/svg-icons` imports with `@material-ui/icons`";
17
+ readonly description: string = "Rewrites the string `material-ui/svg-icons/<set>/<icon-name>` to `@material-ui/icons/<IconName>` wherever it appears, in imports and `require()` calls alike.";
18
+
19
+ async editor(): Promise<TreeVisitor<any, ExecutionContext>> {
20
+ return literalEditor(text => text.replace(/material-ui\/svg-icons\/[^/'"`]+\/([^/'"`]+)/g, (_, icon: string) => `@material-ui/icons/${icon.split("-").map(capitalize).join("")}`));
21
+ }
22
+ }
23
+
24
+ const PATH_CONVERSION: Record<string, string> = {
25
+ "app-bar": "AppBar",
26
+ "auto-complete": "AutoComplete",
27
+ avatar: "Avatar",
28
+ badge: "Badge",
29
+ "flat-button": "FlatButton",
30
+ "raised-button": "RaisedButton",
31
+ "floating-action-button": "FloatingActionButton",
32
+ "icon-button": "IconButton",
33
+ "card/card": "Card/Card",
34
+ "card/card-actions": "Card/CardActions",
35
+ "card/card-header": "Card/CardHeader",
36
+ "card/card-media": "Card/CardMedia",
37
+ "card/card-title": "Card/CardTitle",
38
+ "card/card-text": "Card/CardText",
39
+ "date-picker/date-picker": "DatePicker",
40
+ dialog: "Dialog",
41
+ divider: "Divider",
42
+ "grid-list/grid-list": "GridList/GridList",
43
+ "grid-list/grid-tile": "GridList/GridTile",
44
+ "font-icon": "FontIcon",
45
+ "svg-icon": "SvgIcon",
46
+ "left-nav": "Drawer",
47
+ "lists/list": "List/List",
48
+ "lists/list-item": "List/ListItem",
49
+ "menus/menu": "Menu",
50
+ "menus/menu-item": "MenuItem",
51
+ "menus/icon-menu": "IconMenu",
52
+ paper: "Paper",
53
+ "popover/popover": "Popover",
54
+ "circular-progress": "CircularProgress",
55
+ "linear-progress": "LinearProgress",
56
+ "refresh-indicator": "RefreshIndicator",
57
+ "select-field": "SelectField",
58
+ slider: "Slider",
59
+ checkbox: "Checkbox",
60
+ "radio-button": "RadioButton",
61
+ "radio-button-group": "RadioButton/RadioButtonGroup",
62
+ toggle: "Toggle",
63
+ snackbar: "Snackbar",
64
+ "table/table": "Table/Table",
65
+ "table/table-header-column": "Table/TableHeaderColumn",
66
+ "table/table-row": "Table/TableRow",
67
+ "table/table-header": "Table/TableHeader",
68
+ "table/table-row-column": "Table/TableRowColumn",
69
+ "table/table-body": "Table/TableBody",
70
+ "table/table-footer": "Table/TableFooter",
71
+ "tabs/tab": "Tabs/Tab",
72
+ "tabs/tabs": "Tabs/Tabs",
73
+ "text-field": "TextField",
74
+ "time-picker/time-picker": "TimePicker",
75
+ "toolbar/toolbar": "Toolbar/Toolbar",
76
+ "toolbar/toolbar-group": "Toolbar/ToolbarGroup",
77
+ "toolbar/toolbar-separator": "Toolbar/ToolbarSeparator",
78
+ "toolbar/toolbar-title": "Toolbar/ToolbarTitle",
79
+ MuiThemeProvider: "styles/MuiThemeProvider",
80
+ };
81
+
82
+ /**
83
+ * Moves the `material-ui/lib/*` and `material-ui/src/*` imports of v0.15 to the module paths of v0.15's later
84
+ * layout, such as `material-ui/lib/app-bar` to `material-ui/AppBar`.
85
+ *
86
+ * Before: `import AppBar from 'material-ui/lib/app-bar';`
87
+ * After: `import AppBar from 'material-ui/AppBar';`
88
+ *
89
+ * @see https://github.com/mui/material-ui/tree/master/packages/mui-codemod/src/v0.15.0/import-path
90
+ */
91
+ export class ReplaceMaterialUiImportPaths extends Recipe {
92
+ readonly name = "org.openrewrite.react.mui.replace-material-ui-import-paths";
93
+ readonly displayName: string = "Update the `material-ui` import paths to v0.15's";
94
+ readonly description: string = "Rewrites `material-ui/lib/<kebab-name>` and `material-ui/src/<kebab-name>` module paths to the PascalCase paths of v0.15, using the same table as the MUI codemod.";
95
+
96
+ async editor(): Promise<TreeVisitor<any, ExecutionContext>> {
97
+ return literalEditor(text => text.replace(/^(['"`]?)material-ui\/(src|lib)\/([^'"`]+)\1$/, (_, quote: string, base: string, suffix: string) =>
98
+ `${quote}material-ui/${base === "src" ? "src/" : ""}${PATH_CONVERSION[suffix] ?? suffix}${quote}`));
99
+ }
100
+ }
@@ -0,0 +1,44 @@
1
+ import {ExecutionContext, Option, Recipe, TreeVisitor, check, not} from "@openrewrite/rewrite";
2
+ import {JavaScriptVisitor, isVendoredOrBundled} from "@openrewrite/rewrite/javascript";
3
+ import {J} from "@openrewrite/rewrite/java";
4
+
5
+ /** An editor that rewrites the text of every string and template literal with `change`. */
6
+ export async function literalEditor(change: (text: string) => string): Promise<TreeVisitor<any, ExecutionContext>> {
7
+ return check(not(isVendoredOrBundled()), new class extends JavaScriptVisitor<ExecutionContext> {
8
+ override async visitLiteral(literal: J.Literal, ctx: ExecutionContext): Promise<J | undefined> {
9
+ const visited = await super.visitLiteral(literal, ctx) as J.Literal;
10
+ if (typeof visited.value !== "string" || !visited.valueSource) return visited;
11
+ const source = change(visited.valueSource);
12
+ return source === visited.valueSource ? visited : {...visited, value: change(visited.value), valueSource: source} as J.Literal;
13
+ }
14
+ }());
15
+ }
16
+
17
+ /**
18
+ * Replaces text in the string literals of a file with a regular expression, for names that live in strings, such as
19
+ * style selectors.
20
+ *
21
+ * Before: `'& .JoyButton-root'`
22
+ * After: `'& .MuiButton-root'`
23
+ */
24
+ export class ReplaceInStringLiterals extends Recipe {
25
+ readonly name = "org.openrewrite.react.mui.replace-in-string-literals";
26
+ readonly displayName: string = "Replace text in string literals";
27
+ readonly description: string = "Replaces every match of a regular expression in the string and template literals of JavaScript and TypeScript files.";
28
+
29
+ @Option({displayName: "Pattern", description: "The regular expression, applied globally.", example: "Joy([A-Z]+)"})
30
+ pattern!: string;
31
+
32
+ @Option({displayName: "Replacement", description: "The replacement, which can use `$1` and so on.", example: "Mui$1"})
33
+ replacement!: string;
34
+
35
+ constructor(options?: { pattern?: string; replacement?: string }) {
36
+ super(options);
37
+ }
38
+
39
+ async editor(): Promise<TreeVisitor<any, ExecutionContext>> {
40
+ const regex = new RegExp(this.pattern, "g");
41
+ const {replacement} = this;
42
+ return literalEditor(text => text.replace(regex, replacement));
43
+ }
44
+ }
@@ -0,0 +1,59 @@
1
+ import {ExecutionContext, Option, Recipe, TreeVisitor, check, not} from "@openrewrite/rewrite";
2
+ import {JavaScriptVisitor, JS, JSX, isVendoredOrBundled, maybeAddImport, maybeRemoveImport} from "@openrewrite/rewrite/javascript";
3
+ import {J} from "@openrewrite/rewrite/java";
4
+ import {importBindings} from "../util/module-imports";
5
+ import {findAttribute, renameTag, replaceAttribute} from "./jsx-attributes";
6
+ import {muiComponents, tagComponent} from "./mui-components";
7
+ import {packagePattern} from "./mui-tag-visitor";
8
+
9
+ /**
10
+ * Replaces `<ListItem button>` with `<ListItemButton>`, since v6 removes the `button` prop of `ListItem`.
11
+ *
12
+ * Before: `<ListItem button onClick={go}>x</ListItem>`
13
+ * After: `<ListItemButton onClick={go}>x</ListItemButton>`
14
+ *
15
+ * The import of `ListItemButton` is added next to the one of `ListItem`, which goes when nothing else uses it.
16
+ *
17
+ * @see https://mui.com/material-ui/migration/upgrade-to-v6/#listitem
18
+ */
19
+ export class ReplaceListItemButtonProp extends Recipe {
20
+ readonly name = "org.openrewrite.react.mui.replace-list-item-button-prop";
21
+ readonly displayName: string = "Replace `ListItem button` with `ListItemButton`";
22
+ readonly description: string = "Replaces a `ListItem` with the `button` prop by a `ListItemButton` without it, and adds the import.";
23
+
24
+ @Option({displayName: "Package name", description: "The package the `ListItem` is imported from, when it is not `@mui/material`.", example: "@mui/material", required: false})
25
+ packageName?: string;
26
+
27
+ constructor(options?: { packageName?: string }) {
28
+ super(options);
29
+ }
30
+
31
+ async editor(): Promise<TreeVisitor<any, ExecutionContext>> {
32
+ const pattern = packagePattern(this.packageName);
33
+ return check(not(isVendoredOrBundled()), new class extends JavaScriptVisitor<ExecutionContext> {
34
+ private components = new Map<string, string>();
35
+ private module = "@mui/material";
36
+
37
+ override async visitJsCompilationUnit(cu: JS.CompilationUnit, ctx: ExecutionContext): Promise<J | undefined> {
38
+ this.components = muiComponents(cu, pattern);
39
+ const binding = importBindings(cu).find(b => this.components.get(b.local) === "ListItem" && pattern.test(b.module));
40
+ // `@mui/material/ListItem` is imported from the package root for the new component.
41
+ this.module = binding ? binding.module.replace(/\/ListItem$/, "") : "@mui/material";
42
+ return super.visitJsCompilationUnit(cu, ctx);
43
+ }
44
+
45
+ override async visitJsxTag(tag: JSX.Tag, ctx: ExecutionContext): Promise<J | undefined> {
46
+ const visited = await super.visitJsxTag(tag, ctx) as JSX.Tag;
47
+ if (tagComponent(visited, this.components) !== "ListItem") return visited;
48
+ const button = findAttribute(visited, "button");
49
+ const value = button?.value?.element;
50
+ const isOn = button && (!value || (value.kind === JS.Kind.JsxEmbeddedExpression
51
+ && ((value as JSX.EmbeddedExpression).expression.element as J.Literal).value === true));
52
+ if (!button || !isOn) return visited;
53
+ const local = maybeAddImport(this, {module: this.module, member: "ListItemButton"}) ?? "ListItemButton";
54
+ maybeRemoveImport(this, this.module, "ListItem");
55
+ return renameTag(replaceAttribute(visited, button, undefined), local);
56
+ }
57
+ }());
58
+ }
59
+ }
@@ -0,0 +1,31 @@
1
+ import {ExecutionContext, Recipe, TreeVisitor} from "@openrewrite/rewrite";
2
+ import {JSX} from "@openrewrite/rewrite/javascript";
3
+ import {appendAttribute, findAttribute, newAttribute, replaceAttribute} from "./jsx-attributes";
4
+ import {attributeString} from "./mui-components";
5
+ import {muiTagEditor} from "./mui-tag-visitor";
6
+
7
+ /**
8
+ * Replaces the string values of the `scrollButtons` prop of `Tabs` with the booleans of v5.
9
+ *
10
+ * Before: `<Tabs scrollButtons="on" />`, `<Tabs scrollButtons="desktop" />`, `<Tabs scrollButtons="off" />`
11
+ * After: `<Tabs scrollButtons allowScrollButtonsMobile />`, `<Tabs scrollButtons />`, `<Tabs scrollButtons={false} />`
12
+ *
13
+ * @see https://mui.com/material-ui/migration/v5-component-changes/#tabs
14
+ */
15
+ export class ReplaceTabsScrollButtons extends Recipe {
16
+ readonly name = "org.openrewrite.react.mui.replace-tabs-scroll-buttons";
17
+ readonly displayName: string = "Replace the string values of `Tabs` `scrollButtons`";
18
+ readonly description: string = "Replaces `scrollButtons=\"on\"` and `\"desktop\"` with a bare `scrollButtons`, adding `allowScrollButtonsMobile` for `on`, and `\"off\"` with `scrollButtons={false}`.";
19
+
20
+ async editor(): Promise<TreeVisitor<any, ExecutionContext>> {
21
+ return muiTagEditor("Tabs", undefined, async (tag, cursor) => {
22
+ const attribute = findAttribute(tag, "scrollButtons");
23
+ const value = attribute && attributeString(attribute);
24
+ if (!attribute || !["on", "desktop", "off"].includes(value ?? "")) return tag;
25
+ const bare = {...attribute, value: undefined} as JSX.Attribute;
26
+ if (value === "off") return replaceAttribute(tag, attribute, await newAttribute(cursor, tag, "scrollButtons", "{false}"));
27
+ const result = replaceAttribute(tag, attribute, bare);
28
+ return value === "on" ? appendAttribute(result, {...await newAttribute(cursor, tag, "allowScrollButtonsMobile", "{true}"), value: undefined} as JSX.Attribute) : result;
29
+ });
30
+ }
31
+ }
@@ -0,0 +1,55 @@
1
+ import {ExecutionContext, Recipe, TreeVisitor, check, not} from "@openrewrite/rewrite";
2
+ import {JavaScriptVisitor, JS, isVendoredOrBundled, maybeRemoveImport, raw, template} from "@openrewrite/rewrite/javascript";
3
+ import {isIdentifier, J} from "@openrewrite/rewrite/java";
4
+ import {importBindings} from "../util/module-imports";
5
+
6
+ const FUNCTIONS = new Set(["alpha", "lighten", "darken"]);
7
+ const MODULES = /^@mui\/(system|material|system\/colorManipulator|material\/styles)$/;
8
+
9
+ const rootOf = (e: J): string | undefined => {
10
+ let current: J = e;
11
+ while (current.kind === J.Kind.FieldAccess) current = (current as J.FieldAccess).target;
12
+ return isIdentifier(current) ? current.simpleName : undefined;
13
+ };
14
+
15
+ /**
16
+ * Replaces the color functions `alpha()`, `lighten()` and `darken()` with the ones on the theme, which understand
17
+ * CSS variables.
18
+ *
19
+ * Before: `alpha(theme.palette.primary.main, 0.8)`
20
+ * After: `theme.alpha(theme.palette.primary.main, 0.8)`
21
+ *
22
+ * Only a call whose color is read from a variable called `theme` is rewritten, since the call has to be made on
23
+ * a theme that is in scope. The import goes when nothing else uses the function.
24
+ *
25
+ * @see https://mui.com/material-ui/migration/upgrade-to-native-color/
26
+ */
27
+ export class ReplaceThemeColorFunctions extends Recipe {
28
+ readonly name = "org.openrewrite.react.mui.replace-theme-color-functions";
29
+ readonly displayName: string = "Replace `alpha()`, `lighten()` and `darken()` with the theme's";
30
+ readonly description: string = "Replaces calls of `alpha()`, `lighten()` and `darken()` whose color is read from `theme` with `theme.alpha()`, `theme.lighten()` and `theme.darken()`.";
31
+
32
+ async editor(): Promise<TreeVisitor<any, ExecutionContext>> {
33
+ return check(not(isVendoredOrBundled()), new class extends JavaScriptVisitor<ExecutionContext> {
34
+ private functions = new Map<string, {name: string; module: string}>();
35
+
36
+ override async visitJsCompilationUnit(cu: JS.CompilationUnit, ctx: ExecutionContext): Promise<J | undefined> {
37
+ this.functions = new Map(importBindings(cu).filter(b => MODULES.test(b.module) && FUNCTIONS.has(b.name)).map(b => [b.local, {name: b.name, module: b.module}]));
38
+ return this.functions.size ? super.visitJsCompilationUnit(cu, ctx) : cu;
39
+ }
40
+
41
+ override async visitMethodInvocation(invocation: J.MethodInvocation, ctx: ExecutionContext): Promise<J | undefined> {
42
+ const visited = await super.visitMethodInvocation(invocation, ctx) as J.MethodInvocation;
43
+ const fn = this.functions.get(visited.name.simpleName);
44
+ const [color, ...rest] = visited.arguments.elements;
45
+ if (visited.select || !fn || !color || rootOf(color.element) !== "theme") return visited;
46
+ const replaced = rest.length
47
+ ? await template`theme.${raw(fn.name)}(${color.element}, ${rest[0].element})`.apply(visited, this.cursor)
48
+ : await template`theme.${raw(fn.name)}(${color.element})`.apply(visited, this.cursor);
49
+ if (!replaced) return visited;
50
+ maybeRemoveImport(this, fn.module, fn.name);
51
+ return {...replaced, prefix: visited.prefix} as J;
52
+ }
53
+ }());
54
+ }
55
+ }
@@ -0,0 +1,59 @@
1
+ import {Cursor, ExecutionContext, Recipe, TreeVisitor, check, not} from "@openrewrite/rewrite";
2
+ import {JavaScriptVisitor, isVendoredOrBundled, raw, template} from "@openrewrite/rewrite/javascript";
3
+ import {J} from "@openrewrite/rewrite/java";
4
+ import {endsWithName} from "./theme-access";
5
+
6
+ const isUnit = (e: J | undefined): e is J.FieldAccess =>
7
+ e?.kind === J.Kind.FieldAccess && (e as J.FieldAccess).name.element.simpleName === "unit" && endsWithName((e as J.FieldAccess).target, "spacing");
8
+
9
+ const numberOf = (e: J | undefined): number | undefined =>
10
+ e?.kind === J.Kind.Literal && typeof (e as J.Literal).value === "number" ? (e as J.Literal).value as number : undefined;
11
+
12
+ /** `theme.spacing(argument)` for the `theme.spacing.unit` it replaces. */
13
+ async function spacingCall(unit: J.FieldAccess, argument: string, anchor: J, cursor: Cursor): Promise<J | undefined> {
14
+ const spacing = unit.target;
15
+ if (spacing.kind === J.Kind.FieldAccess) return template`${(spacing as J.FieldAccess).target}.spacing(${raw(argument)})`.apply(anchor, cursor);
16
+ return template`spacing(${raw(argument)})`.apply(anchor, cursor);
17
+ }
18
+
19
+ /**
20
+ * Replaces `theme.spacing.unit`, which v4 removed, with the `theme.spacing()` function, folding a multiplication or
21
+ * division by a number into its argument.
22
+ *
23
+ * Before: `theme.spacing.unit`, `theme.spacing.unit * 5`, `theme.spacing.unit / 5`
24
+ * After: `theme.spacing(1)`, `theme.spacing(5)`, `theme.spacing(0.2)`
25
+ *
26
+ * @see https://mui.com/material-ui/migration/migration-v3/#theme
27
+ */
28
+ export class ReplaceThemeSpacingUnit extends Recipe {
29
+ readonly name = "org.openrewrite.react.mui.replace-theme-spacing-unit";
30
+ readonly displayName: string = "Replace `theme.spacing.unit` with `theme.spacing()`";
31
+ readonly description: string = "Replaces `theme.spacing.unit` with `theme.spacing(1)`, and `theme.spacing.unit * n` or `/ n` with `theme.spacing(n)` or `theme.spacing(1 / n)`.";
32
+
33
+ async editor(): Promise<TreeVisitor<any, ExecutionContext>> {
34
+ return check(not(isVendoredOrBundled()), new class extends JavaScriptVisitor<ExecutionContext> {
35
+ override async visitBinary(binary: J.Binary, ctx: ExecutionContext): Promise<J | undefined> {
36
+ const operator = binary.operator.element;
37
+ const unitLeft = isUnit(binary.left);
38
+ const count = numberOf(unitLeft ? binary.right : binary.left);
39
+ if ((unitLeft || isUnit(binary.right)) && count !== undefined) {
40
+ let argument: number | undefined;
41
+ if ((operator as string) === "Multiplication") argument = count;
42
+ else if ((operator as string) === "Division" && unitLeft && count !== 0) argument = Number((1 / count).toPrecision(6));
43
+ if (argument !== undefined) {
44
+ const replaced = await spacingCall((unitLeft ? binary.left : binary.right) as J.FieldAccess, String(argument), binary, this.cursor);
45
+ if (replaced) return {...replaced, prefix: binary.prefix} as J;
46
+ }
47
+ }
48
+ return super.visitBinary(binary, ctx);
49
+ }
50
+
51
+ override async visitFieldAccess(fieldAccess: J.FieldAccess, ctx: ExecutionContext): Promise<J | undefined> {
52
+ const visited = await super.visitFieldAccess(fieldAccess, ctx) as J.FieldAccess;
53
+ if (!isUnit(visited)) return visited;
54
+ const replaced = await spacingCall(visited, "1", visited, this.cursor);
55
+ return replaced ? {...replaced, prefix: visited.prefix} as J : visited;
56
+ }
57
+ }());
58
+ }
59
+ }