@openrewrite/recipes-react 0.20.0 → 0.21.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 +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +10 -1
- package/dist/index.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 +30 -3
- 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-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 +10 -0
- package/dist/testing/enzyme-render-blockers.d.ts.map +1 -0
- package/dist/testing/enzyme-render-blockers.js +119 -0
- package/dist/testing/enzyme-render-blockers.js.map +1 -0
- 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 +80 -17
- package/dist/testing/enzyme-to-react-testing-library.js.map +1 -1
- package/dist/testing/enzyme-usage.d.ts +2 -0
- package/dist/testing/enzyme-usage.d.ts.map +1 -1
- package/dist/testing/enzyme-usage.js +29 -3
- package/dist/testing/enzyme-usage.js.map +1 -1
- package/dist/testing/enzyme-wrapper-scope.d.ts +5 -1
- package/dist/testing/enzyme-wrapper-scope.d.ts.map +1 -1
- package/dist/testing/enzyme-wrapper-scope.js +12 -3
- 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 +5 -4
- package/dist/zustand/redux-stores.d.ts.map +1 -1
- package/dist/zustand/redux-stores.js +8 -5
- 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 +137 -42
- package/dist/zustand/redux-to-zustand.js.map +1 -1
- package/dist/zustand/zustand-plan.d.ts +7 -5
- package/dist/zustand/zustand-plan.d.ts.map +1 -1
- package/dist/zustand/zustand-plan.js +87 -33
- 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 +7 -0
- package/src/search/find-enzyme-usage.ts +30 -6
- 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-portals.ts +4 -44
- package/src/testing/enzyme-render-blockers.ts +113 -0
- package/src/testing/enzyme-to-react-testing-library.ts +90 -18
- package/src/testing/enzyme-usage.ts +35 -2
- package/src/testing/enzyme-wrapper-scope.ts +13 -2
- 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 +12 -7
- package/src/zustand/redux-to-zustand.ts +168 -48
- package/src/zustand/zustand-plan.ts +98 -32
|
@@ -2,7 +2,7 @@ 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";
|
|
5
|
+
import {ImportBinding, importBindings, isAliasSpecifier, isRelativeSpecifier, Resolve, resolveRelative} from "../util/module-imports";
|
|
6
6
|
import {Finding, localSlices, ReducerStoreKind, objectProperties, RTK, SliceFacts, SliceModule, storeFindings, storeFunctions, TEST_SOURCE} from "./redux-stores";
|
|
7
7
|
|
|
8
8
|
export const REACT_REDUX = "react-redux";
|
|
@@ -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,29 @@ 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
|
+
}
|
|
51
|
+
|
|
52
|
+
/** What to do with an import of react-redux used other than called, by its name. */
|
|
53
|
+
const REACT_REDUX_VALUE_ACTIONS: Readonly<Record<string, string>> = {
|
|
54
|
+
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.",
|
|
55
|
+
TypedUseSelectorHook: "A react-redux type goes with the TypeScript sources, which aren't converted yet.",
|
|
56
|
+
ConnectedProps: "A react-redux type goes with the TypeScript sources, which aren't converted yet.",
|
|
57
|
+
ResolveThunks: "A react-redux type goes with the TypeScript sources, which aren't converted yet."
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
/** What to do with a call of an export of react-redux that the conversion doesn't follow, by its name. */
|
|
61
|
+
const REACT_REDUX_CALL_ACTIONS: Readonly<Record<string, string>> = {
|
|
62
|
+
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.",
|
|
63
|
+
useStore: "Read the state with `useSelector` and dispatch with `useDispatch` instead of `useStore()`, or convert by hand."
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
/** What to do with a `connect` of a store that can't move whole, which only a store moving whole declares. */
|
|
67
|
+
export function connectAction(wholeStores: boolean): string {
|
|
68
|
+
return wholeStores ?
|
|
69
|
+
"`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`." :
|
|
70
|
+
"`connect` converts only where the store moves whole, which `wholeStores` turns off: run with it on, or replace `connect` with `useSelector` and `useDispatch`.";
|
|
48
71
|
}
|
|
49
72
|
|
|
50
73
|
/** What one source tells the plan. */
|
|
@@ -259,7 +282,8 @@ export async function sourceFacts(cu: JS.CompilationUnit): Promise<SourceFacts>
|
|
|
259
282
|
path: cu.sourcePath, typescript: /\.(?:ts|tsx|mts|cts)$/.test(cu.sourcePath), test: TEST_SOURCE.test(cu.sourcePath),
|
|
260
283
|
bindings, declared: new Set(declared.keys()), uses: [], unfollowed: [], stores: [], readSelectors: readSelectors(cu, declared)
|
|
261
284
|
};
|
|
262
|
-
|
|
285
|
+
// A specifier no package can have, such as `@/store`, may name a source through an alias, which the plan resolves
|
|
286
|
+
const followed = new Map(bindings.filter(binding => binding.module === REACT_REDUX || isRelativeSpecifier(binding.module) || isAliasSpecifier(binding.module))
|
|
263
287
|
.map(binding => [binding.local, binding]));
|
|
264
288
|
for (const {element} of cu.statements) {
|
|
265
289
|
if (element.kind === JS.Kind.Import) {
|
|
@@ -314,13 +338,13 @@ export async function sourceFacts(cu: JS.CompilationUnit): Promise<SourceFacts>
|
|
|
314
338
|
}
|
|
315
339
|
|
|
316
340
|
/** 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
|
|
341
|
+
export async function storeFacts(cu: JS.CompilationUnit, modules: ReadonlyMap<string, SliceModule>, resolve?: Resolve): Promise<StoreFacts[]> {
|
|
318
342
|
const stores: StoreFacts[] = [];
|
|
319
343
|
const functions = storeFunctions(cu);
|
|
320
344
|
if (functions.legacy.size === 0 && functions.configure.size === 0) return stores;
|
|
321
345
|
await new class extends JavaScriptVisitor<ExecutionContext> {
|
|
322
346
|
override async visitMethodInvocation(call: J.MethodInvocation, ctx: ExecutionContext): Promise<J | undefined> {
|
|
323
|
-
const store = storeFindings(call, cu, modules, functions);
|
|
347
|
+
const store = storeFindings(call, cu, modules, functions, resolve);
|
|
324
348
|
if (store) {
|
|
325
349
|
const name = storeNameAt(this.cursor);
|
|
326
350
|
const own = await usesInSource(cu, name, call);
|
|
@@ -371,7 +395,7 @@ function classify(identifier: J.Identifier, binding: ImportBinding, cursor: Curs
|
|
|
371
395
|
if (trees.some(tree => tree.kind === JS.Kind.Import)) return undefined;
|
|
372
396
|
const parent = parentTree(cursor);
|
|
373
397
|
const local = identifier.simpleName;
|
|
374
|
-
const other = (detail: string): Use => ({local, kind: "other", detail});
|
|
398
|
+
const other = (detail: string, action?: string): Use => ({local, kind: "other", detail, ...action ? {action} : {}});
|
|
375
399
|
// Names of members, properties and attributes are no use of the import
|
|
376
400
|
if (parent?.kind === J.Kind.FieldAccess && (parent as J.FieldAccess).name.element === identifier) return undefined;
|
|
377
401
|
if (parent?.kind === J.Kind.MethodInvocation && (parent as J.MethodInvocation).name === identifier && (parent as J.MethodInvocation).select) return undefined;
|
|
@@ -387,7 +411,9 @@ function classify(identifier: J.Identifier, binding: ImportBinding, cursor: Curs
|
|
|
387
411
|
return providerUse(tag, followed, local);
|
|
388
412
|
}
|
|
389
413
|
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
|
|
414
|
+
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") ?
|
|
415
|
+
`\`${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.` :
|
|
416
|
+
`\`${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
417
|
const call = parent as J.MethodInvocation;
|
|
392
418
|
if (binding.name === "connect") return connectUse(call, other) ?? {local, kind: "connect"};
|
|
393
419
|
if (binding.name === "useSelector") {
|
|
@@ -401,9 +427,11 @@ function classify(identifier: J.Identifier, binding: ImportBinding, cursor: Curs
|
|
|
401
427
|
if (binding.name === "useDispatch") {
|
|
402
428
|
const variable = parentTree(cursorOf(cursor, call));
|
|
403
429
|
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"
|
|
430
|
+
? {local, kind: "dispatcher"} : other("`useDispatch` not held in a variable of its own",
|
|
431
|
+
"Hold the dispatcher in a `const` of its own, such as `const dispatch = useDispatch()`, and dispatch through it.");
|
|
405
432
|
}
|
|
406
|
-
return other(`\`${binding.name}\` from react-redux
|
|
433
|
+
return other(`\`${binding.name}\` from react-redux`, REACT_REDUX_CALL_ACTIONS[binding.name] ??
|
|
434
|
+
`The recipe converts only \`connect\`, \`useSelector\`, \`useDispatch\` and \`<Provider>\` of react-redux: replace \`${binding.name}(...)\` by hand.`);
|
|
407
435
|
}
|
|
408
436
|
// A relative import: an action creator, the store, a slice or anything else of the repository
|
|
409
437
|
if (parent?.kind === J.Kind.MethodInvocation && (parent as J.MethodInvocation).name === identifier) {
|
|
@@ -442,18 +470,19 @@ function classify(identifier: J.Identifier, binding: ImportBinding, cursor: Curs
|
|
|
442
470
|
}
|
|
443
471
|
|
|
444
472
|
/** 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 {
|
|
473
|
+
function connectUse(call: J.MethodInvocation, other: (detail: string, action?: string) => Use): Use | undefined {
|
|
446
474
|
const args = argumentsOf(call);
|
|
447
475
|
// NOTE: a `null` read back from a stored LST doesn't keep `value: null`, so its source decides
|
|
448
476
|
const absent = (node: J | undefined) => !node ||
|
|
449
477
|
node.kind === J.Kind.Literal && (node as J.Literal).valueSource === "null" ||
|
|
450
478
|
isIdentifier(node) && node.simpleName === "undefined";
|
|
451
|
-
if (args.length > 4 || !absent(args[2])) return other("`connect` with `mergeProps`");
|
|
479
|
+
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
480
|
if (args.length === 4 && !absent(args[3])) {
|
|
453
481
|
const options = objectProperties(args[3]);
|
|
454
482
|
const forwardRef = options?.get("forwardRef");
|
|
455
483
|
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`"
|
|
484
|
+
return other("`connect` with options other than `forwardRef`",
|
|
485
|
+
"Pass `connect` no options, or only `{forwardRef: true}` or `{forwardRef: false}`; others, such as `context`, `areStatesEqual` or `pure`, are converted by hand.");
|
|
457
486
|
}
|
|
458
487
|
}
|
|
459
488
|
return undefined;
|
|
@@ -478,16 +507,23 @@ function dispatcherUse(identifier: J.Identifier, cursor: Cursor, followed: Reado
|
|
|
478
507
|
return {local, kind: "other", detail: "the dispatcher is used other than to dispatch", reason: "DISPATCH"};
|
|
479
508
|
}
|
|
480
509
|
|
|
510
|
+
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.";
|
|
511
|
+
|
|
512
|
+
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`. " +
|
|
513
|
+
"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.";
|
|
514
|
+
|
|
481
515
|
/** `<Provider store={store}>` with one element child and no other prop. */
|
|
482
516
|
function providerUse(tag: JSX.Tag, followed: ReadonlyMap<string, ImportBinding>, local: string): Use {
|
|
483
|
-
const other = (detail: string): Use => ({local, kind: "other", detail});
|
|
517
|
+
const other = (detail: string, action?: string): Use => ({local, kind: "other", detail, ...action ? {action} : {}});
|
|
484
518
|
const attributes = tag.attributes.map(a => a.element);
|
|
485
|
-
if (attributes.length !== 1 || attributes[0].kind !== JS.Kind.JsxAttribute)
|
|
519
|
+
if (attributes.length !== 1 || attributes[0].kind !== JS.Kind.JsxAttribute) {
|
|
520
|
+
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.");
|
|
521
|
+
}
|
|
486
522
|
const value = (attributes[0] as JSX.Attribute).value?.element;
|
|
487
523
|
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");
|
|
524
|
+
if (!isIdentifier(expression) || !followed.has(expression.simpleName)) return other("`Provider` of a store that isn't imported", PROVIDER_NOT_IMPORTED);
|
|
489
525
|
if (!providerChild(tag) && !providerChildren(tag)) return other("`Provider` without children");
|
|
490
|
-
return {local, kind: "providerTag"};
|
|
526
|
+
return {local, kind: "providerTag", via: expression.simpleName};
|
|
491
527
|
}
|
|
492
528
|
|
|
493
529
|
/** Whether a tag has children other than whitespace, which a fragment keeps when the tag goes. */
|
|
@@ -520,6 +556,8 @@ async function usesInSource(cu: JS.CompilationUnit, name: string, call: J.Method
|
|
|
520
556
|
const result = {other: false, called: new Set<string>()};
|
|
521
557
|
const statements = cu.statements.map(s => s.element).flatMap(statement => {
|
|
522
558
|
if (statement.kind === JS.Kind.Import) return [];
|
|
559
|
+
// Exporting the store by name, which `exportedStore` reads, is no use of it
|
|
560
|
+
if (exportList(statement).some(entry => entry.local === name)) return [];
|
|
523
561
|
if (statement.kind === JS.Kind.ExportAssignment && isIdentifier((statement as JS.ExportAssignment).expression.element) &&
|
|
524
562
|
((statement as JS.ExportAssignment).expression.element as J.Identifier).simpleName === name) return [];
|
|
525
563
|
if (statement.kind === J.Kind.VariableDeclarations && (statement as J.VariableDeclarations).variables.some(v => v.element.initializer?.element === call)) {
|
|
@@ -582,7 +620,25 @@ function exportedStore(cu: JS.CompilationUnit, cursor: Cursor): string | undefin
|
|
|
582
620
|
if (declarations.modifiers.some(m => m.keyword === "export")) return name;
|
|
583
621
|
const exported = cu.statements.map(s => s.element).find(s => s.kind === JS.Kind.ExportAssignment &&
|
|
584
622
|
isIdentifier((s as JS.ExportAssignment).expression.element) && ((s as JS.ExportAssignment).expression.element as J.Identifier).simpleName === name);
|
|
585
|
-
|
|
623
|
+
if (exported) return "default";
|
|
624
|
+
// `export {store}`, or `export {store as appStore}`, once
|
|
625
|
+
const listed = cu.statements.flatMap(s => exportList(s.element)).filter(entry => entry.local === name);
|
|
626
|
+
return listed.length === 1 ? listed[0].exported : undefined;
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
/** The local and exported names of `export {a, b as c}`, with no module specifier and no `type`. */
|
|
630
|
+
function exportList(statement: J): {local: string, exported: string}[] {
|
|
631
|
+
if (statement.kind !== JS.Kind.ExportDeclaration) return [];
|
|
632
|
+
const declaration = statement as JS.ExportDeclaration;
|
|
633
|
+
if (declaration.moduleSpecifier || declaration.typeOnly.element || declaration.exportClause?.kind !== JS.Kind.NamedExports) return [];
|
|
634
|
+
return (declaration.exportClause as JS.NamedExports).elements.elements.flatMap(({element}) => {
|
|
635
|
+
if (element.kind !== JS.Kind.ExportSpecifier || (element as JS.ExportSpecifier).typeOnly.element) return [];
|
|
636
|
+
const specifier = (element as JS.ExportSpecifier).specifier;
|
|
637
|
+
if (isIdentifier(specifier)) return [{local: specifier.simpleName, exported: specifier.simpleName}];
|
|
638
|
+
if (specifier.kind !== JS.Kind.Alias) return [];
|
|
639
|
+
const local = (specifier as JS.Alias).propertyName.element, alias = (specifier as JS.Alias).alias;
|
|
640
|
+
return isIdentifier(local) && isIdentifier(alias) ? [{local: local.simpleName, exported: alias.simpleName}] : [];
|
|
641
|
+
});
|
|
586
642
|
}
|
|
587
643
|
|
|
588
644
|
/** A slice of the store, with what the conversion writes for it. */
|
|
@@ -639,7 +695,8 @@ export const hookName = (key: string) => `use${key.charAt(0).toUpperCase()}${key
|
|
|
639
695
|
export function plan(facts: ReadonlyMap<string, SourceFacts>, sliceSources: ReadonlyMap<string, SliceSource>,
|
|
640
696
|
dependencies: (storePath: string, immer?: boolean) => Finding | undefined, scope: (path: string) => boolean = () => true,
|
|
641
697
|
wholeStores = true, renderer: (storePath: string) => string | undefined = () => "react-dom",
|
|
642
|
-
selectsOnEachRead: (storePath: string) => boolean = () => false
|
|
698
|
+
selectsOnEachRead: (storePath: string) => boolean = () => false,
|
|
699
|
+
resolveFrom: Resolve = (from, specifier) => resolveRelative(from, specifier, facts)): Plan {
|
|
643
700
|
const stores = [...facts.values()].filter(source => scope(source.path)).flatMap(source => source.stores);
|
|
644
701
|
const result: Plan = {slices: new Map(), sources: new Set(), blocked: new Map()};
|
|
645
702
|
if (stores.length === 0) return result;
|
|
@@ -649,7 +706,7 @@ export function plan(facts: ReadonlyMap<string, SourceFacts>, sliceSources: Read
|
|
|
649
706
|
}
|
|
650
707
|
const store = stores[0];
|
|
651
708
|
const findings: Finding[] = [...store.findings];
|
|
652
|
-
const block = (reason: string, detail: string) => findings.push({reason, detail});
|
|
709
|
+
const block = (reason: string, detail: string, action?: string) => findings.push({reason, detail, ...action ? {action} : {}});
|
|
653
710
|
// Slices that stay in Redux for a reason of their own, already recorded: their uses add nothing to it
|
|
654
711
|
const held = new Map<string, string>();
|
|
655
712
|
const handled = new Map<string, string>();
|
|
@@ -698,7 +755,7 @@ export function plan(facts: ReadonlyMap<string, SourceFacts>, sliceSources: Read
|
|
|
698
755
|
const slicePaths = new Map([...result.slices.values()].map(slice => [slice.path, slice]));
|
|
699
756
|
const sources = new Set<string>([store.path, ...slicePaths.keys()]);
|
|
700
757
|
for (const source of facts.values()) {
|
|
701
|
-
const resolve = (module: string) =>
|
|
758
|
+
const resolve = (module: string) => resolveFrom(source.path, module);
|
|
702
759
|
const touches = (module: string) => {
|
|
703
760
|
const path = resolve(module);
|
|
704
761
|
return path === store.path || (path !== undefined && slicePaths.has(path));
|
|
@@ -738,8 +795,9 @@ export function plan(facts: ReadonlyMap<string, SourceFacts>, sliceSources: Read
|
|
|
738
795
|
if (source.path === store.path && slice && use.kind === "other") continue;
|
|
739
796
|
changes = true;
|
|
740
797
|
const where = `${use.local} in ${source.path}`;
|
|
798
|
+
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
799
|
if (use.kind === "other") {
|
|
742
|
-
block(use.reason ?? (isStore ? "STORE_USE" : slice ? "SLICE_USE" : !binding ? "DISPATCH" : isReactRedux ? "REACT_REDUX_USE" : "STORE_USE"), `${where}: ${use.detail}
|
|
800
|
+
block(use.reason ?? (isStore ? "STORE_USE" : slice ? "SLICE_USE" : !binding ? "DISPATCH" : isReactRedux ? "REACT_REDUX_USE" : "STORE_USE"), `${where}: ${use.detail}`, use.action);
|
|
743
801
|
} else if (use.kind === "selector" || use.kind === "getState") {
|
|
744
802
|
if (!result.slices.has(use.key!)) block("SELECTOR", `${where} reads \`${use.key}\`, which is no slice of the store`);
|
|
745
803
|
} else if (use.kind === "dispatched") {
|
|
@@ -748,7 +806,7 @@ export function plan(facts: ReadonlyMap<string, SourceFacts>, sliceSources: Read
|
|
|
748
806
|
const via = source.bindings.find(b => b.local === use.via);
|
|
749
807
|
if (via && !(resolve(via.module) === store.path && via.name === store.exportedAs)) block("DISPATCH", `${where} is dispatched through \`${use.via}\``);
|
|
750
808
|
} else if (use.kind === "connect") {
|
|
751
|
-
block("REACT_REDUX_USE", `${where}: \`connect\` from react-redux
|
|
809
|
+
block("REACT_REDUX_USE", `${where}: \`connect\` from react-redux`, connectAction(wholeStores));
|
|
752
810
|
} else if ((use.kind === "provider" || use.kind === "storeDispatch") && !isStore) {
|
|
753
811
|
block("STORE_USE", `${where} isn't the store`);
|
|
754
812
|
}
|
|
@@ -774,7 +832,7 @@ export function plan(facts: ReadonlyMap<string, SourceFacts>, sliceSources: Read
|
|
|
774
832
|
}
|
|
775
833
|
if ([store.path, ...slicePaths.keys()].some(path => /\.(?:ts|tsx|mts|cts)$/.test(path))) block("TYPESCRIPT", store.path);
|
|
776
834
|
// 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);
|
|
835
|
+
if (findings.length > 0 && store.reducerStore && wholeStores) return reducerPlan(facts, sliceSources, store, dependencies, scope, renderer, selectsOnEachRead, resolveFrom);
|
|
778
836
|
if (findings.length > 0) {
|
|
779
837
|
result.blocked.set(store, dedupe(findings));
|
|
780
838
|
result.slices.clear();
|
|
@@ -792,9 +850,9 @@ export function plan(facts: ReadonlyMap<string, SourceFacts>, sliceSources: Read
|
|
|
792
850
|
*/
|
|
793
851
|
function reducerPlan(facts: ReadonlyMap<string, SourceFacts>, sliceSources: ReadonlyMap<string, SliceSource>, store: StoreFacts,
|
|
794
852
|
dependencies: (storePath: string, immer?: boolean) => Finding | undefined, scope: (path: string) => boolean,
|
|
795
|
-
renderer: (storePath: string) => string | undefined, selectsOnEachRead: (storePath: string) => boolean): Plan {
|
|
853
|
+
renderer: (storePath: string) => string | undefined, selectsOnEachRead: (storePath: string) => boolean, resolveFrom: Resolve): Plan {
|
|
796
854
|
const findings: Finding[] = [];
|
|
797
|
-
const block = (reason: string, detail: string) => findings.push({reason, detail});
|
|
855
|
+
const block = (reason: string, detail: string, action?: string) => findings.push({reason, detail, ...action ? {action} : {}});
|
|
798
856
|
const hook = REDUCER_STORE_HOOK;
|
|
799
857
|
// A legacy `createStore` source has no slice source, so its own declarations and imports count too
|
|
800
858
|
const source = facts.get(store.path);
|
|
@@ -815,7 +873,7 @@ function reducerPlan(facts: ReadonlyMap<string, SourceFacts>, sliceSources: Read
|
|
|
815
873
|
// Redux calls a store's listeners after every dispatch, which the store's `dispatch` does for its own `subscribe`
|
|
816
874
|
let subscribes = store.calledInSource?.has("subscribe") ?? false;
|
|
817
875
|
for (const source of facts.values()) {
|
|
818
|
-
const resolve = (module: string) =>
|
|
876
|
+
const resolve = (module: string) => resolveFrom(source.path, module);
|
|
819
877
|
if (!scope(source.path)) {
|
|
820
878
|
for (const binding of source.bindings) {
|
|
821
879
|
if (binding.module !== REACT_REDUX && resolve(binding.module) === store.path) block("STORE_USE", `${binding.local} in ${source.path}, of another package`);
|
|
@@ -837,13 +895,14 @@ function reducerPlan(facts: ReadonlyMap<string, SourceFacts>, sliceSources: Read
|
|
|
837
895
|
if (binding.module === REACT_REDUX) {
|
|
838
896
|
changes = true;
|
|
839
897
|
if (use.kind === "connect") connects = true;
|
|
840
|
-
if (use.kind === "
|
|
898
|
+
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);
|
|
899
|
+
if (use.kind === "other" && use.selector && readSelector(source, use.selector, facts, resolveFrom)) continue;
|
|
841
900
|
// 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
901
|
if (use.kind === "other" && use.anySelector) {
|
|
843
902
|
if (selectsOnEachRead(store.path)) selects = true;
|
|
844
903
|
continue;
|
|
845
904
|
}
|
|
846
|
-
if (use.kind === "other") block(use.reason ?? "REACT_REDUX_USE", `${where}: ${use.detail}
|
|
905
|
+
if (use.kind === "other") block(use.reason ?? "REACT_REDUX_USE", `${where}: ${use.detail}`, use.action);
|
|
847
906
|
continue;
|
|
848
907
|
}
|
|
849
908
|
const path = resolve(binding.module);
|
|
@@ -891,14 +950,21 @@ function reducerPlan(facts: ReadonlyMap<string, SourceFacts>, sliceSources: Read
|
|
|
891
950
|
...subscribes ? {listeners: STORE_LISTENERS} : {}}};
|
|
892
951
|
}
|
|
893
952
|
|
|
953
|
+
/** Whether the `Provider` of a `providerTag` use provides `store`, as its source imports it. */
|
|
954
|
+
function provides(source: SourceFacts, use: Use, store: StoreFacts, resolve: (module: string) => string | undefined): boolean {
|
|
955
|
+
const binding = source.bindings.find(b => b.local === use.via);
|
|
956
|
+
return !!binding && binding.module !== REACT_REDUX && resolve(binding.module) === store.path && binding.name === store.exportedAs;
|
|
957
|
+
}
|
|
958
|
+
|
|
894
959
|
/**
|
|
895
960
|
* Whether `name`, which `source` passes to `useSelector`, is a plain read of the state: a `const` of its own, or one it
|
|
896
961
|
* imports by the name a followed module exports it as.
|
|
897
962
|
*/
|
|
898
|
-
export function readSelector(source: SourceFacts, name: string, facts: ReadonlyMap<string, SourceFacts
|
|
963
|
+
export function readSelector(source: SourceFacts, name: string, facts: ReadonlyMap<string, SourceFacts>,
|
|
964
|
+
resolve: Resolve = (from, specifier) => resolveRelative(from, specifier, facts)): boolean {
|
|
899
965
|
const binding = source.bindings.find(b => b.local === name);
|
|
900
966
|
if (!binding) return source.readSelectors.local.has(name);
|
|
901
|
-
const path =
|
|
967
|
+
const path = resolve(source.path, binding.module);
|
|
902
968
|
return path !== undefined && (facts.get(path)?.readSelectors.exported.has(binding.name) ?? false);
|
|
903
969
|
}
|
|
904
970
|
|
|
@@ -909,10 +975,10 @@ export function readSelector(source: SourceFacts, name: string, facts: ReadonlyM
|
|
|
909
975
|
export function planPackages(facts: ReadonlyMap<string, SourceFacts>, sliceSources: ReadonlyMap<string, SliceSource>,
|
|
910
976
|
packageOf: (path: string) => string | undefined, dependencies: (storePath: string, immer?: boolean) => Finding | undefined,
|
|
911
977
|
wholeStores = true, renderer?: (storePath: string) => string | undefined,
|
|
912
|
-
selectsOnEachRead?: (storePath: string) => boolean): Plan[] {
|
|
978
|
+
selectsOnEachRead?: (storePath: string) => boolean, resolve?: Resolve): Plan[] {
|
|
913
979
|
const root = (path: string) => packageOf(path) ?? "";
|
|
914
980
|
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));
|
|
981
|
+
return [...roots].map(packageRoot => plan(facts, sliceSources, dependencies, path => root(path) === packageRoot, wholeStores, renderer, selectsOnEachRead, resolve));
|
|
916
982
|
}
|
|
917
983
|
|
|
918
984
|
function dedupe(findings: Finding[]): Finding[] {
|