@openrewrite/recipes-react 0.15.0 → 0.17.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.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/migration/cra-to-vite/add-vite-config.d.ts +7 -0
- package/dist/migration/cra-to-vite/add-vite-config.d.ts.map +1 -1
- package/dist/migration/cra-to-vite/add-vite-config.js +58 -1
- package/dist/migration/cra-to-vite/add-vite-config.js.map +1 -1
- package/dist/migration/cra-to-vite/cra-file-gate.d.ts +3 -0
- package/dist/migration/cra-to-vite/cra-file-gate.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/cra-file-gate.js +10 -0
- package/dist/migration/cra-to-vite/cra-file-gate.js.map +1 -0
- package/dist/migration/cra-to-vite/cra-runtime.d.ts +2 -0
- package/dist/migration/cra-to-vite/cra-runtime.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/cra-runtime.js +24 -0
- package/dist/migration/cra-to-vite/cra-runtime.js.map +1 -0
- package/dist/migration/cra-to-vite/cra-script.d.ts +8 -0
- package/dist/migration/cra-to-vite/cra-script.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/cra-script.js +82 -0
- package/dist/migration/cra-to-vite/cra-script.js.map +1 -0
- package/dist/migration/cra-to-vite/cra-to-vite.d.ts +2 -1
- package/dist/migration/cra-to-vite/cra-to-vite.d.ts.map +1 -1
- package/dist/migration/cra-to-vite/cra-to-vite.js +23 -16
- package/dist/migration/cra-to-vite/cra-to-vite.js.map +1 -1
- package/dist/migration/cra-to-vite/cra-typecheck.d.ts +4 -0
- package/dist/migration/cra-to-vite/cra-typecheck.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/cra-typecheck.js +35 -0
- package/dist/migration/cra-to-vite/cra-typecheck.js.map +1 -0
- package/dist/migration/cra-to-vite/find-cra-config-blockers.d.ts.map +1 -1
- package/dist/migration/cra-to-vite/find-cra-config-blockers.js +22 -29
- package/dist/migration/cra-to-vite/find-cra-config-blockers.js.map +1 -1
- package/dist/migration/cra-to-vite/index.d.ts +8 -2
- package/dist/migration/cra-to-vite/index.d.ts.map +1 -1
- package/dist/migration/cra-to-vite/index.js +17 -2
- package/dist/migration/cra-to-vite/index.js.map +1 -1
- package/dist/migration/cra-to-vite/migrate-cra-package.d.ts +17 -0
- package/dist/migration/cra-to-vite/migrate-cra-package.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/migrate-cra-package.js +181 -0
- package/dist/migration/cra-to-vite/migrate-cra-package.js.map +1 -0
- package/dist/migration/cra-to-vite/migrate-cra-svg-imports.d.ts +9 -0
- package/dist/migration/cra-to-vite/migrate-cra-svg-imports.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/migrate-cra-svg-imports.js +66 -0
- package/dist/migration/cra-to-vite/migrate-cra-svg-imports.js.map +1 -0
- package/dist/migration/cra-to-vite/migrate-cra-tests-to-vitest.d.ts +49 -0
- package/dist/migration/cra-to-vite/migrate-cra-tests-to-vitest.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/migrate-cra-tests-to-vitest.js +506 -0
- package/dist/migration/cra-to-vite/migrate-cra-tests-to-vitest.js.map +1 -0
- package/dist/migration/cra-to-vite/move-public-index-html.d.ts +2 -0
- package/dist/migration/cra-to-vite/move-public-index-html.d.ts.map +1 -1
- package/dist/migration/cra-to-vite/move-public-index-html.js +12 -7
- package/dist/migration/cra-to-vite/move-public-index-html.js.map +1 -1
- package/dist/migration/cra-to-vite/plan-cra-to-vite.d.ts +30 -0
- package/dist/migration/cra-to-vite/plan-cra-to-vite.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/plan-cra-to-vite.js +428 -0
- package/dist/migration/cra-to-vite/plan-cra-to-vite.js.map +1 -0
- package/dist/migration/cra-to-vite/prepare-cra-package.d.ts +15 -0
- package/dist/migration/cra-to-vite/prepare-cra-package.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/prepare-cra-package.js +100 -0
- package/dist/migration/cra-to-vite/prepare-cra-package.js.map +1 -0
- package/dist/migration/cra-to-vite/remove-cra-configuration.d.ts +12 -0
- package/dist/migration/cra-to-vite/remove-cra-configuration.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/remove-cra-configuration.js +85 -0
- package/dist/migration/cra-to-vite/remove-cra-configuration.js.map +1 -0
- package/dist/migration/cra-to-vite/rename-jsx-files.d.ts +20 -0
- package/dist/migration/cra-to-vite/rename-jsx-files.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/rename-jsx-files.js +220 -0
- package/dist/migration/cra-to-vite/rename-jsx-files.js.map +1 -0
- package/dist/migration/cra-to-vite/rename-process-env-react-app.d.ts.map +1 -1
- package/dist/migration/cra-to-vite/rename-process-env-react-app.js +6 -3
- package/dist/migration/cra-to-vite/rename-process-env-react-app.js.map +1 -1
- package/dist/migration/cra-to-vite/rename-react-app-env-vars-in-env-files.js +1 -1
- package/dist/migration/cra-to-vite/rename-react-app-env-vars-in-env-files.js.map +1 -1
- package/dist/migration/cra-to-vite/replace-react-scripts-dependency.d.ts +11 -4
- package/dist/migration/cra-to-vite/replace-react-scripts-dependency.d.ts.map +1 -1
- package/dist/migration/cra-to-vite/replace-react-scripts-dependency.js +47 -50
- package/dist/migration/cra-to-vite/replace-react-scripts-dependency.js.map +1 -1
- package/dist/migration/cra-to-vite/resolved-cra-dependency.d.ts +3 -0
- package/dist/migration/cra-to-vite/resolved-cra-dependency.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/resolved-cra-dependency.js +42 -0
- package/dist/migration/cra-to-vite/resolved-cra-dependency.js.map +1 -0
- package/dist/migration/cra-to-vite/scan-cra-projects.d.ts +13 -0
- package/dist/migration/cra-to-vite/scan-cra-projects.d.ts.map +1 -1
- package/dist/migration/cra-to-vite/scan-cra-projects.js +52 -4
- package/dist/migration/cra-to-vite/scan-cra-projects.js.map +1 -1
- package/dist/migration/cra-to-vite/shared.d.ts +2 -15
- package/dist/migration/cra-to-vite/shared.d.ts.map +1 -1
- package/dist/migration/cra-to-vite/shared.js +32 -130
- package/dist/migration/cra-to-vite/shared.js.map +1 -1
- package/dist/migration/cra-to-vite/svg-component.d.ts +5 -0
- package/dist/migration/cra-to-vite/svg-component.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/svg-component.js +60 -0
- package/dist/migration/cra-to-vite/svg-component.js.map +1 -0
- package/dist/migration/cra-to-vite/update-react-app-env-types.d.ts.map +1 -1
- package/dist/migration/cra-to-vite/update-react-app-env-types.js +8 -2
- package/dist/migration/cra-to-vite/update-react-app-env-types.js.map +1 -1
- package/dist/migration/cra-to-vite/update-react-scripts-scripts.d.ts +3 -2
- package/dist/migration/cra-to-vite/update-react-scripts-scripts.d.ts.map +1 -1
- package/dist/migration/cra-to-vite/update-react-scripts-scripts.js +28 -29
- package/dist/migration/cra-to-vite/update-react-scripts-scripts.js.map +1 -1
- package/dist/migration/cra-to-vite/update-tsconfig-for-vite.d.ts.map +1 -1
- package/dist/migration/cra-to-vite/update-tsconfig-for-vite.js +4 -13
- package/dist/migration/cra-to-vite/update-tsconfig-for-vite.js.map +1 -1
- package/dist/react17/migrate-deprecated-lifecycles.js +1 -1
- package/dist/react17/migrate-deprecated-lifecycles.js.map +1 -1
- package/dist/react17/use-new-jsx-transform.d.ts.map +1 -1
- package/dist/react17/use-new-jsx-transform.js +30 -78
- package/dist/react17/use-new-jsx-transform.js.map +1 -1
- package/dist/react18/replace-reactdom-render.d.ts.map +1 -1
- package/dist/react18/replace-reactdom-render.js +16 -8
- package/dist/react18/replace-reactdom-render.js.map +1 -1
- package/dist/react18/replace-unmount-component-at-node.d.ts.map +1 -1
- package/dist/react18/replace-unmount-component-at-node.js +62 -20
- package/dist/react18/replace-unmount-component-at-node.js.map +1 -1
- package/dist/react19/replace-default-props.d.ts.map +1 -1
- package/dist/react19/replace-default-props.js +10 -13
- package/dist/react19/replace-default-props.js.map +1 -1
- package/dist/react19/replace-react-shallow-renderer.d.ts.map +1 -1
- package/dist/react19/replace-react-shallow-renderer.js +1 -6
- package/dist/react19/replace-react-shallow-renderer.js.map +1 -1
- package/dist/refactoring/create-class-to-es6.d.ts.map +1 -1
- package/dist/refactoring/create-class-to-es6.js +4 -7
- package/dist/refactoring/create-class-to-es6.js.map +1 -1
- package/dist/refactoring/create-element-to-jsx.d.ts.map +1 -1
- package/dist/refactoring/create-element-to-jsx.js +3 -4
- package/dist/refactoring/create-element-to-jsx.js.map +1 -1
- package/dist/refactoring/prop-types-to-typescript.js +2 -3
- package/dist/refactoring/prop-types-to-typescript.js.map +1 -1
- package/dist/refactoring/pure-render-mixin.d.ts.map +1 -1
- package/dist/refactoring/pure-render-mixin.js +3 -6
- package/dist/refactoring/pure-render-mixin.js.map +1 -1
- package/dist/testing/enzyme-constant-strings.d.ts +12 -0
- package/dist/testing/enzyme-constant-strings.d.ts.map +1 -0
- package/dist/testing/enzyme-constant-strings.js +101 -0
- package/dist/testing/enzyme-constant-strings.js.map +1 -0
- package/dist/testing/enzyme-host-components.d.ts +14 -0
- package/dist/testing/enzyme-host-components.d.ts.map +1 -0
- package/dist/testing/enzyme-host-components.js +248 -0
- package/dist/testing/enzyme-host-components.js.map +1 -0
- package/dist/testing/enzyme-portals.d.ts +38 -0
- package/dist/testing/enzyme-portals.d.ts.map +1 -0
- package/dist/testing/enzyme-portals.js +226 -0
- package/dist/testing/enzyme-portals.js.map +1 -0
- package/dist/testing/enzyme-selector-names.d.ts +9 -0
- package/dist/testing/enzyme-selector-names.d.ts.map +1 -0
- package/dist/testing/enzyme-selector-names.js +50 -0
- package/dist/testing/enzyme-selector-names.js.map +1 -0
- package/dist/testing/enzyme-to-react-testing-library.d.ts +80 -12
- package/dist/testing/enzyme-to-react-testing-library.d.ts.map +1 -1
- package/dist/testing/enzyme-to-react-testing-library.js +447 -135
- package/dist/testing/enzyme-to-react-testing-library.js.map +1 -1
- package/dist/testing/enzyme-usage.d.ts.map +1 -1
- package/dist/testing/enzyme-usage.js +27 -4
- package/dist/testing/enzyme-usage.js.map +1 -1
- package/dist/testing/enzyme-wrapper-scope.d.ts +23 -8
- package/dist/testing/enzyme-wrapper-scope.d.ts.map +1 -1
- package/dist/testing/enzyme-wrapper-scope.js +291 -169
- package/dist/testing/enzyme-wrapper-scope.js.map +1 -1
- package/dist/util/java-recipe-proxy.d.ts +16 -0
- package/dist/util/java-recipe-proxy.d.ts.map +1 -0
- package/dist/util/java-recipe-proxy.js +85 -0
- package/dist/util/java-recipe-proxy.js.map +1 -0
- package/dist/util/module-imports.d.ts +13 -0
- package/dist/util/module-imports.d.ts.map +1 -0
- package/dist/util/module-imports.js +60 -0
- package/dist/util/module-imports.js.map +1 -0
- package/dist/util/react-class.d.ts +1 -0
- package/dist/util/react-class.d.ts.map +1 -1
- package/dist/util/react-class.js +3 -2
- package/dist/util/react-class.js.map +1 -1
- package/package.json +4 -4
- package/src/index.ts +2 -1
- package/src/migration/cra-to-vite/add-vite-config.ts +53 -1
- package/src/migration/cra-to-vite/cra-file-gate.ts +8 -0
- package/src/migration/cra-to-vite/cra-runtime.ts +16 -0
- package/src/migration/cra-to-vite/cra-script.ts +61 -0
- package/src/migration/cra-to-vite/cra-to-vite.ts +25 -36
- package/src/migration/cra-to-vite/cra-typecheck.ts +33 -0
- package/src/migration/cra-to-vite/find-cra-config-blockers.ts +23 -28
- package/src/migration/cra-to-vite/index.ts +10 -2
- package/src/migration/cra-to-vite/migrate-cra-package.ts +127 -0
- package/src/migration/cra-to-vite/migrate-cra-svg-imports.ts +41 -0
- package/src/migration/cra-to-vite/migrate-cra-tests-to-vitest.ts +376 -0
- package/src/migration/cra-to-vite/move-public-index-html.ts +12 -7
- package/src/migration/cra-to-vite/plan-cra-to-vite.ts +283 -0
- package/src/migration/cra-to-vite/prepare-cra-package.ts +86 -0
- package/src/migration/cra-to-vite/remove-cra-configuration.ts +64 -0
- package/src/migration/cra-to-vite/rename-jsx-files.ts +155 -0
- package/src/migration/cra-to-vite/rename-process-env-react-app.ts +6 -3
- package/src/migration/cra-to-vite/rename-react-app-env-vars-in-env-files.ts +1 -1
- package/src/migration/cra-to-vite/replace-react-scripts-dependency.ts +48 -54
- package/src/migration/cra-to-vite/resolved-cra-dependency.ts +41 -0
- package/src/migration/cra-to-vite/scan-cra-projects.ts +57 -6
- package/src/migration/cra-to-vite/shared.ts +35 -146
- package/src/migration/cra-to-vite/svg-component.ts +39 -0
- package/src/migration/cra-to-vite/update-react-app-env-types.ts +11 -4
- package/src/migration/cra-to-vite/update-react-scripts-scripts.ts +30 -41
- package/src/migration/cra-to-vite/update-tsconfig-for-vite.ts +6 -13
- package/src/react17/migrate-deprecated-lifecycles.ts +2 -2
- package/src/react17/use-new-jsx-transform.ts +30 -72
- package/src/react18/replace-reactdom-render.ts +17 -8
- package/src/react18/replace-unmount-component-at-node.ts +78 -32
- package/src/react19/replace-default-props.ts +11 -15
- package/src/react19/replace-react-shallow-renderer.ts +2 -7
- package/src/refactoring/create-class-to-es6.ts +6 -9
- package/src/refactoring/create-element-to-jsx.ts +4 -6
- package/src/refactoring/prop-types-to-typescript.ts +4 -4
- package/src/refactoring/pure-render-mixin.ts +4 -7
- package/src/testing/enzyme-constant-strings.ts +95 -0
- package/src/testing/enzyme-host-components.ts +287 -0
- package/src/testing/enzyme-portals.ts +279 -0
- package/src/testing/enzyme-selector-names.ts +59 -0
- package/src/testing/enzyme-to-react-testing-library.ts +584 -140
- package/src/testing/enzyme-usage.ts +29 -4
- package/src/testing/enzyme-wrapper-scope.ts +309 -151
- package/src/util/java-recipe-proxy.ts +81 -0
- package/src/util/module-imports.ts +60 -0
- package/src/util/react-class.ts +2 -1
|
@@ -1,68 +1,57 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import {craScript} from './cra-script';
|
|
2
|
+
import {ExecutionContext, Parsers, SourceFile, TreeVisitor} from "@openrewrite/rewrite";
|
|
3
|
+
import {getMemberKeyName, isObject, Json, jsonLiteral, JsonVisitor, membersOf, removeAt, setAt} from "@openrewrite/rewrite/json";
|
|
3
4
|
import {CraProjects, CraScanningRecipe} from "./scan-cra-projects";
|
|
4
|
-
import {dirName, isPackageJson,
|
|
5
|
-
|
|
6
|
-
/** Only an exact match is rewritten: extra flags or environment prefixes need a human. */
|
|
7
|
-
const SCRIPT_REPLACEMENTS = new Map<string, string>([
|
|
8
|
-
["react-scripts start", "vite"],
|
|
9
|
-
["react-scripts build", "vite build"],
|
|
10
|
-
]);
|
|
11
|
-
|
|
12
|
-
/** `react-scripts build` fails the build on type errors; `vite build` does not type-check, so a project with a tsconfig keeps the check. */
|
|
13
|
-
const TYPE_CHECKED_BUILD = "tsc --noEmit && vite build";
|
|
5
|
+
import {dirName, isPackageJson, joinPath, memberObject, stringValue} from "./shared";
|
|
6
|
+
import {CRA_TYPECHECK_PATH, CRA_TYPECHECK_SCRIPT} from './cra-typecheck';
|
|
14
7
|
|
|
15
8
|
/**
|
|
16
|
-
* Rewrites
|
|
9
|
+
* Rewrites CRA start/build commands and literal HOST, PORT, PUBLIC_URL and BUILD_PATH controls.
|
|
17
10
|
*
|
|
18
11
|
* ```json
|
|
19
12
|
* "start": "react-scripts start" -> "start": "vite"
|
|
20
13
|
* "build": "react-scripts build" -> "build": "vite build"
|
|
21
|
-
* "build": "react-scripts build" -> "build": "
|
|
14
|
+
* "build": "react-scripts build" -> "build": "node scripts/vite-typecheck.cjs && vite build" (when there is a tsconfig.json)
|
|
22
15
|
* "eject": "react-scripts eject" -> (removed)
|
|
23
16
|
* ```
|
|
24
17
|
*
|
|
25
18
|
* `react-scripts build` failed on type errors and `vite build` does not type-check, so a package with a `tsconfig.json`
|
|
26
|
-
* keeps
|
|
19
|
+
* keeps a checker that reports production diagnostics under `src/`, matching CRA's scope.
|
|
27
20
|
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
* package has no working `start`. Scripts with extra flags (`cross-env PORT=4000 react-scripts start`) and `test` are left for
|
|
31
|
-
* `FindCraToViteBlockers` to flag.
|
|
21
|
+
* Custom shell commands, HTTPS configuration, unknown flags and wrapper tools require manual review.
|
|
22
|
+
* Tests are left for the separate Vitest migration.
|
|
32
23
|
*
|
|
33
24
|
* @see https://react.dev/blog/2025/02/14/sunsetting-create-react-app#how-to-migrate-to-a-build-tool
|
|
34
25
|
*/
|
|
35
26
|
export class UpdateReactScriptsScripts extends CraScanningRecipe {
|
|
36
27
|
readonly name = "org.openrewrite.react.cra-to-vite.update-package-scripts";
|
|
37
28
|
readonly displayName = "Replace `react-scripts` start and build scripts with Vite";
|
|
38
|
-
readonly description = "
|
|
29
|
+
readonly description = "Replaces CRA start/build scripts with Vite, preserving literal HOST, PORT, PUBLIC_URL and BUILD_PATH settings as CLI options and retaining production TypeScript checking. Removes eject. Custom wrappers, unknown shell syntax and test scripts are left unchanged.";
|
|
30
|
+
|
|
31
|
+
override async generate(acc: CraProjects, ctx: ExecutionContext): Promise<SourceFile[]> {
|
|
32
|
+
const parser = Parsers.createParser('plainText', {ctx});
|
|
33
|
+
return Promise.all([...acc.viteRoots].filter(root => acc.mayMigrate(root) && acc.has(root, 'tsconfig.json') && !acc.has(root, CRA_TYPECHECK_PATH))
|
|
34
|
+
.map(root => parser.parseOne({text: CRA_TYPECHECK_SCRIPT, sourcePath: joinPath(root, CRA_TYPECHECK_PATH)})));
|
|
35
|
+
}
|
|
39
36
|
|
|
40
37
|
async editorWithData(acc: CraProjects): Promise<TreeVisitor<any, ExecutionContext>> {
|
|
41
38
|
return new class extends JsonVisitor<ExecutionContext> {
|
|
42
39
|
protected override async visitDocument(doc: Json.Document): Promise<Json | undefined> {
|
|
43
|
-
if (!isPackageJson(doc.sourcePath) || !isObject(doc.value) || !
|
|
40
|
+
if (!isPackageJson(doc.sourcePath) || !isObject(doc.value) || !acc.isViteFile(doc.sourcePath)) return doc;
|
|
41
|
+
if (acc.has(dirName(doc.sourcePath), 'tsconfig.json') && acc.has(dirName(doc.sourcePath), CRA_TYPECHECK_PATH)) return doc;
|
|
44
42
|
const scripts = memberObject(doc.value, "scripts");
|
|
45
43
|
if (!scripts) return doc;
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
const replacement = typeof literal.value === "string" ? SCRIPT_REPLACEMENTS.get(literal.value.trim()) : undefined;
|
|
58
|
-
if (!replacement) return rp;
|
|
59
|
-
const value = replacement === "vite build" ? build : replacement;
|
|
60
|
-
return {...rp, element: {...m, value: {...literal, source: JSON.stringify(value), value}}};
|
|
61
|
-
})
|
|
62
|
-
};
|
|
63
|
-
const changed = ejects.length > 0 || newScripts.members.some((rp, i) => rp !== kept.members[i]);
|
|
64
|
-
if (!changed) return doc;
|
|
65
|
-
return {...doc, value: replaceMember(doc.value, "scripts", m => ({...m, value: newScripts}))} as Json.Document;
|
|
44
|
+
let updated = doc;
|
|
45
|
+
for (const m of membersOf(scripts)) {
|
|
46
|
+
const name = getMemberKeyName(m);
|
|
47
|
+
const command = stringValue(m.value)?.trim();
|
|
48
|
+
if (name === undefined || command === undefined) continue;
|
|
49
|
+
const migrated = craScript(command, acc.has(dirName(doc.sourcePath), 'tsconfig.json'));
|
|
50
|
+
if (!migrated || migrated.operation === 'test') continue;
|
|
51
|
+
updated = migrated.replacement === null ? removeAt(updated, ['scripts', name]) :
|
|
52
|
+
setAt(updated, ['scripts', name], jsonLiteral(migrated.replacement));
|
|
53
|
+
}
|
|
54
|
+
return updated;
|
|
66
55
|
}
|
|
67
56
|
};
|
|
68
57
|
}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import {ExecutionContext, TreeVisitor} from "@openrewrite/rewrite";
|
|
2
|
-
import {Json, JsonVisitor} from "@openrewrite/rewrite/json";
|
|
2
|
+
import {appendElement, elementsOf, isArray, Json, jsonLiteral, JsonVisitor, updateAt} from "@openrewrite/rewrite/json";
|
|
3
3
|
import {CraProjects, CraScanningRecipe} from "./scan-cra-projects";
|
|
4
|
-
import {
|
|
4
|
+
import {baseName} from "./shared";
|
|
5
5
|
|
|
6
|
-
const
|
|
6
|
+
const TYPES_PATH = ["compilerOptions", "types"];
|
|
7
7
|
|
|
8
8
|
export function isTsConfig(sourcePath: string): boolean {
|
|
9
9
|
return /^tsconfig(\..+)?\.json$/.test(baseName(sourcePath)) && !sourcePath.split("/").includes("node_modules");
|
|
@@ -31,16 +31,9 @@ export class UpdateTsconfigForVite extends CraScanningRecipe {
|
|
|
31
31
|
async editorWithData(acc: CraProjects): Promise<TreeVisitor<any, ExecutionContext>> {
|
|
32
32
|
return new class extends JsonVisitor<ExecutionContext> {
|
|
33
33
|
protected override async visitDocument(doc: Json.Document): Promise<Json | undefined> {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
if (!root || !options || !types || types.value.kind !== Json.Kind.Array) return doc;
|
|
38
|
-
const array = types.value as Json.Array;
|
|
39
|
-
if (stringValues(array).includes(VITE_CLIENT)) return doc;
|
|
40
|
-
const updated = appendString(array, VITE_CLIENT);
|
|
41
|
-
if (updated === array) return doc;
|
|
42
|
-
const newOptions = replaceMember(options, "types", m => ({...m, value: updated}));
|
|
43
|
-
return {...doc, value: replaceMember(root, "compilerOptions", m => ({...m, value: newOptions}))} as Json.Document;
|
|
34
|
+
if (!isTsConfig(doc.sourcePath) || !acc.isViteFile(doc.sourcePath)) return doc;
|
|
35
|
+
return updateAt(doc, TYPES_PATH, types => isArray(types) && elementsOf(types).length > 0 ?
|
|
36
|
+
appendElement(types, jsonLiteral("vite/client"), {unique: true}) : types);
|
|
44
37
|
}
|
|
45
38
|
};
|
|
46
39
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import {ExecutionContext, Recipe, TreeVisitor, check, not, randomId} from "@openrewrite/rewrite";
|
|
2
|
-
import {isVendoredOrBundled, JavaScriptVisitor, JS, namesDeclaredWithin, namesUsedWithin, template} from "@openrewrite/rewrite/javascript";
|
|
2
|
+
import {isObjectLiteral, isVendoredOrBundled, JavaScriptVisitor, JS, namesDeclaredWithin, namesUsedWithin, template} from "@openrewrite/rewrite/javascript";
|
|
3
3
|
import {J} from "@openrewrite/rewrite/java";
|
|
4
4
|
import {anyNode, isThis} from "../util/tree-search";
|
|
5
5
|
import {extendsReactComponentByName} from "../util/react-class";
|
|
@@ -142,7 +142,7 @@ export class MigrateDeprecatedLifecycles extends Recipe {
|
|
|
142
142
|
const hasState = statements.some(s => this.declaresMember(s.element, 'state'));
|
|
143
143
|
const hasConstructor = statements.some(s => s.element.kind === J.Kind.MethodDeclaration
|
|
144
144
|
&& (s.element as J.MethodDeclaration).name.simpleName === 'constructor');
|
|
145
|
-
if (args.length !== 1 || args[0]
|
|
145
|
+
if (args.length !== 1 || !isObjectLiteral(args[0]) || hasState || hasConstructor) return undefined;
|
|
146
146
|
const sample = await template`class Sample { state = ${args[0]}; }`.apply(cd, this.cursor) as J.ClassDeclaration | undefined;
|
|
147
147
|
if (!await usesThisOnlyForPropsAndContext(args[0])) return undefined;
|
|
148
148
|
const field = sample?.body.statements[0];
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import {ExecutionContext, Recipe, TreeVisitor} from "@openrewrite/rewrite";
|
|
2
|
-
import {
|
|
3
|
-
|
|
2
|
+
import {
|
|
3
|
+
elementsOf, findMember, getMemberKeyName, isArray, isLiteral, isObject, Json, JsonVisitor, mapElements, updateMember
|
|
4
|
+
} from "@openrewrite/rewrite/json";
|
|
4
5
|
|
|
5
6
|
type ConfigKind = 'tsconfig' | 'babel' | 'eslint';
|
|
6
7
|
|
|
@@ -9,7 +10,7 @@ const BABEL_JSX_PRESETS = new Set([
|
|
|
9
10
|
'@babel/react',
|
|
10
11
|
'@babel/plugin-transform-react-jsx',
|
|
11
12
|
]);
|
|
12
|
-
const ESLINT_RULES =
|
|
13
|
+
const ESLINT_RULES = ['react/react-in-jsx-scope', 'react/jsx-uses-react'];
|
|
13
14
|
|
|
14
15
|
function configKind(sourcePath: string): ConfigKind | undefined {
|
|
15
16
|
const name = sourcePath.split('/').pop()!;
|
|
@@ -21,33 +22,28 @@ function configKind(sourcePath: string): ConfigKind | undefined {
|
|
|
21
22
|
|
|
22
23
|
function withString(literal: Json.Literal, value: string): Json.Literal {
|
|
23
24
|
const quote = literal.source.startsWith("'") ? "'" : '"';
|
|
24
|
-
return
|
|
25
|
-
draft.source = `${quote}${value}${quote}`;
|
|
26
|
-
draft.value = value;
|
|
27
|
-
});
|
|
25
|
+
return {...literal, source: `${quote}${value}${quote}`, value};
|
|
28
26
|
}
|
|
29
27
|
|
|
30
|
-
function
|
|
31
|
-
|
|
32
|
-
const members = object.members.map(padded => {
|
|
33
|
-
const element = padded.element;
|
|
34
|
-
if (!isMember(element)) return padded;
|
|
35
|
-
const updated = fn(element);
|
|
36
|
-
if (updated === element) return padded;
|
|
37
|
-
changed = true;
|
|
38
|
-
return create(padded, draft => {
|
|
39
|
-
draft.element = updated as any;
|
|
40
|
-
});
|
|
41
|
-
});
|
|
42
|
-
return changed ? create(object, draft => {
|
|
43
|
-
draft.members = members as any;
|
|
44
|
-
}) : object;
|
|
28
|
+
function withValue(member: Json.Member, value: Json.Value): Json.Member {
|
|
29
|
+
return value === member.value ? member : {...member, value};
|
|
45
30
|
}
|
|
46
31
|
|
|
47
|
-
function
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
32
|
+
function updateJsx(value: Json.Value): Json.Value {
|
|
33
|
+
if (!isLiteral(value)) return value;
|
|
34
|
+
if (value.value === 'react') return withString(value, 'react-jsx');
|
|
35
|
+
if (value.value === 'react-dev') return withString(value, 'react-jsxdev');
|
|
36
|
+
return value;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const isOff = (v: unknown) => v === 'off' || v === 0;
|
|
40
|
+
|
|
41
|
+
function turnRuleOff(value: Json.Value): Json.Value {
|
|
42
|
+
if (isLiteral(value)) return isOff(value.value) ? value : withString(value, 'off');
|
|
43
|
+
if (!isArray(value)) return value;
|
|
44
|
+
const [first] = elementsOf(value);
|
|
45
|
+
if (!first || !isLiteral(first) || isOff(first.value)) return value;
|
|
46
|
+
return mapElements(value, element => element === first ? withString(first, 'off') : element);
|
|
51
47
|
}
|
|
52
48
|
|
|
53
49
|
/**
|
|
@@ -87,13 +83,12 @@ export class UseNewJsxTransform extends Recipe {
|
|
|
87
83
|
if (!isObject(visited.value)) return visited;
|
|
88
84
|
const key = getMemberKeyName(visited);
|
|
89
85
|
if (this.kind === 'tsconfig' && key === 'compilerOptions') {
|
|
90
|
-
const names = visited.value.members.map(m => isMember(m.element) ? getMemberKeyName(m.element) : undefined);
|
|
91
86
|
// TS5089: `jsxFactory`/`jsxFragmentFactory` cannot be combined with `react-jsx`
|
|
92
|
-
if (
|
|
93
|
-
return withValue(visited,
|
|
87
|
+
if (findMember(visited.value, 'jsxFactory') || findMember(visited.value, 'jsxFragmentFactory')) return visited;
|
|
88
|
+
return withValue(visited, updateMember(visited.value, 'jsx', updateJsx));
|
|
94
89
|
}
|
|
95
90
|
if (this.kind === 'eslint' && key === 'rules') {
|
|
96
|
-
return withValue(visited,
|
|
91
|
+
return withValue(visited, ESLINT_RULES.reduce((rules, rule) => updateMember(rules, rule, turnRuleOff), visited.value));
|
|
97
92
|
}
|
|
98
93
|
return visited;
|
|
99
94
|
}
|
|
@@ -101,49 +96,12 @@ export class UseNewJsxTransform extends Recipe {
|
|
|
101
96
|
protected override async visitArray(array: Json.Array, ctx: ExecutionContext): Promise<Json | undefined> {
|
|
102
97
|
const visited = await super.visitArray(array, ctx) as Json.Array;
|
|
103
98
|
if (this.kind !== 'babel') return visited;
|
|
104
|
-
const [name, options] = visited
|
|
99
|
+
const [name, options] = elementsOf(visited);
|
|
105
100
|
if (!name || !options || !isLiteral(name) || !BABEL_JSX_PRESETS.has(name.value) || !isObject(options)) return visited;
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
return getMemberKeyName(m) === 'runtime' && isLiteral(value) && value.value === 'classic'
|
|
111
|
-
? withValue(m, withString(value, 'automatic')) : m;
|
|
112
|
-
});
|
|
113
|
-
if (updated === options) return visited;
|
|
114
|
-
return create(visited, draft => {
|
|
115
|
-
draft.values[1] = create(visited.values[1], d => {
|
|
116
|
-
d.element = updated as any;
|
|
117
|
-
}) as any;
|
|
118
|
-
});
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
private updateJsx(member: Json.Member): Json.Member {
|
|
122
|
-
const value = member.value;
|
|
123
|
-
if (getMemberKeyName(member) !== 'jsx' || !isLiteral(value)) return member;
|
|
124
|
-
if (value.value === 'react') return withValue(member, withString(value, 'react-jsx'));
|
|
125
|
-
if (value.value === 'react-dev') return withValue(member, withString(value, 'react-jsxdev'));
|
|
126
|
-
return member;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
private turnRuleOff(member: Json.Member): Json.Member {
|
|
130
|
-
const key = getMemberKeyName(member);
|
|
131
|
-
if (!key || !ESLINT_RULES.has(key)) return member;
|
|
132
|
-
const value = member.value;
|
|
133
|
-
const isOff = (v: any) => v === 'off' || v === 0;
|
|
134
|
-
if (isLiteral(value)) {
|
|
135
|
-
return isOff(value.value) ? member : withValue(member, withString(value, 'off'));
|
|
136
|
-
}
|
|
137
|
-
if (isArray(value)) {
|
|
138
|
-
const first = value.values[0]?.element;
|
|
139
|
-
if (!first || !isLiteral(first) || isOff(first.value)) return member;
|
|
140
|
-
return withValue(member, create(value, draft => {
|
|
141
|
-
draft.values[0] = create(value.values[0], d => {
|
|
142
|
-
d.element = withString(first, 'off') as any;
|
|
143
|
-
}) as any;
|
|
144
|
-
}));
|
|
145
|
-
}
|
|
146
|
-
return member;
|
|
101
|
+
if (findMember(options, 'pragma') || findMember(options, 'pragmaFrag')) return visited;
|
|
102
|
+
const updated = updateMember(options, 'runtime', value =>
|
|
103
|
+
isLiteral(value) && value.value === 'classic' ? withString(value, 'automatic') : value);
|
|
104
|
+
return updated === options ? visited : mapElements(visited, value => value === options ? updated : value);
|
|
147
105
|
}
|
|
148
106
|
};
|
|
149
107
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import {check, ExecutionContext, foundSearchResult, not,
|
|
1
|
+
import {check, ExecutionContext, foundSearchResult, not, Recipe, TreeVisitor} from "@openrewrite/rewrite";
|
|
2
2
|
import {
|
|
3
3
|
capture,
|
|
4
|
+
isEqual,
|
|
4
5
|
isVendoredOrBundled,
|
|
5
6
|
JavaScriptVisitor,
|
|
6
7
|
JS,
|
|
@@ -27,6 +28,7 @@ const RESULT_IS_USED = `\`root.render()\` returns \`void\`, not the component in
|
|
|
27
28
|
* statement that renders a container once is rewritten. A call inside a function (it may run again
|
|
28
29
|
* and would create a second root), a call whose result is used (`root.render()` returns `void`),
|
|
29
30
|
* a container rendered twice, and a file that already uses the name `root` are marked instead.
|
|
31
|
+
* Only the first container in a file is rewritten, as the next one would redeclare `root`.
|
|
30
32
|
*
|
|
31
33
|
* Before:
|
|
32
34
|
* ```tsx
|
|
@@ -46,7 +48,7 @@ const RESULT_IS_USED = `\`root.render()\` returns \`void\`, not the component in
|
|
|
46
48
|
export class ReplaceReactDomRender extends Recipe {
|
|
47
49
|
readonly name = "org.openrewrite.react.18.replace-reactdom-render";
|
|
48
50
|
readonly displayName: string = "Replace `ReactDOM.render` with `createRoot`";
|
|
49
|
-
readonly description: string = "Migrates a top-level `ReactDOM.render()` call to the `createRoot()` API from `react-dom/client` introduced in React 18, creating the root once. Calls inside functions, calls whose result is used and containers that are rendered more than once are marked for manual migration
|
|
51
|
+
readonly description: string = "Migrates a top-level `ReactDOM.render()` call to the `createRoot()` API from `react-dom/client` introduced in React 18, creating the root once. Calls inside functions, calls whose result is used and containers that are rendered more than once are marked for manual migration, as is every container after the first in a file, since each would redeclare `root`.";
|
|
50
52
|
|
|
51
53
|
async editor(): Promise<TreeVisitor<any, ExecutionContext>> {
|
|
52
54
|
const element = capture<Expression>();
|
|
@@ -69,12 +71,12 @@ export class ReplaceReactDomRender extends Recipe {
|
|
|
69
71
|
}));
|
|
70
72
|
|
|
71
73
|
return check(not(isVendoredOrBundled()), new class extends JavaScriptVisitor<ExecutionContext> {
|
|
72
|
-
private
|
|
74
|
+
private containers: Expression[] = [];
|
|
73
75
|
private rootNameTaken = false;
|
|
74
76
|
private inlinedBlocks = new Set<string>();
|
|
75
77
|
|
|
76
78
|
override async visitJsCompilationUnit(cu: JS.CompilationUnit, p: ExecutionContext): Promise<J | undefined> {
|
|
77
|
-
this.
|
|
79
|
+
this.containers = [];
|
|
78
80
|
this.rootNameTaken = false;
|
|
79
81
|
this.inlinedBlocks = new Set();
|
|
80
82
|
await this.survey(cu);
|
|
@@ -94,8 +96,7 @@ export class ReplaceReactDomRender extends Recipe {
|
|
|
94
96
|
if (position === "used") {
|
|
95
97
|
return foundSearchResult(m, RESULT_IS_USED);
|
|
96
98
|
}
|
|
97
|
-
|
|
98
|
-
if (position === "nested" || this.rootNameTaken || this.containerCounts.get(containerSource)! > 1) {
|
|
99
|
+
if (position === "nested" || this.rootNameTaken || await this.rendersInto(match.get(container)!) > 1) {
|
|
99
100
|
return foundSearchResult(m, ONE_ROOT_PER_CONTAINER);
|
|
100
101
|
}
|
|
101
102
|
|
|
@@ -104,6 +105,7 @@ export class ReplaceReactDomRender extends Recipe {
|
|
|
104
105
|
return m;
|
|
105
106
|
}
|
|
106
107
|
maybeRemoveImport(this, "react-dom", "render");
|
|
108
|
+
this.rootNameTaken = true;
|
|
107
109
|
this.inlinedBlocks.add(result.id);
|
|
108
110
|
return result;
|
|
109
111
|
}
|
|
@@ -134,6 +136,14 @@ export class ReplaceReactDomRender extends Recipe {
|
|
|
134
136
|
return "used";
|
|
135
137
|
}
|
|
136
138
|
|
|
139
|
+
private async rendersInto(target: Expression): Promise<number> {
|
|
140
|
+
let count = 0;
|
|
141
|
+
for (const c of this.containers) {
|
|
142
|
+
if (await isEqual(c, target)) count++;
|
|
143
|
+
}
|
|
144
|
+
return count;
|
|
145
|
+
}
|
|
146
|
+
|
|
137
147
|
private async survey(cu: JS.CompilationUnit): Promise<void> {
|
|
138
148
|
const outer = this;
|
|
139
149
|
await new class extends JavaScriptVisitor<void> {
|
|
@@ -147,8 +157,7 @@ export class ReplaceReactDomRender extends Recipe {
|
|
|
147
157
|
override async visitMethodInvocation(m: J.MethodInvocation, q: void): Promise<J | undefined> {
|
|
148
158
|
const match = await renderPattern.match(m, this.cursor);
|
|
149
159
|
if (match) {
|
|
150
|
-
|
|
151
|
-
outer.containerCounts.set(source, (outer.containerCounts.get(source) ?? 0) + 1);
|
|
160
|
+
outer.containers.push(match.get(container)!);
|
|
152
161
|
}
|
|
153
162
|
return super.visitMethodInvocation(m, q);
|
|
154
163
|
}
|
|
@@ -1,68 +1,114 @@
|
|
|
1
|
-
import {ExecutionContext,
|
|
1
|
+
import {and, check, Cursor, ExecutionContext, foundSearchResult, not, Recipe, TreeVisitor} from "@openrewrite/rewrite";
|
|
2
2
|
import {
|
|
3
3
|
capture,
|
|
4
|
+
isEqual,
|
|
4
5
|
JavaScriptVisitor,
|
|
6
|
+
isVendoredOrBundled,
|
|
7
|
+
JS,
|
|
5
8
|
maybeRemoveImport,
|
|
6
9
|
pattern,
|
|
7
|
-
|
|
10
|
+
raw,
|
|
8
11
|
template,
|
|
9
|
-
|
|
12
|
+
usesMethod
|
|
10
13
|
} from "@openrewrite/rewrite/javascript";
|
|
11
|
-
import {Expression, J} from "@openrewrite/rewrite/java";
|
|
14
|
+
import {Expression, isIdentifier, J} from "@openrewrite/rewrite/java";
|
|
15
|
+
|
|
16
|
+
const GUIDE = "https://react.dev/blog/2022/03/08/react-18-upgrade-guide#updates-to-client-rendering-apis";
|
|
17
|
+
const NO_ROOT_MESSAGE = `No \`root\` for this container is in scope. Call \`unmount()\` on the root returned by \`createRoot\`: ${GUIDE}`;
|
|
18
|
+
const VALUE_USED_MESSAGE = `The result of \`unmountComponentAtNode\` is used, but \`root.unmount()\` returns \`void\`: ${GUIDE}`;
|
|
12
19
|
|
|
13
20
|
/**
|
|
14
|
-
* Replaces `
|
|
21
|
+
* Replaces `unmountComponentAtNode(container)` with `root.unmount()` when the
|
|
22
|
+
* `root` returned by `createRoot(container)` for the same container is in scope.
|
|
15
23
|
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
24
|
+
* `createRoot(container).unmount()` would unmount a new, empty root and leave the
|
|
25
|
+
* mounted tree alive, so without a root in scope the call is kept and marked.
|
|
26
|
+
* It is also kept and marked when its `boolean` result is used, because
|
|
27
|
+
* `unmount()` returns `void`.
|
|
18
28
|
*
|
|
19
29
|
* Before:
|
|
20
30
|
* ```tsx
|
|
21
|
-
*
|
|
22
|
-
* unmountComponentAtNode(
|
|
31
|
+
* const root = createRoot(container);
|
|
32
|
+
* unmountComponentAtNode(container);
|
|
23
33
|
* ```
|
|
24
34
|
*
|
|
25
35
|
* After:
|
|
26
36
|
* ```tsx
|
|
27
|
-
*
|
|
28
|
-
*
|
|
37
|
+
* const root = createRoot(container);
|
|
38
|
+
* root.unmount();
|
|
29
39
|
* ```
|
|
30
40
|
*
|
|
31
41
|
* @see https://react.dev/blog/2022/03/08/react-18-upgrade-guide#updates-to-client-rendering-apis
|
|
32
42
|
*/
|
|
33
43
|
export class ReplaceUnmountComponentAtNode extends Recipe {
|
|
34
44
|
readonly name = "org.openrewrite.react.18.replace-unmount-component-at-node";
|
|
35
|
-
readonly displayName: string = "Replace `unmountComponentAtNode` with `
|
|
36
|
-
readonly description: string = "Replaces `ReactDOM.unmountComponentAtNode(container)` with `
|
|
45
|
+
readonly displayName: string = "Replace `unmountComponentAtNode` with `root.unmount()`";
|
|
46
|
+
readonly description: string = "Replaces `ReactDOM.unmountComponentAtNode(container)` with `root.unmount()` when the `root` created by `createRoot(container)` is in scope. Otherwise, and when the call's result is used, the call is kept and marked.";
|
|
37
47
|
|
|
38
48
|
async editor(): Promise<TreeVisitor<any, ExecutionContext>> {
|
|
39
|
-
const
|
|
49
|
+
const container = capture<Expression>();
|
|
50
|
+
const unmountCall = pattern`unmountComponentAtNode(${container})`.configure({
|
|
40
51
|
context: ['import { unmountComponentAtNode } from "react-dom";'],
|
|
41
52
|
dependencies: {'@types/react-dom': '^17.0.0'}
|
|
42
|
-
};
|
|
53
|
+
});
|
|
54
|
+
const rootContainer = capture<Expression>();
|
|
55
|
+
const createRootCall = pattern`createRoot(${rootContainer})`.configure({
|
|
56
|
+
context: ['import { createRoot } from "react-dom/client";'],
|
|
57
|
+
dependencies: {'@types/react-dom': '^18.0.0'}
|
|
58
|
+
});
|
|
43
59
|
|
|
44
|
-
const
|
|
60
|
+
const visitor = new class extends JavaScriptVisitor<ExecutionContext> {
|
|
61
|
+
override async visitMethodInvocation(method: J.MethodInvocation, p: ExecutionContext): Promise<J | undefined> {
|
|
62
|
+
const m = await super.visitMethodInvocation(method, p) as J.MethodInvocation;
|
|
45
63
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
context: ['import { createRoot } from "react-dom/client";'],
|
|
50
|
-
dependencies: {'@types/react-dom': '^18.0.0'}
|
|
51
|
-
})
|
|
52
|
-
}));
|
|
64
|
+
const match = await unmountCall.match(m, this.cursor);
|
|
65
|
+
const containerArg = match?.get(container);
|
|
66
|
+
if (!containerArg) return m;
|
|
53
67
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
68
|
+
if (!isStatement(this.cursor)) {
|
|
69
|
+
return foundSearchResult(m, VALUE_USED_MESSAGE);
|
|
70
|
+
}
|
|
57
71
|
|
|
58
|
-
const
|
|
59
|
-
if (
|
|
60
|
-
|
|
61
|
-
return result;
|
|
72
|
+
const root = await this.findRoot(containerArg);
|
|
73
|
+
if (!root) {
|
|
74
|
+
return foundSearchResult(m, NO_ROOT_MESSAGE);
|
|
62
75
|
}
|
|
63
76
|
|
|
64
|
-
|
|
77
|
+
maybeRemoveImport(this, "react-dom", "unmountComponentAtNode");
|
|
78
|
+
return await template`${raw(root)}.unmount()`.apply(m, this.cursor);
|
|
65
79
|
}
|
|
66
|
-
|
|
80
|
+
|
|
81
|
+
/** The name of a `const root = createRoot(<container>)` in an enclosing scope. */
|
|
82
|
+
private async findRoot(containerArg: Expression): Promise<string | undefined> {
|
|
83
|
+
for (let c: Cursor | undefined = this.cursor; c; c = c.parent) {
|
|
84
|
+
const statements: any[] | undefined = c.value?.kind === J.Kind.Block || c.value?.kind === JS.Kind.CompilationUnit
|
|
85
|
+
? c.value.statements
|
|
86
|
+
: undefined;
|
|
87
|
+
for (const statement of statements ?? []) {
|
|
88
|
+
const decl = statement.element ?? statement;
|
|
89
|
+
if (decl.kind !== J.Kind.VariableDeclarations) continue;
|
|
90
|
+
for (const v of decl.variables) {
|
|
91
|
+
const named = v.element;
|
|
92
|
+
const init = named.initializer?.element;
|
|
93
|
+
if (!init || !isIdentifier(named.name)) continue;
|
|
94
|
+
const rootArg = (await createRootCall.match(init, this.cursor))?.get(rootContainer);
|
|
95
|
+
if (rootArg && await isEqual(rootArg, containerArg)) {
|
|
96
|
+
return named.name.simpleName;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return undefined;
|
|
102
|
+
}
|
|
103
|
+
}();
|
|
104
|
+
|
|
105
|
+
return check(and(usesMethod("react-dom unmountComponentAtNode(..)"), not(isVendoredOrBundled())), visitor);
|
|
67
106
|
}
|
|
68
107
|
}
|
|
108
|
+
|
|
109
|
+
/** Whether the invocation at the cursor is an expression statement, so its value is unused. */
|
|
110
|
+
function isStatement(cursor: Cursor): boolean {
|
|
111
|
+
const parent = cursor.parent?.value;
|
|
112
|
+
const grand = cursor.parent?.parent?.value;
|
|
113
|
+
return parent?.kind === J.Kind.RightPadded && (grand?.kind === J.Kind.Block || grand?.kind === JS.Kind.CompilationUnit);
|
|
114
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import {ExecutionContext, Recipe, TreeVisitor, check, not} from "@openrewrite/rewrite";
|
|
2
|
-
import {JavaScriptVisitor, JS, isVendoredOrBundled} from "@openrewrite/rewrite/javascript";
|
|
2
|
+
import {isObjectLiteral, JavaScriptVisitor, JS, isVendoredOrBundled} from "@openrewrite/rewrite/javascript";
|
|
3
3
|
import {Expression, isIdentifier, J} from "@openrewrite/rewrite/java";
|
|
4
4
|
import {create} from "mutative";
|
|
5
5
|
|
|
@@ -111,20 +111,16 @@ export class ReplaceDefaultProps extends Recipe {
|
|
|
111
111
|
private extractObjectLiteralDefaults(expr: Expression): Map<string, Expression> {
|
|
112
112
|
const defaults = new Map<string, Expression>();
|
|
113
113
|
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
if (
|
|
123
|
-
|
|
124
|
-
const nameExpr = prop.name.element;
|
|
125
|
-
if (isIdentifier(nameExpr) && prop.initializer) {
|
|
126
|
-
defaults.set(nameExpr.simpleName, prop.initializer);
|
|
127
|
-
}
|
|
114
|
+
if (isObjectLiteral(expr) && expr.body) {
|
|
115
|
+
for (const stmt of expr.body.statements) {
|
|
116
|
+
const s = stmt.element;
|
|
117
|
+
if (!s) continue;
|
|
118
|
+
|
|
119
|
+
if (s.kind === JS.Kind.PropertyAssignment) {
|
|
120
|
+
const prop = s as JS.PropertyAssignment;
|
|
121
|
+
const nameExpr = prop.name.element;
|
|
122
|
+
if (isIdentifier(nameExpr) && prop.initializer) {
|
|
123
|
+
defaults.set(nameExpr.simpleName, prop.initializer);
|
|
128
124
|
}
|
|
129
125
|
}
|
|
130
126
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import {ExecutionContext, Recipe, TreeVisitor, check, not} from "@openrewrite/rewrite";
|
|
2
|
-
import {JavaScriptVisitor, JS, isVendoredOrBundled} from "@openrewrite/rewrite/javascript";
|
|
2
|
+
import {JavaScriptVisitor, JS, isVendoredOrBundled, withStringValue} from "@openrewrite/rewrite/javascript";
|
|
3
3
|
import {J} from "@openrewrite/rewrite/java";
|
|
4
4
|
import {create} from "mutative";
|
|
5
5
|
|
|
@@ -43,12 +43,7 @@ export class ReplaceReactShallowRenderer extends Recipe {
|
|
|
43
43
|
const literal = modSpec as J.Literal;
|
|
44
44
|
if (literal.value !== 'react-test-renderer/shallow') return paddedStmt;
|
|
45
45
|
|
|
46
|
-
const newLiteral =
|
|
47
|
-
draft.value = 'react-shallow-renderer';
|
|
48
|
-
if (draft.valueSource) {
|
|
49
|
-
draft.valueSource = draft.valueSource.replace('react-test-renderer/shallow', 'react-shallow-renderer');
|
|
50
|
-
}
|
|
51
|
-
});
|
|
46
|
+
const newLiteral = withStringValue(literal, 'react-shallow-renderer');
|
|
52
47
|
|
|
53
48
|
const newModSpec = (imp.moduleSpecifier as any).element
|
|
54
49
|
? {...(imp.moduleSpecifier as any), element: newLiteral}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {Cursor, emptyMarkers, ExecutionContext, findMarker, Marker, printer, randomId, Recipe, Tree, TreeVisitor, check, not} from "@openrewrite/rewrite";
|
|
2
|
-
import {compilationUnitOf, JavaScriptVisitor, JS, maybeBind, maybeUnbind, moduleBindings, raw, template, isVendoredOrBundled} from "@openrewrite/rewrite/javascript";
|
|
3
|
-
import {Comment, J, TextComment} from "@openrewrite/rewrite/java";
|
|
2
|
+
import {compilationUnitOf, isObjectLiteral, JavaScriptVisitor, JS, maybeBind, maybeUnbind, moduleBindings, raw, template, isVendoredOrBundled} from "@openrewrite/rewrite/javascript";
|
|
3
|
+
import {Comment, isTextComment, J, TextComment} from "@openrewrite/rewrite/java";
|
|
4
4
|
|
|
5
5
|
const LIFECYCLE_METHODS = new Set([
|
|
6
6
|
'render',
|
|
@@ -110,11 +110,8 @@ export class CreateClassToES6 extends Recipe {
|
|
|
110
110
|
const args = methodInv.arguments.elements;
|
|
111
111
|
if (args.length === 0) return vd;
|
|
112
112
|
|
|
113
|
-
const
|
|
114
|
-
if (
|
|
115
|
-
|
|
116
|
-
const specObj = specArg as J.NewClass;
|
|
117
|
-
if (!specObj.body) return vd;
|
|
113
|
+
const specObj = args[0].element;
|
|
114
|
+
if (!isObjectLiteral(specObj) || !specObj.body) return vd;
|
|
118
115
|
|
|
119
116
|
// Check for mixins — skip conversion if present
|
|
120
117
|
if (this.hasMixins(specObj.body)) return vd;
|
|
@@ -434,7 +431,7 @@ async function inPlaceOf(member: J.RightPadded<J>, field: J, value: J, lineFollo
|
|
|
434
431
|
const next = j < space.comments.length - 1 ? '' : around.after[i + 1]?.whitespace;
|
|
435
432
|
// The last one ends the line where a line comment would take in what follows
|
|
436
433
|
const suffix = next !== undefined ? shift(c.suffix + next) :
|
|
437
|
-
((c
|
|
434
|
+
(isTextComment(c) && c.multiline || lineFollows ? '' : lineStart ?? '\n') + member.after.whitespace;
|
|
438
435
|
return {...shiftComment(c, shift), suffix};
|
|
439
436
|
}));
|
|
440
437
|
return {
|
|
@@ -530,7 +527,7 @@ function shiftLines(text: string, from: string, to: string): string {
|
|
|
530
527
|
|
|
531
528
|
/** The comment with `shift` applied to the lines its text spans, as only a block comment's can. */
|
|
532
529
|
function shiftComment(comment: Comment, shift: (text: string) => string): Comment {
|
|
533
|
-
return (comment
|
|
530
|
+
return isTextComment(comment) && comment.multiline ? {...comment, text: shift(comment.text)} as TextComment : comment;
|
|
534
531
|
}
|
|
535
532
|
|
|
536
533
|
/** Whether the file binds `name` with an `import type`, as its default or namespace import. */
|