@openrewrite/recipes-react 0.20.0 → 0.22.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/i18n/translate-hard-coded-strings.d.ts +67 -0
- package/dist/i18n/translate-hard-coded-strings.d.ts.map +1 -0
- package/dist/i18n/translate-hard-coded-strings.js +439 -0
- package/dist/i18n/translate-hard-coded-strings.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +37 -1
- package/dist/index.js.map +1 -1
- package/dist/mui/adapt-v4-theme.d.ts +8 -0
- package/dist/mui/adapt-v4-theme.d.ts.map +1 -0
- package/dist/mui/adapt-v4-theme.js +65 -0
- package/dist/mui/adapt-v4-theme.js.map +1 -0
- package/dist/mui/add-component-prop.d.ts +18 -0
- package/dist/mui/add-component-prop.d.ts.map +1 -0
- package/dist/mui/add-component-prop.js +53 -0
- package/dist/mui/add-component-prop.js.map +1 -0
- package/dist/mui/add-emotion-prepend.d.ts +8 -0
- package/dist/mui/add-emotion-prepend.d.ts.map +1 -0
- package/dist/mui/add-emotion-prepend.js +71 -0
- package/dist/mui/add-emotion-prepend.js.map +1 -0
- package/dist/mui/change-module-specifier.d.ts +14 -0
- package/dist/mui/change-module-specifier.d.ts.map +1 -0
- package/dist/mui/change-module-specifier.js +95 -0
- package/dist/mui/change-module-specifier.js.map +1 -0
- package/dist/mui/convert-mui-imports.d.ts +14 -0
- package/dist/mui/convert-mui-imports.d.ts.map +1 -0
- package/dist/mui/convert-mui-imports.js +195 -0
- package/dist/mui/convert-mui-imports.js.map +1 -0
- package/dist/mui/convert-prop-to-sx.d.ts +22 -0
- package/dist/mui/convert-prop-to-sx.d.ts.map +1 -0
- package/dist/mui/convert-prop-to-sx.js +99 -0
- package/dist/mui/convert-prop-to-sx.js.map +1 -0
- package/dist/mui/index.d.ts +32 -0
- package/dist/mui/index.d.ts.map +1 -0
- package/dist/mui/index.js +67 -0
- package/dist/mui/index.js.map +1 -0
- package/dist/mui/jsx-attributes.d.ts +27 -0
- package/dist/mui/jsx-attributes.d.ts.map +1 -0
- package/dist/mui/jsx-attributes.js +140 -0
- package/dist/mui/jsx-attributes.js.map +1 -0
- package/dist/mui/mark-manual-migration.d.ts +16 -0
- package/dist/mui/mark-manual-migration.d.ts.map +1 -0
- package/dist/mui/mark-manual-migration.js +73 -0
- package/dist/mui/mark-manual-migration.js.map +1 -0
- package/dist/mui/mark-palette-mode-usage.d.ts +12 -0
- package/dist/mui/mark-palette-mode-usage.d.ts.map +1 -0
- package/dist/mui/mark-palette-mode-usage.js +50 -0
- package/dist/mui/mark-palette-mode-usage.js.map +1 -0
- package/dist/mui/move-component-prop-to-slot.d.ts +22 -0
- package/dist/mui/move-component-prop-to-slot.d.ts.map +1 -0
- package/dist/mui/move-component-prop-to-slot.js +87 -0
- package/dist/mui/move-component-prop-to-slot.js.map +1 -0
- package/dist/mui/move-primary-text-to-children.d.ts +12 -0
- package/dist/mui/move-primary-text-to-children.d.ts.map +1 -0
- package/dist/mui/move-primary-text-to-children.js +52 -0
- package/dist/mui/move-primary-text-to-children.js.map +1 -0
- package/dist/mui/move-system-props-to-sx.d.ts +16 -0
- package/dist/mui/move-system-props-to-sx.d.ts.map +1 -0
- package/dist/mui/move-system-props-to-sx.js +122 -0
- package/dist/mui/move-system-props-to-sx.js.map +1 -0
- package/dist/mui/mui-components.d.ts +8 -0
- package/dist/mui/mui-components.d.ts.map +1 -0
- package/dist/mui/mui-components.js +41 -0
- package/dist/mui/mui-components.js.map +1 -0
- package/dist/mui/mui-tag-visitor.d.ts +5 -0
- package/dist/mui/mui-tag-visitor.d.ts.map +1 -0
- package/dist/mui/mui-tag-visitor.js +54 -0
- package/dist/mui/mui-tag-visitor.js.map +1 -0
- package/dist/mui/pre-v5-recipe.d.ts +14 -0
- package/dist/mui/pre-v5-recipe.d.ts.map +1 -0
- package/dist/mui/pre-v5-recipe.js +88 -0
- package/dist/mui/pre-v5-recipe.js.map +1 -0
- package/dist/mui/remove-component-prop.d.ts +18 -0
- package/dist/mui/remove-component-prop.d.ts.map +1 -0
- package/dist/mui/remove-component-prop.js +55 -0
- package/dist/mui/remove-component-prop.js.map +1 -0
- package/dist/mui/remove-root-ref.d.ts +8 -0
- package/dist/mui/remove-root-ref.d.ts.map +1 -0
- package/dist/mui/remove-root-ref.js +61 -0
- package/dist/mui/remove-root-ref.js.map +1 -0
- package/dist/mui/remove-spacing-px-suffix.d.ts +8 -0
- package/dist/mui/remove-spacing-px-suffix.d.ts.map +1 -0
- package/dist/mui/remove-spacing-px-suffix.js +62 -0
- package/dist/mui/remove-spacing-px-suffix.js.map +1 -0
- package/dist/mui/rename-component-prop.d.ts +20 -0
- package/dist/mui/rename-component-prop.d.ts.map +1 -0
- package/dist/mui/rename-component-prop.js +66 -0
- package/dist/mui/rename-component-prop.js.map +1 -0
- package/dist/mui/rename-joy-css-variables.d.ts +8 -0
- package/dist/mui/rename-joy-css-variables.d.ts.map +1 -0
- package/dist/mui/rename-joy-css-variables.js +51 -0
- package/dist/mui/rename-joy-css-variables.js.map +1 -0
- package/dist/mui/rename-theme-palette-type.d.ts +8 -0
- package/dist/mui/rename-theme-palette-type.d.ts.map +1 -0
- package/dist/mui/rename-theme-palette-type.js +61 -0
- package/dist/mui/rename-theme-palette-type.js.map +1 -0
- package/dist/mui/replace-box-border-radius.d.ts +10 -0
- package/dist/mui/replace-box-border-radius.d.ts.map +1 -0
- package/dist/mui/replace-box-border-radius.js +50 -0
- package/dist/mui/replace-box-border-radius.js.map +1 -0
- package/dist/mui/replace-breakpoints-width.d.ts +8 -0
- package/dist/mui/replace-breakpoints-width.d.ts.map +1 -0
- package/dist/mui/replace-breakpoints-width.js +46 -0
- package/dist/mui/replace-breakpoints-width.js.map +1 -0
- package/dist/mui/replace-component-class.d.ts +18 -0
- package/dist/mui/replace-component-class.d.ts.map +1 -0
- package/dist/mui/replace-component-class.js +126 -0
- package/dist/mui/replace-component-class.js.map +1 -0
- package/dist/mui/replace-components-with-slots.d.ts +14 -0
- package/dist/mui/replace-components-with-slots.d.ts.map +1 -0
- package/dist/mui/replace-components-with-slots.js +69 -0
- package/dist/mui/replace-components-with-slots.js.map +1 -0
- package/dist/mui/replace-import-paths.d.ts +14 -0
- package/dist/mui/replace-import-paths.d.ts.map +1 -0
- package/dist/mui/replace-import-paths.js +101 -0
- package/dist/mui/replace-import-paths.js.map +1 -0
- package/dist/mui/replace-in-string-literals.d.ts +15 -0
- package/dist/mui/replace-in-string-literals.d.ts.map +1 -0
- package/dist/mui/replace-in-string-literals.js +62 -0
- package/dist/mui/replace-in-string-literals.js.map +1 -0
- package/dist/mui/replace-list-item-button-prop.d.ts +12 -0
- package/dist/mui/replace-list-item-button-prop.d.ts.map +1 -0
- package/dist/mui/replace-list-item-button-prop.js +80 -0
- package/dist/mui/replace-list-item-button-prop.js.map +1 -0
- package/dist/mui/replace-tabs-scroll-buttons.d.ts +8 -0
- package/dist/mui/replace-tabs-scroll-buttons.d.ts.map +1 -0
- package/dist/mui/replace-tabs-scroll-buttons.js +41 -0
- package/dist/mui/replace-tabs-scroll-buttons.js.map +1 -0
- package/dist/mui/replace-theme-color-functions.d.ts +8 -0
- package/dist/mui/replace-theme-color-functions.d.ts.map +1 -0
- package/dist/mui/replace-theme-color-functions.js +72 -0
- package/dist/mui/replace-theme-color-functions.js.map +1 -0
- package/dist/mui/replace-theme-spacing-unit.d.ts +8 -0
- package/dist/mui/replace-theme-spacing-unit.d.ts.map +1 -0
- package/dist/mui/replace-theme-spacing-unit.js +77 -0
- package/dist/mui/replace-theme-spacing-unit.js.map +1 -0
- package/dist/mui/replace-typography-round.d.ts +8 -0
- package/dist/mui/replace-typography-round.d.ts.map +1 -0
- package/dist/mui/replace-typography-round.js +44 -0
- package/dist/mui/replace-typography-round.js.map +1 -0
- package/dist/mui/shift-hidden-down-props.d.ts +10 -0
- package/dist/mui/shift-hidden-down-props.d.ts.map +1 -0
- package/dist/mui/shift-hidden-down-props.js +49 -0
- package/dist/mui/shift-hidden-down-props.js.map +1 -0
- package/dist/mui/theme-access.d.ts +5 -0
- package/dist/mui/theme-access.d.ts.map +1 -0
- package/dist/mui/theme-access.js +21 -0
- package/dist/mui/theme-access.js.map +1 -0
- package/dist/mui/update-theme-breakpoints.d.ts +10 -0
- package/dist/mui/update-theme-breakpoints.d.ts.map +1 -0
- package/dist/mui/update-theme-breakpoints.js +58 -0
- package/dist/mui/update-theme-breakpoints.js.map +1 -0
- package/dist/mui/upgrade-grid-props.d.ts +16 -0
- package/dist/mui/upgrade-grid-props.d.ts.map +1 -0
- package/dist/mui/upgrade-grid-props.js +90 -0
- package/dist/mui/upgrade-grid-props.js.map +1 -0
- package/dist/search/find-enzyme-usage.d.ts +8 -0
- package/dist/search/find-enzyme-usage.d.ts.map +1 -1
- package/dist/search/find-enzyme-usage.js +119 -8
- package/dist/search/find-enzyme-usage.js.map +1 -1
- package/dist/search/find-hard-coded-strings.d.ts +110 -0
- package/dist/search/find-hard-coded-strings.d.ts.map +1 -0
- package/dist/search/find-hard-coded-strings.js +987 -0
- package/dist/search/find-hard-coded-strings.js.map +1 -0
- package/dist/search/find-missing-use-client-directive.d.ts +2 -0
- package/dist/search/find-missing-use-client-directive.d.ts.map +1 -1
- package/dist/search/find-missing-use-client-directive.js +1 -0
- package/dist/search/find-missing-use-client-directive.js.map +1 -1
- package/dist/testing/enzyme-host-components.d.ts +12 -0
- package/dist/testing/enzyme-host-components.d.ts.map +1 -1
- package/dist/testing/enzyme-host-components.js +100 -0
- package/dist/testing/enzyme-host-components.js.map +1 -1
- package/dist/testing/enzyme-portals.d.ts +3 -5
- package/dist/testing/enzyme-portals.d.ts.map +1 -1
- package/dist/testing/enzyme-portals.js +8 -39
- package/dist/testing/enzyme-portals.js.map +1 -1
- package/dist/testing/enzyme-render-blockers.d.ts +19 -0
- package/dist/testing/enzyme-render-blockers.d.ts.map +1 -0
- package/dist/testing/enzyme-render-blockers.js +275 -0
- package/dist/testing/enzyme-render-blockers.js.map +1 -0
- package/dist/testing/enzyme-to-react-testing-library.d.ts +17 -3
- package/dist/testing/enzyme-to-react-testing-library.d.ts.map +1 -1
- package/dist/testing/enzyme-to-react-testing-library.js +237 -27
- package/dist/testing/enzyme-to-react-testing-library.js.map +1 -1
- package/dist/testing/enzyme-usage.d.ts +9 -0
- package/dist/testing/enzyme-usage.d.ts.map +1 -1
- package/dist/testing/enzyme-usage.js +36 -3
- package/dist/testing/enzyme-usage.js.map +1 -1
- package/dist/testing/enzyme-wrapper-scope.d.ts +8 -1
- package/dist/testing/enzyme-wrapper-scope.d.ts.map +1 -1
- package/dist/testing/enzyme-wrapper-scope.js +26 -5
- package/dist/testing/enzyme-wrapper-scope.js.map +1 -1
- package/dist/util/json-and-javascript.d.ts +4 -0
- package/dist/util/json-and-javascript.d.ts.map +1 -0
- package/dist/util/json-and-javascript.js +37 -0
- package/dist/util/json-and-javascript.js.map +1 -0
- package/dist/util/module-imports.d.ts +36 -0
- package/dist/util/module-imports.d.ts.map +1 -1
- package/dist/util/module-imports.js +216 -0
- package/dist/util/module-imports.js.map +1 -1
- package/dist/util/test-source.d.ts +2 -0
- package/dist/util/test-source.d.ts.map +1 -0
- package/dist/util/test-source.js +5 -0
- package/dist/util/test-source.js.map +1 -0
- package/dist/zustand/entry-store.d.ts +9 -0
- package/dist/zustand/entry-store.d.ts.map +1 -0
- package/dist/zustand/entry-store.js +136 -0
- package/dist/zustand/entry-store.js.map +1 -0
- package/dist/zustand/redux-stores.d.ts +7 -4
- package/dist/zustand/redux-stores.d.ts.map +1 -1
- package/dist/zustand/redux-stores.js +128 -11
- package/dist/zustand/redux-stores.js.map +1 -1
- package/dist/zustand/redux-to-zustand.d.ts +11 -2
- package/dist/zustand/redux-to-zustand.d.ts.map +1 -1
- package/dist/zustand/redux-to-zustand.js +147 -46
- package/dist/zustand/redux-to-zustand.js.map +1 -1
- package/dist/zustand/zustand-plan.d.ts +14 -5
- package/dist/zustand/zustand-plan.d.ts.map +1 -1
- package/dist/zustand/zustand-plan.js +226 -52
- package/dist/zustand/zustand-plan.js.map +1 -1
- package/package.json +1 -1
- package/src/i18n/translate-hard-coded-strings.ts +442 -0
- package/src/index.ts +17 -0
- package/src/mui/adapt-v4-theme.ts +43 -0
- package/src/mui/add-component-prop.ts +43 -0
- package/src/mui/add-emotion-prepend.ts +48 -0
- package/src/mui/change-module-specifier.ts +71 -0
- package/src/mui/convert-mui-imports.ts +184 -0
- package/src/mui/convert-prop-to-sx.ts +89 -0
- package/src/mui/index.ts +31 -0
- package/src/mui/jsx-attributes.ts +133 -0
- package/src/mui/mark-manual-migration.ts +53 -0
- package/src/mui/mark-palette-mode-usage.ts +36 -0
- package/src/mui/move-component-prop-to-slot.ts +84 -0
- package/src/mui/move-primary-text-to-children.ts +41 -0
- package/src/mui/move-system-props-to-sx.ts +118 -0
- package/src/mui/mui-components.ts +40 -0
- package/src/mui/mui-tag-visitor.ts +36 -0
- package/src/mui/pre-v5-recipe.ts +82 -0
- package/src/mui/remove-component-prop.ts +43 -0
- package/src/mui/remove-root-ref.ts +41 -0
- package/src/mui/remove-spacing-px-suffix.ts +43 -0
- package/src/mui/rename-component-prop.ts +55 -0
- package/src/mui/rename-joy-css-variables.ts +41 -0
- package/src/mui/rename-theme-palette-type.ts +42 -0
- package/src/mui/replace-box-border-radius.ts +42 -0
- package/src/mui/replace-breakpoints-width.ts +28 -0
- package/src/mui/replace-component-class.ts +97 -0
- package/src/mui/replace-components-with-slots.ts +61 -0
- package/src/mui/replace-import-paths.ts +100 -0
- package/src/mui/replace-in-string-literals.ts +44 -0
- package/src/mui/replace-list-item-button-prop.ts +59 -0
- package/src/mui/replace-tabs-scroll-buttons.ts +31 -0
- package/src/mui/replace-theme-color-functions.ts +55 -0
- package/src/mui/replace-theme-spacing-unit.ts +59 -0
- package/src/mui/replace-typography-round.ts +29 -0
- package/src/mui/shift-hidden-down-props.ts +44 -0
- package/src/mui/theme-access.ts +18 -0
- package/src/mui/update-theme-breakpoints.ts +47 -0
- package/src/mui/upgrade-grid-props.ts +82 -0
- package/src/search/find-enzyme-usage.ts +109 -12
- package/src/search/find-hard-coded-strings.ts +1006 -0
- package/src/search/find-missing-use-client-directive.ts +1 -1
- package/src/testing/enzyme-host-components.ts +110 -0
- package/src/testing/enzyme-portals.ts +4 -44
- package/src/testing/enzyme-render-blockers.ts +278 -0
- package/src/testing/enzyme-to-react-testing-library.ts +240 -34
- package/src/testing/enzyme-usage.ts +48 -2
- package/src/testing/enzyme-wrapper-scope.ts +35 -3
- package/src/util/json-and-javascript.ts +26 -0
- package/src/util/module-imports.ts +223 -0
- package/src/util/test-source.ts +2 -0
- package/src/zustand/entry-store.ts +131 -0
- package/src/zustand/redux-stores.ts +131 -12
- package/src/zustand/redux-to-zustand.ts +179 -52
- package/src/zustand/zustand-plan.ts +256 -49
|
@@ -2,8 +2,8 @@ import {Cursor, ExecutionContext} from "@openrewrite/rewrite";
|
|
|
2
2
|
import {JavaScriptVisitor, JS, JSX, namesReferencedWithin} from "@openrewrite/rewrite/javascript";
|
|
3
3
|
import {isIdentifier, J} from "@openrewrite/rewrite/java";
|
|
4
4
|
import {posix} from "path";
|
|
5
|
-
import {ImportBinding, importBindings, isRelativeSpecifier, resolveRelative} from "../util/module-imports";
|
|
6
|
-
import {Finding, localSlices, ReducerStoreKind, objectProperties, RTK, SliceFacts, SliceModule, storeFindings, storeFunctions, TEST_SOURCE} from "./redux-stores";
|
|
5
|
+
import {ImportBinding, importBindings, isAliasSpecifier, isRelativeSpecifier, Resolve, resolveRelative} from "../util/module-imports";
|
|
6
|
+
import {Finding, importedAs, localSlices, ReducerStoreKind, objectProperties, RTK, SliceFacts, SliceModule, storeFindings, storeFunctions, TEST_SOURCE} from "./redux-stores";
|
|
7
7
|
|
|
8
8
|
export const REACT_REDUX = "react-redux";
|
|
9
9
|
|
|
@@ -33,7 +33,7 @@ export interface Use {
|
|
|
33
33
|
kind: UseKind;
|
|
34
34
|
/** The slice key a selector or `getState()` read starts with. */
|
|
35
35
|
key?: string;
|
|
36
|
-
/** For `dispatched`, the import the dispatch goes through: a dispatcher from `useDispatch`, or the store. */
|
|
36
|
+
/** For `dispatched`, the import the dispatch goes through: a dispatcher from `useDispatch`, or the store. For `providerTag`, the import it provides. */
|
|
37
37
|
via?: string;
|
|
38
38
|
/** What `other` was, for the review row. */
|
|
39
39
|
detail?: string;
|
|
@@ -45,6 +45,125 @@ export interface Use {
|
|
|
45
45
|
selector?: string;
|
|
46
46
|
/** For `useSelector(selector)` with one argument and no type arguments, which a whole store runs as it is. */
|
|
47
47
|
anySelector?: boolean;
|
|
48
|
+
/** What to do about an `other` use, for the review row, where it says more than its reason's action. */
|
|
49
|
+
action?: string;
|
|
50
|
+
/** For an `other` use of an import as a value, how it is used, which says what to do where the import is the store. */
|
|
51
|
+
valueUse?: StoreValueUse;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** How a source uses the store's variable as a value, other than to call `getState`, `dispatch` or `subscribe`. */
|
|
55
|
+
export interface StoreValueUse {
|
|
56
|
+
kind: "type" | "persist" | "passed" | "provider" | "method" | "member" | "other";
|
|
57
|
+
/** The function the store is passed to, or the member called or used. */
|
|
58
|
+
name?: string;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** How the identifier at `cursor`, the store's variable, is used, where it is used other than to call a member of `STORE_MEMBERS`. */
|
|
62
|
+
function storeValueUse(identifier: J.Identifier, cursor: Cursor, persistStore: ReadonlySet<string>): StoreValueUse {
|
|
63
|
+
const trees = ancestors(cursor);
|
|
64
|
+
if (trees.some(tree => tree.kind === JS.Kind.TypeQuery)) return {kind: "type"};
|
|
65
|
+
const parent = parentTree(cursor);
|
|
66
|
+
if (parent?.kind === J.Kind.MethodInvocation) {
|
|
67
|
+
const call = parent as J.MethodInvocation;
|
|
68
|
+
if (call.select?.element === identifier) return {kind: "method", name: call.name.simpleName};
|
|
69
|
+
if (argumentsOf(call).includes(identifier)) {
|
|
70
|
+
if (!call.select && persistStore.has(call.name.simpleName)) return {kind: "persist"};
|
|
71
|
+
const select = call.select && isIdentifier(call.select.element) ? `${call.select.element.simpleName}.` : "";
|
|
72
|
+
return {kind: "passed", name: select + call.name.simpleName};
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
if (parent?.kind === J.Kind.FieldAccess && (parent as J.FieldAccess).target === identifier && STORE_MEMBERS.has((parent as J.FieldAccess).name.element.simpleName)) {
|
|
76
|
+
return {kind: "member", name: (parent as J.FieldAccess).name.element.simpleName};
|
|
77
|
+
}
|
|
78
|
+
if (parent?.kind === JS.Kind.JsxEmbeddedExpression && trees[2]?.kind === JS.Kind.JsxAttribute && isIdentifier((trees[2] as JSX.Attribute).key) &&
|
|
79
|
+
((trees[2] as JSX.Attribute).key as J.Identifier).simpleName === "store") return {kind: "provider"};
|
|
80
|
+
return {kind: "other"};
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** The calls of a member of the store that stand in for passing the member, by name. */
|
|
84
|
+
const MEMBER_CALLS: Readonly<Record<string, string>> = {getState: "() => STORE.getState()", dispatch: "action => STORE.dispatch(action)", subscribe: "listener => STORE.subscribe(listener)"};
|
|
85
|
+
|
|
86
|
+
/** What to do about the uses of the store `store` as a value, or undefined where none says more than the reason's action does. */
|
|
87
|
+
export function storeValueAction(store: string, uses: readonly StoreValueUse[]): string | undefined {
|
|
88
|
+
if (uses.some(use => use.kind === "persist")) return `\`${store}\` is passed to redux-persist's \`persistStore\`, and redux-persist persists a Redux store: convert the store by hand, with zustand's \`persist\` middleware in place of redux-persist.`;
|
|
89
|
+
const first = (kind: StoreValueUse["kind"]) => uses.find(use => use.kind === kind);
|
|
90
|
+
const actions: string[] = [];
|
|
91
|
+
if (first("type")) actions.push(`\`typeof ${store}\` types, such as \`ReturnType<typeof ${store}.getState>\`, go with the TypeScript sources, which aren't converted yet.`);
|
|
92
|
+
const passed = first("passed");
|
|
93
|
+
// A test's spy on a member of the Redux store watches a store the conversion replaces
|
|
94
|
+
if (passed && /(^|\.)spyOn$/.test(passed.name!)) actions.push(`\`${passed.name}(${store}, ...)\` spies on a member of the Redux store, which the conversion replaces: check what the test spies for another way, such as by the state after the dispatch, or convert the test by hand.`);
|
|
95
|
+
else if (passed) actions.push(`\`${store}\` is passed to \`${passed.name}\`, which the recipe doesn't follow: pass the calls it makes instead, such as \`() => ${store}.getState()\` and \`action => ${store}.dispatch(action)\`, so that the recipe checks each call, or convert by hand.`);
|
|
96
|
+
const member = first("member");
|
|
97
|
+
if (member) actions.push(`Call \`${store}.${member.name}\` rather than pass it, as \`${MEMBER_CALLS[member.name!].replace("STORE", store)}\`, so that the recipe checks each call.`);
|
|
98
|
+
const method = first("method");
|
|
99
|
+
if (method) actions.push(`The zustand store has no \`${method.name}\`: remove \`${store}.${method.name}(...)\`, or convert by hand.`);
|
|
100
|
+
if (first("provider")) actions.push(`Render \`<Provider store={${store}}>\` in a module that imports the store, not in the store's own source.`);
|
|
101
|
+
return actions.length > 0 ? actions.join(" ") : undefined;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** What to do with an import of react-redux used other than called, by its name. */
|
|
105
|
+
const REACT_REDUX_VALUE_ACTIONS: Readonly<Record<string, string>> = {
|
|
106
|
+
shallowEqual: "zustand's hooks take no equality function. Where `shallowEqual` is `useSelector`'s second argument, select one value in each `useSelector` and drop it, or write the selector with zustand's `useShallow` by hand.",
|
|
107
|
+
TypedUseSelectorHook: "A react-redux type goes with the TypeScript sources, which aren't converted yet.",
|
|
108
|
+
ConnectedProps: "A react-redux type goes with the TypeScript sources, which aren't converted yet.",
|
|
109
|
+
ResolveThunks: "A react-redux type goes with the TypeScript sources, which aren't converted yet."
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
/** What to do with a call of an export of react-redux that the conversion doesn't follow, by its name. */
|
|
113
|
+
const REACT_REDUX_CALL_ACTIONS: Readonly<Record<string, string>> = {
|
|
114
|
+
batch: "zustand has no `batch`: on React 18, which batches updates itself, call the callback directly; before React 18, wrap it in `unstable_batchedUpdates` of react-dom.",
|
|
115
|
+
useStore: "Read the state with `useSelector` and dispatch with `useDispatch` instead of `useStore()`, or convert by hand."
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
/** What to do with a `useSelector` whose selector doesn't read one slice's state, by its arguments. */
|
|
119
|
+
function selectorAction(args: number, typed: boolean): string {
|
|
120
|
+
if (typed) return "A `useSelector` with type arguments goes with the TypeScript sources, which aren't converted yet.";
|
|
121
|
+
if (args !== 1) return "Pass `useSelector` the selector alone: zustand's hooks take no equality function. Select one value in each `useSelector`, so that none needs one, or convert by hand.";
|
|
122
|
+
return "Read one slice's state in each selector, such as `state => state.counter.value`, or a top-level `const` selector that does, so that the slice's zustand hook can take it.";
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** What to do with a use checked against the store's slices, which aren't read while the store has findings of its own. */
|
|
126
|
+
const UNREAD_SLICES = "The store's slices aren't read while its own rows, such as MIDDLEWARE or ROOT_REDUCER, hold it back, so this use is recorded without being checked: remove what those rows name, and run the recipe again.";
|
|
127
|
+
|
|
128
|
+
/** What to do with a dispatch of an import that isn't an action creator or thunk of the store's slices, by whether its source declares a slice. */
|
|
129
|
+
function sliceDispatchAction(binding: ImportBinding, fromSlice: boolean): string {
|
|
130
|
+
return fromSlice ?
|
|
131
|
+
`\`${binding.name}\` of ${binding.module} is neither one of its slice's action creators nor a \`createAsyncThunk\` the recipe reads, such as a thunk written as \`() => async dispatch => ...\`: declare it with \`createAsyncThunk\` in that source, or export it from \`slice.actions\`, or convert by hand.` :
|
|
132
|
+
`\`${binding.name}\` comes from ${binding.module}, which declares none of the store's slices, such as a module of hand-written action creators or thunks: write it as a reducer of a \`createSlice\`, or as a \`createAsyncThunk\` in a slice's source, and import it from there, or convert by hand.`;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** What to do with a `connect` of a store that can't move whole, which only a store moving whole declares. */
|
|
136
|
+
export function connectAction(wholeStores: boolean): string {
|
|
137
|
+
return wholeStores ?
|
|
138
|
+
"`connect` converts only where the store moves whole, which takes a `configureStore` of `reducer` alone, or a `createStore` of a reducer with at most a preloaded state, `applyMiddleware(thunk)` and Redux DevTools. Remove what the store's other rows name so it moves whole, or replace `connect` with `useSelector` and `useDispatch`." :
|
|
139
|
+
"`connect` converts only where the store moves whole, which `wholeStores` turns off: run with it on, or replace `connect` with `useSelector` and `useDispatch`.";
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** What to do with an import of the store's source other than the store, which the slice plan removes with the source. */
|
|
143
|
+
function storeExportAction(binding: ImportBinding, source: SourceFacts, store: StoreFacts, storeModule: SourceFacts | undefined, wholeStores: boolean): string {
|
|
144
|
+
// A default import is named by its local name, as the source writes it
|
|
145
|
+
const name = binding.name === "default" ? binding.local : binding.name;
|
|
146
|
+
if (/^use[A-Z]/.test(name)) return `\`${name}\` comes from the store's source, which the conversion removes; most often it is a typed alias of a react-redux hook, such as \`useSelector.withTypes<RootState>()\`: call the hook itself where \`${name}\` is called and remove the alias, or convert by hand.`;
|
|
147
|
+
if (/^[A-Z]/.test(name) && source.typescript) return `\`${name}\` comes from the store's source, which the conversion removes; most often it is a type of the store, such as \`ReturnType<typeof store.getState>\`, which goes with the TypeScript sources, which aren't converted yet.`;
|
|
148
|
+
if (/persist/i.test(name) && storeModule?.bindings.some(b => b.module === "redux-persist")) return `\`${name}\` comes from the store's source, and redux-persist persists a Redux store: convert the store by hand, with zustand's \`persist\` middleware in place of redux-persist.`;
|
|
149
|
+
// A store made by a function, such as `initStore`, is no store until it's called
|
|
150
|
+
if (!store.exportedAs && (name === store.store || /store/i.test(name))) return `The store isn't created at its module's top level, so \`${name}\` is no store the recipe follows: create one store with \`export const store = configureStore(...)\`, and import that in place of \`${name}\`, or convert by hand.`;
|
|
151
|
+
return `\`${name}\` comes from the store's source, ${store.path}, which the conversion removes: move \`${name}\` to a module of its own and import it from there, or convert by hand.` +
|
|
152
|
+
(wholeStores && store.exportedAs ? ` Or remove what the store's other rows name, so that it moves whole and its source keeps \`${name}\`.` : "");
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/** What to do with a store of a package that creates more than one, by how many of them are created outside tests. */
|
|
156
|
+
function multipleStoresAction(store: StoreFacts, stores: readonly StoreFacts[], facts: ReadonlyMap<string, SourceFacts>): string {
|
|
157
|
+
const app = stores.filter(s => !facts.get(s.path)?.test);
|
|
158
|
+
const why = "the recipe converts a package of one store, as react-redux's hooks don't say which store they read";
|
|
159
|
+
if (app.length === 0) return `All ${stores.length} of the package's stores are created in test, mock or story sources: ${why}. Convert by hand.`;
|
|
160
|
+
if (app.length === 1) {
|
|
161
|
+
const one = app[0] === store ? "this store" : `its one other store, \`${app[0].store}\` of ${app[0].path}`;
|
|
162
|
+
return `${stores.length - 1} of the package's ${stores.length} stores ${stores.length === 2 ? "is" : "are"} created in test, mock or story sources, which hold back ${one}: ${why}. Convert by hand.`;
|
|
163
|
+
}
|
|
164
|
+
const paths = [...new Set(app.map(s => s.path))];
|
|
165
|
+
const named = paths.length > 3 ? `${paths.slice(0, 3).join(", ")} and ${paths.length - 3} more` : paths.join(", ");
|
|
166
|
+
return `The package creates ${app.length} stores outside tests, in ${named}: ${why}. Move each store, with the sources that use it, to a package of its own, or convert by hand.`;
|
|
48
167
|
}
|
|
49
168
|
|
|
50
169
|
/** What one source tells the plan. */
|
|
@@ -82,6 +201,8 @@ export interface StoreFacts {
|
|
|
82
201
|
reducerStore?: ReducerStoreKind;
|
|
83
202
|
/** Whether the store's own source uses the store other than to declare and export it, or to call its `getState`, `dispatch` or `subscribe`. */
|
|
84
203
|
usedInSource?: boolean;
|
|
204
|
+
/** How the store's own source uses it, where `usedInSource`. */
|
|
205
|
+
usesInSource?: readonly StoreValueUse[];
|
|
85
206
|
/** The members of the store its own source calls, such as `subscribe`. */
|
|
86
207
|
calledInSource?: ReadonlySet<string>;
|
|
87
208
|
}
|
|
@@ -259,8 +380,10 @@ export async function sourceFacts(cu: JS.CompilationUnit): Promise<SourceFacts>
|
|
|
259
380
|
path: cu.sourcePath, typescript: /\.(?:ts|tsx|mts|cts)$/.test(cu.sourcePath), test: TEST_SOURCE.test(cu.sourcePath),
|
|
260
381
|
bindings, declared: new Set(declared.keys()), uses: [], unfollowed: [], stores: [], readSelectors: readSelectors(cu, declared)
|
|
261
382
|
};
|
|
262
|
-
|
|
383
|
+
// A specifier no package can have, such as `@/store`, may name a source through an alias, which the plan resolves
|
|
384
|
+
const followed = new Map(bindings.filter(binding => binding.module === REACT_REDUX || isRelativeSpecifier(binding.module) || isAliasSpecifier(binding.module))
|
|
263
385
|
.map(binding => [binding.local, binding]));
|
|
386
|
+
const persistStore = new Set(bindings.filter(b => b.module === "redux-persist" && b.name === "persistStore").map(b => b.local));
|
|
264
387
|
for (const {element} of cu.statements) {
|
|
265
388
|
if (element.kind === JS.Kind.Import) {
|
|
266
389
|
const imp = element as JS.Import;
|
|
@@ -295,7 +418,7 @@ export async function sourceFacts(cu: JS.CompilationUnit): Promise<SourceFacts>
|
|
|
295
418
|
if (use) facts.uses.push(use);
|
|
296
419
|
}
|
|
297
420
|
if (binding) {
|
|
298
|
-
const use = classify(identifier, binding, this.cursor, followed, dispatchers, declared);
|
|
421
|
+
const use = classify(identifier, binding, this.cursor, followed, dispatchers, declared, persistStore);
|
|
299
422
|
if (use) facts.uses.push(use);
|
|
300
423
|
}
|
|
301
424
|
return identifier;
|
|
@@ -314,18 +437,18 @@ export async function sourceFacts(cu: JS.CompilationUnit): Promise<SourceFacts>
|
|
|
314
437
|
}
|
|
315
438
|
|
|
316
439
|
/** The Redux stores `cu` creates, read once every slice of the repository is known. */
|
|
317
|
-
export async function storeFacts(cu: JS.CompilationUnit, modules: ReadonlyMap<string, SliceModule
|
|
440
|
+
export async function storeFacts(cu: JS.CompilationUnit, modules: ReadonlyMap<string, SliceModule>, resolve?: Resolve): Promise<StoreFacts[]> {
|
|
318
441
|
const stores: StoreFacts[] = [];
|
|
319
442
|
const functions = storeFunctions(cu);
|
|
320
443
|
if (functions.legacy.size === 0 && functions.configure.size === 0) return stores;
|
|
321
444
|
await new class extends JavaScriptVisitor<ExecutionContext> {
|
|
322
445
|
override async visitMethodInvocation(call: J.MethodInvocation, ctx: ExecutionContext): Promise<J | undefined> {
|
|
323
|
-
const store = storeFindings(call, cu, modules, functions);
|
|
446
|
+
const store = storeFindings(call, cu, modules, functions, resolve);
|
|
324
447
|
if (store) {
|
|
325
448
|
const name = storeNameAt(this.cursor);
|
|
326
449
|
const own = await usesInSource(cu, name, call);
|
|
327
450
|
stores.push({path: cu.sourcePath, api: store.api, store: name, findings: store.findings,
|
|
328
|
-
slices: store.slices, exportedAs: exportedStore(cu, this.cursor), reducerStore: store.reducerStore, usedInSource: own.other, calledInSource: own.called});
|
|
451
|
+
slices: store.slices, exportedAs: exportedStore(cu, this.cursor), reducerStore: store.reducerStore, usedInSource: own.other, usesInSource: own.uses, calledInSource: own.called});
|
|
329
452
|
}
|
|
330
453
|
return super.visitMethodInvocation(call, ctx);
|
|
331
454
|
}
|
|
@@ -366,12 +489,12 @@ function dispatcherCount(cu: JS.CompilationUnit, followed: ReadonlyMap<string, I
|
|
|
366
489
|
|
|
367
490
|
/** How the identifier at `cursor`, which names the import `binding`, is used; undefined where it is no use, such as a property name. */
|
|
368
491
|
function classify(identifier: J.Identifier, binding: ImportBinding, cursor: Cursor, followed: ReadonlyMap<string, ImportBinding>,
|
|
369
|
-
dispatchers: ReadonlySet<string>, declared: ReadonlyMap<string, number>): Use | undefined {
|
|
492
|
+
dispatchers: ReadonlySet<string>, declared: ReadonlyMap<string, number>, persistStore: ReadonlySet<string>): Use | undefined {
|
|
370
493
|
const trees = ancestors(cursor);
|
|
371
494
|
if (trees.some(tree => tree.kind === JS.Kind.Import)) return undefined;
|
|
372
495
|
const parent = parentTree(cursor);
|
|
373
496
|
const local = identifier.simpleName;
|
|
374
|
-
const other = (detail: string): Use => ({local, kind: "other", detail});
|
|
497
|
+
const other = (detail: string, action?: string): Use => ({local, kind: "other", detail, ...action ? {action} : {}});
|
|
375
498
|
// Names of members, properties and attributes are no use of the import
|
|
376
499
|
if (parent?.kind === J.Kind.FieldAccess && (parent as J.FieldAccess).name.element === identifier) return undefined;
|
|
377
500
|
if (parent?.kind === J.Kind.MethodInvocation && (parent as J.MethodInvocation).name === identifier && (parent as J.MethodInvocation).select) return undefined;
|
|
@@ -387,7 +510,9 @@ function classify(identifier: J.Identifier, binding: ImportBinding, cursor: Curs
|
|
|
387
510
|
return providerUse(tag, followed, local);
|
|
388
511
|
}
|
|
389
512
|
if (binding.module === REACT_REDUX) {
|
|
390
|
-
if (parent?.kind !== J.Kind.MethodInvocation || (parent as J.MethodInvocation).name !== identifier) return other(`\`${binding.name}\` from react-redux
|
|
513
|
+
if (parent?.kind !== J.Kind.MethodInvocation || (parent as J.MethodInvocation).name !== identifier) return other(`\`${binding.name}\` from react-redux`, REACT_REDUX_VALUE_ACTIONS[binding.name] ?? (binding.name.startsWith("use") ?
|
|
514
|
+
`\`${binding.name}\` is used other than called, most often in a typed alias such as \`const useApp${binding.name.slice(3)} = ${binding.name}\` or \`${binding.name}.withTypes<RootState>()\`, which the recipe doesn't follow: call \`${binding.name}\` itself where the alias is called and remove the alias, or convert by hand.` :
|
|
515
|
+
`\`${binding.name}\` is used other than called, such as passed or re-exported: call it where it's used, so the recipe follows each call, or convert by hand.`));
|
|
391
516
|
const call = parent as J.MethodInvocation;
|
|
392
517
|
if (binding.name === "connect") return connectUse(call, other) ?? {local, kind: "connect"};
|
|
393
518
|
if (binding.name === "useSelector") {
|
|
@@ -396,14 +521,16 @@ function classify(identifier: J.Identifier, binding: ImportBinding, cursor: Curs
|
|
|
396
521
|
if (read) return {local, kind: "selector", key: read.key};
|
|
397
522
|
// A selector passed by name moves only with its whole store, which calls it as it is
|
|
398
523
|
const selector = args.length === 1 && !call.typeParameters && isIdentifier(args[0]) ? args[0].simpleName : undefined;
|
|
399
|
-
return {...other("a selector that isn't a read of one slice's state"), reason: "SELECTOR", selector, anySelector: args.length === 1 && !call.typeParameters};
|
|
524
|
+
return {...other("a selector that isn't a read of one slice's state", selectorAction(args.length, Boolean(call.typeParameters))), reason: "SELECTOR", selector, anySelector: args.length === 1 && !call.typeParameters};
|
|
400
525
|
}
|
|
401
526
|
if (binding.name === "useDispatch") {
|
|
402
527
|
const variable = parentTree(cursorOf(cursor, call));
|
|
403
528
|
return variable?.kind === J.Kind.NamedVariable && dispatchers.has(((variable as J.VariableDeclarations.NamedVariable).name as J.Identifier).simpleName)
|
|
404
|
-
? {local, kind: "dispatcher"} : other("`useDispatch` not held in a variable of its own"
|
|
529
|
+
? {local, kind: "dispatcher"} : other("`useDispatch` not held in a variable of its own",
|
|
530
|
+
"Hold the dispatcher in a `const` of its own, such as `const dispatch = useDispatch()`, and dispatch through it.");
|
|
405
531
|
}
|
|
406
|
-
return other(`\`${binding.name}\` from react-redux
|
|
532
|
+
return other(`\`${binding.name}\` from react-redux`, REACT_REDUX_CALL_ACTIONS[binding.name] ??
|
|
533
|
+
`The recipe converts only \`connect\`, \`useSelector\`, \`useDispatch\` and \`<Provider>\` of react-redux: replace \`${binding.name}(...)\` by hand.`);
|
|
407
534
|
}
|
|
408
535
|
// A relative import: an action creator, the store, a slice or anything else of the repository
|
|
409
536
|
if (parent?.kind === J.Kind.MethodInvocation && (parent as J.MethodInvocation).name === identifier) {
|
|
@@ -426,7 +553,10 @@ function classify(identifier: J.Identifier, binding: ImportBinding, cursor: Curs
|
|
|
426
553
|
if (member === "dispatch" && dispatchedAction(call)) {
|
|
427
554
|
return isStatement(call, outer) ? {local, kind: "storeDispatch", member} : {...other("the dispatch's result is used"), reason: "DISPATCH", member};
|
|
428
555
|
}
|
|
429
|
-
|
|
556
|
+
const action = member === "getState" ? `Read one slice's state in each call, such as \`${local}.getState().counter\`, rather than the whole state, or convert by hand.` :
|
|
557
|
+
member === "dispatch" ? unimportedDispatchAction(call, undefined, `${local}.dispatch`) :
|
|
558
|
+
member === "subscribe" ? `\`${local}.subscribe\` converts only where the store moves whole: remove what the store's other rows name, or convert by hand.` : undefined;
|
|
559
|
+
return {...other(`\`${local}.${member}\``, action), member};
|
|
430
560
|
}
|
|
431
561
|
if (parent?.kind === JS.Kind.JsxEmbeddedExpression) {
|
|
432
562
|
const attribute = trees[2];
|
|
@@ -438,27 +568,46 @@ function classify(identifier: J.Identifier, binding: ImportBinding, cursor: Curs
|
|
|
438
568
|
if (provider?.module === REACT_REDUX && provider.name === "Provider") return {local, kind: "provider"};
|
|
439
569
|
}
|
|
440
570
|
}
|
|
441
|
-
|
|
571
|
+
// NOTE: a `store` attribute of a tag other than react-redux's `Provider` says nothing of where to render it
|
|
572
|
+
const valueUse = storeValueUse(identifier, cursor, persistStore);
|
|
573
|
+
return {...other("referenced"), valueUse: valueUse.kind === "provider" ? {kind: "other"} : valueUse};
|
|
442
574
|
}
|
|
443
575
|
|
|
444
576
|
/** Why a call of `connect` can't move to the zustand store's own `connect`, if it can't. */
|
|
445
|
-
function connectUse(call: J.MethodInvocation, other: (detail: string) => Use): Use | undefined {
|
|
577
|
+
function connectUse(call: J.MethodInvocation, other: (detail: string, action?: string) => Use): Use | undefined {
|
|
446
578
|
const args = argumentsOf(call);
|
|
447
579
|
// NOTE: a `null` read back from a stored LST doesn't keep `value: null`, so its source decides
|
|
448
580
|
const absent = (node: J | undefined) => !node ||
|
|
449
581
|
node.kind === J.Kind.Literal && (node as J.Literal).valueSource === "null" ||
|
|
450
582
|
isIdentifier(node) && node.simpleName === "undefined";
|
|
451
|
-
if (args.length > 4 || !absent(args[2])) return other("`connect` with `mergeProps`");
|
|
583
|
+
if (args.length > 4 || !absent(args[2])) return other("`connect` with `mergeProps`", "Merge the props in the wrapped component rather than with `mergeProps`, or convert by hand.");
|
|
452
584
|
if (args.length === 4 && !absent(args[3])) {
|
|
453
585
|
const options = objectProperties(args[3]);
|
|
454
586
|
const forwardRef = options?.get("forwardRef");
|
|
455
587
|
if (!options || options.size !== 1 || forwardRef?.kind !== J.Kind.Literal || typeof (forwardRef as J.Literal).value !== "boolean") {
|
|
456
|
-
return other("`connect` with options other than `forwardRef`"
|
|
588
|
+
return other("`connect` with options other than `forwardRef`",
|
|
589
|
+
"Pass `connect` no options, or only `{forwardRef: true}` or `{forwardRef: false}`; others, such as `context`, `areStatesEqual` or `pure`, are converted by hand.");
|
|
457
590
|
}
|
|
458
591
|
}
|
|
459
592
|
return undefined;
|
|
460
593
|
}
|
|
461
594
|
|
|
595
|
+
/** What to do with `dispatch(x)` whose `x` isn't a call of an imported action creator, by what `x` is. */
|
|
596
|
+
function unimportedDispatchAction(dispatch: J.MethodInvocation, action: J.MethodInvocation | undefined, name = dispatch.name.simpleName): string {
|
|
597
|
+
const [arg] = argumentsOf(dispatch);
|
|
598
|
+
if (action) {
|
|
599
|
+
return `\`${action.name.simpleName}\` isn't imported: import the action creator from the slice's source that declares it, and dispatch \`${name}(${action.name.simpleName}(...))\`, or convert by hand.`;
|
|
600
|
+
}
|
|
601
|
+
if (arg?.kind === J.Kind.MethodInvocation && (arg as J.MethodInvocation).select) {
|
|
602
|
+
const member = (arg as J.MethodInvocation).name.simpleName;
|
|
603
|
+
return `Import \`${member}\` by name from the slice's source, as \`export const {${member}} = slice.actions\` exports it, and dispatch \`${name}(${member}(...))\` rather than a member call such as \`actions.${member}(...)\`, or convert by hand.`;
|
|
604
|
+
}
|
|
605
|
+
if (objectProperties(arg)) {
|
|
606
|
+
return `An action object such as \`${name}({type: ...})\` has no zustand equivalent: dispatch a call of a slice's action creator for the same type, \`${name}(x(...))\`, or convert by hand.`;
|
|
607
|
+
}
|
|
608
|
+
return `Dispatch a call of an imported action creator in place, \`${name}(x(...))\`, rather than an action held in a variable or chosen by a condition, or convert by hand.`;
|
|
609
|
+
}
|
|
610
|
+
|
|
462
611
|
/** A use of a dispatcher from `useDispatch`: only `dispatch(x(...))` of an imported `x`, whose result is unused, converts. */
|
|
463
612
|
function dispatcherUse(identifier: J.Identifier, cursor: Cursor, followed: ReadonlyMap<string, ImportBinding>,
|
|
464
613
|
bindings: readonly ImportBinding[]): Use | undefined {
|
|
@@ -473,21 +622,28 @@ function dispatcherUse(identifier: J.Identifier, cursor: Cursor, followed: Reado
|
|
|
473
622
|
const action = dispatchedAction(call);
|
|
474
623
|
const binding = action && followed.get(action.name.simpleName);
|
|
475
624
|
if (binding && binding.module !== REACT_REDUX) return undefined;
|
|
476
|
-
return {local, kind: "other", detail: "dispatches what isn't an imported action creator", reason: "DISPATCH"};
|
|
625
|
+
return {local, kind: "other", detail: "dispatches what isn't an imported action creator", reason: "DISPATCH", action: unimportedDispatchAction(call, action)};
|
|
477
626
|
}
|
|
478
627
|
return {local, kind: "other", detail: "the dispatcher is used other than to dispatch", reason: "DISPATCH"};
|
|
479
628
|
}
|
|
480
629
|
|
|
630
|
+
const PROVIDER_OTHER_STORE = "`Provider` gets something other than the store this package creates: pass it that store, under the name its source exports, or convert by hand.";
|
|
631
|
+
|
|
632
|
+
const PROVIDER_NOT_IMPORTED = "`Provider` gets a store that isn't imported, such as one made in this source, passed in, or held in a ref or `this`. " +
|
|
633
|
+
"A zustand store is one per module: pass `Provider` the store imported from the module that creates it at its top level, or convert by hand.";
|
|
634
|
+
|
|
481
635
|
/** `<Provider store={store}>` with one element child and no other prop. */
|
|
482
636
|
function providerUse(tag: JSX.Tag, followed: ReadonlyMap<string, ImportBinding>, local: string): Use {
|
|
483
|
-
const other = (detail: string): Use => ({local, kind: "other", detail});
|
|
637
|
+
const other = (detail: string, action?: string): Use => ({local, kind: "other", detail, ...action ? {action} : {}});
|
|
484
638
|
const attributes = tag.attributes.map(a => a.element);
|
|
485
|
-
if (attributes.length !== 1 || attributes[0].kind !== JS.Kind.JsxAttribute)
|
|
639
|
+
if (attributes.length !== 1 || attributes[0].kind !== JS.Kind.JsxAttribute) {
|
|
640
|
+
return other("`Provider` with props other than `store`", "`Provider`'s props other than `store`, such as `context` or `serverState`, have no zustand equivalent: convert by hand.");
|
|
641
|
+
}
|
|
486
642
|
const value = (attributes[0] as JSX.Attribute).value?.element;
|
|
487
643
|
const expression = value?.kind === JS.Kind.JsxEmbeddedExpression ? (value as JSX.EmbeddedExpression).expression.element : undefined;
|
|
488
|
-
if (!isIdentifier(expression) || !followed.has(expression.simpleName)) return other("`Provider` of a store that isn't imported");
|
|
644
|
+
if (!isIdentifier(expression) || !followed.has(expression.simpleName)) return other("`Provider` of a store that isn't imported", PROVIDER_NOT_IMPORTED);
|
|
489
645
|
if (!providerChild(tag) && !providerChildren(tag)) return other("`Provider` without children");
|
|
490
|
-
return {local, kind: "providerTag"};
|
|
646
|
+
return {local, kind: "providerTag", via: expression.simpleName};
|
|
491
647
|
}
|
|
492
648
|
|
|
493
649
|
/** Whether a tag has children other than whitespace, which a fragment keeps when the tag goes. */
|
|
@@ -516,10 +672,13 @@ export const STORE_MEMBERS: ReadonlySet<string> = new Set(["getState", "dispatch
|
|
|
516
672
|
* How the statements of `cu` other than the store's declaration, `call`, and `export default name` refer to the store's
|
|
517
673
|
* variable: the members of `STORE_MEMBERS` they call on it, and whether they refer to it any other way.
|
|
518
674
|
*/
|
|
519
|
-
async function usesInSource(cu: JS.CompilationUnit, name: string, call: J.MethodInvocation): Promise<{other: boolean, called: Set<string>}> {
|
|
520
|
-
const result = {other: false, called: new Set<string>()};
|
|
675
|
+
async function usesInSource(cu: JS.CompilationUnit, name: string, call: J.MethodInvocation): Promise<{other: boolean, uses: StoreValueUse[], called: Set<string>}> {
|
|
676
|
+
const result = {other: false, uses: [] as StoreValueUse[], called: new Set<string>()};
|
|
677
|
+
const persistStore = importedAs(cu, "redux-persist", "persistStore");
|
|
521
678
|
const statements = cu.statements.map(s => s.element).flatMap(statement => {
|
|
522
679
|
if (statement.kind === JS.Kind.Import) return [];
|
|
680
|
+
// Exporting the store by name, which `exportedStore` reads, is no use of it
|
|
681
|
+
if (exportList(statement).some(entry => entry.local === name)) return [];
|
|
523
682
|
if (statement.kind === JS.Kind.ExportAssignment && isIdentifier((statement as JS.ExportAssignment).expression.element) &&
|
|
524
683
|
((statement as JS.ExportAssignment).expression.element as J.Identifier).simpleName === name) return [];
|
|
525
684
|
if (statement.kind === J.Kind.VariableDeclarations && (statement as J.VariableDeclarations).variables.some(v => v.element.initializer?.element === call)) {
|
|
@@ -545,6 +704,7 @@ async function usesInSource(cu: JS.CompilationUnit, name: string, call: J.Method
|
|
|
545
704
|
if (parent?.kind === JS.Kind.PropertyAssignment && (parent as JS.PropertyAssignment).name.element === identifier &&
|
|
546
705
|
(parent as JS.PropertyAssignment).initializer) return identifier;
|
|
547
706
|
result.other = true;
|
|
707
|
+
result.uses.push(storeValueUse(identifier, this.cursor, persistStore));
|
|
548
708
|
return identifier;
|
|
549
709
|
}
|
|
550
710
|
}().visit(statement, new ExecutionContext());
|
|
@@ -582,7 +742,25 @@ function exportedStore(cu: JS.CompilationUnit, cursor: Cursor): string | undefin
|
|
|
582
742
|
if (declarations.modifiers.some(m => m.keyword === "export")) return name;
|
|
583
743
|
const exported = cu.statements.map(s => s.element).find(s => s.kind === JS.Kind.ExportAssignment &&
|
|
584
744
|
isIdentifier((s as JS.ExportAssignment).expression.element) && ((s as JS.ExportAssignment).expression.element as J.Identifier).simpleName === name);
|
|
585
|
-
|
|
745
|
+
if (exported) return "default";
|
|
746
|
+
// `export {store}`, or `export {store as appStore}`, once
|
|
747
|
+
const listed = cu.statements.flatMap(s => exportList(s.element)).filter(entry => entry.local === name);
|
|
748
|
+
return listed.length === 1 ? listed[0].exported : undefined;
|
|
749
|
+
}
|
|
750
|
+
|
|
751
|
+
/** The local and exported names of `export {a, b as c}`, with no module specifier and no `type`. */
|
|
752
|
+
function exportList(statement: J): {local: string, exported: string}[] {
|
|
753
|
+
if (statement.kind !== JS.Kind.ExportDeclaration) return [];
|
|
754
|
+
const declaration = statement as JS.ExportDeclaration;
|
|
755
|
+
if (declaration.moduleSpecifier || declaration.typeOnly.element || declaration.exportClause?.kind !== JS.Kind.NamedExports) return [];
|
|
756
|
+
return (declaration.exportClause as JS.NamedExports).elements.elements.flatMap(({element}) => {
|
|
757
|
+
if (element.kind !== JS.Kind.ExportSpecifier || (element as JS.ExportSpecifier).typeOnly.element) return [];
|
|
758
|
+
const specifier = (element as JS.ExportSpecifier).specifier;
|
|
759
|
+
if (isIdentifier(specifier)) return [{local: specifier.simpleName, exported: specifier.simpleName}];
|
|
760
|
+
if (specifier.kind !== JS.Kind.Alias) return [];
|
|
761
|
+
const local = (specifier as JS.Alias).propertyName.element, alias = (specifier as JS.Alias).alias;
|
|
762
|
+
return isIdentifier(local) && isIdentifier(alias) ? [{local: local.simpleName, exported: alias.simpleName}] : [];
|
|
763
|
+
});
|
|
586
764
|
}
|
|
587
765
|
|
|
588
766
|
/** A slice of the store, with what the conversion writes for it. */
|
|
@@ -639,20 +817,23 @@ export const hookName = (key: string) => `use${key.charAt(0).toUpperCase()}${key
|
|
|
639
817
|
export function plan(facts: ReadonlyMap<string, SourceFacts>, sliceSources: ReadonlyMap<string, SliceSource>,
|
|
640
818
|
dependencies: (storePath: string, immer?: boolean) => Finding | undefined, scope: (path: string) => boolean = () => true,
|
|
641
819
|
wholeStores = true, renderer: (storePath: string) => string | undefined = () => "react-dom",
|
|
642
|
-
selectsOnEachRead: (storePath: string) => boolean = () => false
|
|
820
|
+
selectsOnEachRead: (storePath: string) => boolean = () => false,
|
|
821
|
+
resolveFrom: Resolve = (from, specifier) => resolveRelative(from, specifier, facts)): Plan {
|
|
643
822
|
const stores = [...facts.values()].filter(source => scope(source.path)).flatMap(source => source.stores);
|
|
644
823
|
const result: Plan = {slices: new Map(), sources: new Set(), blocked: new Map()};
|
|
645
824
|
if (stores.length === 0) return result;
|
|
646
825
|
if (stores.length > 1) {
|
|
647
|
-
for (const store of stores) result.blocked.set(store, [...store.findings, {reason: "MULTIPLE_STORES", detail: `${stores.length} stores
|
|
826
|
+
for (const store of stores) result.blocked.set(store, [...store.findings, {reason: "MULTIPLE_STORES", detail: `${stores.length} stores`, action: multipleStoresAction(store, stores, facts)}]);
|
|
648
827
|
return result;
|
|
649
828
|
}
|
|
650
829
|
const store = stores[0];
|
|
651
830
|
const findings: Finding[] = [...store.findings];
|
|
652
|
-
const block = (reason: string, detail: string) => findings.push({reason, detail});
|
|
831
|
+
const block = (reason: string, detail: string, action?: string) => findings.push({reason, detail, ...action ? {action} : {}});
|
|
653
832
|
// Slices that stay in Redux for a reason of their own, already recorded: their uses add nothing to it
|
|
654
833
|
const held = new Map<string, string>();
|
|
655
834
|
const handled = new Map<string, string>();
|
|
835
|
+
// The slices are read only for a store with no findings of its own, so a use checked against them is unchecked otherwise
|
|
836
|
+
const slicesRead = findings.length === 0;
|
|
656
837
|
if (findings.length === 0) {
|
|
657
838
|
if (!store.exportedAs) block("STORE_NOT_MODULE_LEVEL", store.store);
|
|
658
839
|
const storeSource = sliceSources.get(store.path);
|
|
@@ -696,9 +877,10 @@ export function plan(facts: ReadonlyMap<string, SourceFacts>, sliceSources: Read
|
|
|
696
877
|
}
|
|
697
878
|
}
|
|
698
879
|
const slicePaths = new Map([...result.slices.values()].map(slice => [slice.path, slice]));
|
|
880
|
+
const storeModule = facts.get(store.path);
|
|
699
881
|
const sources = new Set<string>([store.path, ...slicePaths.keys()]);
|
|
700
882
|
for (const source of facts.values()) {
|
|
701
|
-
const resolve = (module: string) =>
|
|
883
|
+
const resolve = (module: string) => resolveFrom(source.path, module);
|
|
702
884
|
const touches = (module: string) => {
|
|
703
885
|
const path = resolve(module);
|
|
704
886
|
return path === store.path || (path !== undefined && slicePaths.has(path));
|
|
@@ -738,29 +920,44 @@ export function plan(facts: ReadonlyMap<string, SourceFacts>, sliceSources: Read
|
|
|
738
920
|
if (source.path === store.path && slice && use.kind === "other") continue;
|
|
739
921
|
changes = true;
|
|
740
922
|
const where = `${use.local} in ${source.path}`;
|
|
923
|
+
if (use.kind === "providerTag" && !provides(source, use, store, resolve)) block("REACT_REDUX_USE", `${where}: \`Provider\` of \`${use.via}\`, which isn't the store`, PROVIDER_OTHER_STORE);
|
|
741
924
|
if (use.kind === "other") {
|
|
742
|
-
|
|
925
|
+
// A store that moves whole runs any selector of one argument as it is, and dispatches any action as Redux does
|
|
926
|
+
const whole = !wholeStores ? "" : use.anySelector ? " Or remove what the store's other rows name, so that it moves whole and runs the selector as it is." :
|
|
927
|
+
use.reason === "DISPATCH" ? " Or remove what the store's other rows name, so that it moves whole and dispatches it as Redux does." : "";
|
|
928
|
+
const reason = use.reason ?? (isStore ? "STORE_USE" : slice ? "SLICE_USE" : !binding ? "DISPATCH" : isReactRedux ? "REACT_REDUX_USE" : "STORE_USE");
|
|
929
|
+
// A store that moves whole calls its own `getState` and `dispatch` as they are
|
|
930
|
+
const member = isStore && (use.member === "getState" || use.member === "dispatch") && wholeStores ?
|
|
931
|
+
` Or remove what the store's other rows name, so that it moves whole and calls \`${use.member}\` as Redux does.` : "";
|
|
932
|
+
const action = binding && path === store.path && !isStore && reason === "STORE_USE" ? storeExportAction(binding, source, store, storeModule, wholeStores) :
|
|
933
|
+
isStore && use.valueUse ? storeValueAction(use.local, [use.valueUse]) : use.action && use.action + whole + member;
|
|
934
|
+
block(reason, `${where}: ${use.detail}`, action);
|
|
743
935
|
} else if (use.kind === "selector" || use.kind === "getState") {
|
|
744
|
-
if (!result.slices.has(use.key!))
|
|
936
|
+
if (!result.slices.has(use.key!)) {
|
|
937
|
+
block("SELECTOR", `${where} reads \`${use.key}\`, which is no slice of the store`,
|
|
938
|
+
!slicesRead ? UNREAD_SLICES : `\`${use.key}\` isn't a key of the store's \`reducer\`, whose slices are ${[...result.slices.keys()].map(k => `\`${k}\``).join(", ") || "none that convert"}: read one of those, or, where \`${use.key}\` is a reducer of the store that isn't a slice, rewrite it with \`createSlice\`.`);
|
|
939
|
+
}
|
|
745
940
|
} else if (use.kind === "dispatched") {
|
|
746
941
|
// An import without a reducer is recorded once, as UNKNOWN_ACTION
|
|
747
|
-
if (!slice || !binding || !slice.actions.has(binding.name) && !slice.unknown.has(binding.name) && !slice.thunkVariables.has(binding.name))
|
|
942
|
+
if (!slice || !binding || !slice.actions.has(binding.name) && !slice.unknown.has(binding.name) && !slice.thunkVariables.has(binding.name)) {
|
|
943
|
+
block("DISPATCH", `${where} dispatches what isn't an action creator of a slice`, slicesRead ? binding && sliceDispatchAction(binding, slice !== undefined) : UNREAD_SLICES);
|
|
944
|
+
}
|
|
748
945
|
const via = source.bindings.find(b => b.local === use.via);
|
|
749
946
|
if (via && !(resolve(via.module) === store.path && via.name === store.exportedAs)) block("DISPATCH", `${where} is dispatched through \`${use.via}\``);
|
|
750
947
|
} else if (use.kind === "connect") {
|
|
751
|
-
block("REACT_REDUX_USE", `${where}: \`connect\` from react-redux
|
|
948
|
+
block("REACT_REDUX_USE", `${where}: \`connect\` from react-redux`, connectAction(wholeStores));
|
|
752
949
|
} else if ((use.kind === "provider" || use.kind === "storeDispatch") && !isStore) {
|
|
753
950
|
block("STORE_USE", `${where} isn't the store`);
|
|
754
951
|
}
|
|
755
952
|
// An `other` use is recorded above with what it was
|
|
756
953
|
if (slice && use.kind !== "dispatched" && use.kind !== "other") block("SLICE_USE", `${where}: ${use.kind}`);
|
|
757
|
-
if (path === store.path && !isStore && source.path !== store.path) block("STORE_USE", `${where} imports more of the store's source
|
|
954
|
+
if (path === store.path && !isStore && source.path !== store.path) block("STORE_USE", `${where} imports more of the store's source`, binding && storeExportAction(binding, source, store, storeModule, wholeStores));
|
|
758
955
|
}
|
|
759
956
|
// The store's source is removed, so each import of it goes too, even one nothing uses
|
|
760
957
|
for (const binding of source.bindings) {
|
|
761
958
|
if (source.path === store.path || binding.module === REACT_REDUX || resolve(binding.module) !== store.path) continue;
|
|
762
959
|
changes = true;
|
|
763
|
-
if (binding.name !== store.exportedAs) block("STORE_USE", `${binding.local} in ${source.path} imports more of the store's source
|
|
960
|
+
if (binding.name !== store.exportedAs) block("STORE_USE", `${binding.local} in ${source.path} imports more of the store's source`, storeExportAction(binding, source, store, storeModule, wholeStores));
|
|
764
961
|
}
|
|
765
962
|
if (changes && source.path !== store.path) {
|
|
766
963
|
if (source.typescript) block("TYPESCRIPT", source.path);
|
|
@@ -774,7 +971,7 @@ export function plan(facts: ReadonlyMap<string, SourceFacts>, sliceSources: Read
|
|
|
774
971
|
}
|
|
775
972
|
if ([store.path, ...slicePaths.keys()].some(path => /\.(?:ts|tsx|mts|cts)$/.test(path))) block("TYPESCRIPT", store.path);
|
|
776
973
|
// A store whose slices can't move one by one may still move whole, its reducer running on one zustand store
|
|
777
|
-
if (findings.length > 0 && store.reducerStore && wholeStores) return reducerPlan(facts, sliceSources, store, dependencies, scope, renderer, selectsOnEachRead);
|
|
974
|
+
if (findings.length > 0 && store.reducerStore && wholeStores) return reducerPlan(facts, sliceSources, store, dependencies, scope, renderer, selectsOnEachRead, resolveFrom);
|
|
778
975
|
if (findings.length > 0) {
|
|
779
976
|
result.blocked.set(store, dedupe(findings));
|
|
780
977
|
result.slices.clear();
|
|
@@ -792,16 +989,17 @@ export function plan(facts: ReadonlyMap<string, SourceFacts>, sliceSources: Read
|
|
|
792
989
|
*/
|
|
793
990
|
function reducerPlan(facts: ReadonlyMap<string, SourceFacts>, sliceSources: ReadonlyMap<string, SliceSource>, store: StoreFacts,
|
|
794
991
|
dependencies: (storePath: string, immer?: boolean) => Finding | undefined, scope: (path: string) => boolean,
|
|
795
|
-
renderer: (storePath: string) => string | undefined, selectsOnEachRead: (storePath: string) => boolean): Plan {
|
|
992
|
+
renderer: (storePath: string) => string | undefined, selectsOnEachRead: (storePath: string) => boolean, resolveFrom: Resolve): Plan {
|
|
796
993
|
const findings: Finding[] = [];
|
|
797
|
-
const block = (reason: string, detail: string) => findings.push({reason, detail});
|
|
994
|
+
const block = (reason: string, detail: string, action?: string) => findings.push({reason, detail, ...action ? {action} : {}});
|
|
798
995
|
const hook = REDUCER_STORE_HOOK;
|
|
799
996
|
// A legacy `createStore` source has no slice source, so its own declarations and imports count too
|
|
800
997
|
const source = facts.get(store.path);
|
|
801
998
|
const names = new Set<string>([...sliceSources.get(store.path)?.names ?? [], ...source?.declared ?? [], ...source?.bindings.map(b => b.local) ?? []]);
|
|
802
999
|
const reducer = ["rootReducer", "appReducer", "storeReducer"].find(name => !names.has(name)) ?? "rootReducer";
|
|
803
1000
|
if (!store.exportedAs) block("STORE_NOT_MODULE_LEVEL", store.store);
|
|
804
|
-
if (store.usedInSource) block("STORE_USE", `\`${store.store}\` is used in its own source
|
|
1001
|
+
if (store.usedInSource) block("STORE_USE", `\`${store.store}\` is used in its own source`, storeValueAction(store.store, store.usesInSource ?? []) ??
|
|
1002
|
+
`Move what uses \`${store.store}\` in its own source, other than calls of its \`getState\`, \`dispatch\` and \`subscribe\`, to a module that imports the store, or convert by hand.`);
|
|
805
1003
|
const dependency = dependencies(store.path, false);
|
|
806
1004
|
if (dependency) findings.push(dependency);
|
|
807
1005
|
for (const name of [hook, reducer, "create"]) if (names.has(name)) block("NAME_TAKEN", `${name} in ${store.path}`);
|
|
@@ -815,7 +1013,7 @@ function reducerPlan(facts: ReadonlyMap<string, SourceFacts>, sliceSources: Read
|
|
|
815
1013
|
// Redux calls a store's listeners after every dispatch, which the store's `dispatch` does for its own `subscribe`
|
|
816
1014
|
let subscribes = store.calledInSource?.has("subscribe") ?? false;
|
|
817
1015
|
for (const source of facts.values()) {
|
|
818
|
-
const resolve = (module: string) =>
|
|
1016
|
+
const resolve = (module: string) => resolveFrom(source.path, module);
|
|
819
1017
|
if (!scope(source.path)) {
|
|
820
1018
|
for (const binding of source.bindings) {
|
|
821
1019
|
if (binding.module !== REACT_REDUX && resolve(binding.module) === store.path) block("STORE_USE", `${binding.local} in ${source.path}, of another package`);
|
|
@@ -837,13 +1035,14 @@ function reducerPlan(facts: ReadonlyMap<string, SourceFacts>, sliceSources: Read
|
|
|
837
1035
|
if (binding.module === REACT_REDUX) {
|
|
838
1036
|
changes = true;
|
|
839
1037
|
if (use.kind === "connect") connects = true;
|
|
840
|
-
if (use.kind === "
|
|
1038
|
+
if (use.kind === "providerTag" && !provides(source, use, store, resolve)) block("REACT_REDUX_USE", `${where}: \`Provider\` of \`${use.via}\`, which isn't the store`, PROVIDER_OTHER_STORE);
|
|
1039
|
+
if (use.kind === "other" && use.selector && readSelector(source, use.selector, facts, resolveFrom)) continue;
|
|
841
1040
|
// Any other selector runs as it is on the whole store, through a hook that keeps its selection where the store's own doesn't
|
|
842
1041
|
if (use.kind === "other" && use.anySelector) {
|
|
843
1042
|
if (selectsOnEachRead(store.path)) selects = true;
|
|
844
1043
|
continue;
|
|
845
1044
|
}
|
|
846
|
-
if (use.kind === "other") block(use.reason ?? "REACT_REDUX_USE", `${where}: ${use.detail}
|
|
1045
|
+
if (use.kind === "other") block(use.reason ?? "REACT_REDUX_USE", `${where}: ${use.detail}`, use.action);
|
|
847
1046
|
continue;
|
|
848
1047
|
}
|
|
849
1048
|
const path = resolve(binding.module);
|
|
@@ -851,7 +1050,8 @@ function reducerPlan(facts: ReadonlyMap<string, SourceFacts>, sliceSources: Read
|
|
|
851
1050
|
if (path !== store.path || binding.name !== store.exportedAs) continue;
|
|
852
1051
|
if (use.kind === "provider") changes = true;
|
|
853
1052
|
else if (use.kind === "other" && use.member === "subscribe") subscribes = true;
|
|
854
|
-
else if (use.kind === "other" && use.member !== "getState" && use.member !== "dispatch") block(use.reason === "DISPATCH" ? "STORE_USE" : use.reason ?? "STORE_USE", `${where}: ${use.detail}
|
|
1053
|
+
else if (use.kind === "other" && use.member !== "getState" && use.member !== "dispatch") block(use.reason === "DISPATCH" ? "STORE_USE" : use.reason ?? "STORE_USE", `${where}: ${use.detail}`,
|
|
1054
|
+
use.valueUse && storeValueAction(use.local, [use.valueUse]));
|
|
855
1055
|
}
|
|
856
1056
|
if (changes && source.path !== store.path) {
|
|
857
1057
|
if (source.typescript) block("TYPESCRIPT", source.path);
|
|
@@ -891,14 +1091,21 @@ function reducerPlan(facts: ReadonlyMap<string, SourceFacts>, sliceSources: Read
|
|
|
891
1091
|
...subscribes ? {listeners: STORE_LISTENERS} : {}}};
|
|
892
1092
|
}
|
|
893
1093
|
|
|
1094
|
+
/** Whether the `Provider` of a `providerTag` use provides `store`, as its source imports it. */
|
|
1095
|
+
function provides(source: SourceFacts, use: Use, store: StoreFacts, resolve: (module: string) => string | undefined): boolean {
|
|
1096
|
+
const binding = source.bindings.find(b => b.local === use.via);
|
|
1097
|
+
return !!binding && binding.module !== REACT_REDUX && resolve(binding.module) === store.path && binding.name === store.exportedAs;
|
|
1098
|
+
}
|
|
1099
|
+
|
|
894
1100
|
/**
|
|
895
1101
|
* Whether `name`, which `source` passes to `useSelector`, is a plain read of the state: a `const` of its own, or one it
|
|
896
1102
|
* imports by the name a followed module exports it as.
|
|
897
1103
|
*/
|
|
898
|
-
export function readSelector(source: SourceFacts, name: string, facts: ReadonlyMap<string, SourceFacts
|
|
1104
|
+
export function readSelector(source: SourceFacts, name: string, facts: ReadonlyMap<string, SourceFacts>,
|
|
1105
|
+
resolve: Resolve = (from, specifier) => resolveRelative(from, specifier, facts)): boolean {
|
|
899
1106
|
const binding = source.bindings.find(b => b.local === name);
|
|
900
1107
|
if (!binding) return source.readSelectors.local.has(name);
|
|
901
|
-
const path =
|
|
1108
|
+
const path = resolve(source.path, binding.module);
|
|
902
1109
|
return path !== undefined && (facts.get(path)?.readSelectors.exported.has(binding.name) ?? false);
|
|
903
1110
|
}
|
|
904
1111
|
|
|
@@ -909,10 +1116,10 @@ export function readSelector(source: SourceFacts, name: string, facts: ReadonlyM
|
|
|
909
1116
|
export function planPackages(facts: ReadonlyMap<string, SourceFacts>, sliceSources: ReadonlyMap<string, SliceSource>,
|
|
910
1117
|
packageOf: (path: string) => string | undefined, dependencies: (storePath: string, immer?: boolean) => Finding | undefined,
|
|
911
1118
|
wholeStores = true, renderer?: (storePath: string) => string | undefined,
|
|
912
|
-
selectsOnEachRead?: (storePath: string) => boolean): Plan[] {
|
|
1119
|
+
selectsOnEachRead?: (storePath: string) => boolean, resolve?: Resolve): Plan[] {
|
|
913
1120
|
const root = (path: string) => packageOf(path) ?? "";
|
|
914
1121
|
const roots = new Set([...facts.values()].filter(source => source.stores.length > 0).map(source => root(source.path)));
|
|
915
|
-
return [...roots].map(packageRoot => plan(facts, sliceSources, dependencies, path => root(path) === packageRoot, wholeStores, renderer, selectsOnEachRead));
|
|
1122
|
+
return [...roots].map(packageRoot => plan(facts, sliceSources, dependencies, path => root(path) === packageRoot, wholeStores, renderer, selectsOnEachRead, resolve));
|
|
916
1123
|
}
|
|
917
1124
|
|
|
918
1125
|
function dedupe(findings: Finding[]): Finding[] {
|