@openrewrite/recipes-react 0.19.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 +64 -4
- 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-shared-wrapper.d.ts +26 -0
- package/dist/testing/enzyme-shared-wrapper.d.ts.map +1 -0
- package/dist/testing/enzyme-shared-wrapper.js +213 -0
- package/dist/testing/enzyme-shared-wrapper.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 +355 -16
- package/dist/testing/enzyme-to-react-testing-library.js.map +1 -1
- package/dist/testing/enzyme-usage.d.ts +5 -1
- package/dist/testing/enzyme-usage.d.ts.map +1 -1
- package/dist/testing/enzyme-usage.js +48 -6
- package/dist/testing/enzyme-usage.js.map +1 -1
- package/dist/testing/enzyme-wrapper-scope.d.ts +7 -1
- package/dist/testing/enzyme-wrapper-scope.d.ts.map +1 -1
- package/dist/testing/enzyme-wrapper-scope.js +24 -9
- 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 +15 -4
- package/dist/zustand/redux-stores.d.ts.map +1 -1
- package/dist/zustand/redux-stores.js +155 -10
- package/dist/zustand/redux-stores.js.map +1 -1
- package/dist/zustand/redux-to-zustand.d.ts +29 -5
- package/dist/zustand/redux-to-zustand.d.ts.map +1 -1
- package/dist/zustand/redux-to-zustand.js +629 -42
- package/dist/zustand/redux-to-zustand.js.map +1 -1
- package/dist/zustand/zustand-plan.d.ts +31 -6
- package/dist/zustand/zustand-plan.d.ts.map +1 -1
- package/dist/zustand/zustand-plan.js +320 -31
- 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 +59 -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-shared-wrapper.ts +224 -0
- package/src/testing/enzyme-to-react-testing-library.ts +354 -19
- package/src/testing/enzyme-usage.ts +62 -5
- package/src/testing/enzyme-wrapper-scope.ts +24 -6
- 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 +171 -11
- package/src/zustand/redux-to-zustand.ts +689 -48
- package/src/zustand/zustand-plan.ts +350 -29
|
@@ -9,7 +9,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.hookName = exports.REACT_REDUX = void 0;
|
|
12
|
+
exports.hookName = exports.REDUCER_STORE_HOOK = exports.STORE_LISTENERS = exports.SELECTOR_HOOK = exports.STORE_MEMBERS = exports.REACT_REDUX = void 0;
|
|
13
|
+
exports.connectAction = connectAction;
|
|
13
14
|
exports.parentTree = parentTree;
|
|
14
15
|
exports.isStatement = isStatement;
|
|
15
16
|
exports.isReactDependencies = isReactDependencies;
|
|
@@ -23,8 +24,10 @@ exports.walk = walk;
|
|
|
23
24
|
exports.sourceFacts = sourceFacts;
|
|
24
25
|
exports.storeFacts = storeFacts;
|
|
25
26
|
exports.isDispatcherDeclaration = isDispatcherDeclaration;
|
|
27
|
+
exports.providerChildren = providerChildren;
|
|
26
28
|
exports.providerChild = providerChild;
|
|
27
29
|
exports.plan = plan;
|
|
30
|
+
exports.readSelector = readSelector;
|
|
28
31
|
exports.planPackages = planPackages;
|
|
29
32
|
exports.sliceSource = sliceSource;
|
|
30
33
|
exports.actionsExport = actionsExport;
|
|
@@ -37,6 +40,21 @@ const path_1 = require("path");
|
|
|
37
40
|
const module_imports_1 = require("../util/module-imports");
|
|
38
41
|
const redux_stores_1 = require("./redux-stores");
|
|
39
42
|
exports.REACT_REDUX = "react-redux";
|
|
43
|
+
const REACT_REDUX_VALUE_ACTIONS = {
|
|
44
|
+
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.",
|
|
45
|
+
TypedUseSelectorHook: "A react-redux type goes with the TypeScript sources, which aren't converted yet.",
|
|
46
|
+
ConnectedProps: "A react-redux type goes with the TypeScript sources, which aren't converted yet.",
|
|
47
|
+
ResolveThunks: "A react-redux type goes with the TypeScript sources, which aren't converted yet."
|
|
48
|
+
};
|
|
49
|
+
const REACT_REDUX_CALL_ACTIONS = {
|
|
50
|
+
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.",
|
|
51
|
+
useStore: "Read the state with `useSelector` and dispatch with `useDispatch` instead of `useStore()`, or convert by hand."
|
|
52
|
+
};
|
|
53
|
+
function connectAction(wholeStores) {
|
|
54
|
+
return wholeStores ?
|
|
55
|
+
"`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`." :
|
|
56
|
+
"`connect` converts only where the store moves whole, which `wholeStores` turns off: run with it on, or replace `connect` with `useSelector` and `useDispatch`.";
|
|
57
|
+
}
|
|
40
58
|
function parentTree(cursor) {
|
|
41
59
|
for (let c = cursor.parent; c; c = c.parent) {
|
|
42
60
|
const value = c.value;
|
|
@@ -183,6 +201,26 @@ function walk(node, visit) {
|
|
|
183
201
|
}
|
|
184
202
|
}
|
|
185
203
|
const SKIPPED = new Set(["markers", "prefix", "type", "methodType", "fieldType", "variableType", "constructorType", "javaType", "before", "after"]);
|
|
204
|
+
function readSelectors(cu, declared) {
|
|
205
|
+
var _a;
|
|
206
|
+
const local = new Set();
|
|
207
|
+
const exported = new Set();
|
|
208
|
+
for (const { element } of cu.statements) {
|
|
209
|
+
if (element.kind !== java_1.J.Kind.VariableDeclarations)
|
|
210
|
+
continue;
|
|
211
|
+
const declarations = element;
|
|
212
|
+
if (!declarations.modifiers.some(m => m.keyword === "const"))
|
|
213
|
+
continue;
|
|
214
|
+
for (const { element: variable } of declarations.variables) {
|
|
215
|
+
if (!(0, java_1.isIdentifier)(variable.name) || declared.get(variable.name.simpleName) !== 1 || !selectorRead((_a = variable.initializer) === null || _a === void 0 ? void 0 : _a.element))
|
|
216
|
+
continue;
|
|
217
|
+
local.add(variable.name.simpleName);
|
|
218
|
+
if (declarations.modifiers.some(m => m.keyword === "export"))
|
|
219
|
+
exported.add(variable.name.simpleName);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
return { local, exported };
|
|
223
|
+
}
|
|
186
224
|
function sourceFacts(cu) {
|
|
187
225
|
return __awaiter(this, void 0, void 0, function* () {
|
|
188
226
|
var _a, _b, _c, _d, _e;
|
|
@@ -190,9 +228,9 @@ function sourceFacts(cu) {
|
|
|
190
228
|
const declared = declaredNames(cu);
|
|
191
229
|
const facts = {
|
|
192
230
|
path: cu.sourcePath, typescript: /\.(?:ts|tsx|mts|cts)$/.test(cu.sourcePath), test: redux_stores_1.TEST_SOURCE.test(cu.sourcePath),
|
|
193
|
-
bindings, declared: new Set(declared.keys()), uses: [], unfollowed: [], stores: []
|
|
231
|
+
bindings, declared: new Set(declared.keys()), uses: [], unfollowed: [], stores: [], readSelectors: readSelectors(cu, declared)
|
|
194
232
|
};
|
|
195
|
-
const followed = new Map(bindings.filter(binding => binding.module === exports.REACT_REDUX || (0, module_imports_1.isRelativeSpecifier)(binding.module))
|
|
233
|
+
const followed = new Map(bindings.filter(binding => binding.module === exports.REACT_REDUX || (0, module_imports_1.isRelativeSpecifier)(binding.module) || (0, module_imports_1.isAliasSpecifier)(binding.module))
|
|
196
234
|
.map(binding => [binding.local, binding]));
|
|
197
235
|
for (const { element } of cu.statements) {
|
|
198
236
|
if (element.kind === javascript_1.JS.Kind.Import) {
|
|
@@ -256,7 +294,7 @@ function sourceFacts(cu) {
|
|
|
256
294
|
return facts;
|
|
257
295
|
});
|
|
258
296
|
}
|
|
259
|
-
function storeFacts(cu, modules) {
|
|
297
|
+
function storeFacts(cu, modules, resolve) {
|
|
260
298
|
return __awaiter(this, void 0, void 0, function* () {
|
|
261
299
|
const stores = [];
|
|
262
300
|
const functions = (0, redux_stores_1.storeFunctions)(cu);
|
|
@@ -268,10 +306,12 @@ function storeFacts(cu, modules) {
|
|
|
268
306
|
visitMethodInvocation: { get: () => super.visitMethodInvocation }
|
|
269
307
|
});
|
|
270
308
|
return __awaiter(this, void 0, void 0, function* () {
|
|
271
|
-
const store = (0, redux_stores_1.storeFindings)(call, cu, modules, functions);
|
|
309
|
+
const store = (0, redux_stores_1.storeFindings)(call, cu, modules, functions, resolve);
|
|
272
310
|
if (store) {
|
|
273
|
-
|
|
274
|
-
|
|
311
|
+
const name = storeNameAt(this.cursor);
|
|
312
|
+
const own = yield usesInSource(cu, name, call);
|
|
313
|
+
stores.push({ path: cu.sourcePath, api: store.api, store: name, findings: store.findings,
|
|
314
|
+
slices: store.slices, exportedAs: exportedStore(cu, this.cursor), reducerStore: store.reducerStore, usedInSource: own.other, calledInSource: own.called });
|
|
275
315
|
}
|
|
276
316
|
return _super.visitMethodInvocation.call(this, call, ctx);
|
|
277
317
|
});
|
|
@@ -312,13 +352,13 @@ function dispatcherCount(cu, followed, name) {
|
|
|
312
352
|
return count;
|
|
313
353
|
}
|
|
314
354
|
function classify(identifier, binding, cursor, followed, dispatchers, declared) {
|
|
315
|
-
var _a, _b;
|
|
355
|
+
var _a, _b, _c, _d, _e;
|
|
316
356
|
const trees = ancestors(cursor);
|
|
317
357
|
if (trees.some(tree => tree.kind === javascript_1.JS.Kind.Import))
|
|
318
358
|
return undefined;
|
|
319
359
|
const parent = parentTree(cursor);
|
|
320
360
|
const local = identifier.simpleName;
|
|
321
|
-
const other = (detail) => ({ local, kind: "other", detail });
|
|
361
|
+
const other = (detail, action) => (Object.assign({ local, kind: "other", detail }, action ? { action } : {}));
|
|
322
362
|
if ((parent === null || parent === void 0 ? void 0 : parent.kind) === java_1.J.Kind.FieldAccess && parent.name.element === identifier)
|
|
323
363
|
return undefined;
|
|
324
364
|
if ((parent === null || parent === void 0 ? void 0 : parent.kind) === java_1.J.Kind.MethodInvocation && parent.name === identifier && parent.select)
|
|
@@ -342,19 +382,26 @@ function classify(identifier, binding, cursor, followed, dispatchers, declared)
|
|
|
342
382
|
}
|
|
343
383
|
if (binding.module === exports.REACT_REDUX) {
|
|
344
384
|
if ((parent === null || parent === void 0 ? void 0 : parent.kind) !== java_1.J.Kind.MethodInvocation || parent.name !== identifier)
|
|
345
|
-
return other(`\`${binding.name}\` from react-redux
|
|
385
|
+
return other(`\`${binding.name}\` from react-redux`, (_b = REACT_REDUX_VALUE_ACTIONS[binding.name]) !== null && _b !== void 0 ? _b : (binding.name.startsWith("use") ?
|
|
386
|
+
`\`${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.` :
|
|
387
|
+
`\`${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.`));
|
|
346
388
|
const call = parent;
|
|
389
|
+
if (binding.name === "connect")
|
|
390
|
+
return (_c = connectUse(call, other)) !== null && _c !== void 0 ? _c : { local, kind: "connect" };
|
|
347
391
|
if (binding.name === "useSelector") {
|
|
348
392
|
const args = argumentsOf(call);
|
|
349
393
|
const read = args.length === 1 && !call.typeParameters ? selectorRead(args[0]) : undefined;
|
|
350
|
-
|
|
394
|
+
if (read)
|
|
395
|
+
return { local, kind: "selector", key: read.key };
|
|
396
|
+
const selector = args.length === 1 && !call.typeParameters && (0, java_1.isIdentifier)(args[0]) ? args[0].simpleName : undefined;
|
|
397
|
+
return Object.assign(Object.assign({}, other("a selector that isn't a read of one slice's state")), { reason: "SELECTOR", selector, anySelector: args.length === 1 && !call.typeParameters });
|
|
351
398
|
}
|
|
352
399
|
if (binding.name === "useDispatch") {
|
|
353
400
|
const variable = parentTree(cursorOf(cursor, call));
|
|
354
401
|
return (variable === null || variable === void 0 ? void 0 : variable.kind) === java_1.J.Kind.NamedVariable && dispatchers.has(variable.name.simpleName)
|
|
355
|
-
? { local, kind: "dispatcher" } : other("`useDispatch` not held in a variable of its own");
|
|
402
|
+
? { local, kind: "dispatcher" } : other("`useDispatch` not held in a variable of its own", "Hold the dispatcher in a `const` of its own, such as `const dispatch = useDispatch()`, and dispatch through it.");
|
|
356
403
|
}
|
|
357
|
-
return other(`\`${binding.name}\` from react-redux`);
|
|
404
|
+
return other(`\`${binding.name}\` from react-redux`, (_d = REACT_REDUX_CALL_ACTIONS[binding.name]) !== null && _d !== void 0 ? _d : `The recipe converts only \`connect\`, \`useSelector\`, \`useDispatch\` and \`<Provider>\` of react-redux: replace \`${binding.name}(...)\` by hand.`);
|
|
358
405
|
}
|
|
359
406
|
if ((parent === null || parent === void 0 ? void 0 : parent.kind) === java_1.J.Kind.MethodInvocation && parent.name === identifier) {
|
|
360
407
|
const action = parent;
|
|
@@ -370,16 +417,17 @@ function classify(identifier, binding, cursor, followed, dispatchers, declared)
|
|
|
370
417
|
return Object.assign(Object.assign({}, other("the dispatch's result is used")), { reason: "DISPATCH" });
|
|
371
418
|
return { local, kind: "dispatched", via };
|
|
372
419
|
}
|
|
373
|
-
if ((parent === null || parent === void 0 ? void 0 : parent.kind) === java_1.J.Kind.MethodInvocation && ((
|
|
420
|
+
if ((parent === null || parent === void 0 ? void 0 : parent.kind) === java_1.J.Kind.MethodInvocation && ((_e = parent.select) === null || _e === void 0 ? void 0 : _e.element) === identifier) {
|
|
374
421
|
const call = parent;
|
|
375
422
|
const outer = parentTree(cursorOf(cursor, call));
|
|
376
423
|
const read = getStateRead(call, outer);
|
|
377
424
|
if (read)
|
|
378
425
|
return { local, kind: "getState", key: read.key };
|
|
379
|
-
|
|
380
|
-
|
|
426
|
+
const member = call.name.simpleName;
|
|
427
|
+
if (member === "dispatch" && dispatchedAction(call)) {
|
|
428
|
+
return isStatement(call, outer) ? { local, kind: "storeDispatch", member } : Object.assign(Object.assign({}, other("the dispatch's result is used")), { reason: "DISPATCH", member });
|
|
381
429
|
}
|
|
382
|
-
return other(`\`${local}.${
|
|
430
|
+
return Object.assign(Object.assign({}, other(`\`${local}.${member}\``)), { member });
|
|
383
431
|
}
|
|
384
432
|
if ((parent === null || parent === void 0 ? void 0 : parent.kind) === javascript_1.JS.Kind.JsxEmbeddedExpression) {
|
|
385
433
|
const attribute = trees[2];
|
|
@@ -394,6 +442,22 @@ function classify(identifier, binding, cursor, followed, dispatchers, declared)
|
|
|
394
442
|
}
|
|
395
443
|
return other("referenced");
|
|
396
444
|
}
|
|
445
|
+
function connectUse(call, other) {
|
|
446
|
+
const args = argumentsOf(call);
|
|
447
|
+
const absent = (node) => !node ||
|
|
448
|
+
node.kind === java_1.J.Kind.Literal && node.valueSource === "null" ||
|
|
449
|
+
(0, java_1.isIdentifier)(node) && node.simpleName === "undefined";
|
|
450
|
+
if (args.length > 4 || !absent(args[2]))
|
|
451
|
+
return other("`connect` with `mergeProps`", "Merge the props in the wrapped component rather than with `mergeProps`, or convert by hand.");
|
|
452
|
+
if (args.length === 4 && !absent(args[3])) {
|
|
453
|
+
const options = (0, redux_stores_1.objectProperties)(args[3]);
|
|
454
|
+
const forwardRef = options === null || options === void 0 ? void 0 : options.get("forwardRef");
|
|
455
|
+
if (!options || options.size !== 1 || (forwardRef === null || forwardRef === void 0 ? void 0 : forwardRef.kind) !== java_1.J.Kind.Literal || typeof forwardRef.value !== "boolean") {
|
|
456
|
+
return other("`connect` with options other than `forwardRef`", "Pass `connect` no options, or only `{forwardRef: true}` or `{forwardRef: false}`; others, such as `context`, `areStatesEqual` or `pure`, are converted by hand.");
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
return undefined;
|
|
460
|
+
}
|
|
397
461
|
function dispatcherUse(identifier, cursor, followed, bindings) {
|
|
398
462
|
const parent = parentTree(cursor);
|
|
399
463
|
const local = identifier.simpleName;
|
|
@@ -413,19 +477,31 @@ function dispatcherUse(identifier, cursor, followed, bindings) {
|
|
|
413
477
|
}
|
|
414
478
|
return { local, kind: "other", detail: "the dispatcher is used other than to dispatch", reason: "DISPATCH" };
|
|
415
479
|
}
|
|
480
|
+
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.";
|
|
481
|
+
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`. " +
|
|
482
|
+
"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.";
|
|
416
483
|
function providerUse(tag, followed, local) {
|
|
417
484
|
var _a;
|
|
418
|
-
const other = (detail) => ({ local, kind: "other", detail });
|
|
485
|
+
const other = (detail, action) => (Object.assign({ local, kind: "other", detail }, action ? { action } : {}));
|
|
419
486
|
const attributes = tag.attributes.map(a => a.element);
|
|
420
|
-
if (attributes.length !== 1 || attributes[0].kind !== javascript_1.JS.Kind.JsxAttribute)
|
|
421
|
-
return other("`Provider` with props other than `store`");
|
|
487
|
+
if (attributes.length !== 1 || attributes[0].kind !== javascript_1.JS.Kind.JsxAttribute) {
|
|
488
|
+
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.");
|
|
489
|
+
}
|
|
422
490
|
const value = (_a = attributes[0].value) === null || _a === void 0 ? void 0 : _a.element;
|
|
423
491
|
const expression = (value === null || value === void 0 ? void 0 : value.kind) === javascript_1.JS.Kind.JsxEmbeddedExpression ? value.expression.element : undefined;
|
|
424
492
|
if (!(0, java_1.isIdentifier)(expression) || !followed.has(expression.simpleName))
|
|
425
|
-
return other("`Provider` of a store that isn't imported");
|
|
426
|
-
if (!providerChild(tag))
|
|
427
|
-
return other("`Provider` without
|
|
428
|
-
return { local, kind: "providerTag" };
|
|
493
|
+
return other("`Provider` of a store that isn't imported", PROVIDER_NOT_IMPORTED);
|
|
494
|
+
if (!providerChild(tag) && !providerChildren(tag))
|
|
495
|
+
return other("`Provider` without children");
|
|
496
|
+
return { local, kind: "providerTag", via: expression.simpleName };
|
|
497
|
+
}
|
|
498
|
+
function providerChildren(tag) {
|
|
499
|
+
var _a;
|
|
500
|
+
return !tag.selfClosing && ((_a = tag.children) !== null && _a !== void 0 ? _a : []).some(child => {
|
|
501
|
+
var _a;
|
|
502
|
+
return !(child.kind === java_1.J.Kind.Literal && String((_a = child.value) !== null && _a !== void 0 ? _a : "").trim() === "") &&
|
|
503
|
+
child.kind !== java_1.J.Kind.Empty;
|
|
504
|
+
});
|
|
429
505
|
}
|
|
430
506
|
function providerChild(tag) {
|
|
431
507
|
var _a;
|
|
@@ -442,6 +518,56 @@ function cursorOf(cursor, node) {
|
|
|
442
518
|
return c;
|
|
443
519
|
return cursor;
|
|
444
520
|
}
|
|
521
|
+
exports.STORE_MEMBERS = new Set(["getState", "dispatch", "subscribe"]);
|
|
522
|
+
function usesInSource(cu, name, call) {
|
|
523
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
524
|
+
const result = { other: false, called: new Set() };
|
|
525
|
+
const statements = cu.statements.map(s => s.element).flatMap(statement => {
|
|
526
|
+
if (statement.kind === javascript_1.JS.Kind.Import)
|
|
527
|
+
return [];
|
|
528
|
+
if (exportList(statement).some(entry => entry.local === name))
|
|
529
|
+
return [];
|
|
530
|
+
if (statement.kind === javascript_1.JS.Kind.ExportAssignment && (0, java_1.isIdentifier)(statement.expression.element) &&
|
|
531
|
+
statement.expression.element.simpleName === name)
|
|
532
|
+
return [];
|
|
533
|
+
if (statement.kind === java_1.J.Kind.VariableDeclarations && statement.variables.some(v => { var _a; return ((_a = v.element.initializer) === null || _a === void 0 ? void 0 : _a.element) === call; })) {
|
|
534
|
+
return [call];
|
|
535
|
+
}
|
|
536
|
+
return [statement];
|
|
537
|
+
});
|
|
538
|
+
for (const statement of statements) {
|
|
539
|
+
if (![...(0, javascript_1.namesReferencedWithin)(statement)].includes(name))
|
|
540
|
+
continue;
|
|
541
|
+
yield new class extends javascript_1.JavaScriptVisitor {
|
|
542
|
+
visitIdentifier(identifier, ctx) {
|
|
543
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
544
|
+
var _a;
|
|
545
|
+
if (identifier.simpleName !== name)
|
|
546
|
+
return identifier;
|
|
547
|
+
const parent = parentTree(this.cursor);
|
|
548
|
+
if ((parent === null || parent === void 0 ? void 0 : parent.kind) === java_1.J.Kind.FieldAccess && parent.name.element === identifier)
|
|
549
|
+
return identifier;
|
|
550
|
+
if ((parent === null || parent === void 0 ? void 0 : parent.kind) === java_1.J.Kind.MethodInvocation && parent.name === identifier) {
|
|
551
|
+
if (parent.select)
|
|
552
|
+
return identifier;
|
|
553
|
+
}
|
|
554
|
+
else if ((parent === null || parent === void 0 ? void 0 : parent.kind) === java_1.J.Kind.MethodInvocation && ((_a = parent.select) === null || _a === void 0 ? void 0 : _a.element) === identifier &&
|
|
555
|
+
exports.STORE_MEMBERS.has(parent.name.simpleName)) {
|
|
556
|
+
result.called.add(parent.name.simpleName);
|
|
557
|
+
return identifier;
|
|
558
|
+
}
|
|
559
|
+
if ((parent === null || parent === void 0 ? void 0 : parent.kind) === javascript_1.JS.Kind.PropertyAssignment && parent.name.element === identifier &&
|
|
560
|
+
parent.initializer)
|
|
561
|
+
return identifier;
|
|
562
|
+
result.other = true;
|
|
563
|
+
return identifier;
|
|
564
|
+
});
|
|
565
|
+
}
|
|
566
|
+
}().visit(statement, new rewrite_1.ExecutionContext());
|
|
567
|
+
}
|
|
568
|
+
return result;
|
|
569
|
+
});
|
|
570
|
+
}
|
|
445
571
|
function storeNameAt(cursor) {
|
|
446
572
|
for (let c = cursor.parent; c; c = c.parent) {
|
|
447
573
|
const node = c.value;
|
|
@@ -472,11 +598,36 @@ function exportedStore(cu, cursor) {
|
|
|
472
598
|
return name;
|
|
473
599
|
const exported = cu.statements.map(s => s.element).find(s => s.kind === javascript_1.JS.Kind.ExportAssignment &&
|
|
474
600
|
(0, java_1.isIdentifier)(s.expression.element) && s.expression.element.simpleName === name);
|
|
475
|
-
|
|
601
|
+
if (exported)
|
|
602
|
+
return "default";
|
|
603
|
+
const listed = cu.statements.flatMap(s => exportList(s.element)).filter(entry => entry.local === name);
|
|
604
|
+
return listed.length === 1 ? listed[0].exported : undefined;
|
|
605
|
+
}
|
|
606
|
+
function exportList(statement) {
|
|
607
|
+
var _a;
|
|
608
|
+
if (statement.kind !== javascript_1.JS.Kind.ExportDeclaration)
|
|
609
|
+
return [];
|
|
610
|
+
const declaration = statement;
|
|
611
|
+
if (declaration.moduleSpecifier || declaration.typeOnly.element || ((_a = declaration.exportClause) === null || _a === void 0 ? void 0 : _a.kind) !== javascript_1.JS.Kind.NamedExports)
|
|
612
|
+
return [];
|
|
613
|
+
return declaration.exportClause.elements.elements.flatMap(({ element }) => {
|
|
614
|
+
if (element.kind !== javascript_1.JS.Kind.ExportSpecifier || element.typeOnly.element)
|
|
615
|
+
return [];
|
|
616
|
+
const specifier = element.specifier;
|
|
617
|
+
if ((0, java_1.isIdentifier)(specifier))
|
|
618
|
+
return [{ local: specifier.simpleName, exported: specifier.simpleName }];
|
|
619
|
+
if (specifier.kind !== javascript_1.JS.Kind.Alias)
|
|
620
|
+
return [];
|
|
621
|
+
const local = specifier.propertyName.element, alias = specifier.alias;
|
|
622
|
+
return (0, java_1.isIdentifier)(local) && (0, java_1.isIdentifier)(alias) ? [{ local: local.simpleName, exported: alias.simpleName }] : [];
|
|
623
|
+
});
|
|
476
624
|
}
|
|
625
|
+
exports.SELECTOR_HOOK = "useAppStoreSelector";
|
|
626
|
+
exports.STORE_LISTENERS = "storeListeners";
|
|
627
|
+
exports.REDUCER_STORE_HOOK = "useAppStore";
|
|
477
628
|
const hookName = (key) => `use${key.charAt(0).toUpperCase()}${key.slice(1)}Store`;
|
|
478
629
|
exports.hookName = hookName;
|
|
479
|
-
function plan(facts, sliceSources, dependencies, scope = () => true) {
|
|
630
|
+
function plan(facts, sliceSources, dependencies, scope = () => true, wholeStores = true, renderer = () => "react-dom", selectsOnEachRead = () => false, resolveFrom = (from, specifier) => (0, module_imports_1.resolveRelative)(from, specifier, facts)) {
|
|
480
631
|
var _a;
|
|
481
632
|
const stores = [...facts.values()].filter(source => scope(source.path)).flatMap(source => source.stores);
|
|
482
633
|
const result = { slices: new Map(), sources: new Set(), blocked: new Map() };
|
|
@@ -489,7 +640,7 @@ function plan(facts, sliceSources, dependencies, scope = () => true) {
|
|
|
489
640
|
}
|
|
490
641
|
const store = stores[0];
|
|
491
642
|
const findings = [...store.findings];
|
|
492
|
-
const block = (reason, detail) => findings.push({ reason, detail });
|
|
643
|
+
const block = (reason, detail, action) => findings.push(Object.assign({ reason, detail }, action ? { action } : {}));
|
|
493
644
|
const held = new Map();
|
|
494
645
|
const handled = new Map();
|
|
495
646
|
if (findings.length === 0) {
|
|
@@ -540,7 +691,7 @@ function plan(facts, sliceSources, dependencies, scope = () => true) {
|
|
|
540
691
|
const slicePaths = new Map([...result.slices.values()].map(slice => [slice.path, slice]));
|
|
541
692
|
const sources = new Set([store.path, ...slicePaths.keys()]);
|
|
542
693
|
for (const source of facts.values()) {
|
|
543
|
-
const resolve = (module) => (
|
|
694
|
+
const resolve = (module) => resolveFrom(source.path, module);
|
|
544
695
|
const touches = (module) => {
|
|
545
696
|
const path = resolve(module);
|
|
546
697
|
return path === store.path || (path !== undefined && slicePaths.has(path));
|
|
@@ -584,8 +735,10 @@ function plan(facts, sliceSources, dependencies, scope = () => true) {
|
|
|
584
735
|
continue;
|
|
585
736
|
changes = true;
|
|
586
737
|
const where = `${use.local} in ${source.path}`;
|
|
738
|
+
if (use.kind === "providerTag" && !provides(source, use, store, resolve))
|
|
739
|
+
block("REACT_REDUX_USE", `${where}: \`Provider\` of \`${use.via}\`, which isn't the store`, PROVIDER_OTHER_STORE);
|
|
587
740
|
if (use.kind === "other") {
|
|
588
|
-
block((_a = use.reason) !== null && _a !== void 0 ? _a : (isStore ? "STORE_USE" : slice ? "SLICE_USE" : !binding ? "DISPATCH" : isReactRedux ? "REACT_REDUX_USE" : "STORE_USE"), `${where}: ${use.detail}
|
|
741
|
+
block((_a = use.reason) !== null && _a !== void 0 ? _a : (isStore ? "STORE_USE" : slice ? "SLICE_USE" : !binding ? "DISPATCH" : isReactRedux ? "REACT_REDUX_USE" : "STORE_USE"), `${where}: ${use.detail}`, use.action);
|
|
589
742
|
}
|
|
590
743
|
else if (use.kind === "selector" || use.kind === "getState") {
|
|
591
744
|
if (!result.slices.has(use.key))
|
|
@@ -598,6 +751,9 @@ function plan(facts, sliceSources, dependencies, scope = () => true) {
|
|
|
598
751
|
if (via && !(resolve(via.module) === store.path && via.name === store.exportedAs))
|
|
599
752
|
block("DISPATCH", `${where} is dispatched through \`${use.via}\``);
|
|
600
753
|
}
|
|
754
|
+
else if (use.kind === "connect") {
|
|
755
|
+
block("REACT_REDUX_USE", `${where}: \`connect\` from react-redux`, connectAction(wholeStores));
|
|
756
|
+
}
|
|
601
757
|
else if ((use.kind === "provider" || use.kind === "storeDispatch") && !isStore) {
|
|
602
758
|
block("STORE_USE", `${where} isn't the store`);
|
|
603
759
|
}
|
|
@@ -626,6 +782,8 @@ function plan(facts, sliceSources, dependencies, scope = () => true) {
|
|
|
626
782
|
}
|
|
627
783
|
if ([store.path, ...slicePaths.keys()].some(path => /\.(?:ts|tsx|mts|cts)$/.test(path)))
|
|
628
784
|
block("TYPESCRIPT", store.path);
|
|
785
|
+
if (findings.length > 0 && store.reducerStore && wholeStores)
|
|
786
|
+
return reducerPlan(facts, sliceSources, store, dependencies, scope, renderer, selectsOnEachRead, resolveFrom);
|
|
629
787
|
if (findings.length > 0) {
|
|
630
788
|
result.blocked.set(store, dedupe(findings));
|
|
631
789
|
result.slices.clear();
|
|
@@ -635,10 +793,141 @@ function plan(facts, sliceSources, dependencies, scope = () => true) {
|
|
|
635
793
|
result.sources = sources;
|
|
636
794
|
return result;
|
|
637
795
|
}
|
|
638
|
-
function
|
|
796
|
+
function reducerPlan(facts, sliceSources, store, dependencies, scope, renderer, selectsOnEachRead, resolveFrom) {
|
|
797
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j;
|
|
798
|
+
const findings = [];
|
|
799
|
+
const block = (reason, detail, action) => findings.push(Object.assign({ reason, detail }, action ? { action } : {}));
|
|
800
|
+
const hook = exports.REDUCER_STORE_HOOK;
|
|
801
|
+
const source = facts.get(store.path);
|
|
802
|
+
const names = new Set([...(_b = (_a = sliceSources.get(store.path)) === null || _a === void 0 ? void 0 : _a.names) !== null && _b !== void 0 ? _b : [], ...(_c = source === null || source === void 0 ? void 0 : source.declared) !== null && _c !== void 0 ? _c : [], ...(_d = source === null || source === void 0 ? void 0 : source.bindings.map(b => b.local)) !== null && _d !== void 0 ? _d : []]);
|
|
803
|
+
const reducer = (_e = ["rootReducer", "appReducer", "storeReducer"].find(name => !names.has(name))) !== null && _e !== void 0 ? _e : "rootReducer";
|
|
804
|
+
if (!store.exportedAs)
|
|
805
|
+
block("STORE_NOT_MODULE_LEVEL", store.store);
|
|
806
|
+
if (store.usedInSource)
|
|
807
|
+
block("STORE_USE", `\`${store.store}\` is used in its own source`);
|
|
808
|
+
const dependency = dependencies(store.path, false);
|
|
809
|
+
if (dependency)
|
|
810
|
+
findings.push(dependency);
|
|
811
|
+
for (const name of [hook, reducer, "create"])
|
|
812
|
+
if (names.has(name))
|
|
813
|
+
block("NAME_TAKEN", `${name} in ${store.path}`);
|
|
814
|
+
const storeSource = facts.get(store.path);
|
|
815
|
+
const rtkCombine = storeSource === null || storeSource === void 0 ? void 0 : storeSource.bindings.some(b => b.local === "combineReducers" && b.module === redux_stores_1.RTK && b.name === "combineReducers");
|
|
816
|
+
if (store.reducerStore === "combined" && names.has("combineReducers") && !rtkCombine)
|
|
817
|
+
block("NAME_TAKEN", `combineReducers in ${store.path}`);
|
|
818
|
+
const sources = new Set([store.path]);
|
|
819
|
+
let connects = false;
|
|
820
|
+
let selects = false;
|
|
821
|
+
let subscribes = (_g = (_f = store.calledInSource) === null || _f === void 0 ? void 0 : _f.has("subscribe")) !== null && _g !== void 0 ? _g : false;
|
|
822
|
+
for (const source of facts.values()) {
|
|
823
|
+
const resolve = (module) => resolveFrom(source.path, module);
|
|
824
|
+
if (!scope(source.path)) {
|
|
825
|
+
for (const binding of source.bindings) {
|
|
826
|
+
if (binding.module !== exports.REACT_REDUX && resolve(binding.module) === store.path)
|
|
827
|
+
block("STORE_USE", `${binding.local} in ${source.path}, of another package`);
|
|
828
|
+
}
|
|
829
|
+
for (const { module, detail } of source.unfollowed) {
|
|
830
|
+
if (resolve(module) === store.path)
|
|
831
|
+
block("UNFOLLOWED_IMPORT", `${detail} of ${module} in ${source.path}, of another package`);
|
|
832
|
+
}
|
|
833
|
+
continue;
|
|
834
|
+
}
|
|
835
|
+
for (const { module, detail } of source.unfollowed) {
|
|
836
|
+
if (module === exports.REACT_REDUX || resolve(module) === store.path)
|
|
837
|
+
block("UNFOLLOWED_IMPORT", `${detail} of ${module} in ${source.path}`);
|
|
838
|
+
}
|
|
839
|
+
let changes = false;
|
|
840
|
+
for (const use of source.uses) {
|
|
841
|
+
const binding = source.bindings.find(b => b.local === use.local);
|
|
842
|
+
if (!binding)
|
|
843
|
+
continue;
|
|
844
|
+
const where = `${use.local} in ${source.path}`;
|
|
845
|
+
if (binding.module === exports.REACT_REDUX) {
|
|
846
|
+
changes = true;
|
|
847
|
+
if (use.kind === "connect")
|
|
848
|
+
connects = true;
|
|
849
|
+
if (use.kind === "providerTag" && !provides(source, use, store, resolve))
|
|
850
|
+
block("REACT_REDUX_USE", `${where}: \`Provider\` of \`${use.via}\`, which isn't the store`, PROVIDER_OTHER_STORE);
|
|
851
|
+
if (use.kind === "other" && use.selector && readSelector(source, use.selector, facts, resolveFrom))
|
|
852
|
+
continue;
|
|
853
|
+
if (use.kind === "other" && use.anySelector) {
|
|
854
|
+
if (selectsOnEachRead(store.path))
|
|
855
|
+
selects = true;
|
|
856
|
+
continue;
|
|
857
|
+
}
|
|
858
|
+
if (use.kind === "other")
|
|
859
|
+
block((_h = use.reason) !== null && _h !== void 0 ? _h : "REACT_REDUX_USE", `${where}: ${use.detail}`, use.action);
|
|
860
|
+
continue;
|
|
861
|
+
}
|
|
862
|
+
const path = resolve(binding.module);
|
|
863
|
+
if (path !== store.path || binding.name !== store.exportedAs)
|
|
864
|
+
continue;
|
|
865
|
+
if (use.kind === "provider")
|
|
866
|
+
changes = true;
|
|
867
|
+
else if (use.kind === "other" && use.member === "subscribe")
|
|
868
|
+
subscribes = true;
|
|
869
|
+
else if (use.kind === "other" && use.member !== "getState" && use.member !== "dispatch")
|
|
870
|
+
block(use.reason === "DISPATCH" ? "STORE_USE" : (_j = use.reason) !== null && _j !== void 0 ? _j : "STORE_USE", `${where}: ${use.detail}`);
|
|
871
|
+
}
|
|
872
|
+
if (changes && source.path !== store.path) {
|
|
873
|
+
if (source.typescript)
|
|
874
|
+
block("TYPESCRIPT", source.path);
|
|
875
|
+
if (source.bindings.some(b => b.local === hook) || source.declared.has(hook))
|
|
876
|
+
block("NAME_TAKEN", `${hook} in ${source.path}`);
|
|
877
|
+
sources.add(source.path);
|
|
878
|
+
}
|
|
879
|
+
}
|
|
880
|
+
if (/\.(?:ts|tsx|mts|cts)$/.test(store.path))
|
|
881
|
+
block("TYPESCRIPT", store.path);
|
|
882
|
+
const module = connects ? renderer(store.path) : undefined;
|
|
883
|
+
if (connects && !module)
|
|
884
|
+
block("REACT_REDUX_USE", `connect in a package that declares neither react-dom nor react-native`);
|
|
885
|
+
if (connects && storeSource) {
|
|
886
|
+
const helpers = [["connect", ""], ...["createElement", "forwardRef", "memo", "useRef"].map(name => [name, "react"]),
|
|
887
|
+
["unstable_batchedUpdates", module !== null && module !== void 0 ? module : ""]];
|
|
888
|
+
for (const [name, from] of helpers) {
|
|
889
|
+
const bound = storeSource.bindings.find(b => b.local === name);
|
|
890
|
+
if (storeSource.declared.has(name) || bound && (bound.module !== from || bound.name !== name))
|
|
891
|
+
block("NAME_TAKEN", `${name} in ${store.path}`);
|
|
892
|
+
}
|
|
893
|
+
}
|
|
894
|
+
if (selects && storeSource) {
|
|
895
|
+
for (const name of [exports.SELECTOR_HOOK, "useRef", "useSyncExternalStore"]) {
|
|
896
|
+
const bound = storeSource.bindings.find(b => b.local === name);
|
|
897
|
+
if (storeSource.declared.has(name) || bound && (bound.module !== (name === exports.SELECTOR_HOOK ? "" : "react") || bound.name !== name))
|
|
898
|
+
block("NAME_TAKEN", `${name} in ${store.path}`);
|
|
899
|
+
}
|
|
900
|
+
for (const path of sources) {
|
|
901
|
+
const source = facts.get(path);
|
|
902
|
+
if (source && path !== store.path && (source.declared.has(exports.SELECTOR_HOOK) || source.bindings.some(b => b.local === exports.SELECTOR_HOOK)))
|
|
903
|
+
block("NAME_TAKEN", `${exports.SELECTOR_HOOK} in ${path}`);
|
|
904
|
+
}
|
|
905
|
+
}
|
|
906
|
+
if (subscribes && names.has(exports.STORE_LISTENERS))
|
|
907
|
+
block("NAME_TAKEN", `${exports.STORE_LISTENERS} in ${store.path}`);
|
|
908
|
+
const result = { slices: new Map(), sources: new Set(), blocked: new Map() };
|
|
909
|
+
if (findings.length > 0) {
|
|
910
|
+
result.blocked.set(store, dedupe(findings));
|
|
911
|
+
return result;
|
|
912
|
+
}
|
|
913
|
+
return Object.assign(Object.assign({}, result), { store, sources, reducerStore: Object.assign(Object.assign(Object.assign({ hook, reducer }, module ? { renderer: module } : {}), selects ? { selectorHook: exports.SELECTOR_HOOK } : {}), subscribes ? { listeners: exports.STORE_LISTENERS } : {}) });
|
|
914
|
+
}
|
|
915
|
+
function provides(source, use, store, resolve) {
|
|
916
|
+
const binding = source.bindings.find(b => b.local === use.via);
|
|
917
|
+
return !!binding && binding.module !== exports.REACT_REDUX && resolve(binding.module) === store.path && binding.name === store.exportedAs;
|
|
918
|
+
}
|
|
919
|
+
function readSelector(source, name, facts, resolve = (from, specifier) => (0, module_imports_1.resolveRelative)(from, specifier, facts)) {
|
|
920
|
+
var _a, _b;
|
|
921
|
+
const binding = source.bindings.find(b => b.local === name);
|
|
922
|
+
if (!binding)
|
|
923
|
+
return source.readSelectors.local.has(name);
|
|
924
|
+
const path = resolve(source.path, binding.module);
|
|
925
|
+
return path !== undefined && ((_b = (_a = facts.get(path)) === null || _a === void 0 ? void 0 : _a.readSelectors.exported.has(binding.name)) !== null && _b !== void 0 ? _b : false);
|
|
926
|
+
}
|
|
927
|
+
function planPackages(facts, sliceSources, packageOf, dependencies, wholeStores = true, renderer, selectsOnEachRead, resolve) {
|
|
639
928
|
const root = (path) => { var _a; return (_a = packageOf(path)) !== null && _a !== void 0 ? _a : ""; };
|
|
640
929
|
const roots = new Set([...facts.values()].filter(source => source.stores.length > 0).map(source => root(source.path)));
|
|
641
|
-
return [...roots].map(packageRoot => plan(facts, sliceSources, dependencies, path => root(path) === packageRoot));
|
|
930
|
+
return [...roots].map(packageRoot => plan(facts, sliceSources, dependencies, path => root(path) === packageRoot, wholeStores, renderer, selectsOnEachRead, resolve));
|
|
642
931
|
}
|
|
643
932
|
function dedupe(findings) {
|
|
644
933
|
const seen = new Set();
|