@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
|
@@ -1,4 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
2
8
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
9
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
10
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -9,7 +15,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
15
|
});
|
|
10
16
|
};
|
|
11
17
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.ReduxToZustand = exports.AddImmer = exports.AddZustand4 = exports.AddZustand = void 0;
|
|
18
|
+
exports.SELECTOR = exports.ReduxToZustand = exports.AddImmer = exports.AddZustand4 = exports.AddZustand = void 0;
|
|
19
|
+
exports.connectSource = connectSource;
|
|
13
20
|
const rewrite_1 = require("@openrewrite/rewrite");
|
|
14
21
|
const javascript_1 = require("@openrewrite/rewrite/javascript");
|
|
15
22
|
const java_1 = require("@openrewrite/rewrite/java");
|
|
@@ -20,17 +27,23 @@ const rewrite_2 = require("@openrewrite/rewrite");
|
|
|
20
27
|
const java_recipe_proxy_1 = require("../util/java-recipe-proxy");
|
|
21
28
|
const module_imports_1 = require("../util/module-imports");
|
|
22
29
|
const enzyme_to_react_testing_library_1 = require("../testing/enzyme-to-react-testing-library");
|
|
30
|
+
const entry_store_1 = require("./entry-store");
|
|
23
31
|
const redux_stores_1 = require("./redux-stores");
|
|
24
32
|
const zustand_plan_1 = require("./zustand-plan");
|
|
25
33
|
const ZUSTAND = "zustand";
|
|
26
34
|
const IMMER = "immer";
|
|
27
35
|
const ZUSTAND_5 = {
|
|
28
|
-
packageName: ZUSTAND, version: "^5.0.8", scope: "dependencies",
|
|
29
|
-
onlyIfResolving: ["react@>=18.0.0"]
|
|
36
|
+
packageName: ZUSTAND, version: "^5.0.8", scope: "dependencies", onlyIfResolving: ["react@>=18.0.0"]
|
|
30
37
|
};
|
|
31
38
|
const ZUSTAND_4 = Object.assign(Object.assign({}, ZUSTAND_5), { version: "^4.5.7", onlyIfResolving: ["react@>=16.8.0 <18"] });
|
|
32
39
|
const IMMER_10 = Object.assign(Object.assign({}, ZUSTAND_5), { packageName: IMMER, version: "^10.1.3", onlyIfResolving: ["react@>=16.8.0"] });
|
|
33
40
|
const LOCKFILE = /(^|\/)(package-lock\.json|npm-shrinkwrap\.json|yarn\.lock|pnpm-lock\.yaml|bun\.lock)$/;
|
|
41
|
+
function resolverOf(scanned) {
|
|
42
|
+
return (from, specifier) => (0, module_imports_1.resolveModule)(from, specifier, scanned.sources, scanned.configs);
|
|
43
|
+
}
|
|
44
|
+
function followsImport(module) {
|
|
45
|
+
return module === zustand_plan_1.REACT_REDUX || module.startsWith(".") || (0, module_imports_1.isAliasSpecifier)(module);
|
|
46
|
+
}
|
|
34
47
|
class ZustandDependency extends java_recipe_proxy_1.JavaDependencyRecipe {
|
|
35
48
|
constructor(addition, forecast) {
|
|
36
49
|
super("org.openrewrite.javascript.AddDependency", addition);
|
|
@@ -39,6 +52,9 @@ class ZustandDependency extends java_recipe_proxy_1.JavaDependencyRecipe {
|
|
|
39
52
|
this.landed = new Set();
|
|
40
53
|
this.held = [];
|
|
41
54
|
this.locks = [];
|
|
55
|
+
this.owned = new Set();
|
|
56
|
+
this.workspaces = new Map();
|
|
57
|
+
this.pending = [];
|
|
42
58
|
this.unfit = new Set();
|
|
43
59
|
}
|
|
44
60
|
initialValue() {
|
|
@@ -47,6 +63,9 @@ class ZustandDependency extends java_recipe_proxy_1.JavaDependencyRecipe {
|
|
|
47
63
|
this.unfit.clear();
|
|
48
64
|
this.held.length = 0;
|
|
49
65
|
this.locks.length = 0;
|
|
66
|
+
this.owned.clear();
|
|
67
|
+
this.workspaces.clear();
|
|
68
|
+
this.pending.length = 0;
|
|
50
69
|
(_a = this.forecast.refused) === null || _a === void 0 ? void 0 : _a.clear();
|
|
51
70
|
return super.initialValue();
|
|
52
71
|
}
|
|
@@ -55,6 +74,8 @@ class ZustandDependency extends java_recipe_proxy_1.JavaDependencyRecipe {
|
|
|
55
74
|
this.held.push(manifest);
|
|
56
75
|
return false;
|
|
57
76
|
}
|
|
77
|
+
if (/(^|\/)pnpm-workspace\.yaml$/.test(manifest.sourcePath))
|
|
78
|
+
this.pending.push(manifest);
|
|
58
79
|
if (LOCKFILE.test(manifest.sourcePath))
|
|
59
80
|
this.locks.push(manifest);
|
|
60
81
|
return true;
|
|
@@ -64,6 +85,8 @@ class ZustandDependency extends java_recipe_proxy_1.JavaDependencyRecipe {
|
|
|
64
85
|
return false;
|
|
65
86
|
if (LOCKFILE.test(manifest.sourcePath) && this.unfit.has(manifest.sourcePath.slice(0, manifest.sourcePath.lastIndexOf("/") + 1)))
|
|
66
87
|
return false;
|
|
88
|
+
if (LOCKFILE.test(manifest.sourcePath) && !this.owned.has(manifest.sourcePath))
|
|
89
|
+
return false;
|
|
67
90
|
const refused = this.forecast.refused;
|
|
68
91
|
if (!(refused === null || refused === void 0 ? void 0 : refused.size))
|
|
69
92
|
return true;
|
|
@@ -71,6 +94,9 @@ class ZustandDependency extends java_recipe_proxy_1.JavaDependencyRecipe {
|
|
|
71
94
|
return !refused.has(packageRoot(manifest.sourcePath));
|
|
72
95
|
return ![...refused.values()].some(refusal => refusal.lock === manifest.sourcePath);
|
|
73
96
|
}
|
|
97
|
+
skipped() {
|
|
98
|
+
return [];
|
|
99
|
+
}
|
|
74
100
|
fits(root) {
|
|
75
101
|
var _a, _b, _c, _d;
|
|
76
102
|
const declared = (_b = (_a = this.forecast).react) === null || _b === void 0 ? void 0 : _b.call(_a, root);
|
|
@@ -82,20 +108,40 @@ class ZustandDependency extends java_recipe_proxy_1.JavaDependencyRecipe {
|
|
|
82
108
|
}
|
|
83
109
|
lockOf(root) {
|
|
84
110
|
const directory = (path) => path.slice(0, path.lastIndexOf("/") + 1);
|
|
85
|
-
return this.locks.filter(lock =>
|
|
86
|
-
|
|
111
|
+
return this.locks.filter(lock => {
|
|
112
|
+
var _a;
|
|
113
|
+
const base = directory(lock.sourcePath);
|
|
114
|
+
return root === base || root.startsWith(base) && member((_a = this.workspaces.get(base)) !== null && _a !== void 0 ? _a : [], root.slice(base.length, -1));
|
|
115
|
+
}).sort((a, b) => b.sourcePath.length - a.sourcePath.length)[0];
|
|
116
|
+
}
|
|
117
|
+
readWorkspaces() {
|
|
118
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
119
|
+
var _a;
|
|
120
|
+
for (const manifest of this.held) {
|
|
121
|
+
const globs = workspaceGlobs(yield parsed(manifest));
|
|
122
|
+
if (globs.length)
|
|
123
|
+
this.workspaces.set(packageRoot(manifest.sourcePath), globs);
|
|
124
|
+
}
|
|
125
|
+
for (const file of this.pending) {
|
|
126
|
+
const root = file.sourcePath.slice(0, -"pnpm-workspace.yaml".length);
|
|
127
|
+
this.workspaces.set(root, [...(_a = this.workspaces.get(root)) !== null && _a !== void 0 ? _a : [], ...pnpmPackages(yield rewrite_1.TreePrinters.print(file))]);
|
|
128
|
+
}
|
|
129
|
+
});
|
|
87
130
|
}
|
|
88
131
|
generate(acc, ctx) {
|
|
89
132
|
const _super = Object.create(null, {
|
|
90
133
|
generate: { get: () => super.generate }
|
|
91
134
|
});
|
|
92
135
|
return __awaiter(this, void 0, void 0, function* () {
|
|
93
|
-
var _a, _b, _c, _d;
|
|
94
|
-
const converting = (_c = yield ((_b = (_a = this.forecast).converting) === null || _b === void 0 ? void 0 : _b.call(_a))) !== null && _c !== void 0 ? _c :
|
|
136
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
137
|
+
const converting = new Set((_c = yield ((_b = (_a = this.forecast).converting) === null || _b === void 0 ? void 0 : _b.call(_a))) !== null && _c !== void 0 ? _c : []);
|
|
138
|
+
for (const root of this.skipped())
|
|
139
|
+
converting.delete(root);
|
|
95
140
|
for (const root of converting)
|
|
96
141
|
if (!this.fits(root))
|
|
97
142
|
this.unfit.add(root);
|
|
98
143
|
const forwarded = this.held.filter(manifest => converting.has(packageRoot(manifest.sourcePath)) && !this.unfit.has(packageRoot(manifest.sourcePath)));
|
|
144
|
+
yield this.readWorkspaces();
|
|
99
145
|
const scanner = yield this.java.scanner(acc);
|
|
100
146
|
for (const manifest of forwarded)
|
|
101
147
|
yield scanner.visit(manifest, ctx);
|
|
@@ -103,14 +149,22 @@ class ZustandDependency extends java_recipe_proxy_1.JavaDependencyRecipe {
|
|
|
103
149
|
const editor = yield this.java.editorWithData(acc);
|
|
104
150
|
for (const manifest of forwarded) {
|
|
105
151
|
const edited = yield editor.visit(manifest, ctx);
|
|
106
|
-
if (!edited || edited === manifest
|
|
152
|
+
if (!edited || edited === manifest)
|
|
107
153
|
continue;
|
|
108
154
|
const root = packageRoot(manifest.sourcePath);
|
|
155
|
+
if (!(yield declares(edited, this.addition.packageName))) {
|
|
156
|
+
const warning = newWarning(manifest, edited);
|
|
157
|
+
if (warning)
|
|
158
|
+
(_d = this.forecast.refused) === null || _d === void 0 ? void 0 : _d.set(root, { message: warning.message, lock: (_f = (_e = this.lockOf(root)) === null || _e === void 0 ? void 0 : _e.sourcePath) !== null && _f !== void 0 ? _f : manifest.sourcePath });
|
|
159
|
+
continue;
|
|
160
|
+
}
|
|
109
161
|
const lock = this.lockOf(root);
|
|
162
|
+
if (lock)
|
|
163
|
+
this.owned.add(lock.sourcePath);
|
|
110
164
|
const regenerated = lock ? yield editor.visit(lock, ctx) : undefined;
|
|
111
165
|
const warning = regenerated === null || regenerated === void 0 ? void 0 : regenerated.markers.markers.find(marker => marker.kind === rewrite_1.MarkersKind.MarkupWarn || marker.kind === rewrite_1.MarkersKind.MarkupError);
|
|
112
166
|
if (warning)
|
|
113
|
-
(
|
|
167
|
+
(_g = this.forecast.refused) === null || _g === void 0 ? void 0 : _g.set(root, { message: warning.message, lock: lock.sourcePath });
|
|
114
168
|
else
|
|
115
169
|
this.landed.add(root);
|
|
116
170
|
}
|
|
@@ -119,12 +173,17 @@ class ZustandDependency extends java_recipe_proxy_1.JavaDependencyRecipe {
|
|
|
119
173
|
});
|
|
120
174
|
}
|
|
121
175
|
}
|
|
176
|
+
function newWarning(before, after) {
|
|
177
|
+
const had = new Set(before.markers.markers.map(marker => marker.id));
|
|
178
|
+
return after.markers.markers.find(marker => !had.has(marker.id) &&
|
|
179
|
+
(marker.kind === rewrite_1.MarkersKind.MarkupWarn || marker.kind === rewrite_1.MarkersKind.MarkupError));
|
|
180
|
+
}
|
|
122
181
|
class AddZustand extends ZustandDependency {
|
|
123
182
|
constructor(forecast = {}) {
|
|
124
183
|
super(ZUSTAND_5, forecast);
|
|
125
184
|
this.name = "org.openrewrite.react.migrate.add-zustand";
|
|
126
185
|
this.displayName = "Add `zustand` where a Redux store is migrated";
|
|
127
|
-
this.description = "Adds `zustand` 5 to packages that
|
|
186
|
+
this.description = "Adds `zustand` 5 to packages that resolve React 18 or later, where a Redux store moves to zustand.";
|
|
128
187
|
}
|
|
129
188
|
}
|
|
130
189
|
exports.AddZustand = AddZustand;
|
|
@@ -133,7 +192,7 @@ class AddZustand4 extends ZustandDependency {
|
|
|
133
192
|
super(ZUSTAND_4, forecast);
|
|
134
193
|
this.name = "org.openrewrite.react.migrate.add-zustand-4";
|
|
135
194
|
this.displayName = "Add `zustand` 4 where a Redux store is migrated on React 16.8 to 17";
|
|
136
|
-
this.description = "Adds `zustand` 4 to packages that
|
|
195
|
+
this.description = "Adds `zustand` 4 to packages that resolve React 16.8 to 17, where a Redux store moves to zustand.";
|
|
137
196
|
}
|
|
138
197
|
}
|
|
139
198
|
exports.AddZustand4 = AddZustand4;
|
|
@@ -142,7 +201,11 @@ class AddImmer extends ZustandDependency {
|
|
|
142
201
|
super(IMMER_10, forecast);
|
|
143
202
|
this.name = "org.openrewrite.react.migrate.add-immer";
|
|
144
203
|
this.displayName = "Add `immer` where a Redux store is migrated";
|
|
145
|
-
this.description = "Adds `immer` 10 to packages that
|
|
204
|
+
this.description = "Adds `immer` 10 to packages that resolve React 16.8 or later, where a Redux store moves to zustand and its slices' reducers run through `produce`.";
|
|
205
|
+
}
|
|
206
|
+
skipped() {
|
|
207
|
+
var _a;
|
|
208
|
+
return (_a = this.forecast.withoutImmer) !== null && _a !== void 0 ? _a : [];
|
|
146
209
|
}
|
|
147
210
|
}
|
|
148
211
|
exports.AddImmer = AddImmer;
|
|
@@ -155,20 +218,24 @@ const ADDS_DEPENDENCIES = (() => {
|
|
|
155
218
|
}
|
|
156
219
|
})();
|
|
157
220
|
class ReduxToZustand extends rewrite_1.ScanningRecipe {
|
|
158
|
-
constructor() {
|
|
159
|
-
|
|
221
|
+
constructor(options) {
|
|
222
|
+
var _a;
|
|
223
|
+
super(options);
|
|
160
224
|
this.name = "org.openrewrite.react.migrate.redux-to-zustand";
|
|
161
225
|
this.displayName = "Migrate Redux Toolkit slices to zustand";
|
|
162
|
-
this.description = "Moves a repository's one Redux Toolkit store to zustand when it is a `configureStore` of `createSlice` reducers with no other option, its sources are JavaScript, and every use of it converts: each slice becomes a zustand store whose action creators keep their names and run the slice's reducers through immer's `produce`, `useSelector` reads of one slice become the slice's zustand hook, `dispatch` and `store.dispatch` of the slices' action creators become calls of them, `store.getState()` reads become the slice store's `getState()`, and the react-redux `Provider
|
|
163
|
-
this.forecast = { refused: new Map() };
|
|
226
|
+
this.description = "Moves a repository's one Redux Toolkit store to zustand when it is a `configureStore` of `createSlice` reducers with no other option, its sources are JavaScript, and every use of it converts: each slice becomes a zustand store whose action creators keep their names and run the slice's reducers through immer's `produce`, `useSelector` reads of one slice become the slice's zustand hook, `dispatch` and `store.dispatch` of the slices' action creators become calls of them, `store.getState()` reads become the slice store's `getState()`, and the react-redux `Provider`, replaced with its children, and the store's source are removed. A slice's own `createAsyncThunk`s, whose stages its `extraReducers` handles, keep running on Redux Toolkit and send their actions to the slice's zustand store. A store of its `reducer` alone whose slices can't move one by one, such as one with a reducer that is no slice's, thunks that reach the rest of the store or dispatches whose result is used, moves whole: its reducer runs on one zustand store whose `dispatch` runs thunks as Redux Toolkit's default middleware does, its selectors run on that store, through a hook of the store's source that keeps each selection per state where zustand 5 would run the selector on each read, and the store keeps its `getState` and `dispatch`, and its `subscribe`, whose listeners its `dispatch` calls after every action as Redux does, where a source subscribes to it. A legacy `createStore` of a reducer, and perhaps a preloaded state, moves whole too, its `dispatch` taking plain actions only, as Redux's does without middleware, or thunks as well where its enhancer is `applyMiddleware(thunk)`, perhaps inside `compose` or Redux DevTools, which is dropped. Components connected with `connect`, with no `mergeProps` and no option but `forwardRef`, move with a store that moves whole: its source declares a `connect` for the zustand store that passes the same props, copies the same statics and skips the same re-renders as react-redux's, and the store batches its updates as react-redux does. A store that moves whole but is created, and not exported, at the top level of an application's entry source, such as the one that renders the `Provider`, moves to an `appStore` module beside that source, with the imports and top-level `const`s and `let`s only it uses, and the source imports back what it still uses. A store's source may export it in its declaration, as its default export or in an export list such as `export {store}`, and other sources may import it, and its slices, by relative paths or by aliases such as `@/store` that the `paths` of the nearest `tsconfig.json` or `jsconfig.json` declare. Adds `zustand`, and `immer` where slices move one by one, where they are missing. Stores with any other use, such as `connect` with `mergeProps`, other middleware or enhancers, other uses of the store object such as `persistStore(store)`, selectors with their own equality function or several stores, stay in Redux and are recorded for review.";
|
|
227
|
+
this.forecast = { refused: new Map(), withoutImmer: new Set() };
|
|
164
228
|
this.storeSources = [];
|
|
165
229
|
this.steps = ADDS_DEPENDENCIES ? { zustand: new AddZustand(this.forecast), zustand4: new AddZustand4(this.forecast), immer: new AddImmer(this.forecast) } : undefined;
|
|
166
230
|
this.stores = new rewrite_1.DataTable(redux_stores_1.REDUX_STORES_TABLE, "Redux stores", "The Redux stores of the repository and whether each one's setup can move to zustand, with one row for each reason it can't.", redux_stores_1.ReduxStoreRow);
|
|
231
|
+
this.reported = new WeakMap();
|
|
232
|
+
this.splits = new WeakMap();
|
|
167
233
|
this.plans = new WeakMap();
|
|
234
|
+
(_a = this.wholeStores) !== null && _a !== void 0 ? _a : (this.wholeStores = true);
|
|
168
235
|
}
|
|
169
236
|
initialValue() {
|
|
170
237
|
this.storeSources.length = 0;
|
|
171
|
-
return { sources: new Map(), modules: new Map(), sliceSources: new Map(), packages: new Map() };
|
|
238
|
+
return { sources: new Map(), modules: new Map(), sliceSources: new Map(), packages: new Map(), configs: new Map() };
|
|
172
239
|
}
|
|
173
240
|
scanner(scanned) {
|
|
174
241
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -176,8 +243,13 @@ class ReduxToZustand extends rewrite_1.ScanningRecipe {
|
|
|
176
243
|
let forecast;
|
|
177
244
|
this.asked = undefined;
|
|
178
245
|
this.forecast.react = root => { var _a; return (_a = scanned.packages.get(root)) === null || _a === void 0 ? void 0 : _a.react; };
|
|
179
|
-
this.forecast.converting = () => forecast !== null && forecast !== void 0 ? forecast : (forecast = this.planned(scanned, true)
|
|
180
|
-
|
|
246
|
+
this.forecast.converting = () => forecast !== null && forecast !== void 0 ? forecast : (forecast = this.planned(scanned, true).then(plans => {
|
|
247
|
+
this.forecast.withoutImmer.clear();
|
|
248
|
+
for (const p of plans)
|
|
249
|
+
if (p.reducerStore)
|
|
250
|
+
this.forecast.withoutImmer.add(packageOf(scanned.packages, p.store.path));
|
|
251
|
+
return this.asked = new Set(plans.filter(p => p.store).map(p => packageOf(scanned.packages, p.store.path)));
|
|
252
|
+
}));
|
|
181
253
|
const sources = yield (0, rewrite_2.check)((0, rewrite_2.not)((0, javascript_1.isVendoredOrBundled)()), new class extends javascript_1.JavaScriptVisitor {
|
|
182
254
|
visitJsCompilationUnit(cu) {
|
|
183
255
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -199,8 +271,9 @@ class ReduxToZustand extends rewrite_1.ScanningRecipe {
|
|
|
199
271
|
if (/(^|\/)package\.json$/.test(doc.sourcePath)) {
|
|
200
272
|
const declared = (0, javascript_1.findNodeResolutionResult)(doc) ? undefined : yield declaredDependencies(doc);
|
|
201
273
|
const get = (name) => declared ? declared[name] : version(doc, name);
|
|
202
|
-
scanned.packages.set(packageRoot(doc.sourcePath), { react: get("react"), zustand: get(ZUSTAND), immer: get(IMMER) });
|
|
274
|
+
scanned.packages.set(packageRoot(doc.sourcePath), { react: get("react"), reactDom: get("react-dom"), reactNative: get("react-native"), zustand: get(ZUSTAND), immer: get(IMMER), redux: get("redux"), toolkit: get(redux_stores_1.RTK), reactRedux: get("react-redux") });
|
|
203
275
|
}
|
|
276
|
+
yield (0, module_imports_1.addPathsConfig)(scanned.configs, doc);
|
|
204
277
|
return doc;
|
|
205
278
|
});
|
|
206
279
|
}
|
|
@@ -230,19 +303,76 @@ class ReduxToZustand extends rewrite_1.ScanningRecipe {
|
|
|
230
303
|
return cached;
|
|
231
304
|
}
|
|
232
305
|
const result = (() => __awaiter(this, void 0, void 0, function* () {
|
|
306
|
+
var _a, _b;
|
|
307
|
+
const splits = yield this.split(scanned);
|
|
233
308
|
for (const cu of this.storeSources) {
|
|
234
|
-
const
|
|
309
|
+
const source = (_b = (_a = splits.get(cu.sourcePath)) === null || _a === void 0 ? void 0 : _a.module) !== null && _b !== void 0 ? _b : cu;
|
|
310
|
+
const facts = scanned.sources.get(source.sourcePath);
|
|
235
311
|
if (facts)
|
|
236
|
-
facts.stores = yield (0, zustand_plan_1.storeFacts)(
|
|
312
|
+
facts.stores = yield (0, zustand_plan_1.storeFacts)(source, scanned.modules, resolverOf(scanned));
|
|
237
313
|
}
|
|
238
|
-
return (0, zustand_plan_1.planPackages)(scanned.sources, scanned.sliceSources, path => packageOf(scanned.packages, path), storePath => this.dependencies(scanned, storePath, forecast)
|
|
314
|
+
return (0, zustand_plan_1.planPackages)(scanned.sources, scanned.sliceSources, path => packageOf(scanned.packages, path), (storePath, immer) => this.dependencies(scanned, storePath, forecast, immer), this.wholeStores !== false, storePath => {
|
|
315
|
+
var _a;
|
|
316
|
+
const facts = scanned.packages.get((_a = packageOf(scanned.packages, storePath)) !== null && _a !== void 0 ? _a : "");
|
|
317
|
+
return (facts === null || facts === void 0 ? void 0 : facts.reactNative) ? "react-native" : (facts === null || facts === void 0 ? void 0 : facts.reactDom) ? "react-dom" : undefined;
|
|
318
|
+
}, storePath => {
|
|
319
|
+
var _a, _b, _c;
|
|
320
|
+
const facts = scanned.packages.get((_a = packageOf(scanned.packages, storePath)) !== null && _a !== void 0 ? _a : "");
|
|
321
|
+
return (facts === null || facts === void 0 ? void 0 : facts.zustand) !== undefined ? ((_b = major(facts.zustand)) !== null && _b !== void 0 ? _b : 0) >= 5 : ((_c = major(facts === null || facts === void 0 ? void 0 : facts.react)) !== null && _c !== void 0 ? _c : 0) >= 18;
|
|
322
|
+
}, resolverOf(scanned));
|
|
239
323
|
}))();
|
|
240
324
|
if (!forecast)
|
|
241
325
|
this.plans.set(scanned, result);
|
|
242
326
|
return result;
|
|
243
327
|
});
|
|
244
328
|
}
|
|
245
|
-
|
|
329
|
+
split(scanned) {
|
|
330
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
331
|
+
const known = this.splits.get(scanned);
|
|
332
|
+
if (known)
|
|
333
|
+
return known;
|
|
334
|
+
const splits = new Map();
|
|
335
|
+
this.splits.set(scanned, splits);
|
|
336
|
+
const paths = [...scanned.sources.keys()];
|
|
337
|
+
const created = new Map();
|
|
338
|
+
for (const cu of this.storeSources)
|
|
339
|
+
created.set(cu, yield (0, zustand_plan_1.storeFacts)(cu, scanned.modules, resolverOf(scanned)));
|
|
340
|
+
const storesIn = (root) => [...created].filter(([cu]) => packageOf(scanned.packages, cu.sourcePath) === root).reduce((n, [, stores]) => n + stores.length, 0);
|
|
341
|
+
for (const [cu, stores] of created) {
|
|
342
|
+
if (/\.(?:ts|tsx|mts|cts)$/.test(cu.sourcePath) || redux_stores_1.TEST_SOURCE.test(cu.sourcePath))
|
|
343
|
+
continue;
|
|
344
|
+
if (stores.length !== 1 || stores[0].exportedAs || storesIn(packageOf(scanned.packages, cu.sourcePath)) !== 1)
|
|
345
|
+
continue;
|
|
346
|
+
const directory = cu.sourcePath.includes("/") ? cu.sourcePath.slice(0, cu.sourcePath.lastIndexOf("/") + 1) : "";
|
|
347
|
+
const base = directory + entry_store_1.ENTRY_STORE_MODULE;
|
|
348
|
+
if (paths.some(path => path.startsWith(base + ".") || path.startsWith(base + "/")))
|
|
349
|
+
continue;
|
|
350
|
+
const split = yield (0, entry_store_1.splitEntryStore)(cu, stores[0].store, base + ".js", new rewrite_1.ExecutionContext());
|
|
351
|
+
if (!split)
|
|
352
|
+
continue;
|
|
353
|
+
splits.set(cu.sourcePath, Object.assign(Object.assign({}, split), { modulePath: base + ".js" }));
|
|
354
|
+
paths.push(base + ".js");
|
|
355
|
+
for (const source of [split.entry, split.module]) {
|
|
356
|
+
const { reducers, slices } = (0, redux_stores_1.localSlices)(source);
|
|
357
|
+
scanned.modules.set(source.sourcePath, { reducers, slices });
|
|
358
|
+
scanned.sources.set(source.sourcePath, yield (0, zustand_plan_1.sourceFacts)(source));
|
|
359
|
+
const slice = (0, zustand_plan_1.sliceSource)(source);
|
|
360
|
+
if (slice)
|
|
361
|
+
scanned.sliceSources.set(source.sourcePath, slice);
|
|
362
|
+
else
|
|
363
|
+
scanned.sliceSources.delete(source.sourcePath);
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
return splits;
|
|
367
|
+
});
|
|
368
|
+
}
|
|
369
|
+
generate(scanned, ctx) {
|
|
370
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
371
|
+
const plans = yield this.planned(scanned, true);
|
|
372
|
+
return [...(yield this.split(scanned)).values()].filter(split => plans.some(p => { var _a; return ((_a = p.store) === null || _a === void 0 ? void 0 : _a.path) === split.modulePath && p.reducerStore; })).map(split => split.module);
|
|
373
|
+
});
|
|
374
|
+
}
|
|
375
|
+
dependencies(scanned, storePath, forecast, needsImmer = true) {
|
|
246
376
|
var _a, _b, _c, _d, _e, _f, _g, _h, _j;
|
|
247
377
|
const root = packageOf(scanned.packages, storePath);
|
|
248
378
|
const facts = root !== undefined ? scanned.packages.get(root) : undefined;
|
|
@@ -259,7 +389,7 @@ class ReduxToZustand extends rewrite_1.ScanningRecipe {
|
|
|
259
389
|
: landed(react >= 18 ? (_f = this.steps) === null || _f === void 0 ? void 0 : _f.zustand : (_g = this.steps) === null || _g === void 0 ? void 0 : _g.zustand4);
|
|
260
390
|
if (!zustand)
|
|
261
391
|
return { reason: "DEPENDENCY", detail: facts.zustand ? `zustand ${facts.zustand} doesn't support React ${react}` : "zustand can't be added" };
|
|
262
|
-
const immer = facts.immer !== undefined ? ((_h = major(facts.immer)) !== null && _h !== void 0 ? _h : 0) >= 9 : landed((_j = this.steps) === null || _j === void 0 ? void 0 : _j.immer);
|
|
392
|
+
const immer = !needsImmer || (facts.immer !== undefined ? ((_h = major(facts.immer)) !== null && _h !== void 0 ? _h : 0) >= 9 : landed((_j = this.steps) === null || _j === void 0 ? void 0 : _j.immer));
|
|
263
393
|
if (!immer)
|
|
264
394
|
return { reason: "DEPENDENCY", detail: facts.immer ? `immer ${facts.immer} is older than 9` : "immer can't be added" };
|
|
265
395
|
return undefined;
|
|
@@ -268,36 +398,45 @@ class ReduxToZustand extends rewrite_1.ScanningRecipe {
|
|
|
268
398
|
return __awaiter(this, void 0, void 0, function* () {
|
|
269
399
|
const recipe = this;
|
|
270
400
|
const stores = this.stores;
|
|
271
|
-
|
|
401
|
+
let reported = this.reported.get(scanned);
|
|
402
|
+
if (!reported)
|
|
403
|
+
this.reported.set(scanned, reported = new Set());
|
|
272
404
|
return (0, rewrite_2.check)((0, rewrite_2.not)((0, javascript_1.isVendoredOrBundled)()), new class extends javascript_1.JavaScriptVisitor {
|
|
273
405
|
visitJsCompilationUnit(cu, ctx) {
|
|
274
406
|
return __awaiter(this, void 0, void 0, function* () {
|
|
275
|
-
var _a, _b, _c, _d, _e;
|
|
407
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
|
|
276
408
|
const plans = yield recipe.planned(scanned);
|
|
277
409
|
const facts = scanned.sources.get(cu.sourcePath);
|
|
278
|
-
|
|
410
|
+
const splits = yield recipe.split(scanned);
|
|
411
|
+
const split = splits.get(cu.sourcePath);
|
|
412
|
+
const entryOf = (path) => { var _a, _b; return (_b = (_a = [...splits].find(([, s]) => s.modulePath === path)) === null || _a === void 0 ? void 0 : _a[0]) !== null && _b !== void 0 ? _b : path; };
|
|
413
|
+
for (const store of [...(_a = facts === null || facts === void 0 ? void 0 : facts.stores) !== null && _a !== void 0 ? _a : [], ...split ? (_c = (_b = scanned.sources.get(split.modulePath)) === null || _b === void 0 ? void 0 : _b.stores) !== null && _c !== void 0 ? _c : [] : []]) {
|
|
279
414
|
const key = `${store.path}\0${store.store}\0${store.api}`;
|
|
280
415
|
if (reported.has(key))
|
|
281
416
|
continue;
|
|
282
417
|
reported.add(key);
|
|
283
|
-
const p = (
|
|
284
|
-
const findings = (p === null || p === void 0 ? void 0 : p.store) === store ? [] : (
|
|
285
|
-
const rows = findings.length === 0 ? [{ reason: "SLICE_STORE", detail: "" }] : findings;
|
|
286
|
-
for (const { reason, detail } of rows) {
|
|
418
|
+
const p = (_d = plans.find(candidate => candidate.store === store)) !== null && _d !== void 0 ? _d : plans.find(candidate => candidate.blocked.has(store));
|
|
419
|
+
const findings = (p === null || p === void 0 ? void 0 : p.store) === store ? [] : (_e = p === null || p === void 0 ? void 0 : p.blocked.get(store)) !== null && _e !== void 0 ? _e : store.findings;
|
|
420
|
+
const rows = findings.length === 0 ? [{ reason: (p === null || p === void 0 ? void 0 : p.reducerStore) ? "REDUCER_STORE" : "SLICE_STORE", detail: "" }] : findings;
|
|
421
|
+
for (const { reason, detail, action } of rows) {
|
|
287
422
|
stores.insertRow(ctx, {
|
|
288
|
-
sourcePath: store.path, store: store.store, api: store.api,
|
|
423
|
+
sourcePath: entryOf(store.path), store: store.store, api: store.api,
|
|
289
424
|
status: (p === null || p === void 0 ? void 0 : p.store) === store ? "CONVERTED" : "REVIEW", reason, detail,
|
|
290
|
-
testSource: redux_stores_1.TEST_SOURCE.test(store.path), suggestedAction: (
|
|
425
|
+
testSource: redux_stores_1.TEST_SOURCE.test(entryOf(store.path)), suggestedAction: (_g = (_f = action !== null && action !== void 0 ? action : redux_stores_1.SUGGESTED_ACTIONS[reason]) !== null && _f !== void 0 ? _f : CONVERSION_ACTIONS[reason]) !== null && _g !== void 0 ? _g : ""
|
|
291
426
|
});
|
|
292
427
|
}
|
|
293
428
|
}
|
|
294
429
|
const p = plans.find(candidate => candidate.store && candidate.sources.has(cu.sourcePath));
|
|
430
|
+
const generated = [...splits.values()].some(s => s.modulePath === cu.sourcePath);
|
|
295
431
|
if (!(p === null || p === void 0 ? void 0 : p.store))
|
|
296
|
-
return cu;
|
|
432
|
+
return generated ? undefined : cu;
|
|
433
|
+
if (p.reducerStore)
|
|
434
|
+
return cu.sourcePath === p.store.path ? convertReducerStore(cu, p, scanned.packages.get((_h = packageOf(scanned.packages, cu.sourcePath)) !== null && _h !== void 0 ? _h : ""), ctx) : convertReducerConsumer((_j = split === null || split === void 0 ? void 0 : split.entry) !== null && _j !== void 0 ? _j : cu, p, scanned, ctx);
|
|
297
435
|
if (cu.sourcePath === p.store.path)
|
|
298
436
|
return undefined;
|
|
299
437
|
const slice = [...p.slices.values()].find(s => s.path === cu.sourcePath);
|
|
300
|
-
const
|
|
438
|
+
const source = (_k = split === null || split === void 0 ? void 0 : split.entry) !== null && _k !== void 0 ? _k : cu;
|
|
439
|
+
const converted = slice ? yield convertSlice(this, source, slice, scanned, ctx) : source;
|
|
301
440
|
return convertConsumer(converted, p, scanned, ctx);
|
|
302
441
|
});
|
|
303
442
|
}
|
|
@@ -312,9 +451,17 @@ class ReduxToZustand extends rewrite_1.ScanningRecipe {
|
|
|
312
451
|
}
|
|
313
452
|
}
|
|
314
453
|
exports.ReduxToZustand = ReduxToZustand;
|
|
454
|
+
__decorate([
|
|
455
|
+
(0, rewrite_1.Option)({
|
|
456
|
+
displayName: "Move stores whole",
|
|
457
|
+
description: "Whether a store whose slices can't move to zustand one by one moves whole: one zustand store driven by the store's own reducer. Defaults to true.",
|
|
458
|
+
required: false,
|
|
459
|
+
type: "Boolean"
|
|
460
|
+
})
|
|
461
|
+
], ReduxToZustand.prototype, "wholeStores", void 0);
|
|
315
462
|
const CONVERSION_ACTIONS = {
|
|
316
463
|
MULTIPLE_STORES: "Convert by hand, or one store at a time: the uses of react-redux can't be told apart by store.",
|
|
317
|
-
STORE_NOT_MODULE_LEVEL: "Create the store once at the top level of its module; a zustand store is a module-level singleton.",
|
|
464
|
+
STORE_NOT_MODULE_LEVEL: "Create the store once at the top level of its module, and export it; a zustand store is a module-level singleton. A store its source creates at the top level moves to a module of its own where only imports and top-level `const`s and `let`s it uses move with it, nothing that stays uses them, and no `appStore` module is beside the source already.",
|
|
318
465
|
STORE_SOURCE: "Move what else the store's source declares, such as types or helpers, to another module.",
|
|
319
466
|
SLICE_SOURCE: "Declare each slice in a source of its own, held in a `const` of its own.",
|
|
320
467
|
SLICE_KEY: "Use a reducer key that is a JavaScript identifier, which names the zustand hook.",
|
|
@@ -327,10 +474,10 @@ const CONVERSION_ACTIONS = {
|
|
|
327
474
|
ACTIONS_EXPORT: "Export the action creators as `export const {a, b} = slice.actions`, under their own names.",
|
|
328
475
|
UNKNOWN_ACTION: "Remove the import of an action creator that has no reducer, which is `undefined`.",
|
|
329
476
|
SLICE_USE: "Use the slice only through its action creators and the store; other uses of the slice object are converted by hand.",
|
|
330
|
-
STORE_USE: "Use the store only in `<Provider store>`, `store.getState().key` and `store.dispatch(action())`.",
|
|
331
|
-
REACT_REDUX_USE: "Replace `
|
|
477
|
+
STORE_USE: "Use the store only in `<Provider store>`, `store.getState().key` and `store.dispatch(action())`, or, where it moves whole, in calls of its `getState`, `dispatch` and `subscribe`.",
|
|
478
|
+
REACT_REDUX_USE: "Replace `useStore`, `connect` with `mergeProps` or options other than `forwardRef`, and other react-redux APIs with `useSelector` and `useDispatch`, or convert by hand.",
|
|
332
479
|
DISPATCH: "Dispatch only the slices' action creators and their own thunks, as a statement of its own; other thunks and dispatches whose result is used are converted by hand.",
|
|
333
|
-
SELECTOR: "
|
|
480
|
+
SELECTOR: "Pass `useSelector` the selector alone, with no equality function or type arguments; where the slices would move one by one, read one slice's state in each selector, such as `state => state.counter.value` or a top-level `const` of one.",
|
|
334
481
|
UNFOLLOWED_IMPORT: "Import the store, its slices and react-redux with named or default imports; namespace imports, re-exports and module mocks are converted by hand.",
|
|
335
482
|
TYPESCRIPT: "TypeScript sources are not converted yet.",
|
|
336
483
|
NO_PACKAGE: "Add a `package.json` for the store's package.",
|
|
@@ -340,6 +487,42 @@ const CONVERSION_ACTIONS = {
|
|
|
340
487
|
function packageRoot(manifestPath) {
|
|
341
488
|
return manifestPath.slice(0, -"package.json".length);
|
|
342
489
|
}
|
|
490
|
+
function parsed(doc) {
|
|
491
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
492
|
+
try {
|
|
493
|
+
return JSON.parse(yield rewrite_1.TreePrinters.print(doc));
|
|
494
|
+
}
|
|
495
|
+
catch (_a) {
|
|
496
|
+
return {};
|
|
497
|
+
}
|
|
498
|
+
});
|
|
499
|
+
}
|
|
500
|
+
function workspaceGlobs(manifest) {
|
|
501
|
+
var _a;
|
|
502
|
+
const workspaces = Array.isArray(manifest === null || manifest === void 0 ? void 0 : manifest.workspaces) ? manifest.workspaces : (_a = manifest === null || manifest === void 0 ? void 0 : manifest.workspaces) === null || _a === void 0 ? void 0 : _a.packages;
|
|
503
|
+
return Array.isArray(workspaces) ? workspaces.filter((glob) => typeof glob === "string") : [];
|
|
504
|
+
}
|
|
505
|
+
function pnpmPackages(yaml) {
|
|
506
|
+
var _a, _b;
|
|
507
|
+
const globs = [];
|
|
508
|
+
let inPackages = false;
|
|
509
|
+
for (const line of yaml.split(/\r?\n/)) {
|
|
510
|
+
if (/^\S/.test(line))
|
|
511
|
+
inPackages = /^packages\s*:/.test(line);
|
|
512
|
+
const entry = inPackages && /^\s+-\s*(?:'([^']*)'|"([^"]*)"|([^#\s]+))/.exec(line);
|
|
513
|
+
if (entry)
|
|
514
|
+
globs.push((_b = (_a = entry[1]) !== null && _a !== void 0 ? _a : entry[2]) !== null && _b !== void 0 ? _b : entry[3]);
|
|
515
|
+
}
|
|
516
|
+
return globs;
|
|
517
|
+
}
|
|
518
|
+
function member(globs, path) {
|
|
519
|
+
const matches = (glob) => {
|
|
520
|
+
const pattern = glob.replace(/^\.\//, "").replace(/\/+$/, "").replace(/[.+^${}()|[\]\\]/g, "\\$&")
|
|
521
|
+
.replace(/\*\*\/?|\*|\?/g, token => token.startsWith("**") ? "(?:.*/)?" + (token.endsWith("/") ? "" : ".*") : token === "*" ? "[^/]*" : "[^/]");
|
|
522
|
+
return new RegExp(`^${pattern}$`).test(path);
|
|
523
|
+
};
|
|
524
|
+
return globs.some(glob => !glob.startsWith("!") && matches(glob)) && !globs.some(glob => glob.startsWith("!") && matches(glob.slice(1)));
|
|
525
|
+
}
|
|
343
526
|
function packageOf(packages, sourcePath) {
|
|
344
527
|
return [...packages.keys()].filter(root => sourcePath.startsWith(root)).sort((a, b) => b.length - a.length)[0];
|
|
345
528
|
}
|
|
@@ -491,10 +674,10 @@ function withPrefix(node, prefix) {
|
|
|
491
674
|
function convertConsumer(cu, p, scanned, ctx) {
|
|
492
675
|
return __awaiter(this, void 0, void 0, function* () {
|
|
493
676
|
const bindings = (0, module_imports_1.importBindings)(cu);
|
|
494
|
-
const followed = new Map(bindings.filter(b => b.module
|
|
677
|
+
const followed = new Map(bindings.filter(b => followsImport(b.module)).map(b => [b.local, b]));
|
|
495
678
|
const isStore = (name) => {
|
|
496
679
|
const binding = followed.get(name);
|
|
497
|
-
return !!binding && binding.module !== zustand_plan_1.REACT_REDUX && (
|
|
680
|
+
return !!binding && binding.module !== zustand_plan_1.REACT_REDUX && resolverOf(scanned)(cu.sourcePath, binding.module) === p.store.path &&
|
|
498
681
|
binding.name === p.store.exportedAs;
|
|
499
682
|
};
|
|
500
683
|
const reactRedux = (name, member) => { var _a, _b; return ((_a = followed.get(name)) === null || _a === void 0 ? void 0 : _a.module) === zustand_plan_1.REACT_REDUX && ((_b = followed.get(name)) === null || _b === void 0 ? void 0 : _b.name) === member; };
|
|
@@ -603,6 +786,11 @@ function convertConsumer(cu, p, scanned, ctx) {
|
|
|
603
786
|
removed.add("store");
|
|
604
787
|
return withPrefix(yield this.visit(child, q, this.cursor), tag.prefix);
|
|
605
788
|
}
|
|
789
|
+
if ((0, zustand_plan_1.providerChildren)(tag)) {
|
|
790
|
+
removed.add("Provider");
|
|
791
|
+
removed.add("store");
|
|
792
|
+
return fragment(yield _super.visitJsxTag.call(this, tag, q));
|
|
793
|
+
}
|
|
606
794
|
}
|
|
607
795
|
return _super.visitJsxTag.call(this, tag, q);
|
|
608
796
|
});
|
|
@@ -651,4 +839,403 @@ function replaceTree(root, target, replacement) {
|
|
|
651
839
|
replace(draft);
|
|
652
840
|
});
|
|
653
841
|
}
|
|
842
|
+
function storeStatement(cu, store) {
|
|
843
|
+
var _a, _b;
|
|
844
|
+
const legacy = store.reducerStore === "legacy" || store.reducerStore === "legacyThunk";
|
|
845
|
+
const functions = (0, redux_stores_1.storeFunctions)(cu);
|
|
846
|
+
const names = legacy ? functions.legacy : functions.configure;
|
|
847
|
+
const isStoreCall = (node) => (node === null || node === void 0 ? void 0 : node.kind) === java_1.J.Kind.MethodInvocation &&
|
|
848
|
+
!node.select && names.has(node.name.simpleName);
|
|
849
|
+
for (let index = 0; index < cu.statements.length; index++) {
|
|
850
|
+
const statement = cu.statements[index].element;
|
|
851
|
+
const call = statement.kind === javascript_1.JS.Kind.ExportAssignment ? statement.expression.element :
|
|
852
|
+
statement.kind === java_1.J.Kind.VariableDeclarations && statement.variables.length === 1 &&
|
|
853
|
+
(0, java_1.isIdentifier)(statement.variables[0].element.name) &&
|
|
854
|
+
statement.variables[0].element.name.simpleName === store.store
|
|
855
|
+
? (_a = statement.variables[0].element.initializer) === null || _a === void 0 ? void 0 : _a.element : undefined;
|
|
856
|
+
if (!isStoreCall(call))
|
|
857
|
+
continue;
|
|
858
|
+
if (legacy) {
|
|
859
|
+
const args = (0, zustand_plan_1.argumentsOf)(call);
|
|
860
|
+
const preloaded = args.length === 3 || args.length === 2 && (0, redux_stores_1.preloadedValue)(args[1], cu) ? args[1] : undefined;
|
|
861
|
+
const enhancer = args.length === 3 || args.length === 2 && !preloaded ? (0, redux_stores_1.legacyEnhancer)(args[args.length - 1], cu) : undefined;
|
|
862
|
+
return { index, call, reducer: Object.assign(Object.assign({}, args[0]), { prefix: java_1.emptySpace }), preloaded: preloaded && Object.assign(Object.assign({}, preloaded), { prefix: java_1.emptySpace }), consts: enhancer && !("unsupported" in enhancer) ? enhancer.consts : new Set() };
|
|
863
|
+
}
|
|
864
|
+
const reducer = (_b = (0, redux_stores_1.objectProperties)((0, zustand_plan_1.argumentsOf)(call)[0])) === null || _b === void 0 ? void 0 : _b.get("reducer");
|
|
865
|
+
return reducer ? { index, call, reducer } : undefined;
|
|
866
|
+
}
|
|
867
|
+
return undefined;
|
|
868
|
+
}
|
|
869
|
+
function withoutUnused(cu, names) {
|
|
870
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
871
|
+
const declared = (statement) => statement.kind === java_1.J.Kind.VariableDeclarations && statement.variables.length === 1 &&
|
|
872
|
+
(0, java_1.isIdentifier)(statement.variables[0].element.name) ?
|
|
873
|
+
statement.variables[0].element.name.simpleName : undefined;
|
|
874
|
+
for (let removed = true; removed;) {
|
|
875
|
+
const counts = new Map();
|
|
876
|
+
yield new class extends javascript_1.JavaScriptVisitor {
|
|
877
|
+
visitIdentifier(identifier, p) {
|
|
878
|
+
const _super = Object.create(null, {
|
|
879
|
+
visitIdentifier: { get: () => super.visitIdentifier }
|
|
880
|
+
});
|
|
881
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
882
|
+
var _a;
|
|
883
|
+
counts.set(identifier.simpleName, ((_a = counts.get(identifier.simpleName)) !== null && _a !== void 0 ? _a : 0) + 1);
|
|
884
|
+
return _super.visitIdentifier.call(this, identifier, p);
|
|
885
|
+
});
|
|
886
|
+
}
|
|
887
|
+
}().visit(cu, undefined);
|
|
888
|
+
const statements = cu.statements.filter(padded => {
|
|
889
|
+
const name = declared(padded.element);
|
|
890
|
+
return !name || !names.has(name) || counts.get(name) !== 1;
|
|
891
|
+
});
|
|
892
|
+
removed = statements.length < cu.statements.length;
|
|
893
|
+
cu = Object.assign(Object.assign({}, cu), { statements });
|
|
894
|
+
}
|
|
895
|
+
return cu;
|
|
896
|
+
});
|
|
897
|
+
}
|
|
898
|
+
exports.SELECTOR = `export function SELECTOR_HOOK(selector) {
|
|
899
|
+
const last = useRef(undefined);
|
|
900
|
+
const select = () => {
|
|
901
|
+
const state = HOOK.getState();
|
|
902
|
+
const kept = last.current;
|
|
903
|
+
if (kept && kept.state === state && kept.selector === selector) return kept.selection;
|
|
904
|
+
const selection = selector(state);
|
|
905
|
+
last.current = {state, selector, selection};
|
|
906
|
+
return selection;
|
|
907
|
+
};
|
|
908
|
+
return useSyncExternalStore(HOOK.subscribe, select, select);
|
|
909
|
+
}`;
|
|
910
|
+
const STORE_UPDATE_ERROR = {
|
|
911
|
+
swallowed: `if (!update) throw error;`,
|
|
912
|
+
rethrown: `if (update) cache.error = error;
|
|
913
|
+
throw error;`
|
|
914
|
+
};
|
|
915
|
+
function connectSource(hook, reactRedux) {
|
|
916
|
+
var _a;
|
|
917
|
+
return CONNECT.split("HOOK").join(hook).replace("STORE_UPDATE_ERROR", STORE_UPDATE_ERROR[((_a = major(reactRedux)) !== null && _a !== void 0 ? _a : 8) >= 8 ? "swallowed" : "rethrown"]);
|
|
918
|
+
}
|
|
919
|
+
const CONNECT = `export function connect(mapStateToProps, mapDispatchToProps, mergeProps, options) {
|
|
920
|
+
const forwardsRef = Boolean(options && options.forwardRef);
|
|
921
|
+
const selectState = mapStateToProps ? state => state : () => undefined;
|
|
922
|
+
const dependsOnOwnProps = map => map.dependsOnOwnProps !== undefined && map.dependsOnOwnProps !== null ? Boolean(map.dependsOnOwnProps) : map.length !== 1;
|
|
923
|
+
// A map that returns a function on its first call is a factory of the map to use
|
|
924
|
+
const mapper = map => {
|
|
925
|
+
let actual = map;
|
|
926
|
+
let first = true;
|
|
927
|
+
const run = (value, ownProps) => dependsOnOwnProps(actual) ? actual(value, ownProps) : actual(value);
|
|
928
|
+
return {
|
|
929
|
+
map(value, ownProps) {
|
|
930
|
+
let props = run(value, ownProps);
|
|
931
|
+
if (first) {
|
|
932
|
+
first = false;
|
|
933
|
+
if (typeof props === "function") {
|
|
934
|
+
actual = props;
|
|
935
|
+
props = run(value, ownProps);
|
|
936
|
+
}
|
|
937
|
+
}
|
|
938
|
+
return props;
|
|
939
|
+
},
|
|
940
|
+
dependsOnOwnProps: () => dependsOnOwnProps(actual)
|
|
941
|
+
};
|
|
942
|
+
};
|
|
943
|
+
const bound = actionCreators => {
|
|
944
|
+
const props = {};
|
|
945
|
+
for (const key of Object.keys(actionCreators)) {
|
|
946
|
+
const actionCreator = actionCreators[key];
|
|
947
|
+
if (typeof actionCreator === "function") props[key] = (...args) => HOOK.dispatch(actionCreator(...args));
|
|
948
|
+
}
|
|
949
|
+
return props;
|
|
950
|
+
};
|
|
951
|
+
const shallowEqual = (a, b) => {
|
|
952
|
+
const keys = Object.keys(a);
|
|
953
|
+
return keys.length === Object.keys(b).length && keys.every(key => Object.prototype.hasOwnProperty.call(b, key) && Object.is(a[key], b[key]));
|
|
954
|
+
};
|
|
955
|
+
const notHoisted = ["$$typeof", "render", "compare", "type", "childContextTypes", "contextType", "contextTypes", "defaultProps", "displayName",
|
|
956
|
+
"getDefaultProps", "getDerivedStateFromError", "getDerivedStateFromProps", "mixins", "propTypes", "name", "length", "prototype", "caller",
|
|
957
|
+
"callee", "arguments", "arity"];
|
|
958
|
+
const hoistStatics = (target, source) => {
|
|
959
|
+
for (let from = source; from && from !== Function.prototype && from !== Object.prototype; from = Object.getPrototypeOf(from)) {
|
|
960
|
+
for (const key of [...Object.getOwnPropertyNames(from), ...Object.getOwnPropertySymbols(from)]) {
|
|
961
|
+
if (!notHoisted.includes(key) && !Object.prototype.hasOwnProperty.call(target, key)) Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(from, key));
|
|
962
|
+
}
|
|
963
|
+
}
|
|
964
|
+
return target;
|
|
965
|
+
};
|
|
966
|
+
return WrappedComponent => {
|
|
967
|
+
const displayName = \`Connect(\${WrappedComponent.displayName || WrappedComponent.name || "Component"})\`;
|
|
968
|
+
function ConnectFunction(props) {
|
|
969
|
+
const cache = useRef({}).current;
|
|
970
|
+
const state = HOOK(selectState);
|
|
971
|
+
if (!cache.mappers) cache.mappers = {state: mapStateToProps ? mapper(mapStateToProps) : undefined, dispatch: typeof mapDispatchToProps === "function" ? mapper(mapDispatchToProps) : undefined};
|
|
972
|
+
const {state: mapState, dispatch: mapDispatch} = cache.mappers;
|
|
973
|
+
if (cache.props !== props) {
|
|
974
|
+
cache.props = props;
|
|
975
|
+
cache.ownProps = props;
|
|
976
|
+
if (forwardsRef) {
|
|
977
|
+
cache.ownProps = {};
|
|
978
|
+
for (const key of Object.keys(props)) if (key !== "reactReduxForwardedRef") cache.ownProps[key] = props[key];
|
|
979
|
+
}
|
|
980
|
+
}
|
|
981
|
+
const ownProps = cache.ownProps;
|
|
982
|
+
// Own props compare shallowly with those of the last render, whether that render finished or not
|
|
983
|
+
const ownPropsChanged = !cache.seenOwnProps || !shallowEqual(cache.seenOwnProps, ownProps);
|
|
984
|
+
cache.seenOwnProps = ownProps;
|
|
985
|
+
const propsChanged = Boolean(mapState) && mapState.dependsOnOwnProps() && !(cache.stateOwnProps && shallowEqual(cache.stateOwnProps, ownProps));
|
|
986
|
+
if (!cache.stateProps || (mapState && (cache.state !== state || propsChanged))) {
|
|
987
|
+
// The state and props count as mapped before the map runs, so a render React retries after the map
|
|
988
|
+
// throws keeps the props from before
|
|
989
|
+
const update = cache.stateProps !== undefined && cache.state !== state && !propsChanged;
|
|
990
|
+
cache.state = state;
|
|
991
|
+
cache.stateOwnProps = ownProps;
|
|
992
|
+
cache.error = undefined;
|
|
993
|
+
try {
|
|
994
|
+
cache.stateProps = mapState ? mapState.map(state, ownProps) : {};
|
|
995
|
+
cache.kept = false;
|
|
996
|
+
} catch (error) {
|
|
997
|
+
// The props passed last stay, own props too, until the map returns again
|
|
998
|
+
cache.kept = true;
|
|
999
|
+
STORE_UPDATE_ERROR
|
|
1000
|
+
}
|
|
1001
|
+
} else if (cache.error) {
|
|
1002
|
+
throw cache.error;
|
|
1003
|
+
}
|
|
1004
|
+
if (!cache.dispatchProps || (mapDispatch && mapDispatch.dependsOnOwnProps() && cache.dispatchOwnProps !== ownProps)) {
|
|
1005
|
+
cache.dispatchProps = mapDispatch ? mapDispatch.map(HOOK.dispatch, ownProps) : mapDispatchToProps ? bound(mapDispatchToProps) : {dispatch: HOOK.dispatch};
|
|
1006
|
+
cache.dispatchOwnProps = ownProps;
|
|
1007
|
+
}
|
|
1008
|
+
const merged = {...ownProps, ...cache.stateProps, ...cache.dispatchProps};
|
|
1009
|
+
if (!cache.merged || (!cache.kept || ownPropsChanged) && !shallowEqual(cache.merged, merged)) cache.merged = merged;
|
|
1010
|
+
if (!cache.element || cache.rendered !== cache.merged || cache.ref !== props.reactReduxForwardedRef) {
|
|
1011
|
+
cache.rendered = cache.merged;
|
|
1012
|
+
cache.ref = props.reactReduxForwardedRef;
|
|
1013
|
+
cache.element = createElement(WrappedComponent, forwardsRef ? {...cache.merged, ref: cache.ref} : cache.merged);
|
|
1014
|
+
}
|
|
1015
|
+
return cache.element;
|
|
1016
|
+
}
|
|
1017
|
+
ConnectFunction.displayName = displayName;
|
|
1018
|
+
const Connect = memo(ConnectFunction);
|
|
1019
|
+
Connect.displayName = displayName;
|
|
1020
|
+
Connect.WrappedComponent = WrappedComponent;
|
|
1021
|
+
if (!forwardsRef) return hoistStatics(Connect, WrappedComponent);
|
|
1022
|
+
const Forwarded = forwardRef((props, ref) => createElement(Connect, {...props, reactReduxForwardedRef: ref}));
|
|
1023
|
+
Forwarded.displayName = displayName;
|
|
1024
|
+
Forwarded.WrappedComponent = WrappedComponent;
|
|
1025
|
+
return hoistStatics(Forwarded, WrappedComponent);
|
|
1026
|
+
};
|
|
1027
|
+
}`;
|
|
1028
|
+
function typedByString(facts, fromToolkit) {
|
|
1029
|
+
var _a;
|
|
1030
|
+
const redux = fromToolkit ? undefined : major(facts === null || facts === void 0 ? void 0 : facts.redux);
|
|
1031
|
+
if (redux !== undefined)
|
|
1032
|
+
return redux >= 5;
|
|
1033
|
+
return ((_a = major(facts === null || facts === void 0 ? void 0 : facts.toolkit)) !== null && _a !== void 0 ? _a : 0) >= 2;
|
|
1034
|
+
}
|
|
1035
|
+
function convertReducerStore(cu, p, facts, ctx) {
|
|
1036
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1037
|
+
var _a;
|
|
1038
|
+
const store = p.store;
|
|
1039
|
+
const { hook, reducer, renderer, selectorHook, listeners } = p.reducerStore;
|
|
1040
|
+
const found = storeStatement(cu, store);
|
|
1041
|
+
if (!found)
|
|
1042
|
+
return cu;
|
|
1043
|
+
const padded = cu.statements[found.index];
|
|
1044
|
+
const statement = padded.element;
|
|
1045
|
+
const cursor = new rewrite_1.Cursor(statement, new rewrite_1.Cursor(cu, undefined));
|
|
1046
|
+
const combined = store.reducerStore === "combined";
|
|
1047
|
+
const legacy = store.reducerStore === "legacy" || store.reducerStore === "legacyThunk";
|
|
1048
|
+
const thunk = store.reducerStore === "legacyThunk";
|
|
1049
|
+
const checks = `if (typeof action !== "object" || action === null || ![Object.prototype, null].includes(Object.getPrototypeOf(action))) throw new Error("Actions must be plain objects.");
|
|
1050
|
+
if (action.type === undefined) throw new Error("Actions may not have an undefined 'type' property.");` +
|
|
1051
|
+
(typedByString(facts, (0, redux_stores_1.importedAs)(cu, redux_stores_1.RTK, "configureStore", "createStore", "legacy_createStore").has(store.api)) ? `\n if (typeof action.type !== "string") throw new Error("Action 'type' property must be a string.");` : "");
|
|
1052
|
+
const update = `${hook}.setState(${reducer}(${hook}.getState(), action), true);`;
|
|
1053
|
+
const notified = listeners ? `${update}\n for (const listener of [...${listeners}.values()]) listener();` : update;
|
|
1054
|
+
const setState = renderer ? listeners ? `unstable_batchedUpdates(() => {\n ${notified}\n });` : `unstable_batchedUpdates(() => ${update.slice(0, -1)});` : notified;
|
|
1055
|
+
const members = `getState: ${hook}.getState, dispatch: ${hook}.dispatch` + (listeners ? `, subscribe: listener => {
|
|
1056
|
+
if (typeof listener !== "function") throw new Error("Expected the listener to be a function. Instead, received: '" + (listener === null ? "null" : Array.isArray(listener) ? "array" : typeof listener) + "'");
|
|
1057
|
+
const id = Symbol();
|
|
1058
|
+
${listeners}.set(id, listener);
|
|
1059
|
+
return () => {
|
|
1060
|
+
${listeners}.delete(id);
|
|
1061
|
+
};
|
|
1062
|
+
}` : "");
|
|
1063
|
+
const generated = [
|
|
1064
|
+
combined ? yield (0, javascript_1.template) `const ${(0, javascript_1.raw)(reducer)} = combineReducers(${found.reducer});`.apply(statement, cursor) :
|
|
1065
|
+
yield (0, javascript_1.template) `const ${(0, javascript_1.raw)(reducer)} = ${found.reducer};`.apply(statement, cursor),
|
|
1066
|
+
found.preloaded ?
|
|
1067
|
+
yield (0, javascript_1.template) `export const ${(0, javascript_1.raw)(hook)} = create(() => ${(0, javascript_1.raw)(reducer)}(${found.preloaded}, {type: "@@redux/INIT"}));`.apply(statement, cursor) :
|
|
1068
|
+
yield (0, javascript_1.template) `export const ${(0, javascript_1.raw)(hook)} = create(() => ${(0, javascript_1.raw)(reducer)}(undefined, {type: "@@redux/INIT"}));`.apply(statement, cursor),
|
|
1069
|
+
...listeners ? [yield (0, javascript_1.template) `const ${(0, javascript_1.raw)(listeners)} = new Map();`.apply(statement, cursor)] : [],
|
|
1070
|
+
legacy && !thunk ?
|
|
1071
|
+
yield (0, javascript_1.template) `${(0, javascript_1.raw)(hook)}.dispatch = action => {
|
|
1072
|
+
${(0, javascript_1.raw)(checks)}
|
|
1073
|
+
${(0, javascript_1.raw)(setState)}
|
|
1074
|
+
return action;
|
|
1075
|
+
};`.apply(statement, cursor) :
|
|
1076
|
+
yield (0, javascript_1.template) `${(0, javascript_1.raw)(hook)}.dispatch = action => {
|
|
1077
|
+
if (typeof action === "function") return action(${(0, javascript_1.raw)(hook)}.dispatch, ${(0, javascript_1.raw)(hook)}.getState, undefined);
|
|
1078
|
+
${(0, javascript_1.raw)(checks)}
|
|
1079
|
+
${(0, javascript_1.raw)(setState)}
|
|
1080
|
+
return action;
|
|
1081
|
+
};`.apply(statement, cursor),
|
|
1082
|
+
statement.kind === javascript_1.JS.Kind.ExportAssignment ?
|
|
1083
|
+
yield (0, javascript_1.template) `export default {${(0, javascript_1.raw)(members)}};`.apply(statement, cursor) :
|
|
1084
|
+
yield (0, javascript_1.template) `${(0, javascript_1.raw)(statement.modifiers.some(m => m.keyword === "export") ? "export " : "")}const ${(0, javascript_1.raw)(store.store)} = {${(0, javascript_1.raw)(members)}};`.apply(statement, cursor),
|
|
1085
|
+
...selectorHook ? [yield (0, javascript_1.template) `${(0, javascript_1.raw)(exports.SELECTOR.split("SELECTOR_HOOK").join(selectorHook).split("HOOK").join(hook))}`.apply(statement, cursor)] : [],
|
|
1086
|
+
...renderer ? [yield (0, javascript_1.template) `${(0, javascript_1.raw)(connectSource(hook, facts === null || facts === void 0 ? void 0 : facts.reactRedux))}`.apply(statement, cursor)] : []
|
|
1087
|
+
];
|
|
1088
|
+
const statements = [...cu.statements.slice(0, found.index)];
|
|
1089
|
+
for (let i = 0; i < generated.length; i++) {
|
|
1090
|
+
const prefixed = withPrefix(generated[i], i === 0 ? statement.prefix : space("\n\n"));
|
|
1091
|
+
const element = i === 0 ? prefixed : yield (0, javascript_1.autoFormat)(prefixed, ctx, undefined, new rewrite_1.Cursor(cu, undefined));
|
|
1092
|
+
statements.push(element.kind === java_1.J.Kind.MethodDeclaration ? Object.assign(Object.assign({}, padded), { element, markers: rewrite_1.emptyMarkers }) : Object.assign(Object.assign({}, padded), { element }));
|
|
1093
|
+
}
|
|
1094
|
+
statements.push(...cu.statements.slice(found.index + 1));
|
|
1095
|
+
const result = yield withoutUnused(Object.assign(Object.assign({}, cu), { statements }), (_a = found.consts) !== null && _a !== void 0 ? _a : new Set());
|
|
1096
|
+
return new class extends javascript_1.JavaScriptVisitor {
|
|
1097
|
+
visitJsCompilationUnit(unit, q) {
|
|
1098
|
+
const _super = Object.create(null, {
|
|
1099
|
+
visitJsCompilationUnit: { get: () => super.visitJsCompilationUnit }
|
|
1100
|
+
});
|
|
1101
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1102
|
+
const quoteStyle = quoteOf(cu);
|
|
1103
|
+
(0, javascript_1.maybeRemoveImport)(this, redux_stores_1.RTK, "configureStore");
|
|
1104
|
+
for (const module of [redux_stores_1.REDUX, redux_stores_1.RTK])
|
|
1105
|
+
for (const member of ["createStore", "legacy_createStore"])
|
|
1106
|
+
(0, javascript_1.maybeRemoveImport)(this, module, member);
|
|
1107
|
+
if (legacy) {
|
|
1108
|
+
for (const member of ["applyMiddleware", "compose"])
|
|
1109
|
+
(0, javascript_1.maybeRemoveImport)(this, redux_stores_1.REDUX, member);
|
|
1110
|
+
for (const member of ["default", "thunk"])
|
|
1111
|
+
(0, javascript_1.maybeRemoveImport)(this, "redux-thunk", member);
|
|
1112
|
+
for (const module of ["redux-devtools-extension", "@redux-devtools/extension"]) {
|
|
1113
|
+
for (const member of ["composeWithDevTools", "devToolsEnhancer", "devToolsEnhancerLogOnlyInProduction"])
|
|
1114
|
+
(0, javascript_1.maybeRemoveImport)(this, module, member);
|
|
1115
|
+
}
|
|
1116
|
+
}
|
|
1117
|
+
if (combined)
|
|
1118
|
+
(0, javascript_1.maybeAddImport)(this, { module: redux_stores_1.RTK, member: "combineReducers", onlyIfReferenced: false, quoteStyle });
|
|
1119
|
+
(0, javascript_1.maybeAddImport)(this, { module: ZUSTAND, member: "create", onlyIfReferenced: false, quoteStyle });
|
|
1120
|
+
if (selectorHook)
|
|
1121
|
+
for (const member of ["useRef", "useSyncExternalStore"])
|
|
1122
|
+
(0, javascript_1.maybeAddImport)(this, { module: "react", member, onlyIfReferenced: false, quoteStyle });
|
|
1123
|
+
if (renderer) {
|
|
1124
|
+
for (const member of ["createElement", "forwardRef", "memo", "useRef"])
|
|
1125
|
+
(0, javascript_1.maybeAddImport)(this, { module: "react", member, onlyIfReferenced: false, quoteStyle });
|
|
1126
|
+
(0, javascript_1.maybeAddImport)(this, { module: renderer, member: "unstable_batchedUpdates", onlyIfReferenced: false, quoteStyle });
|
|
1127
|
+
}
|
|
1128
|
+
return _super.visitJsCompilationUnit.call(this, unit, q);
|
|
1129
|
+
});
|
|
1130
|
+
}
|
|
1131
|
+
}().visit(result, ctx);
|
|
1132
|
+
});
|
|
1133
|
+
}
|
|
1134
|
+
function fragment(tag) {
|
|
1135
|
+
const empty = () => ({ kind: java_1.J.Kind.Empty, id: (0, rewrite_1.randomId)(), prefix: java_1.emptySpace, markers: rewrite_1.emptyMarkers });
|
|
1136
|
+
return (0, mutative_1.create)(tag, draft => {
|
|
1137
|
+
draft.openName = Object.assign(Object.assign({}, draft.openName), { before: java_1.emptySpace, element: empty() });
|
|
1138
|
+
draft.closingName = Object.assign(Object.assign({}, draft.closingName), { before: java_1.emptySpace, element: empty() });
|
|
1139
|
+
draft.attributes = [];
|
|
1140
|
+
draft.afterName = java_1.emptySpace;
|
|
1141
|
+
draft.afterClosingName = java_1.emptySpace;
|
|
1142
|
+
});
|
|
1143
|
+
}
|
|
1144
|
+
function convertReducerConsumer(cu, p, scanned, ctx) {
|
|
1145
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1146
|
+
const { hook, selectorHook } = p.reducerStore;
|
|
1147
|
+
const source = scanned.sources.get(cu.sourcePath);
|
|
1148
|
+
const reads = (selector) => !!(0, zustand_plan_1.selectorRead)(selector) ||
|
|
1149
|
+
(0, java_1.isIdentifier)(selector) && !!source && (0, zustand_plan_1.readSelector)(source, selector.simpleName, scanned.sources, resolverOf(scanned));
|
|
1150
|
+
const bindings = (0, module_imports_1.importBindings)(cu);
|
|
1151
|
+
const followed = new Map(bindings.filter(b => followsImport(b.module)).map(b => [b.local, b]));
|
|
1152
|
+
const reactRedux = (name, member) => { var _a, _b; return ((_a = followed.get(name)) === null || _a === void 0 ? void 0 : _a.module) === zustand_plan_1.REACT_REDUX && ((_b = followed.get(name)) === null || _b === void 0 ? void 0 : _b.name) === member; };
|
|
1153
|
+
const removed = new Set();
|
|
1154
|
+
const uses = new Set();
|
|
1155
|
+
const result = yield new class extends javascript_1.JavaScriptVisitor {
|
|
1156
|
+
visitVariableDeclarations(declarations, q) {
|
|
1157
|
+
const _super = Object.create(null, {
|
|
1158
|
+
visitVariableDeclarations: { get: () => super.visitVariableDeclarations }
|
|
1159
|
+
});
|
|
1160
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1161
|
+
const variable = declarations.variables.length === 1 ? declarations.variables[0].element : undefined;
|
|
1162
|
+
if (variable && (0, zustand_plan_1.isDispatcherDeclaration)(variable, followed)) {
|
|
1163
|
+
removed.add("useDispatch");
|
|
1164
|
+
uses.add(hook);
|
|
1165
|
+
const initializer = variable.initializer.element;
|
|
1166
|
+
const replaced = yield (0, javascript_1.template) `${(0, javascript_1.raw)(hook)}.dispatch`.apply(initializer, new rewrite_1.Cursor(initializer, this.cursor));
|
|
1167
|
+
return (0, mutative_1.create)(declarations, draft => {
|
|
1168
|
+
draft.variables[0].element.initializer.element = withPrefix(replaced, initializer.prefix);
|
|
1169
|
+
});
|
|
1170
|
+
}
|
|
1171
|
+
return _super.visitVariableDeclarations.call(this, declarations, q);
|
|
1172
|
+
});
|
|
1173
|
+
}
|
|
1174
|
+
visitMethodInvocation(call, q) {
|
|
1175
|
+
const _super = Object.create(null, {
|
|
1176
|
+
visitMethodInvocation: { get: () => super.visitMethodInvocation }
|
|
1177
|
+
});
|
|
1178
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1179
|
+
const visited = yield _super.visitMethodInvocation.call(this, call, q);
|
|
1180
|
+
if (visited.select || !reactRedux(visited.name.simpleName, "useSelector"))
|
|
1181
|
+
return visited;
|
|
1182
|
+
removed.add("useSelector");
|
|
1183
|
+
const through = selectorHook && !reads((0, zustand_plan_1.argumentsOf)(visited)[0]) ? selectorHook : hook;
|
|
1184
|
+
uses.add(through);
|
|
1185
|
+
return (0, mutative_1.create)(visited, draft => {
|
|
1186
|
+
draft.name = Object.assign(Object.assign({}, draft.name), { simpleName: through, fieldType: undefined, type: undefined });
|
|
1187
|
+
draft.methodType = undefined;
|
|
1188
|
+
});
|
|
1189
|
+
});
|
|
1190
|
+
}
|
|
1191
|
+
visitJsxTag(tag, q) {
|
|
1192
|
+
const _super = Object.create(null, {
|
|
1193
|
+
visitJsxTag: { get: () => super.visitJsxTag }
|
|
1194
|
+
});
|
|
1195
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1196
|
+
const name = tag.openName.element;
|
|
1197
|
+
if ((0, java_1.isIdentifier)(name) && reactRedux(name.simpleName, "Provider")) {
|
|
1198
|
+
const child = (0, zustand_plan_1.providerChild)(tag);
|
|
1199
|
+
if (child) {
|
|
1200
|
+
removed.add("Provider");
|
|
1201
|
+
return withPrefix(yield this.visit(child, q, this.cursor), tag.prefix);
|
|
1202
|
+
}
|
|
1203
|
+
if ((0, zustand_plan_1.providerChildren)(tag)) {
|
|
1204
|
+
removed.add("Provider");
|
|
1205
|
+
return fragment(yield _super.visitJsxTag.call(this, tag, q));
|
|
1206
|
+
}
|
|
1207
|
+
}
|
|
1208
|
+
return _super.visitJsxTag.call(this, tag, q);
|
|
1209
|
+
});
|
|
1210
|
+
}
|
|
1211
|
+
}().visit(cu, ctx);
|
|
1212
|
+
const connected = p.reducerStore.renderer && bindings.some(b => b.module === zustand_plan_1.REACT_REDUX && b.name === "connect") ?
|
|
1213
|
+
yield (yield new javascript_1.ChangeImport({ oldModule: zustand_plan_1.REACT_REDUX, oldMember: "connect", newModule: (0, zustand_plan_1.relativeSpecifier)(cu.sourcePath, p.store.path) }).editor()).visit(result, ctx) :
|
|
1214
|
+
result;
|
|
1215
|
+
return yield new class extends javascript_1.JavaScriptVisitor {
|
|
1216
|
+
visitJsCompilationUnit(unit, q) {
|
|
1217
|
+
const _super = Object.create(null, {
|
|
1218
|
+
visitJsCompilationUnit: { get: () => super.visitJsCompilationUnit }
|
|
1219
|
+
});
|
|
1220
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1221
|
+
for (const member of ["useSelector", "useDispatch", "Provider"]) {
|
|
1222
|
+
if (removed.has(member))
|
|
1223
|
+
(0, javascript_1.maybeRemoveImport)(this, zustand_plan_1.REACT_REDUX, member);
|
|
1224
|
+
}
|
|
1225
|
+
if (removed.has("Provider")) {
|
|
1226
|
+
for (const binding of bindings) {
|
|
1227
|
+
if (binding.module !== zustand_plan_1.REACT_REDUX && resolverOf(scanned)(cu.sourcePath, binding.module) === p.store.path)
|
|
1228
|
+
(0, javascript_1.maybeRemoveImport)(this, binding.module, binding.name);
|
|
1229
|
+
}
|
|
1230
|
+
}
|
|
1231
|
+
for (const member of [hook, selectorHook]) {
|
|
1232
|
+
if (member && uses.has(member))
|
|
1233
|
+
(0, javascript_1.maybeAddImport)(this, { module: (0, zustand_plan_1.relativeSpecifier)(cu.sourcePath, p.store.path), member, onlyIfReferenced: false, quoteStyle: quoteOf(cu) });
|
|
1234
|
+
}
|
|
1235
|
+
return _super.visitJsCompilationUnit.call(this, unit, q);
|
|
1236
|
+
});
|
|
1237
|
+
}
|
|
1238
|
+
}().visit(connected, ctx);
|
|
1239
|
+
});
|
|
1240
|
+
}
|
|
654
1241
|
//# sourceMappingURL=redux-to-zustand.js.map
|