@openrewrite/recipes-react 0.21.0 → 0.23.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 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +27 -0
- package/dist/index.js.map +1 -1
- package/dist/migration/change-component-prop-value.d.ts.map +1 -1
- package/dist/migration/change-component-prop-value.js +14 -4
- package/dist/migration/change-component-prop-value.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/react19/use-context-hook.d.ts.map +1 -1
- package/dist/react19/use-context-hook.js +2 -0
- package/dist/react19/use-context-hook.js.map +1 -1
- package/dist/search/find-enzyme-usage.d.ts +4 -0
- package/dist/search/find-enzyme-usage.d.ts.map +1 -1
- package/dist/search/find-enzyme-usage.js +91 -7
- package/dist/search/find-enzyme-usage.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-render-blockers.d.ts +13 -4
- package/dist/testing/enzyme-render-blockers.d.ts.map +1 -1
- package/dist/testing/enzyme-render-blockers.js +162 -6
- package/dist/testing/enzyme-render-blockers.js.map +1 -1
- package/dist/testing/enzyme-to-react-testing-library.d.ts +9 -2
- package/dist/testing/enzyme-to-react-testing-library.d.ts.map +1 -1
- package/dist/testing/enzyme-to-react-testing-library.js +163 -16
- package/dist/testing/enzyme-to-react-testing-library.js.map +1 -1
- package/dist/testing/enzyme-usage.d.ts +7 -0
- package/dist/testing/enzyme-usage.d.ts.map +1 -1
- package/dist/testing/enzyme-usage.js +8 -1
- package/dist/testing/enzyme-usage.js.map +1 -1
- package/dist/testing/enzyme-wrapper-scope.d.ts +8 -5
- package/dist/testing/enzyme-wrapper-scope.d.ts.map +1 -1
- package/dist/testing/enzyme-wrapper-scope.js +16 -4
- package/dist/testing/enzyme-wrapper-scope.js.map +1 -1
- package/dist/zustand/redux-stores.d.ts +2 -0
- package/dist/zustand/redux-stores.d.ts.map +1 -1
- package/dist/zustand/redux-stores.js +120 -6
- package/dist/zustand/redux-stores.js.map +1 -1
- package/dist/zustand/redux-to-zustand.d.ts.map +1 -1
- package/dist/zustand/redux-to-zustand.js +10 -4
- package/dist/zustand/redux-to-zustand.js.map +1 -1
- package/dist/zustand/zustand-plan.d.ts +7 -0
- package/dist/zustand/zustand-plan.d.ts.map +1 -1
- package/dist/zustand/zustand-plan.js +141 -21
- package/dist/zustand/zustand-plan.js.map +1 -1
- package/package.json +2 -2
- package/src/index.ts +10 -0
- package/src/migration/change-component-prop-value.ts +19 -8
- 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/react19/use-context-hook.ts +2 -0
- package/src/search/find-enzyme-usage.ts +80 -7
- package/src/testing/enzyme-host-components.ts +110 -0
- package/src/testing/enzyme-render-blockers.ts +174 -9
- package/src/testing/enzyme-to-react-testing-library.ts +159 -25
- package/src/testing/enzyme-usage.ts +13 -0
- package/src/testing/enzyme-wrapper-scope.ts +27 -6
- package/src/zustand/redux-stores.ts +119 -5
- package/src/zustand/redux-to-zustand.ts +12 -5
- package/src/zustand/zustand-plan.ts +159 -18
|
@@ -35,6 +35,19 @@ export const RENDER_BLOCKERS = new Map<string, ReadonlyMap<string, string>>();
|
|
|
35
35
|
/** The key of {@link RENDER_BLOCKERS} for what keeps every render of a source on Enzyme. */
|
|
36
36
|
export const FILE_BLOCKER = "*";
|
|
37
37
|
|
|
38
|
+
/** What the converter read of a `find` selector written as other than a literal: the string it holds, or the part it can't read. */
|
|
39
|
+
export type SelectorRead = {value: string} | {unread: string};
|
|
40
|
+
|
|
41
|
+
/** Each {@link SelectorRead} of a source, by the `find` call, for the review row `FindEnzymeUsage` writes after the converter. */
|
|
42
|
+
export const SELECTOR_VALUES = new Map<string, ReadonlyMap<string, SelectorRead>>();
|
|
43
|
+
|
|
44
|
+
/** The suggested action for a `find` whose selector is a string the recipe can't read, built from `unread`. */
|
|
45
|
+
export function unreadSelector(unread: string): string {
|
|
46
|
+
return `The recipe can't read this selector as a string: \`${unread}\` isn't a \`const\` string, or a template of \`const\` strings, that the recipe finds in this test or in a module it imports by a relative path or \`baseUrl\`. ` +
|
|
47
|
+
"Where the selector is a tag, class, id or attribute selector, write `container.querySelectorAll` with the same expression by hand. " +
|
|
48
|
+
"Enzyme also counts components given that class, id or attribute, so compare the counts.";
|
|
49
|
+
}
|
|
50
|
+
|
|
38
51
|
export function enzymeModule(value: unknown): value is string {
|
|
39
52
|
return typeof value === "string" && (value === ENZYME || value.startsWith("enzyme/") ||
|
|
40
53
|
(value === "enzyme-to-json" || value.startsWith("enzyme-to-json/")) || /^(?:@[^/]+\/)?enzyme-adapter-/.test(value));
|
|
@@ -401,10 +401,19 @@ export function wrapperDeclaration<R>(statement: J | undefined, classify: (rende
|
|
|
401
401
|
* admits `expect(wrapper).toMatchSnapshot()` and `expect(toJson(wrapper)).toMatchSnapshot()`, whose
|
|
402
402
|
* snapshot RTL records as the DOM rather than Enzyme's tree. `hostNames` decides the selectors {@link selectorLiteral} accepts.
|
|
403
403
|
*/
|
|
404
|
+
/** A reference that keeps a wrapper on Enzyme, with the `toMatchSnapshot` call where it is a snapshot assertion the snapshot recipe converts. */
|
|
405
|
+
export interface BlockingReference {
|
|
406
|
+
cursor: Cursor;
|
|
407
|
+
unmounted: boolean;
|
|
408
|
+
snapshot?: J.MethodInvocation;
|
|
409
|
+
}
|
|
410
|
+
|
|
404
411
|
export function analyzeWrapperScope(block: Cursor, {declaration, name}: WrapperDeclaration,
|
|
405
|
-
{events, html, untaggedPositions, hostNames, snapshots = false, optionalChaining = true, constant = () => undefined, blocked}: {events: ReadonlySet<string>, html: boolean, untaggedPositions: boolean, hostNames: HostNames, snapshots?: boolean,
|
|
406
|
-
/**
|
|
407
|
-
|
|
412
|
+
{events, html, untaggedPositions, hostNames, snapshots = false, optInSnapshots = false, optionalChaining = true, constant = () => undefined, blocked}: {events: ReadonlySet<string>, html: boolean, untaggedPositions: boolean, hostNames: HostNames, snapshots?: boolean,
|
|
413
|
+
/** Whether the snapshot recipe would convert the snapshot assertions this analysis rejects. */
|
|
414
|
+
optInSnapshots?: boolean, optionalChaining?: boolean, constant?: (node: J) => string | undefined,
|
|
415
|
+
/** Told every reference that rejects the scope, whether it comes after an unmount, and the snapshot assertion it is the subject of. */
|
|
416
|
+
blocked?: (references: BlockingReference[]) => void}): WrapperScope | undefined {
|
|
408
417
|
let serializers: ReadonlySet<string> | undefined;
|
|
409
418
|
const uses = new Map<string, WrapperRead>();
|
|
410
419
|
const removedStatements = new Set<string>();
|
|
@@ -438,9 +447,14 @@ export function analyzeWrapperScope(block: Cursor, {declaration, name}: WrapperD
|
|
|
438
447
|
return undefined;
|
|
439
448
|
};
|
|
440
449
|
|
|
441
|
-
const
|
|
450
|
+
const ancestorsOf = (reference: Cursor): Cursor[] => {
|
|
442
451
|
const ancestors: Cursor[] = [];
|
|
443
452
|
for (let c: Cursor | undefined = reference.parentTree(); c && ancestors.length < 5; c = c.parentTree()) ancestors.push(c);
|
|
453
|
+
return ancestors;
|
|
454
|
+
};
|
|
455
|
+
|
|
456
|
+
const use = (identifier: J.Identifier, reference: Cursor): boolean => {
|
|
457
|
+
const ancestors = ancestorsOf(reference);
|
|
444
458
|
const first = ancestors[0]?.value as J | undefined;
|
|
445
459
|
if (snapshots) {
|
|
446
460
|
serializers ??= snapshotSerializers(block);
|
|
@@ -545,13 +559,20 @@ export function analyzeWrapperScope(block: Cursor, {declaration, name}: WrapperD
|
|
|
545
559
|
const declared = new Cursor(variable.name, variableCursor(block, declaration));
|
|
546
560
|
const references = referencesOf(declared, name, "value");
|
|
547
561
|
if (!references.some(reference => reference.identifier === variable.name)) return undefined;
|
|
548
|
-
const rejected:
|
|
562
|
+
const rejected: BlockingReference[] = [];
|
|
549
563
|
for (const reference of references) {
|
|
550
564
|
if (reference.identifier === variable.name) continue;
|
|
551
565
|
const after = unmounted;
|
|
552
566
|
if (reference.declares || after || !use(reference.identifier, reference.cursor)) {
|
|
553
567
|
if (!blocked) return undefined;
|
|
554
|
-
|
|
568
|
+
let snapshot: J.MethodInvocation | undefined;
|
|
569
|
+
if (optInSnapshots && !after && !reference.declares) {
|
|
570
|
+
serializers ??= snapshotSerializers(block);
|
|
571
|
+
const ancestors = ancestorsOf(reference.cursor);
|
|
572
|
+
const expectCall = snapshotAssertion(reference.identifier, ancestors, serializers);
|
|
573
|
+
snapshot = expectCall && ancestors.find(c => c.value.kind === J.Kind.MethodInvocation && (c.value as J.MethodInvocation).select?.element === expectCall)?.value;
|
|
574
|
+
}
|
|
575
|
+
rejected.push({cursor: reference.cursor, unmounted: after, snapshot});
|
|
555
576
|
}
|
|
556
577
|
}
|
|
557
578
|
if (rejected.length > 0) {
|
|
@@ -570,14 +570,20 @@ export function storeFindings(call: J.MethodInvocation, cu: JS.CompilationUnit,
|
|
|
570
570
|
const options = objectProperties(call.arguments.elements[0]?.element);
|
|
571
571
|
if (!options) return {api, findings: [{reason: "OPTIONS_NOT_LITERAL", detail: ""}], slices};
|
|
572
572
|
const findings: Finding[] = [];
|
|
573
|
-
for (const key of options
|
|
574
|
-
if (key
|
|
573
|
+
for (const [key, value] of options) {
|
|
574
|
+
if (key === "reducer") continue;
|
|
575
|
+
const action = optionAction(key, value, options.get("reducer"), cu);
|
|
576
|
+
findings.push({reason: BLOCKING_OPTIONS[key] ?? "UNKNOWN_OPTION", detail: key, ...action ? {action} : {}});
|
|
575
577
|
}
|
|
576
|
-
// A store of its `reducer` alone can also move to zustand whole, driven by that reducer
|
|
578
|
+
// A store of its `reducer` alone can also move to zustand whole, driven by that reducer, as can one whose other
|
|
579
|
+
// options only change what development builds check, which a plain `configureStore` checks and the move drops too
|
|
577
580
|
const reducer = options.get("reducer");
|
|
578
|
-
const reducerStore = options.
|
|
581
|
+
const reducerStore = !reducer || [...options].some(([key, value]) => key !== "reducer" && !developmentOnly(key, value)) ? undefined : reducer.kind === J.Kind.NewClass && !(reducer as J.NewClass).class ? "combined" : "reducer";
|
|
579
582
|
const reducers = objectProperties(options.get("reducer"));
|
|
580
|
-
if (!reducers)
|
|
583
|
+
if (!reducers) {
|
|
584
|
+
const action = rootReducerAction(reducer, reducerStore !== undefined, cu);
|
|
585
|
+
return {api, findings: [...findings, {reason: "ROOT_REDUCER", detail: "", ...action ? {action} : {}}], slices, reducerStore};
|
|
586
|
+
}
|
|
581
587
|
const local = localSlices(cu);
|
|
582
588
|
for (const [key, value] of reducers) {
|
|
583
589
|
const slice = resolveSliceReducer(value, cu, local, modules, resolve);
|
|
@@ -588,6 +594,114 @@ export function storeFindings(call: J.MethodInvocation, cu: JS.CompilationUnit,
|
|
|
588
594
|
return {api, findings, slices, reducerStore};
|
|
589
595
|
}
|
|
590
596
|
|
|
597
|
+
/** What to do about the `configureStore` option `key: value`, where it says more than the option's reason does. */
|
|
598
|
+
function optionAction(key: string, value: J, reducer: J | undefined, cu: JS.CompilationUnit): string | undefined {
|
|
599
|
+
if (developmentOnly(key, value)) return key === "devTools" ?
|
|
600
|
+
"`devTools` only connects the Redux DevTools extension, which a store that moves whole drops, as it drops a plain `configureStore`'s: remove what the store's other rows name, and it moves whole, where `wholeStores` is on." :
|
|
601
|
+
"`middleware` only sets the development checks of the default middleware, which a store that moves whole drops, as it drops a plain `configureStore`'s: remove what the store's other rows name, and it moves whole, where `wholeStores` is on.";
|
|
602
|
+
if (key !== "middleware") return undefined;
|
|
603
|
+
const actions: string[] = [];
|
|
604
|
+
// An RTK Query API adds its middleware and its reducer, the latter under `[api.reducerPath]`
|
|
605
|
+
const apis = new Set<string>();
|
|
606
|
+
walk(value, tree => {
|
|
607
|
+
const access = tree as J.FieldAccess;
|
|
608
|
+
if (tree.kind === J.Kind.FieldAccess && access.name.element.simpleName === "middleware" && isIdentifier(access.target)) apis.add(access.target.simpleName);
|
|
609
|
+
return true;
|
|
610
|
+
});
|
|
611
|
+
const reducedApis = new Set<string>();
|
|
612
|
+
if (reducer) walk(reducer, tree => {
|
|
613
|
+
const access = tree as J.FieldAccess;
|
|
614
|
+
if (tree.kind === J.Kind.FieldAccess && ["reducer", "reducerPath"].includes(access.name.element.simpleName) && isIdentifier(access.target) && apis.has(access.target.simpleName)) reducedApis.add(access.target.simpleName);
|
|
615
|
+
return true;
|
|
616
|
+
});
|
|
617
|
+
const made = madeBy(cu);
|
|
618
|
+
const read = namesReferencedWithin(value);
|
|
619
|
+
const by = (module: string, ...names: string[]) => {
|
|
620
|
+
const locals = importedAs(cu, module, ...names);
|
|
621
|
+
return [...read].some(name => locals.has(name) || locals.has(made.get(name)?.name.simpleName ?? ""));
|
|
622
|
+
};
|
|
623
|
+
if (reducedApis.size > 0) actions.push(`\`${[...reducedApis][0]}.middleware\` is RTK Query's, which has no zustand equivalent: keep this store in Redux, or move its queries to a data-fetching library, such as TanStack Query, by hand.`);
|
|
624
|
+
if (by("redux-saga", "default")) actions.push("redux-saga runs its sagas on the actions the store dispatches, which zustand has no middleware for: rewrite the sagas as functions that call the store, or keep this store in Redux.");
|
|
625
|
+
if ([...apis].some(api => importedAs(cu, RTK, "createListenerMiddleware").has(made.get(api)?.name.simpleName ?? ""))) actions.push("RTK's listener middleware runs its effects on the actions the store dispatches: rewrite them with the zustand store's `subscribe`, or keep this store in Redux.");
|
|
626
|
+
if (by("redux-logger", "default", "logger", "createLogger")) actions.push("redux-logger logs each action the store dispatches: remove it, or use zustand's `devtools` middleware to watch the store, and convert by hand.");
|
|
627
|
+
return actions.length > 0 ? actions.join(" ") : undefined;
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
/** The function each top-level `const` of `cu` is the result of calling, such as `createSagaMiddleware` for `const saga = createSagaMiddleware()`. */
|
|
631
|
+
function madeBy(cu: JS.CompilationUnit): Map<string, J.MethodInvocation> {
|
|
632
|
+
const made = new Map<string, J.MethodInvocation>();
|
|
633
|
+
for (const {element} of cu.statements) {
|
|
634
|
+
if (element.kind !== J.Kind.VariableDeclarations) continue;
|
|
635
|
+
for (const {element: variable} of (element as J.VariableDeclarations).variables) {
|
|
636
|
+
const initializer = variable.initializer?.element;
|
|
637
|
+
if (isIdentifier(variable.name) && initializer?.kind === J.Kind.MethodInvocation && !(initializer as J.MethodInvocation).select) {
|
|
638
|
+
made.set(variable.name.simpleName, initializer as J.MethodInvocation);
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
return made;
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
/**
|
|
646
|
+
* What to do about a `configureStore`'s `reducer` that isn't an object of reducers, where that is known: `whole` when
|
|
647
|
+
* nothing else in the call keeps the store from moving whole, driven by that reducer.
|
|
648
|
+
*/
|
|
649
|
+
function rootReducerAction(reducer: J | undefined, whole: boolean, cu: JS.CompilationUnit): string | undefined {
|
|
650
|
+
if (!reducer) return undefined;
|
|
651
|
+
const call = reducer.kind === J.Kind.MethodInvocation && !(reducer as J.MethodInvocation).select ? reducer as J.MethodInvocation :
|
|
652
|
+
isIdentifier(reducer) ? madeBy(cu).get(reducer.simpleName) : undefined;
|
|
653
|
+
const by = (module: string, name: string) => call !== undefined && importedAs(cu, module, name).has(call.name.simpleName);
|
|
654
|
+
if (by("redux-persist", "persistReducer")) return "`reducer` is redux-persist's `persistReducer`, and redux-persist persists a Redux store: convert the store by hand, with zustand's `persist` middleware in place of redux-persist.";
|
|
655
|
+
if (whole) return "A store of a single root reducer moves whole, as one zustand store that the reducer drives: remove what the store's other rows name, and it moves whole, where `wholeStores` is on.";
|
|
656
|
+
if ((by(REDUX, "combineReducers") || by(RTK, "combineReducers")) && objectProperties(call!.arguments.elements[0]?.element)) {
|
|
657
|
+
const name = isIdentifier(reducer) ? `\`${reducer.simpleName}\`` : "`reducer`";
|
|
658
|
+
return `${name} is \`combineReducers\` of an object of reducers: pass that object as \`reducer\` instead, which \`configureStore\` combines the same way, and the store's slices are read.`;
|
|
659
|
+
}
|
|
660
|
+
return undefined;
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
/** The checks of Redux Toolkit's default middleware, which it runs only in development. */
|
|
664
|
+
const DEVELOPMENT_CHECKS = new Set(["serializableCheck", "immutableCheck", "actionCreatorCheck"]);
|
|
665
|
+
|
|
666
|
+
/**
|
|
667
|
+
* Whether the `configureStore` option `key: value` changes only what a development build checks or shows: `devTools`
|
|
668
|
+
* of any value without effects, or `middleware` of `getDefaultMiddleware => getDefaultMiddleware({...})` that sets
|
|
669
|
+
* only its checks, so that the store's middleware is still the default thunk.
|
|
670
|
+
*/
|
|
671
|
+
export function developmentOnly(key: string, value: J): boolean {
|
|
672
|
+
if (key === "devTools") return withoutEffects(value);
|
|
673
|
+
if (key !== "middleware") return false;
|
|
674
|
+
const lambda = lambdaOf(value);
|
|
675
|
+
const parameters = lambda ? parametersOf(lambda) : [];
|
|
676
|
+
const name = parameters.length === 1 ? plainParameter(parameters[0]) : undefined;
|
|
677
|
+
if (!lambda || !name) return false;
|
|
678
|
+
let body: J | undefined = lambda.body;
|
|
679
|
+
if (body.kind === J.Kind.Block) {
|
|
680
|
+
const statements = (body as J.Block).statements;
|
|
681
|
+
body = statements.length === 1 && statements[0].element.kind === J.Kind.Return ? (statements[0].element as J.Return).expression : undefined;
|
|
682
|
+
}
|
|
683
|
+
if (body?.kind !== J.Kind.MethodInvocation) return false;
|
|
684
|
+
const call = body as J.MethodInvocation;
|
|
685
|
+
if (call.select || call.name.simpleName !== name) return false;
|
|
686
|
+
const args = call.arguments.elements.map(e => e.element).filter(e => e.kind !== J.Kind.Empty);
|
|
687
|
+
if (args.length === 0) return true;
|
|
688
|
+
const checks = args.length === 1 ? objectProperties(args[0]) : undefined;
|
|
689
|
+
// NOTE: `objectProperties` reads only `key: value` and shorthand properties, so a spread leaves it undefined
|
|
690
|
+
return checks !== undefined && [...checks].every(([check, setting]) => DEVELOPMENT_CHECKS.has(check) && withoutEffects(setting));
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
/** Whether evaluating `node` can do nothing but compute a value: it calls, constructs, assigns and awaits nothing. */
|
|
694
|
+
export function withoutEffects(node: J): boolean {
|
|
695
|
+
let effects = false;
|
|
696
|
+
walk(node, tree => {
|
|
697
|
+
// An object literal is a `NewClass` of no class
|
|
698
|
+
if (tree.kind === J.Kind.MethodInvocation || tree.kind === J.Kind.NewClass && (tree as J.NewClass).class || tree.kind === J.Kind.Assignment ||
|
|
699
|
+
tree.kind === J.Kind.AssignmentOperation || tree.kind === JS.Kind.Await || tree.kind === J.Kind.Lambda || tree.kind === JS.Kind.ArrowFunction) effects = true;
|
|
700
|
+
return !effects;
|
|
701
|
+
});
|
|
702
|
+
return !effects;
|
|
703
|
+
}
|
|
704
|
+
|
|
591
705
|
/** The variable or function a store is created in, such as `store` in `export const store = configureStore(...)`. */
|
|
592
706
|
export function storeName(cursor: Cursor): string {
|
|
593
707
|
for (let c: Cursor | undefined = cursor.parent; c; c = c.parent) {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {Cursor, DataTable, emptyMarkers, ExecutionContext, Markup, MarkersKind, Option, or, randomId, ScanningRecipe, SourceFile, Tree, TreePrinters, TreeVisitor} from "@openrewrite/rewrite";
|
|
2
2
|
import {
|
|
3
|
-
AddDependencyOptions, autoFormat, ChangeImport, findNodeResolutionResult, isVendoredOrBundled, JavaScriptVisitor, JS, JSX, maybeAddImport, maybeRemoveImport, raw, template
|
|
3
|
+
AddDependencyOptions, autoFormat, ChangeImport, findNodeResolutionResult, isVendoredOrBundled, JavaScriptVisitor, JS, JSX, maybeAddImport, maybeRemoveImport, namesReferencedWithin, raw, template
|
|
4
4
|
} from "@openrewrite/rewrite/javascript";
|
|
5
5
|
import {emptySpace, isIdentifier, J, Statement} from "@openrewrite/rewrite/java";
|
|
6
6
|
import {Json, JsonVisitor} from "@openrewrite/rewrite/json";
|
|
@@ -11,7 +11,7 @@ import {JavaDependencyRecipe, treesOfType} from "../util/java-recipe-proxy";
|
|
|
11
11
|
import {addPathsConfig, importBindings, isAliasSpecifier, PathsConfigs, Resolve, resolveModule} from "../util/module-imports";
|
|
12
12
|
import {rewriteVersion, supportsResolvingGate} from "../testing/enzyme-to-react-testing-library";
|
|
13
13
|
import {ENTRY_STORE_MODULE, EntrySplit, splitEntryStore} from "./entry-store";
|
|
14
|
-
import {caseReducers, Finding, importedAs, legacyEnhancer, localSlices, objectProperties, preloadedValue, REDUX_STORES_TABLE, ReduxStoreRow, REDUX, RTK, SliceModule, storeFunctions, SUGGESTED_ACTIONS, TEST_SOURCE} from "./redux-stores";
|
|
14
|
+
import {caseReducers, Finding, importedAs, legacyEnhancer, localSlices, objectProperties, preloadedValue, withoutEffects, REDUX_STORES_TABLE, ReduxStoreRow, REDUX, RTK, SliceModule, storeFunctions, SUGGESTED_ACTIONS, TEST_SOURCE} from "./redux-stores";
|
|
15
15
|
import {
|
|
16
16
|
actionsExport, argumentsOf, dispatchedAction, getStateRead, isDispatcherDeclaration, isReactDependencies, isReducerExport, parentTree, Plan, planPackages, PlannedSlice, providerChild, providerChildren,
|
|
17
17
|
REACT_REDUX, readSelector, relativeSpecifier, selectorRead, sliceSource, SliceSource, SourceFacts, sourceFacts, StoreFacts, storeFacts
|
|
@@ -480,7 +480,8 @@ export class ReduxToZustand extends ScanningRecipe<Scanned> {
|
|
|
480
480
|
if (react === undefined || react < 16 || react === 16 && (minVersion(facts.react!)?.minor ?? 0) < 8) return {reason: "REACT_VERSION", detail: facts.react ?? "react not declared"};
|
|
481
481
|
// A package the steps weren't asked to change keeps its store for another reason, recorded already
|
|
482
482
|
const refusal = !forecast ? this.forecast.refused?.get(root) : undefined;
|
|
483
|
-
if (refusal) return {reason: "DEPENDENCY", detail: `zustand isn't added: ${refusal.message}
|
|
483
|
+
if (refusal) return {reason: "DEPENDENCY", detail: `zustand isn't added: ${refusal.message}`,
|
|
484
|
+
action: `The recipe adds \`zustand\` and \`immer\` only where it can write the lockfile the package manager would, which the detail says it can't here: add \`zustand\` ${(react >= 18 ? ZUSTAND_5 : ZUSTAND_4).version} and \`immer\` ${IMMER_10.version} to the package yourself, with its own package manager, and run the recipe again.`};
|
|
484
485
|
const landed = (step: ZustandDependency | undefined) => forecast || !this.asked?.has(root) && this.asked !== undefined ? !!this.steps : !!step?.landed.has(root);
|
|
485
486
|
const zustand = facts.zustand !== undefined ? (major(facts.zustand) ?? 0) >= (react >= 18 ? 4 : 4) && (react >= 18 || major(facts.zustand) === 4)
|
|
486
487
|
: landed(react >= 18 ? this.steps?.zustand : this.steps?.zustand4);
|
|
@@ -919,8 +920,14 @@ function storeStatement(cu: JS.CompilationUnit, store: StoreFacts): {index: numb
|
|
|
919
920
|
// NOTE: the arguments are moved into new calls, so they drop the line breaks they had in the store's call
|
|
920
921
|
return {index, call, reducer: {...args[0], prefix: emptySpace}, preloaded: preloaded && {...preloaded, prefix: emptySpace}, consts: enhancer && !("unsupported" in enhancer) ? enhancer.consts : new Set()};
|
|
921
922
|
}
|
|
922
|
-
const
|
|
923
|
-
|
|
923
|
+
const options = objectProperties(argumentsOf(call)[0]);
|
|
924
|
+
const reducer = options?.get("reducer");
|
|
925
|
+
// The development-only options go with the call, and so may the `const`s only they read, unless making one has effects
|
|
926
|
+
const effectful = new Set(cu.statements.map(padded => padded.element).filter(s => s.kind === J.Kind.VariableDeclarations)
|
|
927
|
+
.flatMap(s => (s as J.VariableDeclarations).variables.map(v => v.element))
|
|
928
|
+
.filter(v => isIdentifier(v.name) && v.initializer && !withoutEffects(v.initializer.element)).map(v => (v.name as J.Identifier).simpleName));
|
|
929
|
+
const consts = new Set([...options ?? []].filter(([key]) => key !== "reducer").flatMap(([, value]) => [...namesReferencedWithin(value)]).filter(name => !effectful.has(name)));
|
|
930
|
+
return reducer ? {index, call, reducer, consts} : undefined;
|
|
924
931
|
}
|
|
925
932
|
return undefined;
|
|
926
933
|
}
|
|
@@ -3,7 +3,7 @@ import {JavaScriptVisitor, JS, JSX, namesReferencedWithin} from "@openrewrite/re
|
|
|
3
3
|
import {isIdentifier, J} from "@openrewrite/rewrite/java";
|
|
4
4
|
import {posix} from "path";
|
|
5
5
|
import {ImportBinding, importBindings, isAliasSpecifier, isRelativeSpecifier, Resolve, resolveRelative} from "../util/module-imports";
|
|
6
|
-
import {Finding, localSlices, ReducerStoreKind, objectProperties, RTK, SliceFacts, SliceModule, storeFindings, storeFunctions, TEST_SOURCE} from "./redux-stores";
|
|
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
|
|
|
@@ -47,6 +47,58 @@ export interface Use {
|
|
|
47
47
|
anySelector?: boolean;
|
|
48
48
|
/** What to do about an `other` use, for the review row, where it says more than its reason's action. */
|
|
49
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;
|
|
50
102
|
}
|
|
51
103
|
|
|
52
104
|
/** What to do with an import of react-redux used other than called, by its name. */
|
|
@@ -63,6 +115,23 @@ const REACT_REDUX_CALL_ACTIONS: Readonly<Record<string, string>> = {
|
|
|
63
115
|
useStore: "Read the state with `useSelector` and dispatch with `useDispatch` instead of `useStore()`, or convert by hand."
|
|
64
116
|
};
|
|
65
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
|
+
|
|
66
135
|
/** What to do with a `connect` of a store that can't move whole, which only a store moving whole declares. */
|
|
67
136
|
export function connectAction(wholeStores: boolean): string {
|
|
68
137
|
return wholeStores ?
|
|
@@ -70,6 +139,33 @@ export function connectAction(wholeStores: boolean): string {
|
|
|
70
139
|
"`connect` converts only where the store moves whole, which `wholeStores` turns off: run with it on, or replace `connect` with `useSelector` and `useDispatch`.";
|
|
71
140
|
}
|
|
72
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.`;
|
|
167
|
+
}
|
|
168
|
+
|
|
73
169
|
/** What one source tells the plan. */
|
|
74
170
|
export interface SourceFacts {
|
|
75
171
|
path: string;
|
|
@@ -105,6 +201,8 @@ export interface StoreFacts {
|
|
|
105
201
|
reducerStore?: ReducerStoreKind;
|
|
106
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`. */
|
|
107
203
|
usedInSource?: boolean;
|
|
204
|
+
/** How the store's own source uses it, where `usedInSource`. */
|
|
205
|
+
usesInSource?: readonly StoreValueUse[];
|
|
108
206
|
/** The members of the store its own source calls, such as `subscribe`. */
|
|
109
207
|
calledInSource?: ReadonlySet<string>;
|
|
110
208
|
}
|
|
@@ -285,6 +383,7 @@ export async function sourceFacts(cu: JS.CompilationUnit): Promise<SourceFacts>
|
|
|
285
383
|
// A specifier no package can have, such as `@/store`, may name a source through an alias, which the plan resolves
|
|
286
384
|
const followed = new Map(bindings.filter(binding => binding.module === REACT_REDUX || isRelativeSpecifier(binding.module) || isAliasSpecifier(binding.module))
|
|
287
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));
|
|
288
387
|
for (const {element} of cu.statements) {
|
|
289
388
|
if (element.kind === JS.Kind.Import) {
|
|
290
389
|
const imp = element as JS.Import;
|
|
@@ -319,7 +418,7 @@ export async function sourceFacts(cu: JS.CompilationUnit): Promise<SourceFacts>
|
|
|
319
418
|
if (use) facts.uses.push(use);
|
|
320
419
|
}
|
|
321
420
|
if (binding) {
|
|
322
|
-
const use = classify(identifier, binding, this.cursor, followed, dispatchers, declared);
|
|
421
|
+
const use = classify(identifier, binding, this.cursor, followed, dispatchers, declared, persistStore);
|
|
323
422
|
if (use) facts.uses.push(use);
|
|
324
423
|
}
|
|
325
424
|
return identifier;
|
|
@@ -349,7 +448,7 @@ export async function storeFacts(cu: JS.CompilationUnit, modules: ReadonlyMap<st
|
|
|
349
448
|
const name = storeNameAt(this.cursor);
|
|
350
449
|
const own = await usesInSource(cu, name, call);
|
|
351
450
|
stores.push({path: cu.sourcePath, api: store.api, store: name, findings: store.findings,
|
|
352
|
-
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});
|
|
353
452
|
}
|
|
354
453
|
return super.visitMethodInvocation(call, ctx);
|
|
355
454
|
}
|
|
@@ -390,7 +489,7 @@ function dispatcherCount(cu: JS.CompilationUnit, followed: ReadonlyMap<string, I
|
|
|
390
489
|
|
|
391
490
|
/** How the identifier at `cursor`, which names the import `binding`, is used; undefined where it is no use, such as a property name. */
|
|
392
491
|
function classify(identifier: J.Identifier, binding: ImportBinding, cursor: Cursor, followed: ReadonlyMap<string, ImportBinding>,
|
|
393
|
-
dispatchers: ReadonlySet<string>, declared: ReadonlyMap<string, number>): Use | undefined {
|
|
492
|
+
dispatchers: ReadonlySet<string>, declared: ReadonlyMap<string, number>, persistStore: ReadonlySet<string>): Use | undefined {
|
|
394
493
|
const trees = ancestors(cursor);
|
|
395
494
|
if (trees.some(tree => tree.kind === JS.Kind.Import)) return undefined;
|
|
396
495
|
const parent = parentTree(cursor);
|
|
@@ -422,7 +521,7 @@ function classify(identifier: J.Identifier, binding: ImportBinding, cursor: Curs
|
|
|
422
521
|
if (read) return {local, kind: "selector", key: read.key};
|
|
423
522
|
// A selector passed by name moves only with its whole store, which calls it as it is
|
|
424
523
|
const selector = args.length === 1 && !call.typeParameters && isIdentifier(args[0]) ? args[0].simpleName : undefined;
|
|
425
|
-
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};
|
|
426
525
|
}
|
|
427
526
|
if (binding.name === "useDispatch") {
|
|
428
527
|
const variable = parentTree(cursorOf(cursor, call));
|
|
@@ -454,7 +553,10 @@ function classify(identifier: J.Identifier, binding: ImportBinding, cursor: Curs
|
|
|
454
553
|
if (member === "dispatch" && dispatchedAction(call)) {
|
|
455
554
|
return isStatement(call, outer) ? {local, kind: "storeDispatch", member} : {...other("the dispatch's result is used"), reason: "DISPATCH", member};
|
|
456
555
|
}
|
|
457
|
-
|
|
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};
|
|
458
560
|
}
|
|
459
561
|
if (parent?.kind === JS.Kind.JsxEmbeddedExpression) {
|
|
460
562
|
const attribute = trees[2];
|
|
@@ -466,7 +568,9 @@ function classify(identifier: J.Identifier, binding: ImportBinding, cursor: Curs
|
|
|
466
568
|
if (provider?.module === REACT_REDUX && provider.name === "Provider") return {local, kind: "provider"};
|
|
467
569
|
}
|
|
468
570
|
}
|
|
469
|
-
|
|
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};
|
|
470
574
|
}
|
|
471
575
|
|
|
472
576
|
/** Why a call of `connect` can't move to the zustand store's own `connect`, if it can't. */
|
|
@@ -488,6 +592,22 @@ function connectUse(call: J.MethodInvocation, other: (detail: string, action?: s
|
|
|
488
592
|
return undefined;
|
|
489
593
|
}
|
|
490
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
|
+
|
|
491
611
|
/** A use of a dispatcher from `useDispatch`: only `dispatch(x(...))` of an imported `x`, whose result is unused, converts. */
|
|
492
612
|
function dispatcherUse(identifier: J.Identifier, cursor: Cursor, followed: ReadonlyMap<string, ImportBinding>,
|
|
493
613
|
bindings: readonly ImportBinding[]): Use | undefined {
|
|
@@ -502,7 +622,7 @@ function dispatcherUse(identifier: J.Identifier, cursor: Cursor, followed: Reado
|
|
|
502
622
|
const action = dispatchedAction(call);
|
|
503
623
|
const binding = action && followed.get(action.name.simpleName);
|
|
504
624
|
if (binding && binding.module !== REACT_REDUX) return undefined;
|
|
505
|
-
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)};
|
|
506
626
|
}
|
|
507
627
|
return {local, kind: "other", detail: "the dispatcher is used other than to dispatch", reason: "DISPATCH"};
|
|
508
628
|
}
|
|
@@ -552,8 +672,9 @@ export const STORE_MEMBERS: ReadonlySet<string> = new Set(["getState", "dispatch
|
|
|
552
672
|
* How the statements of `cu` other than the store's declaration, `call`, and `export default name` refer to the store's
|
|
553
673
|
* variable: the members of `STORE_MEMBERS` they call on it, and whether they refer to it any other way.
|
|
554
674
|
*/
|
|
555
|
-
async function usesInSource(cu: JS.CompilationUnit, name: string, call: J.MethodInvocation): Promise<{other: boolean, called: Set<string>}> {
|
|
556
|
-
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");
|
|
557
678
|
const statements = cu.statements.map(s => s.element).flatMap(statement => {
|
|
558
679
|
if (statement.kind === JS.Kind.Import) return [];
|
|
559
680
|
// Exporting the store by name, which `exportedStore` reads, is no use of it
|
|
@@ -583,6 +704,7 @@ async function usesInSource(cu: JS.CompilationUnit, name: string, call: J.Method
|
|
|
583
704
|
if (parent?.kind === JS.Kind.PropertyAssignment && (parent as JS.PropertyAssignment).name.element === identifier &&
|
|
584
705
|
(parent as JS.PropertyAssignment).initializer) return identifier;
|
|
585
706
|
result.other = true;
|
|
707
|
+
result.uses.push(storeValueUse(identifier, this.cursor, persistStore));
|
|
586
708
|
return identifier;
|
|
587
709
|
}
|
|
588
710
|
}().visit(statement, new ExecutionContext());
|
|
@@ -701,7 +823,7 @@ export function plan(facts: ReadonlyMap<string, SourceFacts>, sliceSources: Read
|
|
|
701
823
|
const result: Plan = {slices: new Map(), sources: new Set(), blocked: new Map()};
|
|
702
824
|
if (stores.length === 0) return result;
|
|
703
825
|
if (stores.length > 1) {
|
|
704
|
-
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)}]);
|
|
705
827
|
return result;
|
|
706
828
|
}
|
|
707
829
|
const store = stores[0];
|
|
@@ -710,6 +832,8 @@ export function plan(facts: ReadonlyMap<string, SourceFacts>, sliceSources: Read
|
|
|
710
832
|
// Slices that stay in Redux for a reason of their own, already recorded: their uses add nothing to it
|
|
711
833
|
const held = new Map<string, string>();
|
|
712
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;
|
|
713
837
|
if (findings.length === 0) {
|
|
714
838
|
if (!store.exportedAs) block("STORE_NOT_MODULE_LEVEL", store.store);
|
|
715
839
|
const storeSource = sliceSources.get(store.path);
|
|
@@ -753,6 +877,7 @@ export function plan(facts: ReadonlyMap<string, SourceFacts>, sliceSources: Read
|
|
|
753
877
|
}
|
|
754
878
|
}
|
|
755
879
|
const slicePaths = new Map([...result.slices.values()].map(slice => [slice.path, slice]));
|
|
880
|
+
const storeModule = facts.get(store.path);
|
|
756
881
|
const sources = new Set<string>([store.path, ...slicePaths.keys()]);
|
|
757
882
|
for (const source of facts.values()) {
|
|
758
883
|
const resolve = (module: string) => resolveFrom(source.path, module);
|
|
@@ -797,12 +922,26 @@ export function plan(facts: ReadonlyMap<string, SourceFacts>, sliceSources: Read
|
|
|
797
922
|
const where = `${use.local} in ${source.path}`;
|
|
798
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);
|
|
799
924
|
if (use.kind === "other") {
|
|
800
|
-
|
|
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);
|
|
801
935
|
} else if (use.kind === "selector" || use.kind === "getState") {
|
|
802
|
-
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
|
+
}
|
|
803
940
|
} else if (use.kind === "dispatched") {
|
|
804
941
|
// An import without a reducer is recorded once, as UNKNOWN_ACTION
|
|
805
|
-
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
|
+
}
|
|
806
945
|
const via = source.bindings.find(b => b.local === use.via);
|
|
807
946
|
if (via && !(resolve(via.module) === store.path && via.name === store.exportedAs)) block("DISPATCH", `${where} is dispatched through \`${use.via}\``);
|
|
808
947
|
} else if (use.kind === "connect") {
|
|
@@ -812,13 +951,13 @@ export function plan(facts: ReadonlyMap<string, SourceFacts>, sliceSources: Read
|
|
|
812
951
|
}
|
|
813
952
|
// An `other` use is recorded above with what it was
|
|
814
953
|
if (slice && use.kind !== "dispatched" && use.kind !== "other") block("SLICE_USE", `${where}: ${use.kind}`);
|
|
815
|
-
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));
|
|
816
955
|
}
|
|
817
956
|
// The store's source is removed, so each import of it goes too, even one nothing uses
|
|
818
957
|
for (const binding of source.bindings) {
|
|
819
958
|
if (source.path === store.path || binding.module === REACT_REDUX || resolve(binding.module) !== store.path) continue;
|
|
820
959
|
changes = true;
|
|
821
|
-
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));
|
|
822
961
|
}
|
|
823
962
|
if (changes && source.path !== store.path) {
|
|
824
963
|
if (source.typescript) block("TYPESCRIPT", source.path);
|
|
@@ -859,7 +998,8 @@ function reducerPlan(facts: ReadonlyMap<string, SourceFacts>, sliceSources: Read
|
|
|
859
998
|
const names = new Set<string>([...sliceSources.get(store.path)?.names ?? [], ...source?.declared ?? [], ...source?.bindings.map(b => b.local) ?? []]);
|
|
860
999
|
const reducer = ["rootReducer", "appReducer", "storeReducer"].find(name => !names.has(name)) ?? "rootReducer";
|
|
861
1000
|
if (!store.exportedAs) block("STORE_NOT_MODULE_LEVEL", store.store);
|
|
862
|
-
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.`);
|
|
863
1003
|
const dependency = dependencies(store.path, false);
|
|
864
1004
|
if (dependency) findings.push(dependency);
|
|
865
1005
|
for (const name of [hook, reducer, "create"]) if (names.has(name)) block("NAME_TAKEN", `${name} in ${store.path}`);
|
|
@@ -910,7 +1050,8 @@ function reducerPlan(facts: ReadonlyMap<string, SourceFacts>, sliceSources: Read
|
|
|
910
1050
|
if (path !== store.path || binding.name !== store.exportedAs) continue;
|
|
911
1051
|
if (use.kind === "provider") changes = true;
|
|
912
1052
|
else if (use.kind === "other" && use.member === "subscribe") subscribes = true;
|
|
913
|
-
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]));
|
|
914
1055
|
}
|
|
915
1056
|
if (changes && source.path !== store.path) {
|
|
916
1057
|
if (source.typescript) block("TYPESCRIPT", source.path);
|