@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.
- package/dist/i18n/translate-hard-coded-strings.d.ts +67 -0
- package/dist/i18n/translate-hard-coded-strings.d.ts.map +1 -0
- package/dist/i18n/translate-hard-coded-strings.js +439 -0
- package/dist/i18n/translate-hard-coded-strings.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +37 -1
- package/dist/index.js.map +1 -1
- package/dist/mui/adapt-v4-theme.d.ts +8 -0
- package/dist/mui/adapt-v4-theme.d.ts.map +1 -0
- package/dist/mui/adapt-v4-theme.js +65 -0
- package/dist/mui/adapt-v4-theme.js.map +1 -0
- package/dist/mui/add-component-prop.d.ts +18 -0
- package/dist/mui/add-component-prop.d.ts.map +1 -0
- package/dist/mui/add-component-prop.js +53 -0
- package/dist/mui/add-component-prop.js.map +1 -0
- package/dist/mui/add-emotion-prepend.d.ts +8 -0
- package/dist/mui/add-emotion-prepend.d.ts.map +1 -0
- package/dist/mui/add-emotion-prepend.js +71 -0
- package/dist/mui/add-emotion-prepend.js.map +1 -0
- package/dist/mui/change-module-specifier.d.ts +14 -0
- package/dist/mui/change-module-specifier.d.ts.map +1 -0
- package/dist/mui/change-module-specifier.js +95 -0
- package/dist/mui/change-module-specifier.js.map +1 -0
- package/dist/mui/convert-mui-imports.d.ts +14 -0
- package/dist/mui/convert-mui-imports.d.ts.map +1 -0
- package/dist/mui/convert-mui-imports.js +195 -0
- package/dist/mui/convert-mui-imports.js.map +1 -0
- package/dist/mui/convert-prop-to-sx.d.ts +22 -0
- package/dist/mui/convert-prop-to-sx.d.ts.map +1 -0
- package/dist/mui/convert-prop-to-sx.js +99 -0
- package/dist/mui/convert-prop-to-sx.js.map +1 -0
- package/dist/mui/index.d.ts +32 -0
- package/dist/mui/index.d.ts.map +1 -0
- package/dist/mui/index.js +67 -0
- package/dist/mui/index.js.map +1 -0
- package/dist/mui/jsx-attributes.d.ts +27 -0
- package/dist/mui/jsx-attributes.d.ts.map +1 -0
- package/dist/mui/jsx-attributes.js +140 -0
- package/dist/mui/jsx-attributes.js.map +1 -0
- package/dist/mui/mark-manual-migration.d.ts +16 -0
- package/dist/mui/mark-manual-migration.d.ts.map +1 -0
- package/dist/mui/mark-manual-migration.js +73 -0
- package/dist/mui/mark-manual-migration.js.map +1 -0
- package/dist/mui/mark-palette-mode-usage.d.ts +12 -0
- package/dist/mui/mark-palette-mode-usage.d.ts.map +1 -0
- package/dist/mui/mark-palette-mode-usage.js +50 -0
- package/dist/mui/mark-palette-mode-usage.js.map +1 -0
- package/dist/mui/move-component-prop-to-slot.d.ts +22 -0
- package/dist/mui/move-component-prop-to-slot.d.ts.map +1 -0
- package/dist/mui/move-component-prop-to-slot.js +87 -0
- package/dist/mui/move-component-prop-to-slot.js.map +1 -0
- package/dist/mui/move-primary-text-to-children.d.ts +12 -0
- package/dist/mui/move-primary-text-to-children.d.ts.map +1 -0
- package/dist/mui/move-primary-text-to-children.js +52 -0
- package/dist/mui/move-primary-text-to-children.js.map +1 -0
- package/dist/mui/move-system-props-to-sx.d.ts +16 -0
- package/dist/mui/move-system-props-to-sx.d.ts.map +1 -0
- package/dist/mui/move-system-props-to-sx.js +122 -0
- package/dist/mui/move-system-props-to-sx.js.map +1 -0
- package/dist/mui/mui-components.d.ts +8 -0
- package/dist/mui/mui-components.d.ts.map +1 -0
- package/dist/mui/mui-components.js +41 -0
- package/dist/mui/mui-components.js.map +1 -0
- package/dist/mui/mui-tag-visitor.d.ts +5 -0
- package/dist/mui/mui-tag-visitor.d.ts.map +1 -0
- package/dist/mui/mui-tag-visitor.js +54 -0
- package/dist/mui/mui-tag-visitor.js.map +1 -0
- package/dist/mui/pre-v5-recipe.d.ts +14 -0
- package/dist/mui/pre-v5-recipe.d.ts.map +1 -0
- package/dist/mui/pre-v5-recipe.js +88 -0
- package/dist/mui/pre-v5-recipe.js.map +1 -0
- package/dist/mui/remove-component-prop.d.ts +18 -0
- package/dist/mui/remove-component-prop.d.ts.map +1 -0
- package/dist/mui/remove-component-prop.js +55 -0
- package/dist/mui/remove-component-prop.js.map +1 -0
- package/dist/mui/remove-root-ref.d.ts +8 -0
- package/dist/mui/remove-root-ref.d.ts.map +1 -0
- package/dist/mui/remove-root-ref.js +61 -0
- package/dist/mui/remove-root-ref.js.map +1 -0
- package/dist/mui/remove-spacing-px-suffix.d.ts +8 -0
- package/dist/mui/remove-spacing-px-suffix.d.ts.map +1 -0
- package/dist/mui/remove-spacing-px-suffix.js +62 -0
- package/dist/mui/remove-spacing-px-suffix.js.map +1 -0
- package/dist/mui/rename-component-prop.d.ts +20 -0
- package/dist/mui/rename-component-prop.d.ts.map +1 -0
- package/dist/mui/rename-component-prop.js +66 -0
- package/dist/mui/rename-component-prop.js.map +1 -0
- package/dist/mui/rename-joy-css-variables.d.ts +8 -0
- package/dist/mui/rename-joy-css-variables.d.ts.map +1 -0
- package/dist/mui/rename-joy-css-variables.js +51 -0
- package/dist/mui/rename-joy-css-variables.js.map +1 -0
- package/dist/mui/rename-theme-palette-type.d.ts +8 -0
- package/dist/mui/rename-theme-palette-type.d.ts.map +1 -0
- package/dist/mui/rename-theme-palette-type.js +61 -0
- package/dist/mui/rename-theme-palette-type.js.map +1 -0
- package/dist/mui/replace-box-border-radius.d.ts +10 -0
- package/dist/mui/replace-box-border-radius.d.ts.map +1 -0
- package/dist/mui/replace-box-border-radius.js +50 -0
- package/dist/mui/replace-box-border-radius.js.map +1 -0
- package/dist/mui/replace-breakpoints-width.d.ts +8 -0
- package/dist/mui/replace-breakpoints-width.d.ts.map +1 -0
- package/dist/mui/replace-breakpoints-width.js +46 -0
- package/dist/mui/replace-breakpoints-width.js.map +1 -0
- package/dist/mui/replace-component-class.d.ts +18 -0
- package/dist/mui/replace-component-class.d.ts.map +1 -0
- package/dist/mui/replace-component-class.js +126 -0
- package/dist/mui/replace-component-class.js.map +1 -0
- package/dist/mui/replace-components-with-slots.d.ts +14 -0
- package/dist/mui/replace-components-with-slots.d.ts.map +1 -0
- package/dist/mui/replace-components-with-slots.js +69 -0
- package/dist/mui/replace-components-with-slots.js.map +1 -0
- package/dist/mui/replace-import-paths.d.ts +14 -0
- package/dist/mui/replace-import-paths.d.ts.map +1 -0
- package/dist/mui/replace-import-paths.js +101 -0
- package/dist/mui/replace-import-paths.js.map +1 -0
- package/dist/mui/replace-in-string-literals.d.ts +15 -0
- package/dist/mui/replace-in-string-literals.d.ts.map +1 -0
- package/dist/mui/replace-in-string-literals.js +62 -0
- package/dist/mui/replace-in-string-literals.js.map +1 -0
- package/dist/mui/replace-list-item-button-prop.d.ts +12 -0
- package/dist/mui/replace-list-item-button-prop.d.ts.map +1 -0
- package/dist/mui/replace-list-item-button-prop.js +80 -0
- package/dist/mui/replace-list-item-button-prop.js.map +1 -0
- package/dist/mui/replace-tabs-scroll-buttons.d.ts +8 -0
- package/dist/mui/replace-tabs-scroll-buttons.d.ts.map +1 -0
- package/dist/mui/replace-tabs-scroll-buttons.js +41 -0
- package/dist/mui/replace-tabs-scroll-buttons.js.map +1 -0
- package/dist/mui/replace-theme-color-functions.d.ts +8 -0
- package/dist/mui/replace-theme-color-functions.d.ts.map +1 -0
- package/dist/mui/replace-theme-color-functions.js +72 -0
- package/dist/mui/replace-theme-color-functions.js.map +1 -0
- package/dist/mui/replace-theme-spacing-unit.d.ts +8 -0
- package/dist/mui/replace-theme-spacing-unit.d.ts.map +1 -0
- package/dist/mui/replace-theme-spacing-unit.js +77 -0
- package/dist/mui/replace-theme-spacing-unit.js.map +1 -0
- package/dist/mui/replace-typography-round.d.ts +8 -0
- package/dist/mui/replace-typography-round.d.ts.map +1 -0
- package/dist/mui/replace-typography-round.js +44 -0
- package/dist/mui/replace-typography-round.js.map +1 -0
- package/dist/mui/shift-hidden-down-props.d.ts +10 -0
- package/dist/mui/shift-hidden-down-props.d.ts.map +1 -0
- package/dist/mui/shift-hidden-down-props.js +49 -0
- package/dist/mui/shift-hidden-down-props.js.map +1 -0
- package/dist/mui/theme-access.d.ts +5 -0
- package/dist/mui/theme-access.d.ts.map +1 -0
- package/dist/mui/theme-access.js +21 -0
- package/dist/mui/theme-access.js.map +1 -0
- package/dist/mui/update-theme-breakpoints.d.ts +10 -0
- package/dist/mui/update-theme-breakpoints.d.ts.map +1 -0
- package/dist/mui/update-theme-breakpoints.js +58 -0
- package/dist/mui/update-theme-breakpoints.js.map +1 -0
- package/dist/mui/upgrade-grid-props.d.ts +16 -0
- package/dist/mui/upgrade-grid-props.d.ts.map +1 -0
- package/dist/mui/upgrade-grid-props.js +90 -0
- package/dist/mui/upgrade-grid-props.js.map +1 -0
- package/dist/search/find-enzyme-usage.d.ts +8 -0
- package/dist/search/find-enzyme-usage.d.ts.map +1 -1
- package/dist/search/find-enzyme-usage.js +119 -8
- package/dist/search/find-enzyme-usage.js.map +1 -1
- package/dist/search/find-hard-coded-strings.d.ts +110 -0
- package/dist/search/find-hard-coded-strings.d.ts.map +1 -0
- package/dist/search/find-hard-coded-strings.js +987 -0
- package/dist/search/find-hard-coded-strings.js.map +1 -0
- package/dist/search/find-missing-use-client-directive.d.ts +2 -0
- package/dist/search/find-missing-use-client-directive.d.ts.map +1 -1
- package/dist/search/find-missing-use-client-directive.js +1 -0
- package/dist/search/find-missing-use-client-directive.js.map +1 -1
- package/dist/testing/enzyme-host-components.d.ts +12 -0
- package/dist/testing/enzyme-host-components.d.ts.map +1 -1
- package/dist/testing/enzyme-host-components.js +100 -0
- package/dist/testing/enzyme-host-components.js.map +1 -1
- package/dist/testing/enzyme-portals.d.ts +3 -5
- package/dist/testing/enzyme-portals.d.ts.map +1 -1
- package/dist/testing/enzyme-portals.js +8 -39
- package/dist/testing/enzyme-portals.js.map +1 -1
- package/dist/testing/enzyme-render-blockers.d.ts +19 -0
- package/dist/testing/enzyme-render-blockers.d.ts.map +1 -0
- package/dist/testing/enzyme-render-blockers.js +275 -0
- package/dist/testing/enzyme-render-blockers.js.map +1 -0
- package/dist/testing/enzyme-to-react-testing-library.d.ts +17 -3
- package/dist/testing/enzyme-to-react-testing-library.d.ts.map +1 -1
- package/dist/testing/enzyme-to-react-testing-library.js +237 -27
- package/dist/testing/enzyme-to-react-testing-library.js.map +1 -1
- package/dist/testing/enzyme-usage.d.ts +9 -0
- package/dist/testing/enzyme-usage.d.ts.map +1 -1
- package/dist/testing/enzyme-usage.js +36 -3
- package/dist/testing/enzyme-usage.js.map +1 -1
- package/dist/testing/enzyme-wrapper-scope.d.ts +8 -1
- package/dist/testing/enzyme-wrapper-scope.d.ts.map +1 -1
- package/dist/testing/enzyme-wrapper-scope.js +26 -5
- package/dist/testing/enzyme-wrapper-scope.js.map +1 -1
- package/dist/util/json-and-javascript.d.ts +4 -0
- package/dist/util/json-and-javascript.d.ts.map +1 -0
- package/dist/util/json-and-javascript.js +37 -0
- package/dist/util/json-and-javascript.js.map +1 -0
- package/dist/util/module-imports.d.ts +36 -0
- package/dist/util/module-imports.d.ts.map +1 -1
- package/dist/util/module-imports.js +216 -0
- package/dist/util/module-imports.js.map +1 -1
- package/dist/util/test-source.d.ts +2 -0
- package/dist/util/test-source.d.ts.map +1 -0
- package/dist/util/test-source.js +5 -0
- package/dist/util/test-source.js.map +1 -0
- package/dist/zustand/entry-store.d.ts +9 -0
- package/dist/zustand/entry-store.d.ts.map +1 -0
- package/dist/zustand/entry-store.js +136 -0
- package/dist/zustand/entry-store.js.map +1 -0
- package/dist/zustand/redux-stores.d.ts +7 -4
- package/dist/zustand/redux-stores.d.ts.map +1 -1
- package/dist/zustand/redux-stores.js +128 -11
- package/dist/zustand/redux-stores.js.map +1 -1
- package/dist/zustand/redux-to-zustand.d.ts +11 -2
- package/dist/zustand/redux-to-zustand.d.ts.map +1 -1
- package/dist/zustand/redux-to-zustand.js +147 -46
- package/dist/zustand/redux-to-zustand.js.map +1 -1
- package/dist/zustand/zustand-plan.d.ts +14 -5
- package/dist/zustand/zustand-plan.d.ts.map +1 -1
- package/dist/zustand/zustand-plan.js +226 -52
- package/dist/zustand/zustand-plan.js.map +1 -1
- package/package.json +1 -1
- package/src/i18n/translate-hard-coded-strings.ts +442 -0
- package/src/index.ts +17 -0
- package/src/mui/adapt-v4-theme.ts +43 -0
- package/src/mui/add-component-prop.ts +43 -0
- package/src/mui/add-emotion-prepend.ts +48 -0
- package/src/mui/change-module-specifier.ts +71 -0
- package/src/mui/convert-mui-imports.ts +184 -0
- package/src/mui/convert-prop-to-sx.ts +89 -0
- package/src/mui/index.ts +31 -0
- package/src/mui/jsx-attributes.ts +133 -0
- package/src/mui/mark-manual-migration.ts +53 -0
- package/src/mui/mark-palette-mode-usage.ts +36 -0
- package/src/mui/move-component-prop-to-slot.ts +84 -0
- package/src/mui/move-primary-text-to-children.ts +41 -0
- package/src/mui/move-system-props-to-sx.ts +118 -0
- package/src/mui/mui-components.ts +40 -0
- package/src/mui/mui-tag-visitor.ts +36 -0
- package/src/mui/pre-v5-recipe.ts +82 -0
- package/src/mui/remove-component-prop.ts +43 -0
- package/src/mui/remove-root-ref.ts +41 -0
- package/src/mui/remove-spacing-px-suffix.ts +43 -0
- package/src/mui/rename-component-prop.ts +55 -0
- package/src/mui/rename-joy-css-variables.ts +41 -0
- package/src/mui/rename-theme-palette-type.ts +42 -0
- package/src/mui/replace-box-border-radius.ts +42 -0
- package/src/mui/replace-breakpoints-width.ts +28 -0
- package/src/mui/replace-component-class.ts +97 -0
- package/src/mui/replace-components-with-slots.ts +61 -0
- package/src/mui/replace-import-paths.ts +100 -0
- package/src/mui/replace-in-string-literals.ts +44 -0
- package/src/mui/replace-list-item-button-prop.ts +59 -0
- package/src/mui/replace-tabs-scroll-buttons.ts +31 -0
- package/src/mui/replace-theme-color-functions.ts +55 -0
- package/src/mui/replace-theme-spacing-unit.ts +59 -0
- package/src/mui/replace-typography-round.ts +29 -0
- package/src/mui/shift-hidden-down-props.ts +44 -0
- package/src/mui/theme-access.ts +18 -0
- package/src/mui/update-theme-breakpoints.ts +47 -0
- package/src/mui/upgrade-grid-props.ts +82 -0
- package/src/search/find-enzyme-usage.ts +109 -12
- package/src/search/find-hard-coded-strings.ts +1006 -0
- package/src/search/find-missing-use-client-directive.ts +1 -1
- package/src/testing/enzyme-host-components.ts +110 -0
- package/src/testing/enzyme-portals.ts +4 -44
- package/src/testing/enzyme-render-blockers.ts +278 -0
- package/src/testing/enzyme-to-react-testing-library.ts +240 -34
- package/src/testing/enzyme-usage.ts +48 -2
- package/src/testing/enzyme-wrapper-scope.ts +35 -3
- package/src/util/json-and-javascript.ts +26 -0
- package/src/util/module-imports.ts +223 -0
- package/src/util/test-source.ts +2 -0
- package/src/zustand/entry-store.ts +131 -0
- package/src/zustand/redux-stores.ts +131 -12
- package/src/zustand/redux-to-zustand.ts +179 -52
- package/src/zustand/zustand-plan.ts +256 -49
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import {ExecutionContext, Option, Recipe, TreeVisitor} from "@openrewrite/rewrite";
|
|
2
|
+
import {
|
|
3
|
+
appendAttribute, appendProperty, attributeExpression, attributeObject, bare, findAttribute, findProperty,
|
|
4
|
+
newAttribute, newObject, newObjectAttribute, newProperty, replaceAttribute, withAttributes
|
|
5
|
+
} from "./jsx-attributes";
|
|
6
|
+
import {muiTagEditor} from "./mui-tag-visitor";
|
|
7
|
+
import {isObjectLiteral} from "./jsx-attributes";
|
|
8
|
+
import {JS, JSX} from "@openrewrite/rewrite/javascript";
|
|
9
|
+
import {J} from "@openrewrite/rewrite/java";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Moves a deprecated prop of a Material UI component into its `slots` or `slotProps` prop.
|
|
13
|
+
*
|
|
14
|
+
* Before: `<Dialog TransitionComponent={Fade} PaperProps={{elevation: 1}} />`
|
|
15
|
+
* After: `<Dialog slots={{ transition: Fade }} slotProps={{ paper: {elevation: 1} }} />`
|
|
16
|
+
*
|
|
17
|
+
* With `slotPropName`, the value goes one level deeper, as in `tooltipTitle` becoming `slotProps.tooltip.title`.
|
|
18
|
+
* A tag whose `slots`, `slotProps` or slot is already set to something other than an object literal, or to a slot
|
|
19
|
+
* that is set already, is left alone, since merging it would change what the code does.
|
|
20
|
+
*
|
|
21
|
+
* @see https://mui.com/material-ui/migration/migrating-from-deprecated-apis/
|
|
22
|
+
*/
|
|
23
|
+
export class MoveComponentPropToSlot extends Recipe {
|
|
24
|
+
readonly name = "org.openrewrite.react.mui.move-component-prop-to-slot";
|
|
25
|
+
readonly displayName: string = "Move a Material UI component prop into a slot";
|
|
26
|
+
readonly description: string = "Moves a deprecated prop of a Material UI component into its `slots` or `slotProps` prop. Tags whose target is not an object literal, or already sets that slot, are left alone.";
|
|
27
|
+
|
|
28
|
+
@Option({displayName: "Component name", description: "The Material UI component, such as `Dialog`.", example: "Dialog"})
|
|
29
|
+
componentName!: string;
|
|
30
|
+
|
|
31
|
+
@Option({displayName: "Prop name", description: "The deprecated prop to move.", example: "TransitionComponent"})
|
|
32
|
+
propName!: string;
|
|
33
|
+
|
|
34
|
+
@Option({displayName: "Target prop", description: "The prop the value moves into, `slots` or `slotProps`.", example: "slots", valid: ["slots", "slotProps"]})
|
|
35
|
+
targetProp!: string;
|
|
36
|
+
|
|
37
|
+
@Option({displayName: "Slot name", description: "The key under the target prop.", example: "transition"})
|
|
38
|
+
slotName!: string;
|
|
39
|
+
|
|
40
|
+
@Option({displayName: "Slot prop name", description: "A key under the slot, when the prop becomes one prop of the slot.", example: "ref", required: false})
|
|
41
|
+
slotPropName?: string;
|
|
42
|
+
|
|
43
|
+
@Option({displayName: "Package name", description: "A package that re-exports Material UI, when the components are not imported from Material UI itself.", example: "@org/ui", required: false})
|
|
44
|
+
packageName?: string;
|
|
45
|
+
|
|
46
|
+
constructor(options?: { componentName?: string; propName?: string; targetProp?: string; slotName?: string; slotPropName?: string; packageName?: string }) {
|
|
47
|
+
super(options);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
async editor(): Promise<TreeVisitor<any, ExecutionContext>> {
|
|
51
|
+
const {propName, targetProp, slotName, slotPropName} = this;
|
|
52
|
+
return muiTagEditor(this.componentName, this.packageName, async (tag, cursor) => {
|
|
53
|
+
const attribute = findAttribute(tag, propName);
|
|
54
|
+
if (!attribute) return tag;
|
|
55
|
+
const value = bare(attributeExpression(attribute));
|
|
56
|
+
const target = findAttribute(tag, targetProp);
|
|
57
|
+
|
|
58
|
+
if (!target) {
|
|
59
|
+
const moved = slotPropName
|
|
60
|
+
? await newObjectAttribute(cursor, tag, targetProp, slotName, await newObject(cursor, tag, slotPropName, value))
|
|
61
|
+
: await newObjectAttribute(cursor, tag, targetProp, slotName, value);
|
|
62
|
+
return replaceAttribute(tag, attribute, moved);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const object = attributeObject(target);
|
|
66
|
+
if (!object) return tag;
|
|
67
|
+
const slot = findProperty(object, slotName);
|
|
68
|
+
let updated: J.NewClass;
|
|
69
|
+
if (!slot) {
|
|
70
|
+
const inner = slotPropName ? await newObject(cursor, tag, slotPropName, value) : value;
|
|
71
|
+
updated = appendProperty(object, await newProperty(cursor, tag, slotName, inner));
|
|
72
|
+
} else if (slotPropName && slot.initializer && isObjectLiteral(slot.initializer) && !findProperty(slot.initializer, slotPropName)) {
|
|
73
|
+
const added = appendProperty(slot.initializer, await newProperty(cursor, tag, slotPropName, value));
|
|
74
|
+
updated = {...object, body: {...object.body!, statements: object.body!.statements.map(s =>
|
|
75
|
+
s.element === slot ? {...s, element: {...slot, initializer: added}} : s)}} as J.NewClass;
|
|
76
|
+
} else {
|
|
77
|
+
return tag;
|
|
78
|
+
}
|
|
79
|
+
const embedded = target.value!.element as JSX.EmbeddedExpression;
|
|
80
|
+
const retargeted = {...target, value: {...target.value!, element: {...embedded, expression: {...embedded.expression, element: updated}}}} as JSX.Attribute;
|
|
81
|
+
return replaceAttribute(replaceAttribute(tag, target, retargeted), attribute, undefined);
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import {ExecutionContext, Option, Recipe, TreeVisitor} from "@openrewrite/rewrite";
|
|
2
|
+
import {JSX, raw, template} from "@openrewrite/rewrite/javascript";
|
|
3
|
+
import {J} from "@openrewrite/rewrite/java";
|
|
4
|
+
import {attributeExpression, bare, findAttribute, replaceAttribute} from "./jsx-attributes";
|
|
5
|
+
import {muiTagEditor} from "./mui-tag-visitor";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Moves the `primaryText` of a self-closing `MenuItem` into its children, which is where v1 reads the text from.
|
|
9
|
+
*
|
|
10
|
+
* Before: `<MenuItem primaryText="Profile" />`
|
|
11
|
+
* After: `<MenuItem>Profile</MenuItem>`
|
|
12
|
+
*
|
|
13
|
+
* @see https://github.com/mui/material-ui/tree/master/packages/mui-codemod/src/v1.0.0/menu-item-primary-text
|
|
14
|
+
*/
|
|
15
|
+
export class MovePrimaryTextToChildren extends Recipe {
|
|
16
|
+
readonly name = "org.openrewrite.react.mui.move-primary-text-to-children";
|
|
17
|
+
readonly displayName: string = "Move `MenuItem`'s `primaryText` into its children";
|
|
18
|
+
readonly description: string = "Turns `<MenuItem primaryText=\"x\" />` into `<MenuItem>x</MenuItem>`. Only a self-closing `MenuItem` is changed.";
|
|
19
|
+
|
|
20
|
+
@Option({displayName: "Package name", description: "The package the component is imported from, when it is not one of Material UI's.", required: false, example: "@acme/ui"})
|
|
21
|
+
packageName?: string;
|
|
22
|
+
|
|
23
|
+
constructor(options?: {packageName?: string}) {
|
|
24
|
+
super(options);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
async editor(): Promise<TreeVisitor<any, ExecutionContext>> {
|
|
28
|
+
return muiTagEditor("MenuItem", this.packageName, async (tag, cursor) => {
|
|
29
|
+
const attribute = findAttribute(tag, "primaryText");
|
|
30
|
+
if (!attribute || tag.children) return tag;
|
|
31
|
+
const value = attributeExpression(attribute);
|
|
32
|
+
const isText = value.kind === J.Kind.Literal && typeof (value as J.Literal).value === "string" && !/[{}<>]/.test((value as J.Literal).value as string);
|
|
33
|
+
const probe = (isText
|
|
34
|
+
? await template`<X>${raw((value as J.Literal).value as string)}</X>`.apply(tag, cursor)
|
|
35
|
+
: await template`<X>{${bare(value)}}</X>`.apply(tag, cursor)) as JSX.Tag;
|
|
36
|
+
const without = replaceAttribute(tag, attribute, undefined);
|
|
37
|
+
return {...without, selfClosing: undefined, children: probe.children, closingName: {...probe.closingName!, element: without.openName.element},
|
|
38
|
+
afterClosingName: probe.afterClosingName} as JSX.Tag;
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import {Cursor, ExecutionContext, Option, Recipe, TreeVisitor} from "@openrewrite/rewrite";
|
|
2
|
+
import {JS, JSX} from "@openrewrite/rewrite/javascript";
|
|
3
|
+
import {J} from "@openrewrite/rewrite/java";
|
|
4
|
+
import {
|
|
5
|
+
appendProperty, attributeExpression, attributeObject, bare, findAttribute, hasSpread, newAttribute, newObject, newProperty,
|
|
6
|
+
objectProperties, withAttributes
|
|
7
|
+
} from "./jsx-attributes";
|
|
8
|
+
import {attributeName} from "./mui-components";
|
|
9
|
+
import {stringValue} from "./theme-access";
|
|
10
|
+
import {muiTagEditor} from "./mui-tag-visitor";
|
|
11
|
+
|
|
12
|
+
/** The style props of the `sx` prop, from `defaultSxConfig` of `@mui/system`. */
|
|
13
|
+
const SYSTEM_PROPS = new Set(["border", "borderTop", "borderRight", "borderBottom", "borderLeft", "borderColor", "borderTopColor", "borderRightColor",
|
|
14
|
+
"borderBottomColor", "borderLeftColor", "outline", "outlineColor", "borderRadius", "color", "bgcolor", "backgroundColor", "p", "pt", "pr", "pb",
|
|
15
|
+
"pl", "px", "py", "padding", "paddingTop", "paddingRight", "paddingBottom", "paddingLeft", "paddingX", "paddingY", "paddingInline",
|
|
16
|
+
"paddingInlineStart", "paddingInlineEnd", "paddingBlock", "paddingBlockStart", "paddingBlockEnd", "m", "mt", "mr", "mb", "ml", "mx", "my",
|
|
17
|
+
"margin", "marginTop", "marginRight", "marginBottom", "marginLeft", "marginX", "marginY", "marginInline", "marginInlineStart", "marginInlineEnd",
|
|
18
|
+
"marginBlock", "marginBlockStart", "marginBlockEnd", "displayPrint", "display", "overflow", "textOverflow", "visibility", "whiteSpace", "flexBasis",
|
|
19
|
+
"flexDirection", "flexWrap", "justifyContent", "alignItems", "alignContent", "order", "flex", "flexGrow", "flexShrink", "alignSelf",
|
|
20
|
+
"justifyItems", "justifySelf", "gap", "rowGap", "columnGap", "gridColumn", "gridRow", "gridAutoFlow", "gridAutoColumns", "gridAutoRows",
|
|
21
|
+
"gridTemplateColumns", "gridTemplateRows", "gridTemplateAreas", "gridArea", "position", "zIndex", "top", "right", "bottom", "left", "boxShadow",
|
|
22
|
+
"width", "maxWidth", "minWidth", "height", "maxHeight", "minHeight", "boxSizing", "font", "fontFamily", "fontSize", "fontStyle", "fontWeight",
|
|
23
|
+
"letterSpacing", "textTransform", "lineHeight", "textAlign", "typography"]);
|
|
24
|
+
|
|
25
|
+
/** Whether a `color` is a theme path or a CSS value, which `sx` resolves, rather than a named color of the component. */
|
|
26
|
+
const isSxColor = (value: J, keepInherit: boolean): boolean => {
|
|
27
|
+
const text = stringValue(value);
|
|
28
|
+
if (text === undefined) return true;
|
|
29
|
+
return (!keepInherit && text === "inherit") || text.includes(".") || text === "divider" || text.startsWith("#") || /\(.*\)/.test(text);
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Moves the system props of a component, such as `mt` and `color`, into its `sx` prop, since they are removed in
|
|
34
|
+
* Material UI v6 and v9.
|
|
35
|
+
*
|
|
36
|
+
* Before: `<Box ml="2px" py={1} color="primary.main" />`
|
|
37
|
+
* After: `<Box sx={{ ml: "2px", py: 1, color: "primary.main" }} />`
|
|
38
|
+
*
|
|
39
|
+
* A tag with a spread attribute, or with an `sx` that is not an object literal, is left alone, since where the
|
|
40
|
+
* props go then depends on what is spread. Of `Typography` and `Link` the `color` stays when it names a color of the
|
|
41
|
+
* component, such as `primary`, and `Link` keeps `color` altogether in v6.
|
|
42
|
+
*
|
|
43
|
+
* @see https://mui.com/material-ui/migration/upgrade-to-v6/#system-props
|
|
44
|
+
*/
|
|
45
|
+
export class MoveSystemPropsToSx extends Recipe {
|
|
46
|
+
readonly name = "org.openrewrite.react.mui.move-system-props-to-sx";
|
|
47
|
+
readonly displayName: string = "Move system props into the `sx` prop";
|
|
48
|
+
readonly description: string = "Moves the system props of Box, Stack, Typography, Link, Grid and the like into their `sx` prop. Tags with a spread attribute, or an `sx` that is not an object literal, are left alone.";
|
|
49
|
+
|
|
50
|
+
@Option({displayName: "Component names", description: "The components to migrate, separated by commas.", example: "Box,Stack,Typography,Link,Grid"})
|
|
51
|
+
componentNames!: string;
|
|
52
|
+
|
|
53
|
+
@Option({displayName: "Version", description: "`v6` or `v9`, which differ in what is kept as a prop of `Typography` and `Link`; anything else moves every system prop.", example: "v6", required: false})
|
|
54
|
+
version?: string;
|
|
55
|
+
|
|
56
|
+
@Option({displayName: "Package name", description: "A package that re-exports Material UI.", example: "@org/ui", required: false})
|
|
57
|
+
packageName?: string;
|
|
58
|
+
|
|
59
|
+
constructor(options?: { componentNames?: string; version?: string; packageName?: string }) {
|
|
60
|
+
super(options);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
async editor(): Promise<TreeVisitor<any, ExecutionContext>> {
|
|
64
|
+
const names = new Set(this.componentNames.split(",").map(s => s.trim()));
|
|
65
|
+
const v9 = this.version === "v9";
|
|
66
|
+
const v6 = this.version === "v6";
|
|
67
|
+
const typographyLike = new Set(["Typography", ...(v9 ? ["DialogContentText", "TimelineContent", "TimelineOppositeContent"] : [])]);
|
|
68
|
+
|
|
69
|
+
/** Whether a system prop of the component moves into `sx`. */
|
|
70
|
+
const moves = (component: string, key: string, value: J): boolean => {
|
|
71
|
+
if (!SYSTEM_PROPS.has(key)) return false;
|
|
72
|
+
if (typographyLike.has(component) && (v6 || v9)) return key !== "color" || isSxColor(value, !v9);
|
|
73
|
+
if (component === "Link" && v6) return key !== "color";
|
|
74
|
+
if (component === "Link" && v9) return key !== "color" || isSxColor(value, true);
|
|
75
|
+
return true;
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
return muiTagEditor(c => names.has(c), this.packageName, async (visited, cursor, component) => {
|
|
79
|
+
if (hasSpread(visited)) return visited;
|
|
80
|
+
const moved = visited.attributes.map(a => a.element).filter((a): a is JSX.Attribute => {
|
|
81
|
+
const key = attributeName(a);
|
|
82
|
+
return a.kind === JS.Kind.JsxAttribute && !!key && !!(a as JSX.Attribute).value && moves(component, key, attributeExpression(a as JSX.Attribute));
|
|
83
|
+
});
|
|
84
|
+
if (!moved.length) return visited;
|
|
85
|
+
const sx = findAttribute(visited, "sx");
|
|
86
|
+
if (sx && !attributeObject(sx)) return visited;
|
|
87
|
+
return move(visited, moved, sx, cursor);
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
async function move(tag: JSX.Tag, moved: JSX.Attribute[], sx: JSX.Attribute | undefined, cursor: Cursor): Promise<JSX.Tag> {
|
|
93
|
+
const keyOf = (a: JSX.Attribute) => attributeName(a)!;
|
|
94
|
+
const valueOf = (a: JSX.Attribute) => bare(attributeExpression(a));
|
|
95
|
+
const existing = sx && attributeObject(sx);
|
|
96
|
+
const keys = existing ? new Set(objectProperties(existing).map(p => p.element.name?.element?.simpleName)) : new Set<string>();
|
|
97
|
+
// A prop that `sx` sets already keeps the value of `sx`, which is the one that won before.
|
|
98
|
+
const fresh = moved.filter(a => !keys.has(keyOf(a)));
|
|
99
|
+
let object: J.NewClass | undefined = existing;
|
|
100
|
+
for (const attribute of fresh) {
|
|
101
|
+
object = object
|
|
102
|
+
? appendProperty(object, await newProperty(cursor, tag, keyOf(attribute), valueOf(attribute)))
|
|
103
|
+
: await newObject(cursor, tag, keyOf(attribute), valueOf(attribute));
|
|
104
|
+
}
|
|
105
|
+
const movedSet = new Set<unknown>(moved);
|
|
106
|
+
const first = moved[0];
|
|
107
|
+
let attributes = tag.attributes;
|
|
108
|
+
if (sx) {
|
|
109
|
+
const embedded = sx.value!.element as JSX.EmbeddedExpression;
|
|
110
|
+
const updated = {...sx, value: {...sx.value!, element: {...embedded, expression: {...embedded.expression, element: object}}}} as JSX.Attribute;
|
|
111
|
+
attributes = attributes.filter(a => !movedSet.has(a.element)).map(a => a.element === sx ? {...a, element: updated} : a);
|
|
112
|
+
} else {
|
|
113
|
+
const created = await newAttribute(cursor, tag, "sx", object!);
|
|
114
|
+
attributes = attributes.flatMap(a => a.element === first ? [{...a, element: {...created, prefix: first.prefix}}]
|
|
115
|
+
: movedSet.has(a.element) ? [] : [a]);
|
|
116
|
+
}
|
|
117
|
+
return withAttributes(tag, attributes);
|
|
118
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import {JS, JSX} from "@openrewrite/rewrite/javascript";
|
|
2
|
+
import {isIdentifier, J} from "@openrewrite/rewrite/java";
|
|
3
|
+
import {importBindings} from "../util/module-imports";
|
|
4
|
+
|
|
5
|
+
export const escapeRegExp = (s: string): string => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
6
|
+
|
|
7
|
+
/** Every npm package of Material UI, old and new, whose components the MUI codemods target. */
|
|
8
|
+
export const MUI_PACKAGES = /^(@mui\/(material|lab|system|base|joy|styles|icons-material|utils|x-date-pickers|x-date-pickers-pro|x-tree-view)|@material-ui\/(core|lab|styles|system|icons|utils|types)|material-ui)(\/|$)/;
|
|
9
|
+
|
|
10
|
+
/** Local names that bind a Material UI component in a source, with the component each one is. */
|
|
11
|
+
export function muiComponents(cu: JS.CompilationUnit, packages: RegExp = MUI_PACKAGES): Map<string, string> {
|
|
12
|
+
const components = new Map<string, string>();
|
|
13
|
+
for (const {local, name, module} of importBindings(cu)) {
|
|
14
|
+
if (!packages.test(module)) continue;
|
|
15
|
+
// A default import of `@mui/material/Button` is `Button`; one of the package root is not a component.
|
|
16
|
+
const component = name === "default" ? module.split("/").pop()! : name;
|
|
17
|
+
if (name === "default" && !module.includes("/")) continue;
|
|
18
|
+
components.set(local, component);
|
|
19
|
+
}
|
|
20
|
+
return components;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** The component a JSX tag renders, when it is one of the Material UI components imported into the source. */
|
|
24
|
+
export function tagComponent(tag: JSX.Tag, components: Map<string, string>): string | undefined {
|
|
25
|
+
const name = tag.openName.element;
|
|
26
|
+
return isIdentifier(name) ? components.get(name.simpleName) : undefined;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** The name of a JSX attribute, or undefined for a spread or namespaced one. */
|
|
30
|
+
export function attributeName(attribute: JSX.Attribute | JSX.SpreadAttribute): string | undefined {
|
|
31
|
+
return attribute.kind === JS.Kind.JsxAttribute && isIdentifier((attribute as JSX.Attribute).key)
|
|
32
|
+
? ((attribute as JSX.Attribute).key as J.Identifier).simpleName : undefined;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** The string a JSX attribute is set to, `a="x"` and `a={"x"}` alike, when it is a plain string literal. */
|
|
36
|
+
export function attributeString(attribute: JSX.Attribute): string | undefined {
|
|
37
|
+
let value: J | undefined = attribute.value?.element;
|
|
38
|
+
if (value?.kind === JS.Kind.JsxEmbeddedExpression) value = (value as JSX.EmbeddedExpression).expression.element;
|
|
39
|
+
return value?.kind === J.Kind.Literal && typeof (value as J.Literal).value === "string" ? (value as J.Literal).value as string : undefined;
|
|
40
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import {Cursor, ExecutionContext, TreeVisitor, check, not} from "@openrewrite/rewrite";
|
|
2
|
+
import {JavaScriptVisitor, JS, JSX, isVendoredOrBundled} from "@openrewrite/rewrite/javascript";
|
|
3
|
+
import {J} from "@openrewrite/rewrite/java";
|
|
4
|
+
import {MUI_PACKAGES, escapeRegExp, muiComponents, tagComponent} from "./mui-components";
|
|
5
|
+
|
|
6
|
+
/** The modules a recipe looks at: Material UI's own packages, or a package of the user's that re-exports them. */
|
|
7
|
+
export function packagePattern(packageName?: string): RegExp {
|
|
8
|
+
if (!packageName) return MUI_PACKAGES;
|
|
9
|
+
return new RegExp(`^${escapeRegExp(packageName)}(/|$)`);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* An editor that calls `handle` for every JSX tag rendering a Material UI component, found through the imports of
|
|
14
|
+
* the source rather than the spelling of the tag. `component` is the component's name, or a test of it. A source
|
|
15
|
+
* that imports no such component is not visited at all.
|
|
16
|
+
*/
|
|
17
|
+
export function muiTagEditor(component: string | ((name: string) => boolean), packageName: string | undefined,
|
|
18
|
+
handle: (tag: JSX.Tag, cursor: Cursor, component: string) => Promise<JSX.Tag>): Promise<TreeVisitor<any, ExecutionContext>> {
|
|
19
|
+
const pattern = packagePattern(packageName);
|
|
20
|
+
const matches = typeof component === "string" ? (name: string) => name === component : component;
|
|
21
|
+
return check(not(isVendoredOrBundled()), new class extends JavaScriptVisitor<ExecutionContext> {
|
|
22
|
+
private components = new Map<string, string>();
|
|
23
|
+
|
|
24
|
+
override async visitJsCompilationUnit(cu: JS.CompilationUnit, ctx: ExecutionContext): Promise<J | undefined> {
|
|
25
|
+
this.components = muiComponents(cu, pattern);
|
|
26
|
+
for (const name of this.components.values()) if (matches(name)) return super.visitJsCompilationUnit(cu, ctx);
|
|
27
|
+
return cu;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
override async visitJsxTag(tag: JSX.Tag, ctx: ExecutionContext): Promise<J | undefined> {
|
|
31
|
+
const visited = await super.visitJsxTag(tag, ctx) as JSX.Tag;
|
|
32
|
+
const name = tagComponent(visited, this.components);
|
|
33
|
+
return name && matches(name) ? handle(visited, this.cursor, name) : visited;
|
|
34
|
+
}
|
|
35
|
+
}());
|
|
36
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import {check, ExecutionContext, foundSearchResult, not, ScanningRecipe, TreeVisitor} from "@openrewrite/rewrite";
|
|
2
|
+
import {findNodeResolutionResult, isVendoredOrBundled, JavaScriptVisitor, JS} from "@openrewrite/rewrite/javascript";
|
|
3
|
+
import {J} from "@openrewrite/rewrite/java";
|
|
4
|
+
import {Json, JsonVisitor} from "@openrewrite/rewrite/json";
|
|
5
|
+
import {importBindings} from "../util/module-imports";
|
|
6
|
+
import {jsonAndJavaScript} from "../util/json-and-javascript";
|
|
7
|
+
import {treesOfType} from "../util/java-recipe-proxy";
|
|
8
|
+
|
|
9
|
+
/** The packages of Material UI before v5, which `@mui/*` replaced. */
|
|
10
|
+
const V4_PACKAGE = /^(@material-ui\/|material-ui(\/|$))/;
|
|
11
|
+
const V4_DEPENDENCY = /^@material-ui\/core$/;
|
|
12
|
+
|
|
13
|
+
/** The package roots of a repository, the ones that declare Material UI v4, and the sources that import it. */
|
|
14
|
+
export interface V4Projects {
|
|
15
|
+
roots: Set<string>;
|
|
16
|
+
declaring: Set<string>;
|
|
17
|
+
importing: Set<string>;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const rootOf = (path: string, roots: ReadonlySet<string>): string => {
|
|
21
|
+
let best = "";
|
|
22
|
+
for (const root of roots) if (path.startsWith(root) && root.length > best.length) best = root;
|
|
23
|
+
return best;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* A recipe that moves code from Material UI v4 to v5 and is not idempotent, since it reads `mdDown` or
|
|
28
|
+
* `down('md')` differently in v5 than v4 did. Run over code that is already on v5 or later, which
|
|
29
|
+
* a cumulative `upgrade-to-mui-*` recipe does, it would shift that code a second time. So it only edits the sources of a
|
|
30
|
+
* package that is still on v4: one that declares `@material-ui/core`, or has a source that imports a `@material-ui/*` package.
|
|
31
|
+
*
|
|
32
|
+
* Both are read before the first cycle edits anything, so a project that a step of the same run renames to `@mui/*`
|
|
33
|
+
* is still read as v4 for the steps that follow.
|
|
34
|
+
*/
|
|
35
|
+
export abstract class PreV5Recipe extends ScanningRecipe<V4Projects> {
|
|
36
|
+
/** The editor to run over the sources of the packages that are on v4. */
|
|
37
|
+
protected abstract v4Editor(): Promise<TreeVisitor<any, ExecutionContext>>;
|
|
38
|
+
|
|
39
|
+
initialValue(): V4Projects {
|
|
40
|
+
return {roots: new Set(), declaring: new Set(), importing: new Set()};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
async scanner(projects: V4Projects): Promise<TreeVisitor<any, ExecutionContext>> {
|
|
44
|
+
const json = new class extends JsonVisitor<ExecutionContext> {
|
|
45
|
+
override async visitDocument(doc: Json.Document): Promise<Json | undefined> {
|
|
46
|
+
if (!/(^|\/)package\.json$/.test(doc.sourcePath)) return doc;
|
|
47
|
+
const root = doc.sourcePath.slice(0, -"package.json".length);
|
|
48
|
+
projects.roots.add(root);
|
|
49
|
+
const resolution = findNodeResolutionResult(doc);
|
|
50
|
+
const declared = [...resolution?.dependencies ?? [], ...resolution?.devDependencies ?? [],
|
|
51
|
+
...resolution?.peerDependencies ?? [], ...resolution?.optionalDependencies ?? []];
|
|
52
|
+
if (declared.some(dep => V4_DEPENDENCY.test(dep.name))) projects.declaring.add(root);
|
|
53
|
+
return doc;
|
|
54
|
+
}
|
|
55
|
+
}();
|
|
56
|
+
const javascript = await check(not(isVendoredOrBundled()), new class extends JavaScriptVisitor<ExecutionContext> {
|
|
57
|
+
override async visitJsCompilationUnit(cu: JS.CompilationUnit): Promise<J | undefined> {
|
|
58
|
+
if (importBindings(cu).some(({module}) => V4_PACKAGE.test(module))) projects.importing.add(cu.sourcePath);
|
|
59
|
+
return cu;
|
|
60
|
+
}
|
|
61
|
+
}());
|
|
62
|
+
return jsonAndJavaScript(json, javascript, true);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
async editorWithData(projects: V4Projects): Promise<TreeVisitor<any, ExecutionContext>> {
|
|
66
|
+
// The scan is over by now, so a source's package.json is known wherever it came in the scan
|
|
67
|
+
const v4 = new Set(projects.declaring);
|
|
68
|
+
for (const path of projects.importing) v4.add(rootOf(path, projects.roots));
|
|
69
|
+
const onV4 = new class extends JavaScriptVisitor<ExecutionContext> {
|
|
70
|
+
override async visitJsCompilationUnit(cu: JS.CompilationUnit): Promise<J | undefined> {
|
|
71
|
+
return v4.has(rootOf(cu.sourcePath, projects.roots)) ? foundSearchResult(cu) : cu;
|
|
72
|
+
}
|
|
73
|
+
}();
|
|
74
|
+
return check(onV4, await this.v4Editor());
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// HACK: core's ScanningRecipe.editor() hides editorWithData behind a lazy wrapper, so the host would send the editor
|
|
78
|
+
// every tree (openrewrite/rewrite#9734)
|
|
79
|
+
override async editor(): Promise<TreeVisitor<any, ExecutionContext>> {
|
|
80
|
+
return check(treesOfType("org.openrewrite.javascript.tree.JS"), super.editor());
|
|
81
|
+
}
|
|
82
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import {ExecutionContext, Option, Recipe, TreeVisitor} from "@openrewrite/rewrite";
|
|
2
|
+
import {findAttribute, replaceAttribute} from "./jsx-attributes";
|
|
3
|
+
import {attributeString} from "./mui-components";
|
|
4
|
+
import {muiTagEditor} from "./mui-tag-visitor";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Removes a prop of a Material UI component, optionally only when it is set to a given string.
|
|
8
|
+
*
|
|
9
|
+
* Before: `<Dialog disableBackdropClick />`
|
|
10
|
+
* After: `<Dialog />`
|
|
11
|
+
*
|
|
12
|
+
* @see https://mui.com/material-ui/migration/migration-v4/
|
|
13
|
+
*/
|
|
14
|
+
export class RemoveComponentProp extends Recipe {
|
|
15
|
+
readonly name = "org.openrewrite.react.mui.remove-component-prop";
|
|
16
|
+
readonly displayName: string = "Remove a Material UI component prop";
|
|
17
|
+
readonly description: string = "Removes a prop of a Material UI component, found through the imports of the file. With `value` set, only a prop set to that string literal is removed.";
|
|
18
|
+
|
|
19
|
+
@Option({displayName: "Component name", description: "The Material UI component, such as `Dialog`.", example: "Dialog"})
|
|
20
|
+
componentName!: string;
|
|
21
|
+
|
|
22
|
+
@Option({displayName: "Prop name", description: "The prop to remove.", example: "disableBackdropClick"})
|
|
23
|
+
propName!: string;
|
|
24
|
+
|
|
25
|
+
@Option({displayName: "Value", description: "Only remove the prop when it is set to this string literal.", example: "default", required: false})
|
|
26
|
+
value?: string;
|
|
27
|
+
|
|
28
|
+
@Option({displayName: "Package name", description: "A package that re-exports Material UI, when the components are not imported from Material UI itself.", example: "@org/ui", required: false})
|
|
29
|
+
packageName?: string;
|
|
30
|
+
|
|
31
|
+
constructor(options?: { componentName?: string; propName?: string; value?: string; packageName?: string }) {
|
|
32
|
+
super(options);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
async editor(): Promise<TreeVisitor<any, ExecutionContext>> {
|
|
36
|
+
const {propName, value} = this;
|
|
37
|
+
return muiTagEditor(this.componentName, this.packageName, async tag => {
|
|
38
|
+
const attribute = findAttribute(tag, propName);
|
|
39
|
+
if (!attribute || (value !== undefined && attributeString(attribute) !== value)) return tag;
|
|
40
|
+
return replaceAttribute(tag, attribute, undefined);
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import {ExecutionContext, Recipe, TreeVisitor, check, not} from "@openrewrite/rewrite";
|
|
2
|
+
import {JavaScriptVisitor, JS, JSX, isVendoredOrBundled, maybeRemoveImport} from "@openrewrite/rewrite/javascript";
|
|
3
|
+
import {isIdentifier, J} from "@openrewrite/rewrite/java";
|
|
4
|
+
import {emptySpace} from "@openrewrite/rewrite/java";
|
|
5
|
+
import {MUI_PACKAGES, muiComponents, tagComponent} from "./mui-components";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Removes `RootRef`, which v5 removed, leaving a fragment around what it wrapped. The `rootRef` it set has to be
|
|
9
|
+
* passed to the child as `ref` by hand.
|
|
10
|
+
*
|
|
11
|
+
* Before: `<RootRef rootRef={ref}><div /></RootRef>`
|
|
12
|
+
* After: `<><div /></>`
|
|
13
|
+
*
|
|
14
|
+
* @see https://mui.com/material-ui/migration/v5-component-changes/#rootref
|
|
15
|
+
*/
|
|
16
|
+
export class RemoveRootRef extends Recipe {
|
|
17
|
+
readonly name = "org.openrewrite.react.mui.remove-root-ref";
|
|
18
|
+
readonly displayName: string = "Remove `RootRef`";
|
|
19
|
+
readonly description: string = "Replaces `<RootRef rootRef={ref}>...</RootRef>` by a fragment and removes the import, since `RootRef` is gone in v5. The `rootRef` has to become the `ref` of the child by hand.";
|
|
20
|
+
|
|
21
|
+
async editor(): Promise<TreeVisitor<any, ExecutionContext>> {
|
|
22
|
+
return check(not(isVendoredOrBundled()), new class extends JavaScriptVisitor<ExecutionContext> {
|
|
23
|
+
private components = new Map<string, string>();
|
|
24
|
+
|
|
25
|
+
override async visitJsCompilationUnit(cu: JS.CompilationUnit, ctx: ExecutionContext): Promise<J | undefined> {
|
|
26
|
+
this.components = muiComponents(cu, MUI_PACKAGES);
|
|
27
|
+
return super.visitJsCompilationUnit(cu, ctx);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
override async visitJsxTag(tag: JSX.Tag, ctx: ExecutionContext): Promise<J | undefined> {
|
|
31
|
+
const visited = await super.visitJsxTag(tag, ctx) as JSX.Tag;
|
|
32
|
+
if (tagComponent(visited, this.components) !== "RootRef" || !visited.children) return visited;
|
|
33
|
+
const local = (visited.openName.element as J.Identifier).simpleName;
|
|
34
|
+
for (const module of ["@material-ui/core", "@material-ui/core/RootRef", "@mui/material"]) maybeRemoveImport(this, module, module.endsWith("RootRef") ? "default" : "RootRef");
|
|
35
|
+
const empty = {kind: J.Kind.Empty, id: visited.id, prefix: emptySpace, markers: visited.markers} as J.Empty;
|
|
36
|
+
return {...visited, openName: {...visited.openName, element: empty}, afterName: emptySpace, attributes: [],
|
|
37
|
+
closingName: {...visited.closingName, element: empty}} as JSX.Tag;
|
|
38
|
+
}
|
|
39
|
+
}());
|
|
40
|
+
}
|
|
41
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import {ExecutionContext, Recipe, TreeVisitor, check, not} from "@openrewrite/rewrite";
|
|
2
|
+
import {JavaScriptVisitor, JS, isVendoredOrBundled} from "@openrewrite/rewrite/javascript";
|
|
3
|
+
import {J} from "@openrewrite/rewrite/java";
|
|
4
|
+
import {stringValue} from "./theme-access";
|
|
5
|
+
|
|
6
|
+
const isSpacingCall = (e: J | undefined): boolean => e?.kind === J.Kind.MethodInvocation && (e as J.MethodInvocation).name.simpleName === "spacing";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Removes the `px` that is added after `theme.spacing()`, which returns a string with its unit since v5.
|
|
10
|
+
*
|
|
11
|
+
* Before: `` `${theme.spacing(2)}px` ``, `theme.spacing(2) + 'px'`
|
|
12
|
+
* After: `` `${theme.spacing(2)}` ``, `theme.spacing(2)`
|
|
13
|
+
*
|
|
14
|
+
* @see https://mui.com/material-ui/migration/v5-component-changes/#theme
|
|
15
|
+
*/
|
|
16
|
+
export class RemoveSpacingPxSuffix extends Recipe {
|
|
17
|
+
readonly name = "org.openrewrite.react.mui.remove-spacing-px-suffix";
|
|
18
|
+
readonly displayName: string = "Remove the `px` suffix after `theme.spacing()`";
|
|
19
|
+
readonly description: string = "Removes a `px` that follows a `theme.spacing()` call in a template literal, or is added to it as a string, since the call returns the unit.";
|
|
20
|
+
|
|
21
|
+
async editor(): Promise<TreeVisitor<any, ExecutionContext>> {
|
|
22
|
+
return check(not(isVendoredOrBundled()), new class extends JavaScriptVisitor<ExecutionContext> {
|
|
23
|
+
override async visitTemplateExpression(template: JS.TemplateExpression, ctx: ExecutionContext): Promise<J | undefined> {
|
|
24
|
+
const visited = await super.visitTemplateExpression(template, ctx) as JS.TemplateExpression;
|
|
25
|
+
let changed = false;
|
|
26
|
+
const spans = visited.spans.map(span => {
|
|
27
|
+
const tail = span.element.tail;
|
|
28
|
+
// The source of a tail starts at the `}` that closes the substitution.
|
|
29
|
+
if (!isSpacingCall(span.element.expression) || typeof tail.value !== "string" || !tail.value.startsWith("px") || !tail.valueSource?.startsWith("}px")) return span;
|
|
30
|
+
changed = true;
|
|
31
|
+
return {...span, element: {...span.element, tail: {...tail, value: tail.value.slice(2), valueSource: `}${tail.valueSource.slice(3)}`}}};
|
|
32
|
+
});
|
|
33
|
+
return changed ? {...visited, spans} as JS.TemplateExpression : visited;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
override async visitBinary(binary: J.Binary, ctx: ExecutionContext): Promise<J | undefined> {
|
|
37
|
+
const visited = await super.visitBinary(binary, ctx) as J.Binary;
|
|
38
|
+
if ((visited.operator.element as string) !== "Addition" || !isSpacingCall(visited.left) || stringValue(visited.right) !== "px") return visited;
|
|
39
|
+
return {...visited.left, prefix: visited.prefix} as J;
|
|
40
|
+
}
|
|
41
|
+
}());
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import {ExecutionContext, Option, Recipe, TreeVisitor} from "@openrewrite/rewrite";
|
|
2
|
+
import {J} from "@openrewrite/rewrite/java";
|
|
3
|
+
import {findAttribute} from "./jsx-attributes";
|
|
4
|
+
import {attributeExpression, newAttribute, replaceAttribute} from "./jsx-attributes";
|
|
5
|
+
import {muiTagEditor} from "./mui-tag-visitor";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Renames a prop of a Material UI component.
|
|
9
|
+
*
|
|
10
|
+
* Before: `<Autocomplete closeIcon={icon} />`
|
|
11
|
+
* After: `<Autocomplete clearIcon={icon} />`
|
|
12
|
+
*
|
|
13
|
+
* A tag that already sets the new name is left alone, since renaming would set the prop twice.
|
|
14
|
+
*
|
|
15
|
+
* @see https://mui.com/material-ui/migration/migration-v4/
|
|
16
|
+
*/
|
|
17
|
+
export class RenameComponentProp extends Recipe {
|
|
18
|
+
readonly name = "org.openrewrite.react.mui.rename-component-prop";
|
|
19
|
+
readonly displayName: string = "Rename a Material UI component prop";
|
|
20
|
+
readonly description: string = "Renames a prop of a Material UI component, found through the imports of the file. Tags that already set the new prop are left alone.";
|
|
21
|
+
|
|
22
|
+
@Option({displayName: "Component name", description: "The Material UI component, such as `Autocomplete`.", example: "Autocomplete"})
|
|
23
|
+
componentName!: string;
|
|
24
|
+
|
|
25
|
+
@Option({displayName: "Old prop name", description: "The prop to rename.", example: "closeIcon"})
|
|
26
|
+
oldPropName!: string;
|
|
27
|
+
|
|
28
|
+
@Option({displayName: "New prop name", description: "The name the prop gets.", example: "clearIcon"})
|
|
29
|
+
newPropName!: string;
|
|
30
|
+
|
|
31
|
+
@Option({displayName: "New prop value", description: "Set the renamed prop to this string. Only a prop that is set without a value, or to `true`, is renamed then.", example: "horizontal", required: false})
|
|
32
|
+
newPropValue?: string;
|
|
33
|
+
|
|
34
|
+
@Option({displayName: "Package name", description: "A package that re-exports Material UI, when the components are not imported from Material UI itself.", example: "@org/ui", required: false})
|
|
35
|
+
packageName?: string;
|
|
36
|
+
|
|
37
|
+
constructor(options?: { componentName?: string; oldPropName?: string; newPropName?: string; newPropValue?: string; packageName?: string }) {
|
|
38
|
+
super(options);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
async editor(): Promise<TreeVisitor<any, ExecutionContext>> {
|
|
42
|
+
const {oldPropName, newPropName, newPropValue} = this;
|
|
43
|
+
return muiTagEditor(this.componentName, this.packageName, async (tag, cursor) => {
|
|
44
|
+
const attribute = findAttribute(tag, oldPropName);
|
|
45
|
+
if (!attribute || findAttribute(tag, newPropName)) return tag;
|
|
46
|
+
if (newPropValue !== undefined) {
|
|
47
|
+
const value = attributeExpression(attribute);
|
|
48
|
+
if (attribute.value && !(value.kind === J.Kind.Literal && (value as J.Literal).value === true)) return tag;
|
|
49
|
+
return replaceAttribute(tag, attribute, await newAttribute(cursor, tag, newPropName, JSON.stringify(newPropValue)));
|
|
50
|
+
}
|
|
51
|
+
const renamed = {...attribute, key: {...(attribute.key as J.Identifier), simpleName: newPropName}};
|
|
52
|
+
return {...tag, attributes: tag.attributes.map(a => a.element === attribute ? {...a, element: renamed} : a)} as typeof tag;
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import {ExecutionContext, Recipe, TreeVisitor} from "@openrewrite/rewrite";
|
|
2
|
+
import {literalEditor} from "./replace-in-string-literals";
|
|
3
|
+
|
|
4
|
+
const JOY_COMPONENTS = new Set(["Alert", "AspectRatio", "Autocomplete", "AutocompleteListbox", "AutocompleteOption", "Avatar", "AvatarGroup", "Badge",
|
|
5
|
+
"Box", "Breadcrumbs", "Button", "Card", "CardContent", "CardCover", "CardOverflow", "Checkbox", "Chip", "ChipDelete", "CircularProgress",
|
|
6
|
+
"Container", "CssBaseline", "Divider", "FormControl", "FormHelperText", "FormLabel", "Grid", "IconButton", "Input", "LinearProgress", "Link",
|
|
7
|
+
"List", "ListDivider", "ListItem", "ListItemButton", "ListItemContent", "ListItemDecorator", "ListSubheader", "Menu", "MenuItem", "MenuList",
|
|
8
|
+
"Modal", "ModalClose", "ModalDialog", "ModalOverflow", "Option", "Radio", "RadioGroup", "Select", "Sheet", "Skeleton", "Slider", "Stack",
|
|
9
|
+
"SvgIcon", "Switch", "Tab", "Table", "TableCell", "TabList", "TabPanel", "Tabs", "Textarea", "TextField", "Tooltip", "Typography"]);
|
|
10
|
+
|
|
11
|
+
const capitalize = (s: string) => s.charAt(0).toUpperCase() + s.slice(1);
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Renames the CSS variables of Joy UI components to the naming standard of v5: `--Component-slot-property` becomes
|
|
15
|
+
* `--Component-slotProperty`.
|
|
16
|
+
*
|
|
17
|
+
* Before: `'--List-item-paddingY'`
|
|
18
|
+
* After: `'--ListItem-paddingY'`
|
|
19
|
+
*
|
|
20
|
+
* @see https://mui.com/joy-ui/migration/migrating-default-theme/
|
|
21
|
+
*/
|
|
22
|
+
export class RenameJoyCssVariables extends Recipe {
|
|
23
|
+
readonly name = "org.openrewrite.react.mui.rename-joy-css-variables";
|
|
24
|
+
readonly displayName: string = "Rename Joy UI CSS variables";
|
|
25
|
+
readonly description: string = "Renames the CSS variables of Joy UI components in string literals to the new naming standard, and `--internal-` and `--private_` to `--unstable_`.";
|
|
26
|
+
|
|
27
|
+
async editor(): Promise<TreeVisitor<any, ExecutionContext>> {
|
|
28
|
+
return literalEditor(text => text
|
|
29
|
+
.replace(/--([a-zA-Z]+)([-_])([a-zA-Z]+)-([a-zA-Z]+)/gm, (matched, c1: string, c2: string, c3: string, c4: string) => {
|
|
30
|
+
if (!JOY_COMPONENTS.has(c1) && !["internal", "unstable", "private"].includes(c1)) return matched;
|
|
31
|
+
if (c1 === "List" && ["divider", "item"].includes(c3)) return `--${c1}${capitalize(c3)}-${c4}`;
|
|
32
|
+
if (c1 === "List" && c3 === "decorator") return `--${c1}Item${capitalize(c3)}-${c4}`;
|
|
33
|
+
if (!JOY_COMPONENTS.has(c3)) return `--${c1}${c2}${c3}${capitalize(c4)}`;
|
|
34
|
+
return matched;
|
|
35
|
+
})
|
|
36
|
+
.replace(/--List-decoratorSize/gm, "--ListItemDecorator-size")
|
|
37
|
+
.replace(/--List-decoratorColor/gm, "--ListItemDecorator-color")
|
|
38
|
+
.replace(/--internal-/gm, "--unstable_")
|
|
39
|
+
.replace(/--private_/gm, "--unstable_"));
|
|
40
|
+
}
|
|
41
|
+
}
|