@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,71 @@
|
|
|
1
|
+
import {ExecutionContext, Option, Recipe, TreeVisitor, check, not} from "@openrewrite/rewrite";
|
|
2
|
+
import {JavaScriptVisitor, JS, isVendoredOrBundled} from "@openrewrite/rewrite/javascript";
|
|
3
|
+
import {J} from "@openrewrite/rewrite/java";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Rewrites the module a file imports from, re-exports from or `require`s, with a regular expression.
|
|
7
|
+
*
|
|
8
|
+
* Before: `import Button from '@material-ui/core/Button';`
|
|
9
|
+
* After: `import Button from '@mui/material/Button';`
|
|
10
|
+
*
|
|
11
|
+
* The names that are imported are untouched, so it is for moves of a whole module path. To move one export, use
|
|
12
|
+
* `ChangeImport`.
|
|
13
|
+
*
|
|
14
|
+
* @see https://mui.com/material-ui/migration/migration-v4/
|
|
15
|
+
*/
|
|
16
|
+
export class ChangeModuleSpecifier extends Recipe {
|
|
17
|
+
readonly name = "org.openrewrite.react.mui.change-module-specifier";
|
|
18
|
+
readonly displayName: string = "Change a module specifier";
|
|
19
|
+
readonly description: string = "Rewrites the module of `import`, `export ... from` and `require` with a regular expression that must match the whole module name. Names in the replacement can refer to groups, as `$1`.";
|
|
20
|
+
|
|
21
|
+
@Option({displayName: "Old pattern", description: "A regular expression that matches the whole module name.", example: "@material-ui/core/(.*)"})
|
|
22
|
+
oldPattern!: string;
|
|
23
|
+
|
|
24
|
+
@Option({displayName: "New module", description: "The replacement, which can use `$1` and so on.", example: "@mui/material/$1"})
|
|
25
|
+
newModule!: string;
|
|
26
|
+
|
|
27
|
+
constructor(options?: { oldPattern?: string; newModule?: string }) {
|
|
28
|
+
super(options);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
async editor(): Promise<TreeVisitor<any, ExecutionContext>> {
|
|
32
|
+
const pattern = new RegExp(`^(?:${this.oldPattern})$`);
|
|
33
|
+
const replacement = this.newModule;
|
|
34
|
+
const change = (specifier: J | undefined): J | undefined => {
|
|
35
|
+
if (specifier?.kind !== J.Kind.Literal) return specifier;
|
|
36
|
+
const literal = specifier as J.Literal;
|
|
37
|
+
if (typeof literal.value !== "string" || !literal.valueSource || !pattern.test(literal.value)) return specifier;
|
|
38
|
+
const module = literal.value.replace(pattern, replacement);
|
|
39
|
+
if (module === literal.value) return specifier;
|
|
40
|
+
const quote = literal.valueSource.charAt(0);
|
|
41
|
+
return {...literal, value: module, valueSource: `${quote}${module}${quote}`} as J.Literal;
|
|
42
|
+
};
|
|
43
|
+
const leftPadded = (padded?: J.LeftPadded<any>) => {
|
|
44
|
+
const element = padded && change(padded.element);
|
|
45
|
+
return padded && element !== padded.element ? {...padded, element} : padded;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
return check(not(isVendoredOrBundled()), new class extends JavaScriptVisitor<ExecutionContext> {
|
|
49
|
+
override async visitImportDeclaration(declaration: JS.Import, ctx: ExecutionContext): Promise<J | undefined> {
|
|
50
|
+
const visited = await super.visitImportDeclaration(declaration, ctx) as JS.Import;
|
|
51
|
+
const moduleSpecifier = leftPadded(visited.moduleSpecifier);
|
|
52
|
+
return moduleSpecifier === visited.moduleSpecifier ? visited : {...visited, moduleSpecifier} as JS.Import;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
override async visitExportDeclaration(declaration: JS.ExportDeclaration, ctx: ExecutionContext): Promise<J | undefined> {
|
|
56
|
+
const visited = await super.visitExportDeclaration(declaration, ctx) as JS.ExportDeclaration;
|
|
57
|
+
const moduleSpecifier = leftPadded(visited.moduleSpecifier);
|
|
58
|
+
return moduleSpecifier === visited.moduleSpecifier ? visited : {...visited, moduleSpecifier} as JS.ExportDeclaration;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
override async visitMethodInvocation(invocation: J.MethodInvocation, ctx: ExecutionContext): Promise<J | undefined> {
|
|
62
|
+
const visited = await super.visitMethodInvocation(invocation, ctx) as J.MethodInvocation;
|
|
63
|
+
if (visited.select || visited.name.simpleName !== "require" || visited.arguments.elements.length !== 1) return visited;
|
|
64
|
+
const [argument] = visited.arguments.elements;
|
|
65
|
+
const changed = change(argument.element);
|
|
66
|
+
return changed === argument.element ? visited
|
|
67
|
+
: {...visited, arguments: {...visited.arguments, elements: [{...argument, element: changed}]}} as J.MethodInvocation;
|
|
68
|
+
}
|
|
69
|
+
}());
|
|
70
|
+
}
|
|
71
|
+
}
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import {Cursor, ExecutionContext, Option, Recipe, TreeVisitor, check, not} from "@openrewrite/rewrite";
|
|
2
|
+
import {JavaScriptVisitor, JS, isVendoredOrBundled, moduleNameOf, raw, template} from "@openrewrite/rewrite/javascript";
|
|
3
|
+
import {isIdentifier, J} from "@openrewrite/rewrite/java";
|
|
4
|
+
import {escapeRegExp} from "./mui-components";
|
|
5
|
+
|
|
6
|
+
/** What `@mui/material/styles` exports, which `@mui/material/<Name>` has no module for. */
|
|
7
|
+
const STYLES = new Set(["StyledEngineProvider", "THEME_ID", "ThemeProvider", "adaptV4Theme", "alpha", "createColorScheme", "createStyles",
|
|
8
|
+
"createTheme", "createMuiTheme", "createTransitions", "css", "darken", "decomposeColor", "duration", "easing", "emphasize", "experimental_extendTheme",
|
|
9
|
+
"experimental_sx", "extendTheme", "getContrastRatio", "getLuminance", "getOverlayAlpha", "hexToRgb", "hslToRgb", "keyframes", "lighten",
|
|
10
|
+
"makeStyles", "recomposeColor", "responsiveFontSizes", "rgbToHex", "shouldSkipGeneratingVar", "styled", "useTheme", "useThemeProps",
|
|
11
|
+
"withStyles", "withTheme", "CssVarsProvider", "Experimental_CssVarsProvider", "getInitColorSchemeScript", "useColorScheme"]);
|
|
12
|
+
|
|
13
|
+
interface Specifier {
|
|
14
|
+
imported: string;
|
|
15
|
+
local: string;
|
|
16
|
+
typeOnly: boolean;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
interface Parsed {
|
|
20
|
+
statement: JS.Import;
|
|
21
|
+
module: string;
|
|
22
|
+
quote: string;
|
|
23
|
+
defaultLocal?: string;
|
|
24
|
+
named: Specifier[];
|
|
25
|
+
other: boolean;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function parse(statement: JS.Import): Parsed | undefined {
|
|
29
|
+
const specifier = statement.moduleSpecifier?.element;
|
|
30
|
+
const clause = statement.importClause;
|
|
31
|
+
if (specifier?.kind !== J.Kind.Literal || !clause || clause.typeOnly) return undefined;
|
|
32
|
+
const literal = specifier as J.Literal;
|
|
33
|
+
const parsed: Parsed = {statement, module: moduleNameOf(literal), quote: literal.valueSource?.charAt(0) ?? "'", defaultLocal: clause.name?.element.simpleName, named: [], other: false};
|
|
34
|
+
const bindings = clause.namedBindings;
|
|
35
|
+
if (bindings && bindings.kind !== JS.Kind.NamedImports) parsed.other = true;
|
|
36
|
+
if (bindings?.kind === JS.Kind.NamedImports) {
|
|
37
|
+
for (const {element} of (bindings as JS.NamedImports).elements.elements) {
|
|
38
|
+
if (element.kind !== JS.Kind.ImportSpecifier) { parsed.other = true; continue; }
|
|
39
|
+
const named = element.specifier;
|
|
40
|
+
const typeOnly = element.importType.element;
|
|
41
|
+
if (isIdentifier(named)) parsed.named.push({imported: named.simpleName, local: named.simpleName, typeOnly});
|
|
42
|
+
else if (named.kind === JS.Kind.Alias && isIdentifier(named.alias)) {
|
|
43
|
+
const property = named.propertyName.element;
|
|
44
|
+
parsed.named.push({imported: isIdentifier(property) ? property.simpleName : String((property as J.Literal).value), local: named.alias.simpleName, typeOnly});
|
|
45
|
+
} else parsed.other = true;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return parsed;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const spec = (s: Specifier) => `${s.typeOnly ? "type " : ""}${s.imported === s.local ? s.local : `${s.imported} as ${s.local}`}`;
|
|
52
|
+
|
|
53
|
+
interface Target {
|
|
54
|
+
module: string;
|
|
55
|
+
defaultLocal?: string;
|
|
56
|
+
named: Specifier[];
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const importSource = (t: Target, quote: string) => {
|
|
60
|
+
const parts = [t.defaultLocal, t.named.length ? `{ ${t.named.map(spec).join(", ")} }` : undefined].filter(Boolean).join(", ");
|
|
61
|
+
return `import ${parts} from ${quote}${t.module}${quote};`;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
async function build(cursor: Cursor, like: JS.Import, source: string): Promise<JS.Import> {
|
|
65
|
+
return await template`${raw(source)}`.apply(like, cursor) as JS.Import;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
type Mode = "path" | "top-level" | "optimal";
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Rewrites how a file imports Material UI: from the package root to one module per component (`path`), from one module
|
|
72
|
+
* per component to the package root (`top-level`), or from private paths deeper than the package's public ones to them
|
|
73
|
+
* (`optimal`).
|
|
74
|
+
*
|
|
75
|
+
* `path`: `import { List } from '@mui/material';` becomes `import List from '@mui/material/List';`.
|
|
76
|
+
* `top-level`: `import List from '@mui/material/List';` becomes `import { List } from '@mui/material';`.
|
|
77
|
+
* `optimal`: `import red from '@material-ui/core/colors/red';` becomes `import { red } from '@material-ui/core/colors';`.
|
|
78
|
+
*
|
|
79
|
+
* The package exports are not read, so a default import whose module name is not an export of the package root is
|
|
80
|
+
* moved all the same in `top-level`; run it on code that imports Material UI's own components.
|
|
81
|
+
*
|
|
82
|
+
* @see https://mui.com/material-ui/guides/minimizing-bundle-size/
|
|
83
|
+
*/
|
|
84
|
+
export class ConvertMuiImports extends Recipe {
|
|
85
|
+
readonly name = "org.openrewrite.react.mui.convert-mui-imports";
|
|
86
|
+
readonly displayName: string = "Convert how Material UI is imported";
|
|
87
|
+
readonly description: string = "Rewrites imports of a Material UI package between the package root and one module per component, or from private deep paths to public ones. Type-only imports and namespace imports are left alone.";
|
|
88
|
+
|
|
89
|
+
@Option({displayName: "Mode", description: "`path` for one module per component, `top-level` for the package root, `optimal` for public module paths.", example: "path", valid: ["path", "top-level", "optimal"]})
|
|
90
|
+
mode!: string;
|
|
91
|
+
|
|
92
|
+
@Option({displayName: "Package names", description: "The packages to rewrite, separated by commas.", example: "@mui/material,@mui/icons-material"})
|
|
93
|
+
packageNames!: string;
|
|
94
|
+
|
|
95
|
+
constructor(options?: { mode?: string; packageNames?: string }) {
|
|
96
|
+
super(options);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
async editor(): Promise<TreeVisitor<any, ExecutionContext>> {
|
|
100
|
+
const mode = this.mode as Mode;
|
|
101
|
+
const packages = this.packageNames.split(",").map(s => s.trim()).filter(Boolean);
|
|
102
|
+
const alternatives = packages.map(escapeRegExp).join("|");
|
|
103
|
+
const deep = new RegExp(`^(${alternatives})/((?:[^/]+/)*[^/]+)$`);
|
|
104
|
+
const optimal = new RegExp(`^(${alternatives})/([^/]+)/([^/]+)$`);
|
|
105
|
+
|
|
106
|
+
/** The imports that replace one import, or undefined when this one is not for rewriting. */
|
|
107
|
+
const replace = (p: Parsed, firstTarget: Map<string, number>, index: number): Target[] | undefined => {
|
|
108
|
+
if (p.other || p.module.includes("internal/")) return undefined;
|
|
109
|
+
const targets = new Map<string, Target>();
|
|
110
|
+
const target = (module: string) => targets.get(module) ?? targets.set(module, {module, named: []}).get(module)!;
|
|
111
|
+
const kept: Specifier[] = [];
|
|
112
|
+
if (mode === "path") {
|
|
113
|
+
const pkg = packages.find(x => x === p.module);
|
|
114
|
+
if (!pkg || p.defaultLocal || !p.named.length) return undefined;
|
|
115
|
+
const material = pkg.endsWith("/material") || pkg.endsWith("/core");
|
|
116
|
+
for (const s of p.named) {
|
|
117
|
+
if (s.typeOnly) kept.push(s);
|
|
118
|
+
else if (material && STYLES.has(s.imported)) target(`${pkg}/styles`).named.push(s);
|
|
119
|
+
else if (material && s.imported.endsWith("Classes") && s.imported !== "Classes") {
|
|
120
|
+
const base = s.imported.replace(/Classes$/, "");
|
|
121
|
+
target(`${pkg}/${base.charAt(0).toUpperCase()}${base.slice(1)}`).named.push(s);
|
|
122
|
+
} else target(`${pkg}/${s.imported}`).defaultLocal = s.local;
|
|
123
|
+
}
|
|
124
|
+
} else if (mode === "top-level") {
|
|
125
|
+
const match = deep.exec(p.module);
|
|
126
|
+
if (!match) return undefined;
|
|
127
|
+
const moduleName = match[2].split("/").pop()!;
|
|
128
|
+
if (p.defaultLocal) {
|
|
129
|
+
if (!/^[A-Z]/.test(moduleName)) return undefined;
|
|
130
|
+
target(match[1]).named.push({imported: moduleName, local: p.defaultLocal, typeOnly: false});
|
|
131
|
+
}
|
|
132
|
+
for (const s of p.named) target(match[1]).named.push(s);
|
|
133
|
+
} else {
|
|
134
|
+
const match = optimal.exec(p.module);
|
|
135
|
+
if (!match) return undefined;
|
|
136
|
+
if (p.defaultLocal) target(`${match[1]}/${match[2]}`).named.push({imported: match[3], local: p.defaultLocal, typeOnly: false});
|
|
137
|
+
for (const s of p.named) target(`${match[1]}/${match[2]}`).named.push(s);
|
|
138
|
+
}
|
|
139
|
+
if (!targets.size) return undefined;
|
|
140
|
+
const result = [...targets.values()];
|
|
141
|
+
if (kept.length) result.push({module: p.module, named: kept});
|
|
142
|
+
// Several imports that land in one module join into the first of them.
|
|
143
|
+
for (const t of result) if (!firstTarget.has(t.module)) firstTarget.set(t.module, index);
|
|
144
|
+
return result;
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
return check(not(isVendoredOrBundled()), new class extends JavaScriptVisitor<ExecutionContext> {
|
|
148
|
+
override async visitJsCompilationUnit(cu: JS.CompilationUnit, ctx: ExecutionContext): Promise<J | undefined> {
|
|
149
|
+
const result = await super.visitJsCompilationUnit(cu, ctx) as JS.CompilationUnit;
|
|
150
|
+
const parsed = result.statements.map(s => s.element.kind === JS.Kind.Import ? parse(s.element as JS.Import) : undefined);
|
|
151
|
+
const firstTarget = new Map<string, number>();
|
|
152
|
+
const replacements = parsed.map((p, i) => p && replace(p, firstTarget, i));
|
|
153
|
+
if (!replacements.some(Boolean)) return result;
|
|
154
|
+
|
|
155
|
+
// Merge targets landing in the same module, at the statement of the first one.
|
|
156
|
+
const merged = new Map<string, Target>();
|
|
157
|
+
replacements.forEach((targets, i) => targets?.forEach(t => {
|
|
158
|
+
const existing = merged.get(t.module);
|
|
159
|
+
if (!existing) merged.set(t.module, {...t, named: [...t.named]});
|
|
160
|
+
else {
|
|
161
|
+
existing.defaultLocal ??= t.defaultLocal;
|
|
162
|
+
existing.named.push(...t.named.filter(n => !existing.named.some(e => e.imported === n.imported && e.local === n.local)));
|
|
163
|
+
}
|
|
164
|
+
}));
|
|
165
|
+
const statements: typeof result.statements = [];
|
|
166
|
+
for (let i = 0; i < result.statements.length; i++) {
|
|
167
|
+
const statement = result.statements[i];
|
|
168
|
+
const p = parsed[i];
|
|
169
|
+
const targets = replacements[i];
|
|
170
|
+
if (!p || !targets) { statements.push(statement); continue; }
|
|
171
|
+
let first = true;
|
|
172
|
+
for (const t of targets) {
|
|
173
|
+
if (firstTarget.get(t.module) !== i) continue;
|
|
174
|
+
const node = await build(new Cursor(statement.element, this.cursor), p.statement, importSource(merged.get(t.module)!, p.quote));
|
|
175
|
+
const prefix = first ? p.statement.prefix : {...p.statement.prefix, comments: [], whitespace: "\n"};
|
|
176
|
+
statements.push({...statement, element: {...node, prefix}});
|
|
177
|
+
first = false;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
return {...result, statements} as JS.CompilationUnit;
|
|
181
|
+
}
|
|
182
|
+
}());
|
|
183
|
+
}
|
|
184
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import {ExecutionContext, Option, Recipe, TreeVisitor} from "@openrewrite/rewrite";
|
|
2
|
+
import {J} from "@openrewrite/rewrite/java";
|
|
3
|
+
import {JS, JSX, raw, template} from "@openrewrite/rewrite/javascript";
|
|
4
|
+
import {
|
|
5
|
+
appendAttribute, appendProperty, attributeExpression, attributeObject, findAttribute, findProperty, isObjectLiteral, newAttribute, newPropertyFromSource,
|
|
6
|
+
replaceAttribute, spaced
|
|
7
|
+
} from "./jsx-attributes";
|
|
8
|
+
import {muiTagEditor} from "./mui-tag-visitor";
|
|
9
|
+
|
|
10
|
+
/** The attribute with the one-line object literal of its value, or of the true branch of a conditional, spaced inside its braces. */
|
|
11
|
+
function withSpacedObject(attribute: JSX.Attribute): JSX.Attribute {
|
|
12
|
+
const embedded = attribute.value!.element as JSX.EmbeddedExpression;
|
|
13
|
+
const inner = embedded.expression.element;
|
|
14
|
+
let expression: J = inner;
|
|
15
|
+
if (isObjectLiteral(inner)) expression = spaced(inner);
|
|
16
|
+
else if (inner.kind === J.Kind.Ternary) {
|
|
17
|
+
const ternary = inner as J.Ternary;
|
|
18
|
+
if (isObjectLiteral(ternary.truePart.element)) expression = {...ternary, truePart: {...ternary.truePart, element: spaced(ternary.truePart.element)}} as J.Ternary;
|
|
19
|
+
}
|
|
20
|
+
return {...attribute, value: {...attribute.value!, element: {...embedded, expression: {...embedded.expression, element: expression}}}} as JSX.Attribute;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Replaces a prop that the component no longer has with the style it used to apply, in the `sx` prop.
|
|
25
|
+
*
|
|
26
|
+
* Before: `<Divider light />`, `<Typography paragraph />`
|
|
27
|
+
* After: `<Divider sx={{ opacity: '0.6' }} />`, `<Typography sx={{ marginBottom: '16px' }} />`
|
|
28
|
+
*
|
|
29
|
+
* A prop set to `false` is only removed. A prop set to another expression becomes a conditional style when the
|
|
30
|
+
* element has no `sx` yet. An existing `sx` that is not an object literal is left alone, with the prop in place.
|
|
31
|
+
*
|
|
32
|
+
* @see https://mui.com/material-ui/migration/migrating-from-deprecated-apis/
|
|
33
|
+
*/
|
|
34
|
+
export class ConvertPropToSx extends Recipe {
|
|
35
|
+
readonly name = "org.openrewrite.react.mui.convert-prop-to-sx";
|
|
36
|
+
readonly displayName: string = "Replace a component prop with an `sx` style";
|
|
37
|
+
readonly description: string = "Replaces a prop of a Material UI component with a property of its `sx` prop, such as `<Divider light />` with `<Divider sx={{ opacity: '0.6' }} />`. The style is not added when the `sx` already sets it.";
|
|
38
|
+
|
|
39
|
+
@Option({displayName: "Component name", description: "The Material UI component.", example: "Divider"})
|
|
40
|
+
componentName!: string;
|
|
41
|
+
|
|
42
|
+
@Option({displayName: "Prop name", description: "The prop to replace.", example: "light"})
|
|
43
|
+
propName!: string;
|
|
44
|
+
|
|
45
|
+
@Option({displayName: "Style property", description: "The `sx` property that replaces it.", example: "opacity"})
|
|
46
|
+
sxProperty!: string;
|
|
47
|
+
|
|
48
|
+
@Option({displayName: "Style value", description: "The value of the property, as source code.", example: "'0.6'"})
|
|
49
|
+
sxValue!: string;
|
|
50
|
+
|
|
51
|
+
@Option({displayName: "Equivalent properties", description: "Other `sx` properties that already set the style, separated by commas.", example: "mb", required: false})
|
|
52
|
+
equivalents?: string;
|
|
53
|
+
|
|
54
|
+
@Option({displayName: "Package name", description: "A package that re-exports Material UI.", example: "@org/ui", required: false})
|
|
55
|
+
packageName?: string;
|
|
56
|
+
|
|
57
|
+
constructor(options?: { componentName?: string; propName?: string; sxProperty?: string; sxValue?: string; equivalents?: string; packageName?: string }) {
|
|
58
|
+
super(options);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
async editor(): Promise<TreeVisitor<any, ExecutionContext>> {
|
|
62
|
+
const {propName, sxProperty, sxValue} = this;
|
|
63
|
+
const keys = [sxProperty, ...(this.equivalents ?? "").split(",").map(k => k.trim()).filter(Boolean)];
|
|
64
|
+
return muiTagEditor(this.componentName, this.packageName, async (tag, cursor) => {
|
|
65
|
+
const attribute = findAttribute(tag, propName);
|
|
66
|
+
if (!attribute) return tag;
|
|
67
|
+
const value = attributeExpression(attribute);
|
|
68
|
+
if (value.kind === J.Kind.Literal && (value as J.Literal).value === false) return replaceAttribute(tag, attribute, undefined);
|
|
69
|
+
const always = !attribute.value || (value.kind === J.Kind.Literal && (value as J.Literal).value === true);
|
|
70
|
+
const sx = findAttribute(tag, "sx");
|
|
71
|
+
const without = replaceAttribute(tag, attribute, undefined);
|
|
72
|
+
if (!sx) {
|
|
73
|
+
const source = always ? `{ ${sxProperty}: ${sxValue} }` : null;
|
|
74
|
+
const created = source
|
|
75
|
+
? await newAttribute(cursor, tag, "sx", source.startsWith("{") ? `{${source}}` : source)
|
|
76
|
+
: await template`<X sx={${value} ? ${raw(`{ ${sxProperty}: ${sxValue} }`)} : undefined} />`.apply(tag, cursor).then(p => (p as JSX.Tag).attributes[0].element as JSX.Attribute);
|
|
77
|
+
return appendAttribute(without, withSpacedObject(created));
|
|
78
|
+
}
|
|
79
|
+
const object = attributeObject(sx);
|
|
80
|
+
if (!object || !always) return tag;
|
|
81
|
+
if (keys.some(k => findProperty(object, k))) return without;
|
|
82
|
+
const property = await newPropertyFromSource(cursor, tag, sxProperty, sxValue);
|
|
83
|
+
const updated = appendProperty(object, property);
|
|
84
|
+
const embedded = sx.value!.element as JSX.EmbeddedExpression;
|
|
85
|
+
const replacement = {...sx, value: {...sx.value!, element: {...embedded, expression: {...embedded.expression, element: updated}}}} as JSX.Attribute;
|
|
86
|
+
return replaceAttribute(replaceAttribute(tag, attribute, undefined), sx, replacement);
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
}
|
package/src/mui/index.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export {AdaptV4Theme} from "./adapt-v4-theme";
|
|
2
|
+
export {AddComponentProp} from "./add-component-prop";
|
|
3
|
+
export {AddEmotionPrepend} from "./add-emotion-prepend";
|
|
4
|
+
export {ChangeModuleSpecifier} from "./change-module-specifier";
|
|
5
|
+
export {ConvertMuiImports} from "./convert-mui-imports";
|
|
6
|
+
export {ConvertPropToSx} from "./convert-prop-to-sx";
|
|
7
|
+
export {MarkManualMigration} from "./mark-manual-migration";
|
|
8
|
+
export {MarkPaletteModeUsage} from "./mark-palette-mode-usage";
|
|
9
|
+
export {MoveComponentPropToSlot} from "./move-component-prop-to-slot";
|
|
10
|
+
export {MovePrimaryTextToChildren} from "./move-primary-text-to-children";
|
|
11
|
+
export {MoveSystemPropsToSx} from "./move-system-props-to-sx";
|
|
12
|
+
export {RemoveComponentProp} from "./remove-component-prop";
|
|
13
|
+
export {RemoveRootRef} from "./remove-root-ref";
|
|
14
|
+
export {RemoveSpacingPxSuffix} from "./remove-spacing-px-suffix";
|
|
15
|
+
export {RenameComponentProp} from "./rename-component-prop";
|
|
16
|
+
export {RenameJoyCssVariables} from "./rename-joy-css-variables";
|
|
17
|
+
export {RenameThemePaletteType} from "./rename-theme-palette-type";
|
|
18
|
+
export {ReplaceBoxBorderRadius} from "./replace-box-border-radius";
|
|
19
|
+
export {ReplaceBreakpointsWidth} from "./replace-breakpoints-width";
|
|
20
|
+
export {ReplaceComponentClass} from "./replace-component-class";
|
|
21
|
+
export {ReplaceComponentsWithSlots} from "./replace-components-with-slots";
|
|
22
|
+
export {ReplaceSvgIconImports, ReplaceMaterialUiImportPaths} from "./replace-import-paths";
|
|
23
|
+
export {ReplaceInStringLiterals} from "./replace-in-string-literals";
|
|
24
|
+
export {ReplaceListItemButtonProp} from "./replace-list-item-button-prop";
|
|
25
|
+
export {ReplaceTabsScrollButtons} from "./replace-tabs-scroll-buttons";
|
|
26
|
+
export {ReplaceThemeColorFunctions} from "./replace-theme-color-functions";
|
|
27
|
+
export {ReplaceThemeSpacingUnit} from "./replace-theme-spacing-unit";
|
|
28
|
+
export {ReplaceTypographyRound} from "./replace-typography-round";
|
|
29
|
+
export {ShiftHiddenDownProps} from "./shift-hidden-down-props";
|
|
30
|
+
export {UpdateThemeBreakpoints} from "./update-theme-breakpoints";
|
|
31
|
+
export {UpgradeGridProps} from "./upgrade-grid-props";
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import {Cursor} from "@openrewrite/rewrite";
|
|
2
|
+
import {JS, JSX, raw, template} from "@openrewrite/rewrite/javascript";
|
|
3
|
+
import {emptySpace, isIdentifier, J} from "@openrewrite/rewrite/java";
|
|
4
|
+
import {attributeName} from "./mui-components";
|
|
5
|
+
|
|
6
|
+
type TagAttribute = JSX.Attribute | JSX.SpreadAttribute;
|
|
7
|
+
|
|
8
|
+
export function findAttribute(tag: JSX.Tag, name: string): JSX.Attribute | undefined {
|
|
9
|
+
return tag.attributes.map(a => a.element).find((a): a is JSX.Attribute => attributeName(a) === name);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export const hasSpread = (tag: JSX.Tag): boolean => tag.attributes.some(a => a.element.kind === JS.Kind.JsxSpreadAttribute);
|
|
13
|
+
|
|
14
|
+
/** The expression an attribute is set to: `a={x}` and `a="x"` give `x`, and a bare `a` gives `true`. */
|
|
15
|
+
export function attributeExpression(attribute: JSX.Attribute): J {
|
|
16
|
+
const value: J | undefined = attribute.value?.element;
|
|
17
|
+
if (!value) return {kind: J.Kind.Literal, id: attribute.id, prefix: emptySpace, markers: {kind: "org.openrewrite.marker.Markers", id: attribute.id, markers: []}, value: true, valueSource: "true"} as J.Literal;
|
|
18
|
+
return value.kind === JS.Kind.JsxEmbeddedExpression ? (value as JSX.EmbeddedExpression).expression.element : value;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** An object literal, `{...}`, which this tree models as an anonymous `new` with a body. */
|
|
22
|
+
export const isObjectLiteral = (e: J | undefined): e is J.NewClass => e?.kind === J.Kind.NewClass && !(e as J.NewClass).class && !!(e as J.NewClass).body;
|
|
23
|
+
|
|
24
|
+
/** The expression an attribute holds when it is `a={{...}}`, an object literal in an expression container. */
|
|
25
|
+
export function attributeObject(attribute: JSX.Attribute): J.NewClass | undefined {
|
|
26
|
+
const value = attribute.value?.element;
|
|
27
|
+
if (value?.kind !== JS.Kind.JsxEmbeddedExpression) return undefined;
|
|
28
|
+
const inner = (value as JSX.EmbeddedExpression).expression.element;
|
|
29
|
+
return isObjectLiteral(inner) ? inner : undefined;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export const objectProperties = (object: J.NewClass): J.RightPadded<any>[] => object.body!.statements;
|
|
33
|
+
|
|
34
|
+
export function propertyKey(property: any): string | undefined {
|
|
35
|
+
if (property?.kind !== JS.Kind.PropertyAssignment) return undefined;
|
|
36
|
+
const name = property.name.element;
|
|
37
|
+
if (isIdentifier(name)) return name.simpleName;
|
|
38
|
+
return name.kind === J.Kind.Literal && typeof name.value === "string" ? name.value : undefined;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function findProperty(object: J.NewClass, key: string): JS.PropertyAssignment | undefined {
|
|
42
|
+
return objectProperties(object).map(p => p.element).find(p => propertyKey(p) === key);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const trimmed = <T extends J>(node: T): T => ({...node, prefix: emptySpace});
|
|
46
|
+
|
|
47
|
+
/** An expression without the whitespace before it, ready to be placed in a new construct. */
|
|
48
|
+
export const bare = trimmed;
|
|
49
|
+
|
|
50
|
+
/** The tag with its attribute list replaced, keeping the spacing of the tag. */
|
|
51
|
+
export const withAttributes = (tag: JSX.Tag, attributes: J.RightPadded<TagAttribute>[]): JSX.Tag => ({...tag, attributes}) as JSX.Tag;
|
|
52
|
+
|
|
53
|
+
const attributeRightPadded = (attribute: TagAttribute, like: J.RightPadded<any>): J.RightPadded<TagAttribute> =>
|
|
54
|
+
({...like, element: attribute}) as J.RightPadded<TagAttribute>;
|
|
55
|
+
|
|
56
|
+
/** Builds `name={value}` for a value that is a source expression. */
|
|
57
|
+
export async function newAttribute(cursor: Cursor, tag: JSX.Tag, name: string, value: J | string): Promise<JSX.Attribute> {
|
|
58
|
+
const probe = typeof value === "string"
|
|
59
|
+
? await template`<X ${raw(name)}=${raw(value)} />`.apply(tag, cursor)
|
|
60
|
+
: await template`<X ${raw(name)}={${trimmed(value)}} />`.apply(tag, cursor);
|
|
61
|
+
return (probe as JSX.Tag).attributes[0].element as JSX.Attribute;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Builds `name={{ key: value }}`. */
|
|
65
|
+
export async function newObjectAttribute(cursor: Cursor, tag: JSX.Tag, name: string, key: string, value: J): Promise<JSX.Attribute> {
|
|
66
|
+
const probe = await template`<X ${raw(name)}={{ ${raw(key)}: ${trimmed(value)} }} />`.apply(tag, cursor);
|
|
67
|
+
const attribute = (probe as JSX.Tag).attributes[0].element as JSX.Attribute;
|
|
68
|
+
const embedded = attribute.value!.element as JSX.EmbeddedExpression;
|
|
69
|
+
return {...attribute, value: {...attribute.value!, element: {...embedded, expression: {...embedded.expression, element: spaced(attributeObject(attribute)!)}}}} as JSX.Attribute;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const space = (whitespace: string): J.Space => ({...emptySpace, whitespace});
|
|
73
|
+
|
|
74
|
+
/** A one-line object literal with a space inside its braces, as `{ key: value }`, which a template prints without. */
|
|
75
|
+
export function spaced(object: J.NewClass): J.NewClass {
|
|
76
|
+
const [first, ...rest] = objectProperties(object);
|
|
77
|
+
return {...object, body: {...object.body!, statements: [{...first, element: {...first.element, prefix: space(" ")}}, ...rest], end: space(" ")}} as J.NewClass;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Builds the object literal `{ key: value }`, for placing under another key. */
|
|
81
|
+
export async function newObject(cursor: Cursor, tag: JSX.Tag, key: string, value: J): Promise<J.NewClass> {
|
|
82
|
+
const attribute = await newObjectAttribute(cursor, tag, "x", key, value);
|
|
83
|
+
return attributeObject(attribute)!;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** Builds the property `key: value` for adding to an object literal that is already there. */
|
|
87
|
+
export async function newProperty(cursor: Cursor, tag: JSX.Tag, key: string, value: J): Promise<J.RightPadded<any>> {
|
|
88
|
+
const object = await newObject(cursor, tag, key, value);
|
|
89
|
+
return objectProperties(object)[0];
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** Appends a property to an object literal, spaced like the properties it already has. */
|
|
93
|
+
export function appendProperty(object: J.NewClass, property: J.RightPadded<any>): J.NewClass {
|
|
94
|
+
const statements = objectProperties(object);
|
|
95
|
+
const last = statements[statements.length - 1];
|
|
96
|
+
const prefix = last ? last.element.prefix : {...property.element.prefix};
|
|
97
|
+
const added = {...property, element: {...property.element, prefix: statements.length ? prefix : property.element.prefix}};
|
|
98
|
+
return {...object, body: {...object.body!, statements: [...statements, added]}} as J.NewClass;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** Replaces one attribute of a tag, in the same position. */
|
|
102
|
+
export function replaceAttribute(tag: JSX.Tag, old: JSX.Attribute, replacement: JSX.Attribute | undefined): JSX.Tag {
|
|
103
|
+
const attributes = tag.attributes.flatMap(a => a.element !== old ? [a] :
|
|
104
|
+
replacement ? [attributeRightPadded({...replacement, prefix: old.prefix}, a)] : []);
|
|
105
|
+
return withAttributes(tag, attributes);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** Adds an attribute after the last one. */
|
|
109
|
+
export function appendAttribute(tag: JSX.Tag, attribute: JSX.Attribute): JSX.Tag {
|
|
110
|
+
const last = tag.attributes[tag.attributes.length - 1];
|
|
111
|
+
const prefix = last ? last.element.prefix : {...emptySpace, whitespace: " "};
|
|
112
|
+
const added = {kind: J.Kind.RightPadded, element: {...attribute, prefix}, after: emptySpace, markers: last?.markers ?? attribute.markers} as J.RightPadded<TagAttribute>;
|
|
113
|
+
return withAttributes(tag, [...tag.attributes, added]);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** Builds the property `key: source`, for a value written as source text, to add to an object literal. */
|
|
117
|
+
export async function newPropertyFromSource(cursor: Cursor, anchor: J, key: string, source: string): Promise<J.RightPadded<any>> {
|
|
118
|
+
const probe = await template`<X a={{ ${raw(key)}: ${raw(source)} }} />`.apply(anchor, cursor);
|
|
119
|
+
return objectProperties(attributeObject((probe as JSX.Tag).attributes[0].element as JSX.Attribute)!)[0];
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** Parses source text as an expression, for a value the recipe writes itself. */
|
|
123
|
+
export async function sourceExpression(cursor: Cursor, anchor: J, source: string): Promise<J> {
|
|
124
|
+
const probe = await template`<X a={${raw(source)}} />` .apply(anchor, cursor) as JSX.Tag;
|
|
125
|
+
return attributeExpression(probe.attributes[0].element as JSX.Attribute);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** The tag with its opening and closing names replaced by `name`. */
|
|
129
|
+
export function renameTag(tag: JSX.Tag, name: string): JSX.Tag {
|
|
130
|
+
const rename = (n: any) => isIdentifier(n) ? {...n, simpleName: name} : n;
|
|
131
|
+
return {...tag, openName: {...tag.openName, element: rename(tag.openName.element)},
|
|
132
|
+
...(tag.closingName ? {closingName: {...tag.closingName, element: rename(tag.closingName.element)}} : {})} as JSX.Tag;
|
|
133
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import {ExecutionContext, Option, Recipe, TreeVisitor, check, foundSearchResult, not} from "@openrewrite/rewrite";
|
|
2
|
+
import {JavaScriptVisitor, JS, isVendoredOrBundled} from "@openrewrite/rewrite/javascript";
|
|
3
|
+
import {J} from "@openrewrite/rewrite/java";
|
|
4
|
+
import {importBindings} from "../util/module-imports";
|
|
5
|
+
import {packagePattern} from "./mui-tag-visitor";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Marks the imports of an API of Material UI that has to be migrated by hand, because no mechanical rewrite of its
|
|
9
|
+
* call sites is safe, with a comment that says what to do.
|
|
10
|
+
*
|
|
11
|
+
* Before: `import { makeStyles } from '@material-ui/core/styles';`
|
|
12
|
+
* After: the same import, marked with the message.
|
|
13
|
+
*
|
|
14
|
+
* @see https://mui.com/material-ui/migration/migrating-from-jss/
|
|
15
|
+
*/
|
|
16
|
+
export class MarkManualMigration extends Recipe {
|
|
17
|
+
readonly name = "org.openrewrite.react.mui.mark-manual-migration";
|
|
18
|
+
readonly displayName: string = "Mark a Material UI API that needs a manual migration";
|
|
19
|
+
readonly description: string = "Marks every import of the named export of a Material UI package with a message saying how to migrate it, where no mechanical rewrite is safe.";
|
|
20
|
+
|
|
21
|
+
@Option({displayName: "Imported name", description: "The export that is imported, such as `makeStyles`, or `*` for any import of the package.", example: "makeStyles"})
|
|
22
|
+
importedName!: string;
|
|
23
|
+
|
|
24
|
+
@Option({displayName: "Message", description: "What to do, shown in the marker.", example: "Migrate to the `styled` API"})
|
|
25
|
+
message!: string;
|
|
26
|
+
|
|
27
|
+
@Option({displayName: "Package name", description: "A package that re-exports Material UI.", example: "@org/ui", required: false})
|
|
28
|
+
packageName?: string;
|
|
29
|
+
|
|
30
|
+
constructor(options?: { importedName?: string; message?: string; packageName?: string }) {
|
|
31
|
+
super(options);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
async editor(): Promise<TreeVisitor<any, ExecutionContext>> {
|
|
35
|
+
const {importedName, message} = this;
|
|
36
|
+
const pattern = packagePattern(this.packageName);
|
|
37
|
+
const matches = (b: {name: string; module: string}) => (importedName === "*" || b.name === importedName) && pattern.test(b.module);
|
|
38
|
+
return check(not(isVendoredOrBundled()), new class extends JavaScriptVisitor<ExecutionContext> {
|
|
39
|
+
private matches = false;
|
|
40
|
+
|
|
41
|
+
override async visitJsCompilationUnit(cu: JS.CompilationUnit, ctx: ExecutionContext): Promise<J | undefined> {
|
|
42
|
+
this.matches = importBindings(cu).some(matches);
|
|
43
|
+
return this.matches ? super.visitJsCompilationUnit(cu, ctx) : cu;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
override async visitImportDeclaration(declaration: JS.Import, ctx: ExecutionContext): Promise<J | undefined> {
|
|
47
|
+
const visited = await super.visitImportDeclaration(declaration, ctx) as JS.Import;
|
|
48
|
+
const bindings = importBindings({statements: [{element: visited}]} as unknown as JS.CompilationUnit);
|
|
49
|
+
return bindings.some(matches) ? foundSearchResult(visited, message) : visited;
|
|
50
|
+
}
|
|
51
|
+
}());
|
|
52
|
+
}
|
|
53
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import {ExecutionContext, Option, Recipe, TreeVisitor, check, foundSearchResult, not} from "@openrewrite/rewrite";
|
|
2
|
+
import {JavaScriptVisitor, isVendoredOrBundled} from "@openrewrite/rewrite/javascript";
|
|
3
|
+
import {J} from "@openrewrite/rewrite/java";
|
|
4
|
+
import {endsWithName} from "./theme-access";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Marks the reads of `theme.palette.mode`, which v6 styles should replace with `theme.applyStyles()` so that dark
|
|
8
|
+
* mode works with CSS variables. No mechanical rewrite of the surrounding style object is safe.
|
|
9
|
+
*
|
|
10
|
+
* Before: `({ theme }) => ({ color: theme.palette.mode === 'dark' ? 'white' : 'black' })`
|
|
11
|
+
* After: the same, with the read of `theme.palette.mode` marked.
|
|
12
|
+
*
|
|
13
|
+
* @see https://mui.com/material-ui/migration/upgrade-to-v6/#migrate-theme-styles-to-support-css-variables-and-color-schemes
|
|
14
|
+
*/
|
|
15
|
+
export class MarkPaletteModeUsage extends Recipe {
|
|
16
|
+
readonly name = "org.openrewrite.react.mui.mark-palette-mode-usage";
|
|
17
|
+
readonly displayName: string = "Mark reads of `theme.palette.mode`";
|
|
18
|
+
readonly description: string = "Marks every read of `<something>.palette.mode` with a message saying to use `theme.applyStyles()` for dark mode instead.";
|
|
19
|
+
|
|
20
|
+
@Option({displayName: "Message", description: "What to do, shown in the marker.", example: "Use theme.applyStyles('dark', {...}) in place of theme.palette.mode checks"})
|
|
21
|
+
message!: string;
|
|
22
|
+
|
|
23
|
+
constructor(options?: { message?: string }) {
|
|
24
|
+
super(options);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
async editor(): Promise<TreeVisitor<any, ExecutionContext>> {
|
|
28
|
+
const {message} = this;
|
|
29
|
+
return check(not(isVendoredOrBundled()), new class extends JavaScriptVisitor<ExecutionContext> {
|
|
30
|
+
override async visitFieldAccess(fieldAccess: J.FieldAccess, ctx: ExecutionContext): Promise<J | undefined> {
|
|
31
|
+
const visited = await super.visitFieldAccess(fieldAccess, ctx) as J.FieldAccess;
|
|
32
|
+
return visited.name.element.simpleName === "mode" && endsWithName(visited.target, "palette") ? foundSearchResult(visited, message) : visited;
|
|
33
|
+
}
|
|
34
|
+
}());
|
|
35
|
+
}
|
|
36
|
+
}
|