@openrewrite/recipes-react 0.16.0 → 0.18.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +10 -5
- 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 +5 -6
- 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 -30
- 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 +1 -1
- package/dist/migration/cra-to-vite/shared.d.ts.map +1 -1
- package/dist/migration/cra-to-vite/shared.js +19 -4
- 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 +12 -3
- 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 +18 -14
- package/dist/migration/cra-to-vite/update-react-scripts-scripts.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 +8 -1
- package/dist/react18/replace-unmount-component-at-node.js.map +1 -1
- package/dist/react19/find-legacy-context-api.d.ts.map +1 -1
- package/dist/react19/find-legacy-context-api.js +21 -38
- package/dist/react19/find-legacy-context-api.js.map +1 -1
- package/dist/{react18 → react19}/remove-unstable-batched-updates.d.ts +2 -2
- package/dist/react19/remove-unstable-batched-updates.d.ts.map +1 -0
- package/dist/react19/remove-unstable-batched-updates.js +295 -0
- package/dist/react19/remove-unstable-batched-updates.js.map +1 -0
- package/dist/react19/replace-default-props.d.ts +1 -0
- package/dist/react19/replace-default-props.d.ts.map +1 -1
- package/dist/react19/replace-default-props.js +198 -151
- 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 +10 -25
- package/dist/react19/replace-react-shallow-renderer.js.map +1 -1
- package/dist/refactoring/class-to-functional.d.ts.map +1 -1
- package/dist/refactoring/class-to-functional.js +2 -0
- package/dist/refactoring/class-to-functional.js.map +1 -1
- package/dist/refactoring/create-class-to-es6.d.ts.map +1 -1
- package/dist/refactoring/create-class-to-es6.js +14 -49
- package/dist/refactoring/create-class-to-es6.js.map +1 -1
- package/dist/search/find-function-component-default-props.d.ts.map +1 -1
- package/dist/search/find-function-component-default-props.js +6 -15
- package/dist/search/find-function-component-default-props.js.map +1 -1
- package/dist/search/find-redux-stores.d.ts +12 -0
- package/dist/search/find-redux-stores.d.ts.map +1 -0
- package/dist/search/find-redux-stores.js +87 -0
- package/dist/search/find-redux-stores.js.map +1 -0
- package/dist/simplify-object-pattern-property.d.ts.map +1 -1
- package/dist/simplify-object-pattern-property.js +14 -21
- package/dist/simplify-object-pattern-property.js.map +1 -1
- package/dist/simplify-react-imports.d.ts.map +1 -1
- package/dist/simplify-react-imports.js +10 -59
- package/dist/simplify-react-imports.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.map +1 -1
- package/dist/testing/enzyme-host-components.js +5 -2
- package/dist/testing/enzyme-host-components.js.map +1 -1
- 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 -8
- package/dist/testing/enzyme-to-react-testing-library.d.ts.map +1 -1
- package/dist/testing/enzyme-to-react-testing-library.js +384 -40
- 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 +14 -6
- package/dist/testing/enzyme-wrapper-scope.d.ts.map +1 -1
- package/dist/testing/enzyme-wrapper-scope.js +143 -16
- package/dist/testing/enzyme-wrapper-scope.js.map +1 -1
- package/dist/util/java-recipe-proxy.d.ts +18 -0
- package/dist/util/java-recipe-proxy.d.ts.map +1 -0
- package/dist/util/java-recipe-proxy.js +97 -0
- package/dist/util/java-recipe-proxy.js.map +1 -0
- package/dist/util/module-imports.d.ts +1 -0
- package/dist/util/module-imports.d.ts.map +1 -1
- package/dist/util/module-imports.js +5 -1
- package/dist/util/module-imports.js.map +1 -1
- package/dist/util/reindent.d.ts +5 -0
- package/dist/util/reindent.d.ts.map +1 -0
- package/dist/util/reindent.js +44 -0
- package/dist/util/reindent.js.map +1 -0
- package/dist/zustand/redux-stores.d.ts +74 -0
- package/dist/zustand/redux-stores.d.ts.map +1 -0
- package/dist/zustand/redux-stores.js +402 -0
- package/dist/zustand/redux-stores.js.map +1 -0
- package/dist/zustand/redux-to-zustand.d.ts +76 -0
- package/dist/zustand/redux-to-zustand.d.ts.map +1 -0
- package/dist/zustand/redux-to-zustand.js +653 -0
- package/dist/zustand/redux-to-zustand.js.map +1 -0
- package/dist/zustand/zustand-plan.d.ts +96 -0
- package/dist/zustand/zustand-plan.d.ts.map +1 -0
- package/dist/zustand/zustand-plan.js +822 -0
- package/dist/zustand/zustand-plan.js.map +1 -0
- package/package.json +2 -2
- package/src/index.ts +9 -4
- 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 +5 -6
- 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 +52 -33
- 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 +20 -10
- package/src/migration/cra-to-vite/svg-component.ts +39 -0
- package/src/migration/cra-to-vite/update-react-app-env-types.ts +13 -6
- package/src/migration/cra-to-vite/update-react-scripts-scripts.ts +22 -28
- package/src/react18/replace-reactdom-render.ts +17 -8
- package/src/react18/replace-unmount-component-at-node.ts +13 -2
- package/src/react19/find-legacy-context-api.ts +21 -42
- package/src/react19/remove-unstable-batched-updates.ts +301 -0
- package/src/react19/replace-default-props.ts +212 -159
- package/src/react19/replace-react-shallow-renderer.ts +13 -36
- package/src/refactoring/class-to-functional.ts +3 -1
- package/src/refactoring/create-class-to-es6.ts +12 -63
- package/src/search/find-function-component-default-props.ts +8 -10
- package/src/search/find-redux-stores.ts +76 -0
- package/src/simplify-object-pattern-property.ts +22 -49
- package/src/simplify-react-imports.ts +12 -66
- package/src/testing/enzyme-constant-strings.ts +95 -0
- package/src/testing/enzyme-host-components.ts +6 -2
- 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 +517 -57
- package/src/testing/enzyme-usage.ts +29 -4
- package/src/testing/enzyme-wrapper-scope.ts +186 -30
- package/src/util/java-recipe-proxy.ts +94 -0
- package/src/util/module-imports.ts +6 -1
- package/src/util/reindent.ts +35 -0
- package/src/zustand/redux-stores.ts +409 -0
- package/src/zustand/redux-to-zustand.ts +657 -0
- package/src/zustand/zustand-plan.ts +843 -0
- package/dist/react18/remove-unstable-batched-updates.d.ts.map +0 -1
- package/dist/react18/remove-unstable-batched-updates.js +0 -170
- package/dist/react18/remove-unstable-batched-updates.js.map +0 -1
- package/src/react18/remove-unstable-batched-updates.ts +0 -192
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import {Column, DataTable, ExecutionContext, foundSearchResult, Recipe, TreeVisitor, check, not} from "@openrewrite/rewrite";
|
|
2
|
-
import {isVendoredOrBundled, JavaScriptVisitor, JS} from "@openrewrite/rewrite/javascript";
|
|
2
|
+
import {isVendoredOrBundled, JavaScriptVisitor, JS, referencesOf} from "@openrewrite/rewrite/javascript";
|
|
3
3
|
import {J} from "@openrewrite/rewrite/java";
|
|
4
4
|
import {extendsReactComponentByName} from "../util/react-class";
|
|
5
5
|
|
|
@@ -41,25 +41,23 @@ export class FindFunctionComponentDefaultProps extends Recipe {
|
|
|
41
41
|
|
|
42
42
|
return check(not(isVendoredOrBundled()), new class extends JavaScriptVisitor<ExecutionContext> {
|
|
43
43
|
private filePath = "";
|
|
44
|
-
private classComponents = new Set<string>();
|
|
45
44
|
|
|
46
45
|
override async visitJsCompilationUnit(cu: JS.CompilationUnit, ctx: ExecutionContext): Promise<J | undefined> {
|
|
47
46
|
this.filePath = cu.sourcePath ?? "";
|
|
48
|
-
const classes = this.classComponents = new Set<string>();
|
|
49
|
-
await new class extends JavaScriptVisitor<ExecutionContext> {
|
|
50
|
-
protected override async visitClassDeclaration(decl: J.ClassDeclaration, c: ExecutionContext): Promise<J | undefined> {
|
|
51
|
-
if (extendsReactComponentByName(decl)) classes.add(decl.name.simpleName);
|
|
52
|
-
return super.visitClassDeclaration(decl, c);
|
|
53
|
-
}
|
|
54
|
-
}().visit(cu, ctx);
|
|
55
47
|
return super.visitJsCompilationUnit(cu, ctx);
|
|
56
48
|
}
|
|
57
49
|
|
|
50
|
+
/** Resolved rather than matched by name, so a parameter or local that shadows a class component still counts. */
|
|
51
|
+
private isClassComponent(name: string): boolean {
|
|
52
|
+
const declaration = referencesOf(this.cursor, name, 'value').find(r => r.declares)?.cursor.parent?.value;
|
|
53
|
+
return declaration?.kind === J.Kind.ClassDeclaration && extendsReactComponentByName(declaration as J.ClassDeclaration);
|
|
54
|
+
}
|
|
55
|
+
|
|
58
56
|
override async visitFieldAccess(fieldAccess: J.FieldAccess, ctx: ExecutionContext): Promise<J | undefined> {
|
|
59
57
|
const fa = await super.visitFieldAccess(fieldAccess, ctx) as J.FieldAccess;
|
|
60
58
|
if (fa.name.element.simpleName !== 'defaultProps' || fa.target.kind !== J.Kind.Identifier) return fa;
|
|
61
59
|
const receiver = (fa.target as J.Identifier).simpleName;
|
|
62
|
-
if (!/^[A-Z]/.test(receiver) || this.
|
|
60
|
+
if (!/^[A-Z]/.test(receiver) || this.isClassComponent(receiver)) return fa;
|
|
63
61
|
dataTable.insertRow(ctx, {filePath: this.filePath, component: receiver, upgradeStep: DEFAULT_PROPS_STEP});
|
|
64
62
|
return foundSearchResult(fa, 'defaultProps') as J.FieldAccess;
|
|
65
63
|
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import {check, DataTable, ExecutionContext, foundSearchResult, not, ScanningRecipe, TreeVisitor} from "@openrewrite/rewrite";
|
|
2
|
+
import {isVendoredOrBundled, JavaScriptVisitor, JS} from "@openrewrite/rewrite/javascript";
|
|
3
|
+
import {J} from "@openrewrite/rewrite/java";
|
|
4
|
+
import {
|
|
5
|
+
localSlices, REDUX_STORES_TABLE, ReduxStoreRow, SliceModule, storeFindings, storeFunctions, storeName, SUGGESTED_ACTIONS, TEST_SOURCE, ZUSTAND_GUIDE
|
|
6
|
+
} from "../zustand/redux-stores";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Inventories the Redux stores of a repository for a migration to zustand. A store is a candidate when it is a
|
|
10
|
+
* `configureStore` call whose `reducer` is an object of `createSlice` reducers, in the same source or imported from
|
|
11
|
+
* a relative module, and whose options are only `reducer`. Every other store gets a row for each reason it is not,
|
|
12
|
+
* such as a legacy `createStore`, custom middleware or a hand-written reducer. Stores are attributed by what the
|
|
13
|
+
* source imports from `redux` or `@reduxjs/toolkit`; namespace imports and `require` are not followed.
|
|
14
|
+
*
|
|
15
|
+
* Only the store's setup is checked; the components and modules that read or dispatch to it are not.
|
|
16
|
+
*
|
|
17
|
+
* @see https://zustand.docs.pmnd.rs/guides/migrating-to-zustand
|
|
18
|
+
*/
|
|
19
|
+
export class FindReduxStores extends ScanningRecipe<Map<string, SliceModule>> {
|
|
20
|
+
readonly name = "org.openrewrite.react.search.find-redux-stores";
|
|
21
|
+
readonly displayName = "Find Redux stores";
|
|
22
|
+
readonly description = "Finds the Redux stores created with `createStore` or Redux Toolkit's `configureStore` and records whether each one's setup can move to zustand: a `configureStore` call whose `reducer` is an object of `createSlice` reducers without `extraReducers`, and no other option, is a candidate. Every other store is recorded with the reasons it is not, such as a legacy `createStore`, custom middleware, enhancers, preloaded state or a hand-written reducer. The components and modules that use a store are not checked.";
|
|
23
|
+
|
|
24
|
+
private readonly stores = new DataTable<ReduxStoreRow>(
|
|
25
|
+
REDUX_STORES_TABLE, "Redux stores",
|
|
26
|
+
"The Redux stores of the repository and whether each one's setup can move to zustand, with one row for each reason it can't.",
|
|
27
|
+
ReduxStoreRow
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
initialValue(): Map<string, SliceModule> {
|
|
31
|
+
return new Map();
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
async scanner(modules: Map<string, SliceModule>): Promise<TreeVisitor<any, ExecutionContext>> {
|
|
35
|
+
return check(not(isVendoredOrBundled()), new class extends JavaScriptVisitor<ExecutionContext> {
|
|
36
|
+
override async visitJsCompilationUnit(cu: JS.CompilationUnit): Promise<J | undefined> {
|
|
37
|
+
const {reducers, slices} = localSlices(cu);
|
|
38
|
+
// Every source is kept, so that a relative import resolves to the file Node would load
|
|
39
|
+
modules.set(cu.sourcePath, {reducers, slices});
|
|
40
|
+
return cu;
|
|
41
|
+
}
|
|
42
|
+
}());
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
async editorWithData(modules: Map<string, SliceModule>): Promise<TreeVisitor<any, ExecutionContext>> {
|
|
46
|
+
const stores = this.stores;
|
|
47
|
+
return check(not(isVendoredOrBundled()), new class extends JavaScriptVisitor<ExecutionContext> {
|
|
48
|
+
private cu?: JS.CompilationUnit;
|
|
49
|
+
private functions = {legacy: new Set<string>(), configure: new Set<string>()};
|
|
50
|
+
|
|
51
|
+
override async visitJsCompilationUnit(cu: JS.CompilationUnit, ctx: ExecutionContext): Promise<J | undefined> {
|
|
52
|
+
this.functions = storeFunctions(cu);
|
|
53
|
+
if (this.functions.legacy.size === 0 && this.functions.configure.size === 0) return cu;
|
|
54
|
+
this.cu = cu;
|
|
55
|
+
return super.visitJsCompilationUnit(cu, ctx);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
override async visitMethodInvocation(method: J.MethodInvocation, ctx: ExecutionContext): Promise<J | undefined> {
|
|
59
|
+
const visited = await super.visitMethodInvocation(method, ctx) as J.MethodInvocation;
|
|
60
|
+
const found = storeFindings(method, this.cu!, modules, this.functions);
|
|
61
|
+
if (!found) return visited;
|
|
62
|
+
const {api, findings} = found;
|
|
63
|
+
const cu = this.cu!;
|
|
64
|
+
const rows = findings.length === 0 ? [{reason: "SLICE_STORE", detail: ""}] : findings;
|
|
65
|
+
for (const {reason, detail} of rows) {
|
|
66
|
+
stores.insertRow(ctx, {
|
|
67
|
+
sourcePath: cu.sourcePath, store: storeName(this.cursor), api,
|
|
68
|
+
status: findings.length === 0 ? "CANDIDATE" : "REVIEW", reason, detail,
|
|
69
|
+
testSource: TEST_SOURCE.test(cu.sourcePath), suggestedAction: SUGGESTED_ACTIONS[reason]
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
return foundSearchResult(visited, `Redux store: ${rows.map(row => row.reason).join(", ")}. See ${ZUSTAND_GUIDE}`);
|
|
73
|
+
}
|
|
74
|
+
}());
|
|
75
|
+
}
|
|
76
|
+
}
|
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import {ExecutionContext, Recipe, TreeVisitor, check, not} from "@openrewrite/rewrite";
|
|
1
|
+
import {ExecutionContext, Recipe, TreeVisitor, check, mapAsync, not, updateIfChanged} from "@openrewrite/rewrite";
|
|
2
2
|
import {JavaScriptVisitor, JS, isVendoredOrBundled} from "@openrewrite/rewrite/javascript";
|
|
3
3
|
import {J} from "@openrewrite/rewrite/java";
|
|
4
|
-
import {create} from "mutative";
|
|
5
4
|
|
|
6
5
|
/**
|
|
7
6
|
* Simplifies object pattern properties where the key and value have the same name.
|
|
@@ -19,55 +18,29 @@ export class SimplifyObjectPatternProperty extends Recipe {
|
|
|
19
18
|
async editor(): Promise<TreeVisitor<any, ExecutionContext>> {
|
|
20
19
|
return check(not(isVendoredOrBundled()), new class extends JavaScriptVisitor<ExecutionContext> {
|
|
21
20
|
protected async visitObjectBindingPattern(pattern: JS.ObjectBindingPattern, p: ExecutionContext): Promise<J | undefined> {
|
|
22
|
-
// First, recursively visit children
|
|
23
21
|
const visited = await super.visitObjectBindingPattern(pattern, p) as JS.ObjectBindingPattern;
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
if (element.kind === JS.Kind.BindingElement) {
|
|
31
|
-
const binding = element as JS.BindingElement;
|
|
32
|
-
|
|
33
|
-
// Check if the property name is an identifier
|
|
34
|
-
if (binding.propertyName && binding.propertyName.element.kind === J.Kind.Identifier) {
|
|
35
|
-
const propName = (binding.propertyName.element as J.Identifier).simpleName;
|
|
36
|
-
|
|
37
|
-
// Check if the assignment is also an identifier with the same name
|
|
38
|
-
if (binding.name && binding.name.kind === J.Kind.Identifier) {
|
|
39
|
-
const assignmentName = (binding.name as J.Identifier).simpleName;
|
|
40
|
-
|
|
41
|
-
// If they match, simplify to just the identifier
|
|
42
|
-
if (propName === assignmentName) {
|
|
43
|
-
// Create a new binding element with propertyName set to undefined (shorthand syntax)
|
|
44
|
-
// Also need to update the name's prefix to remove extra spacing
|
|
45
|
-
return {
|
|
46
|
-
...right,
|
|
47
|
-
element: {
|
|
48
|
-
...binding,
|
|
49
|
-
propertyName: undefined,
|
|
50
|
-
name: {
|
|
51
|
-
...binding.name,
|
|
52
|
-
prefix: binding.propertyName!.element.prefix
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
} as J.RightPadded<JS.BindingElement>;
|
|
56
|
-
}
|
|
22
|
+
return updateIfChanged(visited, {
|
|
23
|
+
bindings: updateIfChanged(visited.bindings, {
|
|
24
|
+
elements: await mapAsync(visited.bindings.elements, async right => {
|
|
25
|
+
const binding = right.element;
|
|
26
|
+
if (binding.kind !== JS.Kind.BindingElement) {
|
|
27
|
+
return right;
|
|
57
28
|
}
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
29
|
+
const {propertyName, name} = binding as JS.BindingElement;
|
|
30
|
+
if (propertyName?.element.kind !== J.Kind.Identifier || name?.kind !== J.Kind.Identifier ||
|
|
31
|
+
(propertyName.element as J.Identifier).simpleName !== (name as J.Identifier).simpleName) {
|
|
32
|
+
return right;
|
|
33
|
+
}
|
|
34
|
+
return {
|
|
35
|
+
...right,
|
|
36
|
+
element: {
|
|
37
|
+
...binding,
|
|
38
|
+
propertyName: undefined,
|
|
39
|
+
name: {...name, prefix: propertyName.element.prefix}
|
|
40
|
+
}
|
|
41
|
+
} as J.RightPadded<JS.BindingElement>;
|
|
42
|
+
})
|
|
43
|
+
})
|
|
71
44
|
});
|
|
72
45
|
}
|
|
73
46
|
}());
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import {ExecutionContext, Recipe, TreeVisitor, check, not} from "@openrewrite/rewrite";
|
|
2
|
-
import {JavaScriptVisitor, maybeAddImport, isVendoredOrBundled} from "@openrewrite/rewrite/javascript";
|
|
2
|
+
import {JavaScriptVisitor, JS, maybeAddImport, isVendoredOrBundled, namesDeclaredIn} from "@openrewrite/rewrite/javascript";
|
|
3
3
|
import {J} from "@openrewrite/rewrite/java";
|
|
4
|
-
import {
|
|
4
|
+
import {importBindings} from "./util/module-imports";
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Common React hooks and utilities that can be imported directly instead of
|
|
@@ -100,24 +100,20 @@ export class SimplifyReactImports extends Recipe {
|
|
|
100
100
|
|
|
101
101
|
async editor(): Promise<TreeVisitor<any, ExecutionContext>> {
|
|
102
102
|
return check(not(isVendoredOrBundled()), new class extends JavaScriptVisitor<ExecutionContext> {
|
|
103
|
-
// Track which React exports we've used in this file
|
|
104
103
|
private usedExports = new Set<string>();
|
|
105
|
-
|
|
106
|
-
private localIdentifiers = new Set<string>();
|
|
104
|
+
private declaredNames = new Set<string>();
|
|
107
105
|
|
|
108
106
|
override async visitJsCompilationUnit(cu: JS.CompilationUnit, ctx: ExecutionContext): Promise<J | undefined> {
|
|
109
|
-
// Reset tracking for each file
|
|
110
107
|
this.usedExports = new Set<string>();
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
108
|
+
// File-wide rather than in scope, as `maybeAddImport` aliases the import when any binding of the name is declared
|
|
109
|
+
this.declaredNames = new Set(namesDeclaredIn(cu));
|
|
110
|
+
for (const {local, name, module} of importBindings(cu)) {
|
|
111
|
+
if (module === 'react' && local === name) this.declaredNames.delete(local);
|
|
112
|
+
}
|
|
115
113
|
|
|
116
|
-
|
|
117
|
-
let result = await super.visitJsCompilationUnit(cu, ctx) as JS.CompilationUnit;
|
|
114
|
+
const result = await super.visitJsCompilationUnit(cu, ctx) as JS.CompilationUnit;
|
|
118
115
|
|
|
119
|
-
//
|
|
120
|
-
// We set onlyIfReferenced: false since we already know these are used (we just transformed them)
|
|
116
|
+
// Already known to be referenced, since they were just unqualified
|
|
121
117
|
for (const exportName of this.usedExports) {
|
|
122
118
|
maybeAddImport(this, {module: "react", member: exportName, onlyIfReferenced: false});
|
|
123
119
|
}
|
|
@@ -125,54 +121,6 @@ export class SimplifyReactImports extends Recipe {
|
|
|
125
121
|
return result;
|
|
126
122
|
}
|
|
127
123
|
|
|
128
|
-
/**
|
|
129
|
-
* Collect all locally defined identifiers to detect potential naming conflicts.
|
|
130
|
-
* This includes variable declarations, function declarations, and non-React imports.
|
|
131
|
-
*/
|
|
132
|
-
private async collectLocalIdentifiers(cu: JS.CompilationUnit, ctx: ExecutionContext): Promise<void> {
|
|
133
|
-
const collector = new class extends JavaScriptVisitor<ExecutionContext> {
|
|
134
|
-
constructor(private identifiers: Set<string>) {
|
|
135
|
-
super();
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
override async visitVariableDeclarations(varDecl: J.VariableDeclarations, ctx: ExecutionContext): Promise<J | undefined> {
|
|
139
|
-
for (const v of varDecl.variables) {
|
|
140
|
-
const name = v.element.name;
|
|
141
|
-
if (name.kind === J.Kind.Identifier) {
|
|
142
|
-
this.identifiers.add((name as J.Identifier).simpleName);
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
return super.visitVariableDeclarations(varDecl, ctx);
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
override async visitMethodDeclaration(method: J.MethodDeclaration, ctx: ExecutionContext): Promise<J | undefined> {
|
|
149
|
-
this.identifiers.add(method.name.simpleName);
|
|
150
|
-
return super.visitMethodDeclaration(method, ctx);
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
override async visitImportSpecifier(spec: JS.ImportSpecifier, ctx: ExecutionContext): Promise<J | undefined> {
|
|
154
|
-
// Track named imports
|
|
155
|
-
const specifier = spec.specifier;
|
|
156
|
-
|
|
157
|
-
if (specifier.kind === JS.Kind.Alias) {
|
|
158
|
-
// Aliased import: import { useState as myUseState }
|
|
159
|
-
const alias = specifier as JS.Alias;
|
|
160
|
-
const aliasIdent = alias.alias;
|
|
161
|
-
if (aliasIdent.kind === J.Kind.Identifier) {
|
|
162
|
-
this.identifiers.add((aliasIdent as J.Identifier).simpleName);
|
|
163
|
-
}
|
|
164
|
-
} else if (specifier.kind === J.Kind.Identifier) {
|
|
165
|
-
// Direct import: import { useState }
|
|
166
|
-
// We don't track these as conflicts since they're the imports we manage
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
return super.visitImportSpecifier(spec, ctx);
|
|
170
|
-
}
|
|
171
|
-
}(this.localIdentifiers);
|
|
172
|
-
|
|
173
|
-
await collector.visit(cu, ctx);
|
|
174
|
-
}
|
|
175
|
-
|
|
176
124
|
override async visitMethodInvocation(method: J.MethodInvocation, ctx: ExecutionContext): Promise<J | undefined> {
|
|
177
125
|
let m = await super.visitMethodInvocation(method, ctx) as J.MethodInvocation;
|
|
178
126
|
|
|
@@ -181,8 +129,7 @@ export class SimplifyReactImports extends Recipe {
|
|
|
181
129
|
return m;
|
|
182
130
|
}
|
|
183
131
|
|
|
184
|
-
|
|
185
|
-
if (this.localIdentifiers.has(methodName)) {
|
|
132
|
+
if (this.declaredNames.has(methodName)) {
|
|
186
133
|
return m;
|
|
187
134
|
}
|
|
188
135
|
|
|
@@ -220,8 +167,7 @@ export class SimplifyReactImports extends Recipe {
|
|
|
220
167
|
return fa;
|
|
221
168
|
}
|
|
222
169
|
|
|
223
|
-
|
|
224
|
-
if (this.localIdentifiers.has(memberName)) {
|
|
170
|
+
if (this.declaredNames.has(memberName)) {
|
|
225
171
|
return fa;
|
|
226
172
|
}
|
|
227
173
|
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import {JS} from "@openrewrite/rewrite/javascript";
|
|
2
|
+
import {isIdentifier, J} from "@openrewrite/rewrite/java";
|
|
3
|
+
import {Modules, Resolver} from "./enzyme-portals";
|
|
4
|
+
|
|
5
|
+
/** A string a `const` holds, as literal text and the names whose strings its template interpolates. */
|
|
6
|
+
export type ConstantParts = readonly ({text: string} | {name: string})[];
|
|
7
|
+
|
|
8
|
+
/** The `const` declarations of a source's top level that hold a string literal or a template of other names. */
|
|
9
|
+
export function constantStrings(cu: JS.CompilationUnit): Map<string, ConstantParts> {
|
|
10
|
+
const strings = new Map<string, ConstantParts>();
|
|
11
|
+
for (const {element} of cu.statements) {
|
|
12
|
+
const declarations = (element.kind === JS.Kind.ExportDeclaration ? (element as JS.ExportDeclaration).exportClause : element) as J | undefined;
|
|
13
|
+
if (declarations?.kind !== J.Kind.VariableDeclarations) continue;
|
|
14
|
+
const variables = declarations as J.VariableDeclarations;
|
|
15
|
+
if (!variables.modifiers.some(modifier => modifier.keyword === "const")) continue;
|
|
16
|
+
for (const {element: variable} of variables.variables) {
|
|
17
|
+
const name = variable.name;
|
|
18
|
+
const parts = variable.initializer && isIdentifier(name) ? stringParts(variable.initializer.element) : undefined;
|
|
19
|
+
if (parts && isIdentifier(name)) strings.set(name.simpleName, parts);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return strings;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** A string literal, or a template whose every interpolation is a bare name, as parts; anything else is not constant. */
|
|
26
|
+
export function stringParts(node: J): ConstantParts | undefined {
|
|
27
|
+
if (node.kind === J.Kind.Literal) return typeof (node as J.Literal).value === "string" ? [{text: (node as J.Literal).value as string}] : undefined;
|
|
28
|
+
if (node.kind !== JS.Kind.TemplateExpression) return undefined;
|
|
29
|
+
const template = node as JS.TemplateExpression;
|
|
30
|
+
const parts: ({text: string} | {name: string})[] = [];
|
|
31
|
+
const text = (literal: J.Literal) => typeof literal.value === "string" ? literal.value : undefined;
|
|
32
|
+
const head = text(template.head);
|
|
33
|
+
if (head === undefined) return undefined;
|
|
34
|
+
parts.push({text: head});
|
|
35
|
+
for (const {element: span} of template.spans) {
|
|
36
|
+
const tail = text(span.tail);
|
|
37
|
+
if (!isIdentifier(span.expression) || tail === undefined) return undefined;
|
|
38
|
+
parts.push({name: span.expression.simpleName}, {text: tail});
|
|
39
|
+
}
|
|
40
|
+
return parts;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* The string `parts` evaluate to in the source at `path`, following each name to a `const` of that source, or
|
|
45
|
+
* through its imports and re-exports to one of the module it names, as {@link resolve} finds the module.
|
|
46
|
+
*/
|
|
47
|
+
export function evaluate(parts: ConstantParts, path: string, modules: Modules, resolve: Resolver, seen = new Set<string>()): string | undefined {
|
|
48
|
+
let value = "";
|
|
49
|
+
for (const part of parts) {
|
|
50
|
+
if ("text" in part) {
|
|
51
|
+
value += part.text;
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
const named = constantOf(path, part.name, modules, resolve, seen);
|
|
55
|
+
if (named === undefined) return undefined;
|
|
56
|
+
value += named;
|
|
57
|
+
}
|
|
58
|
+
return value;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** The string the binding `name` of the source at `path` holds, where it is a constant one. */
|
|
62
|
+
function constantOf(path: string, name: string, modules: Modules, resolve: Resolver, seen: Set<string>): string | undefined {
|
|
63
|
+
const key = `${path}#${name}`;
|
|
64
|
+
if (seen.has(key)) return undefined;
|
|
65
|
+
seen.add(key);
|
|
66
|
+
try {
|
|
67
|
+
const facts = modules.get(path);
|
|
68
|
+
if (!facts) return undefined;
|
|
69
|
+
const own = facts.strings.get(name);
|
|
70
|
+
if (own) return evaluate(own, path, modules, resolve, seen);
|
|
71
|
+
const imported = facts.imports.get(name) ?? facts.forwards.get(name);
|
|
72
|
+
if (imported) {
|
|
73
|
+
const target = resolve(path, imported.module);
|
|
74
|
+
return target === undefined ? undefined : exportedConstant(target, imported.name, modules, resolve, seen);
|
|
75
|
+
}
|
|
76
|
+
return undefined;
|
|
77
|
+
} finally {
|
|
78
|
+
seen.delete(key);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** The string the export `name` of the source at `path` holds, through the names it forwards or re-exports. */
|
|
83
|
+
function exportedConstant(path: string, name: string, modules: Modules, resolve: Resolver, seen: Set<string>): string | undefined {
|
|
84
|
+
const facts = modules.get(path);
|
|
85
|
+
if (!facts) return undefined;
|
|
86
|
+
if (facts.strings.has(name) || facts.forwards.has(name)) return constantOf(path, name, modules, resolve, seen);
|
|
87
|
+
for (const {module, names} of facts.reExports) {
|
|
88
|
+
const exported = names === "*" ? name : names.get(name);
|
|
89
|
+
if (exported === undefined) continue;
|
|
90
|
+
const target = resolve(path, module);
|
|
91
|
+
const value = target === undefined ? undefined : exportedConstant(target, exported, modules, resolve, seen);
|
|
92
|
+
if (value !== undefined) return value;
|
|
93
|
+
}
|
|
94
|
+
return undefined;
|
|
95
|
+
}
|
|
@@ -223,7 +223,7 @@ export function hostComponentExports(cu: JS.CompilationUnit): ReadonlySet<string
|
|
|
223
223
|
const bindings = importBindings(cu);
|
|
224
224
|
// A hook React declares behaves alike only where it is React's own
|
|
225
225
|
if (bindings.some(({local, module}) => HOOK.test(local) && module !== "react")) return NONE;
|
|
226
|
-
const imports =
|
|
226
|
+
const imports = localNames(bindings, cu);
|
|
227
227
|
if (!hostElementsOnly(cu, imports)) return NONE;
|
|
228
228
|
// What a component returns is checked as a child is, since a helper may return any element
|
|
229
229
|
const exported = [...candidates].filter(([, renders]) => returnsAsHost(renders, imports)).map(([name]) => name);
|
|
@@ -232,7 +232,11 @@ export function hostComponentExports(cu: JS.CompilationUnit): ReadonlySet<string
|
|
|
232
232
|
|
|
233
233
|
/** Each local name a source binds with an import, including namespace imports. */
|
|
234
234
|
export function importedNames(cu: JS.CompilationUnit): ReadonlySet<string> {
|
|
235
|
-
return
|
|
235
|
+
return localNames(importBindings(cu), cu);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
function localNames(bindings: ReturnType<typeof importBindings>, cu: JS.CompilationUnit): ReadonlySet<string> {
|
|
239
|
+
return new Set([...bindings.map(({local}) => local), ...namespaceImports(cu)]);
|
|
236
240
|
}
|
|
237
241
|
|
|
238
242
|
/** Each local name a source binds to an export of another scanned source. */
|