@openrewrite/recipes-react 0.21.0 → 0.23.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/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +27 -0
- package/dist/index.js.map +1 -1
- package/dist/migration/change-component-prop-value.d.ts.map +1 -1
- package/dist/migration/change-component-prop-value.js +14 -4
- package/dist/migration/change-component-prop-value.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/react19/use-context-hook.d.ts.map +1 -1
- package/dist/react19/use-context-hook.js +2 -0
- package/dist/react19/use-context-hook.js.map +1 -1
- package/dist/search/find-enzyme-usage.d.ts +4 -0
- package/dist/search/find-enzyme-usage.d.ts.map +1 -1
- package/dist/search/find-enzyme-usage.js +91 -7
- package/dist/search/find-enzyme-usage.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-render-blockers.d.ts +13 -4
- package/dist/testing/enzyme-render-blockers.d.ts.map +1 -1
- package/dist/testing/enzyme-render-blockers.js +162 -6
- package/dist/testing/enzyme-render-blockers.js.map +1 -1
- package/dist/testing/enzyme-to-react-testing-library.d.ts +9 -2
- package/dist/testing/enzyme-to-react-testing-library.d.ts.map +1 -1
- package/dist/testing/enzyme-to-react-testing-library.js +163 -16
- package/dist/testing/enzyme-to-react-testing-library.js.map +1 -1
- package/dist/testing/enzyme-usage.d.ts +7 -0
- package/dist/testing/enzyme-usage.d.ts.map +1 -1
- package/dist/testing/enzyme-usage.js +8 -1
- package/dist/testing/enzyme-usage.js.map +1 -1
- package/dist/testing/enzyme-wrapper-scope.d.ts +8 -5
- package/dist/testing/enzyme-wrapper-scope.d.ts.map +1 -1
- package/dist/testing/enzyme-wrapper-scope.js +16 -4
- package/dist/testing/enzyme-wrapper-scope.js.map +1 -1
- package/dist/zustand/redux-stores.d.ts +2 -0
- package/dist/zustand/redux-stores.d.ts.map +1 -1
- package/dist/zustand/redux-stores.js +120 -6
- package/dist/zustand/redux-stores.js.map +1 -1
- package/dist/zustand/redux-to-zustand.d.ts.map +1 -1
- package/dist/zustand/redux-to-zustand.js +10 -4
- package/dist/zustand/redux-to-zustand.js.map +1 -1
- package/dist/zustand/zustand-plan.d.ts +7 -0
- package/dist/zustand/zustand-plan.d.ts.map +1 -1
- package/dist/zustand/zustand-plan.js +141 -21
- package/dist/zustand/zustand-plan.js.map +1 -1
- package/package.json +2 -2
- package/src/index.ts +10 -0
- package/src/migration/change-component-prop-value.ts +19 -8
- 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/react19/use-context-hook.ts +2 -0
- package/src/search/find-enzyme-usage.ts +80 -7
- package/src/testing/enzyme-host-components.ts +110 -0
- package/src/testing/enzyme-render-blockers.ts +174 -9
- package/src/testing/enzyme-to-react-testing-library.ts +159 -25
- package/src/testing/enzyme-usage.ts +13 -0
- package/src/testing/enzyme-wrapper-scope.ts +27 -6
- package/src/zustand/redux-stores.ts +119 -5
- package/src/zustand/redux-to-zustand.ts +12 -5
- package/src/zustand/zustand-plan.ts +159 -18
|
@@ -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
|
+
}
|