@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,10 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
newStringMember, removeMember, replaceMember, rootObject, scriptsKeepReactScripts,
|
|
6
|
-
REACT_SCRIPTS, VITE, VITE_PLUGIN_REACT
|
|
7
|
-
} from "./shared";
|
|
1
|
+
import {ExecutionContext, Option, SourceFile} from "@openrewrite/rewrite";
|
|
2
|
+
import {JavaDependencyRecipe} from "../../util/java-recipe-proxy";
|
|
3
|
+
import {getMemberKeyName, Json, membersOf} from "@openrewrite/rewrite/json";
|
|
4
|
+
import {hasDependency, memberObject, runsOnVite, isPackageJson, rootObject, stringValue, VITE, VITE_PLUGIN_REACT} from "./shared";
|
|
8
5
|
|
|
9
6
|
/**
|
|
10
7
|
* Adds `vite` and `@vitejs/plugin-react` to `devDependencies` of a package that depends on `react-scripts`,
|
|
@@ -15,16 +12,15 @@ import {
|
|
|
15
12
|
* ```
|
|
16
13
|
*
|
|
17
14
|
* The default versions are a peer-consistent pair that both need Node 20.19 or later. `react-scripts` stays
|
|
18
|
-
* while a script such as `test` still runs it, because it provides Jest. The `eject` script
|
|
19
|
-
*
|
|
20
|
-
* `build` script still depends on `react-scripts` is left to the finders.
|
|
15
|
+
* while a script such as `test` still runs it, because it provides Jest. The `eject` script also retains the dependency until script migration removes it.
|
|
16
|
+
* Packages using `craco`, `react-app-rewired` or wrapped `start` or `build` scripts are left to the finders.
|
|
21
17
|
*
|
|
22
18
|
* @see https://react.dev/blog/2025/02/14/sunsetting-create-react-app#how-to-migrate-to-a-build-tool
|
|
23
19
|
*/
|
|
24
|
-
export class ReplaceReactScriptsDependency extends
|
|
20
|
+
export class ReplaceReactScriptsDependency extends JavaDependencyRecipe {
|
|
25
21
|
readonly name = "org.openrewrite.react.cra-to-vite.replace-react-scripts-dependency";
|
|
26
22
|
readonly displayName = "Replace `react-scripts` dependency with Vite";
|
|
27
|
-
readonly description = "Adds `vite` and `@vitejs/plugin-react` as dev dependencies
|
|
23
|
+
readonly description = "Adds `vite` and `@vitejs/plugin-react` together as dev dependencies and removes `react-scripts` or scoped forks only when no script still invokes them. Regenerates the lock natively; declined dependency edits leave the manifest and lock consistent. Wrapped CRA packages are left unchanged.";
|
|
28
24
|
|
|
29
25
|
@Option({
|
|
30
26
|
displayName: "Vite version",
|
|
@@ -42,55 +38,53 @@ export class ReplaceReactScriptsDependency extends Recipe {
|
|
|
42
38
|
})
|
|
43
39
|
pluginReactVersion?: string;
|
|
44
40
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
this.pluginReactVersion ??= "^5.2.0";
|
|
49
|
-
}
|
|
41
|
+
/** Package directories (with a trailing slash, or an empty string at root) accepted by the native engine. Known after generate. */
|
|
42
|
+
readonly landed = new Set<string>();
|
|
43
|
+
private readonly manifests: Json.Document[] = [];
|
|
50
44
|
|
|
51
|
-
|
|
52
|
-
const viteVersion =
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
45
|
+
constructor(options?: { viteVersion?: string; pluginReactVersion?: string; addSvgr?: boolean }) {
|
|
46
|
+
const {viteVersion = "^7.0.0", pluginReactVersion = "^5.2.0", addSvgr = false} = options ?? {};
|
|
47
|
+
super("org.openrewrite.react.migrate.ReplaceReactScriptsDependency", {viteVersion, pluginReactVersion, addSvgr}, true);
|
|
48
|
+
this.viteVersion = viteVersion;
|
|
49
|
+
this.pluginReactVersion = pluginReactVersion;
|
|
50
|
+
this.addSvgr = addSvgr;
|
|
51
|
+
}
|
|
58
52
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
if (!hasDependency(pkg, VITE_PLUGIN_REACT)) additions.push([VITE_PLUGIN_REACT, pluginVersion]);
|
|
53
|
+
@Option({displayName: "Add SVGR", description: "Also add `vite-plugin-svgr` when the caller migrates SVG ReactComponent imports in this package.", required: false})
|
|
54
|
+
addSvgr?: boolean;
|
|
62
55
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
}
|
|
68
|
-
};
|
|
56
|
+
override initialValue(): number {
|
|
57
|
+
this.landed.clear();
|
|
58
|
+
this.manifests.length = 0;
|
|
59
|
+
return super.initialValue();
|
|
69
60
|
}
|
|
70
|
-
}
|
|
71
61
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
value: additions.reduce((obj, [name, version]) =>
|
|
78
|
-
insertMember(obj, newStringMember(name, version, emptySpace), nestedLayout(pkg)), dev)
|
|
79
|
-
}));
|
|
62
|
+
protected override scanned(manifest: SourceFile): boolean {
|
|
63
|
+
if (manifest.kind !== Json.Kind.Document || !isPackageJson(manifest.sourcePath)) return true;
|
|
64
|
+
const pkg = rootObject(manifest as Json.Document);
|
|
65
|
+
if (pkg && runsOnVite(pkg)) this.manifests.push(manifest as Json.Document);
|
|
66
|
+
return true;
|
|
80
67
|
}
|
|
81
|
-
const layout = nestedLayout(pkg);
|
|
82
|
-
let block: Json.Object = {kind: Json.Kind.Object, id: randomId(), prefix: emptySpace, markers: emptyMarkers, members: []};
|
|
83
|
-
for (const [name, version] of additions) block = insertMember(block, newStringMember(name, version, emptySpace), layout);
|
|
84
|
-
return insertMember(pkg, newMember("devDependencies", block, pkg.members[0]?.element.prefix ?? emptySpace));
|
|
85
|
-
}
|
|
86
68
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
const
|
|
91
|
-
|
|
92
|
-
|
|
69
|
+
override async generate(acc: number, ctx: ExecutionContext): Promise<SourceFile[]> {
|
|
70
|
+
const generated = await super.generate(acc, ctx);
|
|
71
|
+
const editor = await this.java.editorWithData(acc);
|
|
72
|
+
for (const manifest of this.manifests) {
|
|
73
|
+
const edited = await editor.visit<Json.Document>(manifest, ctx);
|
|
74
|
+
const pkg = edited && rootObject(edited);
|
|
75
|
+
const before = rootObject(manifest)!;
|
|
76
|
+
const scripts = memberObject(before, "scripts");
|
|
77
|
+
const keepCra = scripts && membersOf(scripts).some(member => stringValue(member.value)?.includes("react-scripts"));
|
|
78
|
+
const removed = keepCra || ["dependencies", "devDependencies", "optionalDependencies", "peerDependencies"].every(section => {
|
|
79
|
+
const deps = pkg && memberObject(pkg, section);
|
|
80
|
+
return !deps || membersOf(deps).every(member => !/^(?:@[^/]+\/)?react-scripts$/.test(getMemberKeyName(member) ?? ""));
|
|
81
|
+
});
|
|
82
|
+
// A warning-only result from a declined removal is not a landed dependency edit.
|
|
83
|
+
if (edited !== manifest && removed && pkg && hasDependency(pkg, VITE) && hasDependency(pkg, VITE_PLUGIN_REACT) &&
|
|
84
|
+
(!this.addSvgr || hasDependency(pkg, "vite-plugin-svgr"))) {
|
|
85
|
+
this.landed.add(manifest.sourcePath.slice(0, -"package.json".length));
|
|
86
|
+
}
|
|
93
87
|
}
|
|
88
|
+
return generated;
|
|
94
89
|
}
|
|
95
|
-
return result;
|
|
96
90
|
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import {isBuiltin} from 'node:module';
|
|
2
|
+
import {satisfies, valid, validRange} from 'semver';
|
|
3
|
+
import {NodeResolutionResult, NodeResolutionResultQueries} from '@openrewrite/rewrite/javascript';
|
|
4
|
+
|
|
5
|
+
/** Select an installed version only when the original requests establish ordinary semver provenance. */
|
|
6
|
+
export function originalCraDependencyVersion(
|
|
7
|
+
resolution: NodeResolutionResult | undefined,
|
|
8
|
+
packageName: string,
|
|
9
|
+
): string | undefined {
|
|
10
|
+
if (!resolution?.resolvedDependencies || isBuiltin(packageName) || packageName.startsWith('node:')) {
|
|
11
|
+
return undefined;
|
|
12
|
+
}
|
|
13
|
+
const installed = NodeResolutionResultQueries.getAllResolvedVersions(resolution, packageName);
|
|
14
|
+
const versions = new Set(installed.map(dependency => dependency.version));
|
|
15
|
+
if (versions.size !== 1) return undefined;
|
|
16
|
+
const version = installed[0].version;
|
|
17
|
+
if (!version || !valid(version) || !/^\d/.test(version) || version.trim() !== version) return undefined;
|
|
18
|
+
|
|
19
|
+
let hasProvenance = false;
|
|
20
|
+
// Inspect every original request: a semver-looking fork can share the registry package's version.
|
|
21
|
+
for (const owner of [resolution, ...resolution.resolvedDependencies]) {
|
|
22
|
+
for (const scope of ['dependencies', 'devDependencies', 'peerDependencies', 'optionalDependencies', 'bundledDependencies'] as const) {
|
|
23
|
+
const requests = scope === 'bundledDependencies'
|
|
24
|
+
? ('bundledDependencies' in owner ? owner.bundledDependencies : undefined)
|
|
25
|
+
: owner[scope];
|
|
26
|
+
for (const request of requests ?? []) {
|
|
27
|
+
const targetsAlias = request.versionConstraint === `npm:${packageName}` ||
|
|
28
|
+
request.versionConstraint?.startsWith(`npm:${packageName}@`);
|
|
29
|
+
if (request.name !== packageName && request.resolved?.name !== packageName && !targetsAlias) continue;
|
|
30
|
+
if (request.name !== packageName || !request.versionConstraint?.trim() ||
|
|
31
|
+
!validRange(request.versionConstraint) ||
|
|
32
|
+
!satisfies(version, request.versionConstraint) ||
|
|
33
|
+
(request.resolved && (request.resolved.name !== packageName || request.resolved.version !== version))) {
|
|
34
|
+
return undefined;
|
|
35
|
+
}
|
|
36
|
+
hasProvenance = true;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return hasProvenance ? version : undefined;
|
|
41
|
+
}
|
|
@@ -1,10 +1,16 @@
|
|
|
1
1
|
import {ExecutionContext, ScanningRecipe, Tree, TreeVisitor} from "@openrewrite/rewrite";
|
|
2
|
-
import {Json} from "@openrewrite/rewrite/json";
|
|
2
|
+
import {Json, valueAt} from "@openrewrite/rewrite/json";
|
|
3
3
|
import {PlainText} from "@openrewrite/rewrite/text";
|
|
4
|
-
import {
|
|
4
|
+
import {findNodeResolutionResult, JS} from "@openrewrite/rewrite/javascript";
|
|
5
|
+
import {satisfies, valid, validRange} from 'semver';
|
|
6
|
+
import {craFilesOnly} from './cra-file-gate';
|
|
7
|
+
import {svgComponentName} from "./svg-component";
|
|
8
|
+
import {baseName, dirName, isCraPackage, runsOnVite, nearestRoot, isPackageJson, joinPath, rootObject, stringValue} from "./shared";
|
|
5
9
|
|
|
6
10
|
/** What the recipes that add or move files need to know about the whole repository before they edit it. */
|
|
7
11
|
export class CraProjects {
|
|
12
|
+
/** Optional admission decision populated before generators run, after all scanners have completed. */
|
|
13
|
+
allowedRoots?: ReadonlySet<string>;
|
|
8
14
|
/** Directories of the `package.json` files that Create React App built; `''` is the repository root. */
|
|
9
15
|
readonly roots = new Set<string>();
|
|
10
16
|
/** The subset of `roots` whose `start` and `build` will run on Vite, the only ones the edit recipes change. */
|
|
@@ -13,8 +19,11 @@ export class CraProjects {
|
|
|
13
19
|
readonly packageDirs = new Set<string>();
|
|
14
20
|
/** Every source path in the repository, to tell whether a file already exists. */
|
|
15
21
|
readonly paths = new Set<string>();
|
|
22
|
+
readonly svgComponentFiles = new Set<string>();
|
|
16
23
|
/** The `public/index.html` text of each directory. */
|
|
17
24
|
readonly indexHtml = new Map<string, string>();
|
|
25
|
+
/** Original manifests used to carry CRA's configuration into generated files. */
|
|
26
|
+
readonly configSettings = new Map<string, {homepage?: string; proxy?: string; reactVersion?: string}>();
|
|
18
27
|
|
|
19
28
|
/**
|
|
20
29
|
* True when the file belongs to a Create React App package: the nearest `package.json` above it, not any
|
|
@@ -28,13 +37,27 @@ export class CraProjects {
|
|
|
28
37
|
/** True when the file belongs to a package whose `start` and `build` will run on Vite, so the edit recipes may change it. */
|
|
29
38
|
isViteFile(sourcePath: string): boolean {
|
|
30
39
|
const owner = nearestRoot(this.packageDirs, sourcePath);
|
|
31
|
-
return owner !== undefined && this.
|
|
40
|
+
return owner !== undefined && this.mayMigrate(owner);
|
|
32
41
|
}
|
|
33
42
|
|
|
34
43
|
/** True when the file is client code that CRA bundled: below the `src/` of a package that runs on Vite. */
|
|
35
44
|
isViteClientSource(sourcePath: string): boolean {
|
|
36
45
|
const owner = nearestRoot(this.packageDirs, sourcePath);
|
|
37
|
-
return owner !== undefined && this.
|
|
46
|
+
return owner !== undefined && this.mayMigrate(owner) && sourcePath.startsWith(joinPath(owner, "src/"));
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** True when the file is client code (see isViteClientSource) of the package rooted at `root`. */
|
|
50
|
+
isClientSourceOf(root: string | undefined, sourcePath: string): boolean {
|
|
51
|
+
return nearestRoot(this.packageDirs, sourcePath) === root && this.isViteClientSource(sourcePath);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** True when some client source of the package rooted at `root` imports an SVG component. */
|
|
55
|
+
usesSvgr(root: string | undefined): boolean {
|
|
56
|
+
return [...this.svgComponentFiles].some(path => this.isClientSourceOf(root, path));
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
mayMigrate(root: string): boolean {
|
|
60
|
+
return this.viteRoots.has(root) && (this.allowedRoots === undefined || this.allowedRoots.has(root));
|
|
38
61
|
}
|
|
39
62
|
|
|
40
63
|
has(root: string, name: string): boolean {
|
|
@@ -61,9 +84,23 @@ export function scanCraProjects(acc: CraProjects): TreeVisitor<any, ExecutionCon
|
|
|
61
84
|
const sourcePath = (tree as { sourcePath?: string }).sourcePath;
|
|
62
85
|
if (sourcePath === undefined) return tree as R;
|
|
63
86
|
acc.paths.add(sourcePath);
|
|
87
|
+
if (tree.kind === JS.Kind.CompilationUnit && (tree as JS.CompilationUnit).statements.some(rp => {
|
|
88
|
+
if (rp.element.kind !== JS.Kind.Import) return false;
|
|
89
|
+
const imp = rp.element as JS.Import;
|
|
90
|
+
const path = (imp.moduleSpecifier?.element as {value?: unknown} | undefined)?.value;
|
|
91
|
+
return !!svgComponentName(imp) || typeof path === "string" && path.endsWith(".svg?react");
|
|
92
|
+
})) acc.svgComponentFiles.add(sourcePath);
|
|
64
93
|
if (tree.kind === Json.Kind.Document && isPackageJson(sourcePath)) {
|
|
65
94
|
acc.packageDirs.add(dirName(sourcePath));
|
|
66
95
|
const pkg = rootObject(tree as Json.Document);
|
|
96
|
+
const resolution = findNodeResolutionResult(tree as Json.Document);
|
|
97
|
+
const react = [...resolution?.dependencies ?? [], ...resolution?.devDependencies ?? []].find(dep => dep.name === 'react');
|
|
98
|
+
const declared = pkg && (stringValue(valueAt(pkg, ['dependencies', 'react'])) ?? stringValue(valueAt(pkg, ['devDependencies', 'react'])));
|
|
99
|
+
const resolved = react?.resolved?.name === 'react' && react.resolved.version;
|
|
100
|
+
if (pkg) acc.configSettings.set(dirName(sourcePath), {
|
|
101
|
+
homepage: stringValue(valueAt(pkg, ["homepage"])), proxy: stringValue(valueAt(pkg, ["proxy"])),
|
|
102
|
+
reactVersion: resolved && valid(resolved) && (!declared || !validRange(declared) || satisfies(resolved, declared)) ? resolved : declared
|
|
103
|
+
});
|
|
67
104
|
if (pkg && isCraPackage(pkg)) acc.roots.add(dirName(sourcePath));
|
|
68
105
|
if (pkg && runsOnVite(pkg)) acc.viteRoots.add(dirName(sourcePath));
|
|
69
106
|
} else if (tree.kind === PlainText.Kind.PlainText && indexHtmlRoot(sourcePath) !== undefined) {
|
|
@@ -76,11 +113,25 @@ export function scanCraProjects(acc: CraProjects): TreeVisitor<any, ExecutionCon
|
|
|
76
113
|
|
|
77
114
|
/** A recipe that scans the whole repository for Create React App packages. Finders use `isCraFile`, edits `isViteFile`. */
|
|
78
115
|
export abstract class CraScanningRecipe extends ScanningRecipe<CraProjects> {
|
|
116
|
+
// Keep the framework's accumulator delegate, but expose its file gate at PrepareRecipe time too.
|
|
117
|
+
override async editor() { return craFilesOnly(await super.editor()); }
|
|
118
|
+
private allowedRoots?: ReadonlySet<string>;
|
|
119
|
+
|
|
120
|
+
/** Gate a composite step on a shared, later-populated native dependency decision. Standalone steps are unrestricted. */
|
|
121
|
+
restrictTo(roots: ReadonlySet<string>): this {
|
|
122
|
+
this.allowedRoots = roots;
|
|
123
|
+
return this;
|
|
124
|
+
}
|
|
125
|
+
|
|
79
126
|
initialValue(): CraProjects {
|
|
80
|
-
|
|
127
|
+
const acc = new CraProjects();
|
|
128
|
+
acc.allowedRoots = this.allowedRoots;
|
|
129
|
+
return acc;
|
|
81
130
|
}
|
|
82
131
|
|
|
83
132
|
async scanner(acc: CraProjects): Promise<TreeVisitor<any, ExecutionContext>> {
|
|
84
|
-
|
|
133
|
+
// PrepareRecipe asks for a scanner with no accumulator to infer its tree gate.
|
|
134
|
+
if (acc) acc.allowedRoots = this.allowedRoots;
|
|
135
|
+
return craFilesOnly(scanCraProjects(acc));
|
|
85
136
|
}
|
|
86
137
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import {isStartOrBuildScript} from './cra-script';
|
|
2
|
+
import {elementsOf, findMember, getMemberKeyName, isLiteral, isObject, Json, membersOf, valueAt} from "@openrewrite/rewrite/json";
|
|
3
3
|
|
|
4
4
|
export const REACT_SCRIPTS = "react-scripts";
|
|
5
5
|
export const VITE = "vite";
|
|
@@ -41,15 +41,8 @@ export function rootObject(doc: Json.Document): Json.Object | undefined {
|
|
|
41
41
|
return isObject(doc.value) ? doc.value : undefined;
|
|
42
42
|
}
|
|
43
43
|
|
|
44
|
-
export function getMember(obj: Json.Object, name: string): Json.Member | undefined {
|
|
45
|
-
for (const rp of obj.members) {
|
|
46
|
-
if (isMember(rp.element) && getMemberKeyName(rp.element) === name) return rp.element;
|
|
47
|
-
}
|
|
48
|
-
return undefined;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
44
|
export function memberObject(obj: Json.Object, name: string): Json.Object | undefined {
|
|
52
|
-
const value =
|
|
45
|
+
const value = findMember(obj, name)?.value;
|
|
53
46
|
return value && isObject(value) ? value : undefined;
|
|
54
47
|
}
|
|
55
48
|
|
|
@@ -58,52 +51,53 @@ export function stringValue(value: Json | undefined): string | undefined {
|
|
|
58
51
|
return value && isLiteral(value) && typeof value.value === "string" ? value.value : undefined;
|
|
59
52
|
}
|
|
60
53
|
|
|
61
|
-
/** The string value of a member, or `undefined` when it is absent or not a string literal. */
|
|
62
|
-
export function stringMember(obj: Json.Object, name: string): string | undefined {
|
|
63
|
-
return stringValue(getMember(obj, name)?.value);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
54
|
export function hasDependency(pkg: Json.Object, name: string): boolean {
|
|
67
|
-
return DEPENDENCY_SECTIONS.some(section =>
|
|
68
|
-
const dependencies = memberObject(pkg, section);
|
|
69
|
-
return !!dependencies && !!getMember(dependencies, name);
|
|
70
|
-
});
|
|
55
|
+
return DEPENDENCY_SECTIONS.some(section => valueAt(pkg, [section, name]) !== undefined);
|
|
71
56
|
}
|
|
72
57
|
|
|
73
58
|
/** True when a package.json script invokes `react-scripts`. */
|
|
74
59
|
export function scriptsUseReactScripts(pkg: Json.Object): boolean {
|
|
75
60
|
const scripts = memberObject(pkg, "scripts");
|
|
76
|
-
return !!scripts && scripts.
|
|
77
|
-
const command =
|
|
61
|
+
return !!scripts && membersOf(scripts).some(m => {
|
|
62
|
+
const command = stringValue(m.value);
|
|
78
63
|
return command !== undefined && /(^|[\s&;|])react-scripts(\s|$)/.test(command);
|
|
79
64
|
});
|
|
80
65
|
}
|
|
81
66
|
|
|
82
67
|
/** A `package.json` that Create React App built: `react-scripts` is a dependency. */
|
|
83
68
|
export function isCraPackage(pkg: Json.Object): boolean {
|
|
84
|
-
return
|
|
69
|
+
return reactScriptsDependencies(pkg).length > 0;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Scoped forks keep CRA's command and project layout. */
|
|
73
|
+
export function reactScriptsDependencies(pkg: Json.Object): string[] {
|
|
74
|
+
return DEPENDENCY_SECTIONS.flatMap(section => {
|
|
75
|
+
const dependencies = memberObject(pkg, section);
|
|
76
|
+
return dependencies ? membersOf(dependencies).flatMap(m => {
|
|
77
|
+
const name = getMemberKeyName(m);
|
|
78
|
+
return name && /^(?:@[^/]+\/)?react-scripts$/.test(name) ? [name] : [];
|
|
79
|
+
}) : [];
|
|
80
|
+
});
|
|
85
81
|
}
|
|
86
82
|
|
|
87
83
|
/** Tools that wrap or replace `react-scripts`, and which Vite does not replace. */
|
|
88
84
|
const WRAPPER_DEPENDENCIES = ["@craco/craco", "craco", "react-app-rewired"];
|
|
89
85
|
const WRAPPER_COMMAND = /(^|[\s&;|])(craco|react-app-rewired)(\s|$)/;
|
|
86
|
+
const VITE_SCRIPT = /^(?:(?:tsc --noEmit|node scripts\/vite-typecheck\.cjs) && )?vite(?: build)?(?: --(?:host|base|outDir) "[\w./:@%+ -]+"| --port \d+)*$/;
|
|
90
87
|
const START_OR_BUILD = /(^|[\s&;|])react-scripts\s+(start|build)(\s|$)/;
|
|
91
|
-
/** The only commands the migration rewrites. */
|
|
92
|
-
export const EXACT_START_OR_BUILD = /^react-scripts (start|build)$/;
|
|
93
88
|
|
|
94
|
-
/**
|
|
95
|
-
* True when the package's `start` and `build` will run on Vite once the migration has edited it: it is a Create
|
|
96
|
-
* React App package that neither depends on `craco` or `react-app-rewired` nor has a script that wraps those or
|
|
97
|
-
* `react-scripts start|build` (`cross-env PORT=4000 react-scripts start`, `react-scripts build --profile`).
|
|
98
|
-
* Such a package is only found by the finders; editing it would leave `start` or `build` without a working tool.
|
|
99
|
-
*/
|
|
89
|
+
/** Admit standard CRA commands and literal host/port/base/output controls; custom wrappers stay for review. */
|
|
100
90
|
export function runsOnVite(pkg: Json.Object): boolean {
|
|
101
91
|
if (!isCraPackage(pkg) || WRAPPER_DEPENDENCIES.some(name => hasDependency(pkg, name))) return false;
|
|
102
92
|
const scripts = memberObject(pkg, "scripts");
|
|
103
|
-
return !scripts || scripts.
|
|
104
|
-
const command =
|
|
93
|
+
return !scripts || membersOf(scripts).every(m => {
|
|
94
|
+
const command = stringValue(m.value)?.trim();
|
|
95
|
+
// Custom entry scripts can load react-scripts internally, e.g. responsive-viewer's scripts/build.js.
|
|
96
|
+
const name = getMemberKeyName(m);
|
|
97
|
+
if ((name === "start" || name === "build") && command &&
|
|
98
|
+
!isStartOrBuildScript(command) && !VITE_SCRIPT.test(command)) return false;
|
|
105
99
|
return command === undefined || (!WRAPPER_COMMAND.test(command) &&
|
|
106
|
-
(!START_OR_BUILD.test(command) ||
|
|
100
|
+
(!START_OR_BUILD.test(command) || isStartOrBuildScript(command)));
|
|
107
101
|
});
|
|
108
102
|
}
|
|
109
103
|
|
|
@@ -113,113 +107,20 @@ export function runsOnVite(pkg: Json.Object): boolean {
|
|
|
113
107
|
*/
|
|
114
108
|
export function scriptsKeepReactScripts(pkg: Json.Object): boolean {
|
|
115
109
|
const scripts = memberObject(pkg, "scripts");
|
|
116
|
-
return !!scripts && scripts.
|
|
117
|
-
const command =
|
|
110
|
+
return !!scripts && membersOf(scripts).some(m => {
|
|
111
|
+
const command = stringValue(m.value);
|
|
118
112
|
return command !== undefined && /(^|[\s&;|])react-scripts(\s|$)/.test(command) &&
|
|
119
113
|
!/(^|[\s&;|])react-scripts\s+eject\s*$/.test(command);
|
|
120
114
|
});
|
|
121
115
|
}
|
|
122
116
|
|
|
123
|
-
function stringLiteral(value: string, prefix = emptySpace): Json.Literal {
|
|
124
|
-
return {
|
|
125
|
-
kind: Json.Kind.Literal, id: randomId(), prefix, markers: emptyMarkers,
|
|
126
|
-
source: JSON.stringify(value), value
|
|
127
|
-
};
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
export function newMember(name: string, value: Json.Value, prefix: Json.Space): Json.Member {
|
|
131
|
-
return {
|
|
132
|
-
kind: Json.Kind.Member, id: randomId(), prefix, markers: emptyMarkers,
|
|
133
|
-
key: {kind: Json.Kind.RightPadded, element: stringLiteral(name), after: emptySpace, markers: emptyMarkers},
|
|
134
|
-
value: {...value, prefix: space(" ")} as Json.Value
|
|
135
|
-
};
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
export function newStringMember(name: string, value: string, prefix: Json.Space): Json.Member {
|
|
139
|
-
return newMember(name, stringLiteral(value), prefix);
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
export function rightPadded<T extends Json>(element: T, after: Json.Space = emptySpace): Json.RightPadded<T> {
|
|
143
|
-
return paddedElement(element, after);
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
/** The whitespace of a prefix without its comments, which belong to the member that carries them. */
|
|
147
|
-
function layoutOf(prefix: Json.Space): Json.Space {
|
|
148
|
-
// After a comment, the line break that follows it is the comment's suffix.
|
|
149
|
-
const ws = prefix.comments.length > 0 ? prefix.comments[prefix.comments.length - 1].suffix : prefix.whitespace;
|
|
150
|
-
return space(ws.includes("\n") ? ws : " ");
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
/** How to lay out the first member of an empty object: the space before it and before the closing brace. */
|
|
154
|
-
export interface EmptyObjectLayout {
|
|
155
|
-
readonly inner: Json.Space;
|
|
156
|
-
readonly closing: Json.Space;
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
/** The layout for an object nested one level inside `parent`, which is indented like its own members. */
|
|
160
|
-
export function nestedLayout(parent: Json.Object): EmptyObjectLayout {
|
|
161
|
-
const ws = parent.members[0]?.element.prefix.whitespace ?? "\n ";
|
|
162
|
-
if (!ws.includes("\n")) return {inner: emptySpace, closing: emptySpace};
|
|
163
|
-
const unit = ws.substring(ws.lastIndexOf("\n") + 1) || " ";
|
|
164
|
-
return {inner: space(ws + unit), closing: space(ws)};
|
|
165
|
-
}
|
|
166
|
-
|
|
167
117
|
/**
|
|
168
|
-
*
|
|
169
|
-
*
|
|
118
|
+
* Where a new member goes: in key order when the object's keys are already sorted, the way npm keeps its dependency
|
|
119
|
+
* sections, and last otherwise, so a hand-ordered `package.json` keeps its order.
|
|
170
120
|
*/
|
|
171
|
-
export function
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
if (members.length === 0) {
|
|
175
|
-
const layout = whenEmpty ?? {inner: emptySpace, closing: emptySpace};
|
|
176
|
-
return {...obj, members: [rightPadded({...member, prefix: layout.inner} as Json.Member, layout.closing)]};
|
|
177
|
-
}
|
|
178
|
-
const names = members.map(rp => isMember(rp.element) ? getMemberKeyName(rp.element) ?? "" : "");
|
|
179
|
-
const sorted = names.every((n, i) => i === 0 || names[i - 1] <= n);
|
|
180
|
-
let index = sorted ? names.findIndex(n => n > getMemberKeyName(member)!) : -1;
|
|
181
|
-
if (index < 0) index = members.length;
|
|
182
|
-
const first = members[0].element.prefix;
|
|
183
|
-
const separator = layoutOf(members.length > 1 ? members[1].element.prefix : first);
|
|
184
|
-
if (index === 0) {
|
|
185
|
-
// The old first member keeps the comments written above it.
|
|
186
|
-
members[0] = {...members[0], element: {...members[0].element, prefix: first.comments.length > 0 ? first : {...first, whitespace: separator.whitespace}}};
|
|
187
|
-
members.unshift(rightPadded({...member, prefix: space(first.whitespace)} as Json.Member));
|
|
188
|
-
} else if (index === members.length) {
|
|
189
|
-
const last = members[members.length - 1];
|
|
190
|
-
members[members.length - 1] = {...last, after: emptySpace};
|
|
191
|
-
members.push(rightPadded({...member, prefix: separator} as Json.Member, last.after));
|
|
192
|
-
} else {
|
|
193
|
-
members.splice(index, 0, rightPadded({...member, prefix: separator} as Json.Member));
|
|
194
|
-
}
|
|
195
|
-
return {...obj, members};
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
/** Removes a member by name, handing the last member's trailing space to the one before it. */
|
|
199
|
-
export function removeMember(obj: Json.Object, name: string): Json.Object {
|
|
200
|
-
const index = obj.members.findIndex(rp => isMember(rp.element) && getMemberKeyName(rp.element) === name);
|
|
201
|
-
if (index < 0) return obj;
|
|
202
|
-
const members = [...obj.members];
|
|
203
|
-
const [removed] = members.splice(index, 1);
|
|
204
|
-
if (index === 0 && members.length > 0) {
|
|
205
|
-
members[0] = {...members[0], element: {...members[0].element, prefix: removed.element.prefix}};
|
|
206
|
-
}
|
|
207
|
-
if (index === obj.members.length - 1 && members.length > 0) {
|
|
208
|
-
members[members.length - 1] = {...members[members.length - 1], after: removed.after};
|
|
209
|
-
}
|
|
210
|
-
return {...obj, members};
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
export function replaceMember(obj: Json.Object, name: string, update: (m: Json.Member) => Json.Member): Json.Object {
|
|
214
|
-
let changed = false;
|
|
215
|
-
const members = obj.members.map(rp => {
|
|
216
|
-
if (!isMember(rp.element) || getMemberKeyName(rp.element) !== name) return rp;
|
|
217
|
-
const updated = update(rp.element);
|
|
218
|
-
if (updated === rp.element) return rp;
|
|
219
|
-
changed = true;
|
|
220
|
-
return {...rp, element: updated};
|
|
221
|
-
});
|
|
222
|
-
return changed ? {...obj, members} : obj;
|
|
121
|
+
export function newMemberPosition(obj: Json.Object | undefined): "sorted" | "last" {
|
|
122
|
+
const keys = obj ? membersOf(obj).map(m => getMemberKeyName(m) ?? "") : [];
|
|
123
|
+
return keys.every((key, i) => i === 0 || keys[i - 1].localeCompare(key, "en") <= 0) ? "sorted" : "last";
|
|
223
124
|
}
|
|
224
125
|
|
|
225
126
|
/** `.env`, `.env.local`, `.env.production.local` and the like. */
|
|
@@ -227,18 +128,6 @@ export function isEnvFile(sourcePath: string): boolean {
|
|
|
227
128
|
return /^\.env(\..+)?$/.test(baseName(sourcePath));
|
|
228
129
|
}
|
|
229
130
|
|
|
230
|
-
/** Appends a string to a JSON array, copying the whitespace the array already uses between values. */
|
|
231
|
-
export function appendString(array: Json.Array, value: string): Json.Array {
|
|
232
|
-
const values = array.values.filter(rp => rp.element.kind !== Json.Kind.Empty);
|
|
233
|
-
if (values.length === 0) return array;
|
|
234
|
-
const last = values[values.length - 1];
|
|
235
|
-
const separator = layoutOf(values.length > 1 ? values[1].element.prefix : values[0].element.prefix);
|
|
236
|
-
return {
|
|
237
|
-
...array,
|
|
238
|
-
values: [...values.slice(0, -1), {...last, after: emptySpace}, rightPadded<Json.Value>(stringLiteral(value, separator), last.after)]
|
|
239
|
-
};
|
|
240
|
-
}
|
|
241
|
-
|
|
242
131
|
export function stringValues(array: Json.Array): string[] {
|
|
243
|
-
return array.
|
|
132
|
+
return elementsOf(array).flatMap(value => stringValue(value) ?? []);
|
|
244
133
|
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import {JS} from "@openrewrite/rewrite/javascript";
|
|
2
|
+
import {isIdentifier, J} from "@openrewrite/rewrite/java";
|
|
3
|
+
import {TreePrinters} from '@openrewrite/rewrite';
|
|
4
|
+
|
|
5
|
+
export function importsSvgComponent(imp: JS.Import): boolean {
|
|
6
|
+
const module = imp.moduleSpecifier?.element as J.Literal | undefined;
|
|
7
|
+
const bindings = imp.importClause?.namedBindings;
|
|
8
|
+
if (typeof module?.value !== 'string' || !module.value.endsWith('.svg') || bindings?.kind !== JS.Kind.NamedImports) return false;
|
|
9
|
+
return (bindings as JS.NamedImports).elements.elements.some(element => {
|
|
10
|
+
const specifier = element.element.specifier;
|
|
11
|
+
const name = specifier.kind === JS.Kind.Alias ? (specifier as JS.Alias).propertyName.element : specifier;
|
|
12
|
+
return isIdentifier(name) && name.simpleName === 'ReactComponent';
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** Parsed replacement imports cannot carry comments attached to the original inner tokens. */
|
|
17
|
+
export async function svgImportNeedsReview(imp: JS.Import): Promise<boolean> {
|
|
18
|
+
if (!importsSvgComponent(imp)) return false;
|
|
19
|
+
if (!svgComponentName(imp)) return true;
|
|
20
|
+
const printed = await TreePrinters.printer(JS.Kind.CompilationUnit).print(imp);
|
|
21
|
+
const withoutLeadingComments = printed.replace(/^(?:\s|\/\*[\s\S]*?\*\/|\/\/[^\n]*(?:\n|$))*/, '');
|
|
22
|
+
return /\/[*\/]/.test(withoutLeadingComments);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** The unambiguous CRA form, with exactly one named export and a relative SVG path. */
|
|
26
|
+
export function svgComponentName(imp: JS.Import): string | undefined {
|
|
27
|
+
const module = imp.moduleSpecifier?.element as J.Literal | undefined;
|
|
28
|
+
const bindings = imp.importClause?.namedBindings;
|
|
29
|
+
if (typeof module?.value !== "string" || !/^\.{1,2}\/.*\.svg$/.test(module.value) ||
|
|
30
|
+
imp.importClause?.typeOnly || bindings?.kind !== JS.Kind.NamedImports) return undefined;
|
|
31
|
+
const elements = (bindings as JS.NamedImports).elements.elements;
|
|
32
|
+
if (elements.length !== 1) return undefined;
|
|
33
|
+
const specifier = elements[0].element.specifier;
|
|
34
|
+
if (isIdentifier(specifier)) return specifier.simpleName === "ReactComponent" ? specifier.simpleName : undefined;
|
|
35
|
+
if (specifier.kind !== JS.Kind.Alias) return undefined;
|
|
36
|
+
const alias = specifier as JS.Alias;
|
|
37
|
+
const property = alias.propertyName.element;
|
|
38
|
+
return isIdentifier(property) && property.simpleName === "ReactComponent" && isIdentifier(alias.alias) ? alias.alias.simpleName : undefined;
|
|
39
|
+
}
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import {ExecutionContext, TreeVisitor} from "@openrewrite/rewrite";
|
|
2
|
-
import {JavaScriptVisitor, JS} from "@openrewrite/rewrite/javascript";
|
|
2
|
+
import {JavaScriptParser, JavaScriptVisitor, JS} from "@openrewrite/rewrite/javascript";
|
|
3
3
|
import {J} from "@openrewrite/rewrite/java";
|
|
4
4
|
import {CraProjects, CraScanningRecipe} from "./scan-cra-projects";
|
|
5
|
-
import {baseName} from "./shared";
|
|
5
|
+
import {baseName, nearestRoot} from "./shared";
|
|
6
6
|
|
|
7
7
|
/** The `///` of a triple-slash directive is parsed as the comment's `//`, leaving `/ <reference ... />` as its text. */
|
|
8
|
-
const REACT_SCRIPTS_REFERENCE =
|
|
8
|
+
const REACT_SCRIPTS_REFERENCE = /^\/\s*<reference\s+types=["'](?:@[^/"']+\/)?react-scripts["']\s*\/>$/;
|
|
9
9
|
const VITE_CLIENT_REFERENCE = '/ <reference types="vite/client" />';
|
|
10
10
|
|
|
11
11
|
/**
|
|
@@ -29,9 +29,16 @@ export class UpdateReactAppEnvTypes extends CraScanningRecipe {
|
|
|
29
29
|
return new class extends JavaScriptVisitor<ExecutionContext> {
|
|
30
30
|
override async visitJsCompilationUnit(cu: JS.CompilationUnit, ctx: ExecutionContext): Promise<J | undefined> {
|
|
31
31
|
if (!baseName(cu.sourcePath).endsWith(".d.ts") || !acc.isViteFile(cu.sourcePath)) return cu;
|
|
32
|
-
|
|
32
|
+
let comments = cu.prefix.comments.map(c =>
|
|
33
33
|
c.kind === J.Kind.TextComment && REACT_SCRIPTS_REFERENCE.test((c as unknown as {text: string}).text) ?
|
|
34
34
|
{...c, text: VITE_CLIENT_REFERENCE} : c);
|
|
35
|
+
const owner = nearestRoot(acc.packageDirs, cu.sourcePath);
|
|
36
|
+
const svgr = acc.usesSvgr(owner);
|
|
37
|
+
if (svgr && baseName(cu.sourcePath) === "react-app-env.d.ts" && !comments.some(c =>
|
|
38
|
+
c.kind === J.Kind.TextComment && (c as unknown as {text: string}).text.includes('types="vite-plugin-svgr/client"'))) {
|
|
39
|
+
const parsed = await new JavaScriptParser().parseOne({text: '/// <reference types="vite-plugin-svgr/client" />\n', sourcePath: "svgr-env.d.ts"}) as JS.CompilationUnit;
|
|
40
|
+
comments = [...comments, ...parsed.prefix.comments];
|
|
41
|
+
}
|
|
35
42
|
if (comments.every((c, i) => c === cu.prefix.comments[i])) return super.visitJsCompilationUnit(cu, ctx);
|
|
36
43
|
return {...cu, prefix: {...cu.prefix, comments}} as JS.CompilationUnit;
|
|
37
44
|
}
|