@openrewrite/recipes-react 0.17.0 → 0.18.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +9 -5
- package/dist/index.js.map +1 -1
- package/dist/migration/cra-to-vite/find-cra-config-blockers.d.ts.map +1 -1
- package/dist/migration/cra-to-vite/find-cra-config-blockers.js +3 -5
- package/dist/migration/cra-to-vite/find-cra-config-blockers.js.map +1 -1
- package/dist/migration/cra-to-vite/update-react-app-env-types.d.ts.map +1 -1
- package/dist/migration/cra-to-vite/update-react-app-env-types.js +5 -2
- package/dist/migration/cra-to-vite/update-react-app-env-types.js.map +1 -1
- package/dist/react18/replace-unmount-component-at-node.d.ts.map +1 -1
- package/dist/react18/replace-unmount-component-at-node.js +8 -1
- package/dist/react18/replace-unmount-component-at-node.js.map +1 -1
- package/dist/react19/find-legacy-context-api.d.ts.map +1 -1
- package/dist/react19/find-legacy-context-api.js +21 -38
- package/dist/react19/find-legacy-context-api.js.map +1 -1
- package/dist/{react18 → react19}/remove-unstable-batched-updates.d.ts +2 -2
- package/dist/react19/remove-unstable-batched-updates.d.ts.map +1 -0
- package/dist/react19/remove-unstable-batched-updates.js +295 -0
- package/dist/react19/remove-unstable-batched-updates.js.map +1 -0
- package/dist/react19/replace-default-props.d.ts +1 -0
- package/dist/react19/replace-default-props.d.ts.map +1 -1
- package/dist/react19/replace-default-props.js +198 -151
- package/dist/react19/replace-default-props.js.map +1 -1
- package/dist/react19/replace-react-shallow-renderer.d.ts.map +1 -1
- package/dist/react19/replace-react-shallow-renderer.js +10 -25
- package/dist/react19/replace-react-shallow-renderer.js.map +1 -1
- package/dist/refactoring/class-to-functional.d.ts.map +1 -1
- package/dist/refactoring/class-to-functional.js +2 -0
- package/dist/refactoring/class-to-functional.js.map +1 -1
- package/dist/refactoring/create-class-to-es6.d.ts.map +1 -1
- package/dist/refactoring/create-class-to-es6.js +14 -49
- package/dist/refactoring/create-class-to-es6.js.map +1 -1
- package/dist/search/find-function-component-default-props.d.ts.map +1 -1
- package/dist/search/find-function-component-default-props.js +6 -15
- package/dist/search/find-function-component-default-props.js.map +1 -1
- package/dist/search/find-redux-stores.d.ts +12 -0
- package/dist/search/find-redux-stores.d.ts.map +1 -0
- package/dist/search/find-redux-stores.js +87 -0
- package/dist/search/find-redux-stores.js.map +1 -0
- package/dist/simplify-object-pattern-property.d.ts.map +1 -1
- package/dist/simplify-object-pattern-property.js +14 -21
- package/dist/simplify-object-pattern-property.js.map +1 -1
- package/dist/simplify-react-imports.d.ts.map +1 -1
- package/dist/simplify-react-imports.js +10 -59
- package/dist/simplify-react-imports.js.map +1 -1
- package/dist/testing/enzyme-to-react-testing-library.d.ts +10 -1
- package/dist/testing/enzyme-to-react-testing-library.d.ts.map +1 -1
- package/dist/testing/enzyme-to-react-testing-library.js +79 -22
- package/dist/testing/enzyme-to-react-testing-library.js.map +1 -1
- package/dist/testing/enzyme-wrapper-scope.d.ts +3 -1
- package/dist/testing/enzyme-wrapper-scope.d.ts.map +1 -1
- package/dist/testing/enzyme-wrapper-scope.js +10 -7
- package/dist/testing/enzyme-wrapper-scope.js.map +1 -1
- package/dist/util/java-recipe-proxy.d.ts +2 -0
- package/dist/util/java-recipe-proxy.d.ts.map +1 -1
- package/dist/util/java-recipe-proxy.js +14 -2
- package/dist/util/java-recipe-proxy.js.map +1 -1
- package/dist/util/reindent.d.ts +5 -0
- package/dist/util/reindent.d.ts.map +1 -0
- package/dist/util/reindent.js +44 -0
- package/dist/util/reindent.js.map +1 -0
- package/dist/zustand/redux-stores.d.ts +74 -0
- package/dist/zustand/redux-stores.d.ts.map +1 -0
- package/dist/zustand/redux-stores.js +402 -0
- package/dist/zustand/redux-stores.js.map +1 -0
- package/dist/zustand/redux-to-zustand.d.ts +76 -0
- package/dist/zustand/redux-to-zustand.d.ts.map +1 -0
- package/dist/zustand/redux-to-zustand.js +653 -0
- package/dist/zustand/redux-to-zustand.js.map +1 -0
- package/dist/zustand/zustand-plan.d.ts +96 -0
- package/dist/zustand/zustand-plan.d.ts.map +1 -0
- package/dist/zustand/zustand-plan.js +822 -0
- package/dist/zustand/zustand-plan.js.map +1 -0
- package/package.json +1 -1
- package/src/index.ts +7 -3
- package/src/migration/cra-to-vite/find-cra-config-blockers.ts +3 -5
- package/src/migration/cra-to-vite/update-react-app-env-types.ts +3 -3
- package/src/react18/replace-unmount-component-at-node.ts +13 -2
- package/src/react19/find-legacy-context-api.ts +21 -42
- package/src/react19/remove-unstable-batched-updates.ts +301 -0
- package/src/react19/replace-default-props.ts +212 -159
- package/src/react19/replace-react-shallow-renderer.ts +13 -36
- package/src/refactoring/class-to-functional.ts +3 -1
- package/src/refactoring/create-class-to-es6.ts +12 -63
- package/src/search/find-function-component-default-props.ts +8 -10
- package/src/search/find-redux-stores.ts +76 -0
- package/src/simplify-object-pattern-property.ts +22 -49
- package/src/simplify-react-imports.ts +12 -66
- package/src/testing/enzyme-to-react-testing-library.ts +112 -27
- package/src/testing/enzyme-wrapper-scope.ts +15 -10
- package/src/util/java-recipe-proxy.ts +15 -2
- package/src/util/reindent.ts +35 -0
- package/src/zustand/redux-stores.ts +409 -0
- package/src/zustand/redux-to-zustand.ts +657 -0
- package/src/zustand/zustand-plan.ts +843 -0
- package/dist/react18/remove-unstable-batched-updates.d.ts.map +0 -1
- package/dist/react18/remove-unstable-batched-updates.js +0 -170
- package/dist/react18/remove-unstable-batched-updates.js.map +0 -1
- package/src/react18/remove-unstable-batched-updates.ts +0 -192
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"remove-unstable-batched-updates.d.ts","sourceRoot":"","sources":["../../src/react18/remove-unstable-batched-updates.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,gBAAgB,EAAE,MAAM,EAAE,WAAW,EAAa,MAAM,sBAAsB,CAAC;AA8BvF,qBAAa,4BAA6B,SAAQ,MAAM;IACpD,QAAQ,CAAC,IAAI,8DAA8D;IAC3E,QAAQ,CAAC,WAAW,sCAAsC;IAC1D,QAAQ,CAAC,WAAW,sJAAsJ;IAEpK,MAAM,IAAI,OAAO,CAAC,WAAW,CAAC,GAAG,EAAE,gBAAgB,CAAC,CAAC;CA4J9D"}
|
|
@@ -1,170 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.RemoveUnstableBatchedUpdates = void 0;
|
|
13
|
-
const rewrite_1 = require("@openrewrite/rewrite");
|
|
14
|
-
const javascript_1 = require("@openrewrite/rewrite/javascript");
|
|
15
|
-
const java_1 = require("@openrewrite/rewrite/java");
|
|
16
|
-
const mutative_1 = require("mutative");
|
|
17
|
-
class RemoveUnstableBatchedUpdates extends rewrite_1.Recipe {
|
|
18
|
-
constructor() {
|
|
19
|
-
super(...arguments);
|
|
20
|
-
this.name = "org.openrewrite.react.18.remove-unstable-batched-updates";
|
|
21
|
-
this.displayName = "Remove `unstable_batchedUpdates`";
|
|
22
|
-
this.description = "Removes `unstable_batchedUpdates` wrappers from `react-dom`. React 18 automatically batches all state updates, making this function unnecessary.";
|
|
23
|
-
}
|
|
24
|
-
editor() {
|
|
25
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
26
|
-
return (0, rewrite_1.check)((0, rewrite_1.not)((0, javascript_1.isVendoredOrBundled)()), new class extends javascript_1.JavaScriptVisitor {
|
|
27
|
-
constructor() {
|
|
28
|
-
super(...arguments);
|
|
29
|
-
this.changed = false;
|
|
30
|
-
}
|
|
31
|
-
visitJsCompilationUnit(cu, ctx) {
|
|
32
|
-
const _super = Object.create(null, {
|
|
33
|
-
visitJsCompilationUnit: { get: () => super.visitJsCompilationUnit }
|
|
34
|
-
});
|
|
35
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
36
|
-
this.changed = false;
|
|
37
|
-
let result = yield _super.visitJsCompilationUnit.call(this, cu, ctx);
|
|
38
|
-
result = this.inlineTopLevelStatements(result);
|
|
39
|
-
if (this.changed) {
|
|
40
|
-
(0, javascript_1.maybeRemoveImport)(this, "react-dom", "unstable_batchedUpdates");
|
|
41
|
-
}
|
|
42
|
-
return result;
|
|
43
|
-
});
|
|
44
|
-
}
|
|
45
|
-
visitBlock(block, ctx) {
|
|
46
|
-
const _super = Object.create(null, {
|
|
47
|
-
visitBlock: { get: () => super.visitBlock }
|
|
48
|
-
});
|
|
49
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
50
|
-
let b = yield _super.visitBlock.call(this, block, ctx);
|
|
51
|
-
return this.inlineBlockStatements(b);
|
|
52
|
-
});
|
|
53
|
-
}
|
|
54
|
-
inlineTopLevelStatements(cu) {
|
|
55
|
-
const result = this.spliceStatements(cu.statements);
|
|
56
|
-
if (!result)
|
|
57
|
-
return cu;
|
|
58
|
-
return (0, mutative_1.create)(cu, draft => {
|
|
59
|
-
draft.statements = result;
|
|
60
|
-
});
|
|
61
|
-
}
|
|
62
|
-
inlineBlockStatements(block) {
|
|
63
|
-
const result = this.spliceStatements(block.statements);
|
|
64
|
-
if (!result)
|
|
65
|
-
return block;
|
|
66
|
-
return (0, mutative_1.create)(block, draft => {
|
|
67
|
-
draft.statements = result;
|
|
68
|
-
});
|
|
69
|
-
}
|
|
70
|
-
spliceStatements(statements) {
|
|
71
|
-
const newStatements = [];
|
|
72
|
-
let changed = false;
|
|
73
|
-
for (const paddedStmt of statements) {
|
|
74
|
-
const stmt = paddedStmt.element;
|
|
75
|
-
if (!stmt) {
|
|
76
|
-
newStatements.push(paddedStmt);
|
|
77
|
-
continue;
|
|
78
|
-
}
|
|
79
|
-
const bodyStatements = this.extractBatchedUpdatesBody(stmt);
|
|
80
|
-
if (bodyStatements !== null && bodyStatements.length > 0) {
|
|
81
|
-
const outerPrefix = paddedStmt.element.prefix;
|
|
82
|
-
const outerIndent = this.getIndentation(outerPrefix);
|
|
83
|
-
const innerIndent = bodyStatements.length > 0
|
|
84
|
-
? this.getIndentation(bodyStatements[0].element.prefix)
|
|
85
|
-
: outerIndent;
|
|
86
|
-
for (const bodyStmt of bodyStatements) {
|
|
87
|
-
const adjustedPrefix = this.adjustIndentation(bodyStmt.element.prefix, innerIndent, outerIndent);
|
|
88
|
-
newStatements.push(Object.assign(Object.assign({}, bodyStmt), { element: Object.assign(Object.assign({}, bodyStmt.element), { prefix: adjustedPrefix }) }));
|
|
89
|
-
}
|
|
90
|
-
changed = true;
|
|
91
|
-
this.changed = true;
|
|
92
|
-
}
|
|
93
|
-
else {
|
|
94
|
-
newStatements.push(paddedStmt);
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
return changed ? newStatements : null;
|
|
98
|
-
}
|
|
99
|
-
getIndentation(prefix) {
|
|
100
|
-
var _a;
|
|
101
|
-
const ws = (_a = prefix === null || prefix === void 0 ? void 0 : prefix.whitespace) !== null && _a !== void 0 ? _a : '';
|
|
102
|
-
const lastNewline = ws.lastIndexOf('\n');
|
|
103
|
-
return lastNewline >= 0 ? ws.substring(lastNewline + 1) : ws;
|
|
104
|
-
}
|
|
105
|
-
adjustIndentation(prefix, fromIndent, toIndent) {
|
|
106
|
-
var _a;
|
|
107
|
-
if (fromIndent === toIndent)
|
|
108
|
-
return prefix;
|
|
109
|
-
const ws = (_a = prefix === null || prefix === void 0 ? void 0 : prefix.whitespace) !== null && _a !== void 0 ? _a : '';
|
|
110
|
-
const lastNewline = ws.lastIndexOf('\n');
|
|
111
|
-
if (lastNewline < 0)
|
|
112
|
-
return Object.assign(Object.assign({}, prefix), { whitespace: toIndent });
|
|
113
|
-
const currentIndent = ws.substring(lastNewline + 1);
|
|
114
|
-
const adjusted = currentIndent.startsWith(fromIndent)
|
|
115
|
-
? toIndent + currentIndent.substring(fromIndent.length)
|
|
116
|
-
: toIndent;
|
|
117
|
-
return Object.assign(Object.assign({}, prefix), { whitespace: ws.substring(0, lastNewline + 1) + adjusted });
|
|
118
|
-
}
|
|
119
|
-
extractBatchedUpdatesBody(stmt) {
|
|
120
|
-
let expr;
|
|
121
|
-
if (stmt.kind === javascript_1.JS.Kind.ExpressionStatement) {
|
|
122
|
-
expr = stmt.expression;
|
|
123
|
-
}
|
|
124
|
-
else if (stmt.kind === java_1.J.Kind.MethodInvocation) {
|
|
125
|
-
expr = stmt;
|
|
126
|
-
}
|
|
127
|
-
else {
|
|
128
|
-
return null;
|
|
129
|
-
}
|
|
130
|
-
if (!expr || expr.kind !== java_1.J.Kind.MethodInvocation)
|
|
131
|
-
return null;
|
|
132
|
-
const method = expr;
|
|
133
|
-
if (!this.isBatchedUpdatesCall(method))
|
|
134
|
-
return null;
|
|
135
|
-
const args = method.arguments;
|
|
136
|
-
if (!args)
|
|
137
|
-
return null;
|
|
138
|
-
const elements = args.elements;
|
|
139
|
-
if (!elements || elements.length === 0)
|
|
140
|
-
return null;
|
|
141
|
-
const callback = elements[0].element;
|
|
142
|
-
if (!callback)
|
|
143
|
-
return null;
|
|
144
|
-
if (callback.kind === javascript_1.JS.Kind.ArrowFunction) {
|
|
145
|
-
const arrow = callback;
|
|
146
|
-
const body = arrow.lambda.body;
|
|
147
|
-
if (body && body.kind === java_1.J.Kind.Block) {
|
|
148
|
-
return body.statements;
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
return null;
|
|
152
|
-
}
|
|
153
|
-
isBatchedUpdatesCall(method) {
|
|
154
|
-
var _a;
|
|
155
|
-
if (method.name.simpleName === 'unstable_batchedUpdates') {
|
|
156
|
-
if (!method.select)
|
|
157
|
-
return true;
|
|
158
|
-
const select = (_a = method.select.element) !== null && _a !== void 0 ? _a : method.select;
|
|
159
|
-
if ((0, java_1.isIdentifier)(select) && select.simpleName === 'ReactDOM') {
|
|
160
|
-
return true;
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
return false;
|
|
164
|
-
}
|
|
165
|
-
}());
|
|
166
|
-
});
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
exports.RemoveUnstableBatchedUpdates = RemoveUnstableBatchedUpdates;
|
|
170
|
-
//# sourceMappingURL=remove-unstable-batched-updates.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"remove-unstable-batched-updates.js","sourceRoot":"","sources":["../../src/react18/remove-unstable-batched-updates.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,kDAAuF;AACvF,gEAA8G;AAC9G,oDAA0D;AAC1D,uCAAgC;AA2BhC,MAAa,4BAA6B,SAAQ,gBAAM;IAAxD;;QACa,SAAI,GAAG,0DAA0D,CAAC;QAClE,gBAAW,GAAG,kCAAkC,CAAC;QACjD,gBAAW,GAAG,kJAAkJ,CAAC;IA8J9K,CAAC;IA5JS,MAAM;;YACR,OAAO,IAAA,eAAK,EAAC,IAAA,aAAG,EAAC,IAAA,gCAAmB,GAAE,CAAC,EAAE,IAAI,KAAM,SAAQ,8BAAmC;gBAAjD;;oBACjC,YAAO,GAAG,KAAK,CAAC;gBAwJ5B,CAAC;gBAtJkB,sBAAsB,CAAC,EAAsB,EAAE,GAAqB;;;;;wBAC/E,IAAI,CAAC,OAAO,GAAG,KAAK,CAAC;wBACrB,IAAI,MAAM,GAAG,MAAM,OAAM,sBAAsB,YAAC,EAAE,EAAE,GAAG,CAAuB,CAAC;wBAE/E,MAAM,GAAG,IAAI,CAAC,wBAAwB,CAAC,MAAM,CAAC,CAAC;wBAE/C,IAA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MAAC,MAAM,CAAC,MAAc,CAAC,OAAO,mCAAI,MAAM,CAAC,MAAM,CAAC;wBAC/D,IAAI,IAAA,mBAAY,EAAC,MAAM,CAAC,IAAI,MAAM,CAAC,UAAU,KAAK,UAAU,EAAE,CAAC;4BAC3D,OAAO,IAAI,CAAC;wBAChB,CAAC;oBACL,CAAC;oBAED,OAAO,KAAK,CAAC;gBACjB,CAAC;aACJ,EAAE,CAAC,CAAC;QACT,CAAC;KAAA;CACJ;AAjKD,oEAiKC"}
|
|
@@ -1,192 +0,0 @@
|
|
|
1
|
-
import {ExecutionContext, Recipe, TreeVisitor, check, not} from "@openrewrite/rewrite";
|
|
2
|
-
import {JavaScriptVisitor, JS, maybeRemoveImport, isVendoredOrBundled} from "@openrewrite/rewrite/javascript";
|
|
3
|
-
import {isIdentifier, J} from "@openrewrite/rewrite/java";
|
|
4
|
-
import {create} from "mutative";
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Removes `unstable_batchedUpdates` wrappers from `react-dom`.
|
|
8
|
-
*
|
|
9
|
-
* In React 18, all state updates are automatically batched,
|
|
10
|
-
* making `unstable_batchedUpdates` unnecessary. This recipe
|
|
11
|
-
* unwraps the callback body and removes the wrapping call.
|
|
12
|
-
*
|
|
13
|
-
* Before:
|
|
14
|
-
* ```tsx
|
|
15
|
-
* import { unstable_batchedUpdates } from 'react-dom';
|
|
16
|
-
*
|
|
17
|
-
* unstable_batchedUpdates(() => {
|
|
18
|
-
* setCount(1);
|
|
19
|
-
* setFlag(true);
|
|
20
|
-
* });
|
|
21
|
-
* ```
|
|
22
|
-
*
|
|
23
|
-
* After:
|
|
24
|
-
* ```tsx
|
|
25
|
-
* setCount(1);
|
|
26
|
-
* setFlag(true);
|
|
27
|
-
* ```
|
|
28
|
-
*
|
|
29
|
-
* @see https://react.dev/blog/2022/03/29/react-v18#new-feature-automatic-batching
|
|
30
|
-
*/
|
|
31
|
-
export class RemoveUnstableBatchedUpdates extends Recipe {
|
|
32
|
-
readonly name = "org.openrewrite.react.18.remove-unstable-batched-updates";
|
|
33
|
-
readonly displayName = "Remove `unstable_batchedUpdates`";
|
|
34
|
-
readonly description = "Removes `unstable_batchedUpdates` wrappers from `react-dom`. React 18 automatically batches all state updates, making this function unnecessary.";
|
|
35
|
-
|
|
36
|
-
async editor(): Promise<TreeVisitor<any, ExecutionContext>> {
|
|
37
|
-
return check(not(isVendoredOrBundled()), new class extends JavaScriptVisitor<ExecutionContext> {
|
|
38
|
-
private changed = false;
|
|
39
|
-
|
|
40
|
-
override async visitJsCompilationUnit(cu: JS.CompilationUnit, ctx: ExecutionContext): Promise<J | undefined> {
|
|
41
|
-
this.changed = false;
|
|
42
|
-
let result = await super.visitJsCompilationUnit(cu, ctx) as JS.CompilationUnit;
|
|
43
|
-
|
|
44
|
-
result = this.inlineTopLevelStatements(result);
|
|
45
|
-
|
|
46
|
-
if (this.changed) {
|
|
47
|
-
maybeRemoveImport(this, "react-dom", "unstable_batchedUpdates");
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
return result;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
override async visitBlock(block: J.Block, ctx: ExecutionContext): Promise<J | undefined> {
|
|
54
|
-
let b = await super.visitBlock(block, ctx) as J.Block;
|
|
55
|
-
return this.inlineBlockStatements(b);
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
private inlineTopLevelStatements(cu: JS.CompilationUnit): JS.CompilationUnit {
|
|
59
|
-
const result = this.spliceStatements(cu.statements);
|
|
60
|
-
if (!result) return cu;
|
|
61
|
-
return create(cu, draft => {
|
|
62
|
-
draft.statements = result as any;
|
|
63
|
-
});
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
private inlineBlockStatements(block: J.Block): J.Block {
|
|
67
|
-
const result = this.spliceStatements(block.statements);
|
|
68
|
-
if (!result) return block;
|
|
69
|
-
return create(block, draft => {
|
|
70
|
-
draft.statements = result as any;
|
|
71
|
-
});
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
private spliceStatements(statements: any[]): any[] | null {
|
|
75
|
-
const newStatements: any[] = [];
|
|
76
|
-
let changed = false;
|
|
77
|
-
|
|
78
|
-
for (const paddedStmt of statements) {
|
|
79
|
-
const stmt = paddedStmt.element;
|
|
80
|
-
if (!stmt) {
|
|
81
|
-
newStatements.push(paddedStmt);
|
|
82
|
-
continue;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
const bodyStatements = this.extractBatchedUpdatesBody(stmt);
|
|
86
|
-
if (bodyStatements !== null && bodyStatements.length > 0) {
|
|
87
|
-
// Compute indentation adjustment: replace the inner callback
|
|
88
|
-
// indentation with the outer statement's indentation
|
|
89
|
-
const outerPrefix = paddedStmt.element.prefix;
|
|
90
|
-
const outerIndent = this.getIndentation(outerPrefix);
|
|
91
|
-
const innerIndent = bodyStatements.length > 0
|
|
92
|
-
? this.getIndentation(bodyStatements[0].element.prefix)
|
|
93
|
-
: outerIndent;
|
|
94
|
-
|
|
95
|
-
for (const bodyStmt of bodyStatements) {
|
|
96
|
-
const adjustedPrefix = this.adjustIndentation(
|
|
97
|
-
bodyStmt.element.prefix, innerIndent, outerIndent
|
|
98
|
-
);
|
|
99
|
-
newStatements.push({
|
|
100
|
-
...bodyStmt,
|
|
101
|
-
element: {
|
|
102
|
-
...bodyStmt.element,
|
|
103
|
-
prefix: adjustedPrefix
|
|
104
|
-
}
|
|
105
|
-
});
|
|
106
|
-
}
|
|
107
|
-
changed = true;
|
|
108
|
-
this.changed = true;
|
|
109
|
-
} else {
|
|
110
|
-
newStatements.push(paddedStmt);
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
return changed ? newStatements : null;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
private getIndentation(prefix: any): string {
|
|
118
|
-
const ws: string = prefix?.whitespace ?? '';
|
|
119
|
-
// Get the indentation from the last line of the whitespace
|
|
120
|
-
const lastNewline = ws.lastIndexOf('\n');
|
|
121
|
-
return lastNewline >= 0 ? ws.substring(lastNewline + 1) : ws;
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
private adjustIndentation(prefix: any, fromIndent: string, toIndent: string): any {
|
|
125
|
-
if (fromIndent === toIndent) return prefix;
|
|
126
|
-
const ws: string = prefix?.whitespace ?? '';
|
|
127
|
-
const lastNewline = ws.lastIndexOf('\n');
|
|
128
|
-
if (lastNewline < 0) return {...prefix, whitespace: toIndent};
|
|
129
|
-
const currentIndent = ws.substring(lastNewline + 1);
|
|
130
|
-
// Replace the inner indentation with the outer one
|
|
131
|
-
const adjusted = currentIndent.startsWith(fromIndent)
|
|
132
|
-
? toIndent + currentIndent.substring(fromIndent.length)
|
|
133
|
-
: toIndent;
|
|
134
|
-
return {...prefix, whitespace: ws.substring(0, lastNewline + 1) + adjusted};
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
private extractBatchedUpdatesBody(stmt: any): any[] | null {
|
|
138
|
-
let expr: any;
|
|
139
|
-
|
|
140
|
-
if (stmt.kind === JS.Kind.ExpressionStatement) {
|
|
141
|
-
expr = (stmt as JS.ExpressionStatement).expression;
|
|
142
|
-
} else if (stmt.kind === J.Kind.MethodInvocation) {
|
|
143
|
-
expr = stmt;
|
|
144
|
-
} else {
|
|
145
|
-
return null;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
if (!expr || expr.kind !== J.Kind.MethodInvocation) return null;
|
|
149
|
-
|
|
150
|
-
const method = expr as J.MethodInvocation;
|
|
151
|
-
|
|
152
|
-
if (!this.isBatchedUpdatesCall(method)) return null;
|
|
153
|
-
|
|
154
|
-
// Get the callback argument
|
|
155
|
-
const args = method.arguments;
|
|
156
|
-
if (!args) return null;
|
|
157
|
-
|
|
158
|
-
const elements = args.elements;
|
|
159
|
-
if (!elements || elements.length === 0) return null;
|
|
160
|
-
|
|
161
|
-
const callback = elements[0].element;
|
|
162
|
-
if (!callback) return null;
|
|
163
|
-
|
|
164
|
-
// Arrow function with block body
|
|
165
|
-
if (callback.kind === JS.Kind.ArrowFunction) {
|
|
166
|
-
const arrow = callback as JS.ArrowFunction;
|
|
167
|
-
const body = arrow.lambda.body;
|
|
168
|
-
if (body && body.kind === J.Kind.Block) {
|
|
169
|
-
return (body as J.Block).statements;
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
return null;
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
private isBatchedUpdatesCall(method: J.MethodInvocation): boolean {
|
|
177
|
-
if (method.name.simpleName === 'unstable_batchedUpdates') {
|
|
178
|
-
// Direct call: unstable_batchedUpdates(...)
|
|
179
|
-
if (!method.select) return true;
|
|
180
|
-
|
|
181
|
-
// Namespace call: ReactDOM.unstable_batchedUpdates(...)
|
|
182
|
-
const select = (method.select as any).element ?? method.select;
|
|
183
|
-
if (isIdentifier(select) && select.simpleName === 'ReactDOM') {
|
|
184
|
-
return true;
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
return false;
|
|
189
|
-
}
|
|
190
|
-
}());
|
|
191
|
-
}
|
|
192
|
-
}
|