@openrewrite/recipes-react 0.11.0 → 0.12.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/react16/error-boundaries.d.ts.map +1 -1
- package/dist/react16/error-boundaries.js.map +1 -1
- package/dist/react16/find-dom-node.d.ts.map +1 -1
- package/dist/react16/find-dom-node.js.map +1 -1
- package/dist/react16/react-dom-factories.d.ts.map +1 -1
- package/dist/react16/react-dom-factories.js +8 -7
- package/dist/react16/react-dom-factories.js.map +1 -1
- package/dist/react16/react-prop-types.d.ts.map +1 -1
- package/dist/react16/react-prop-types.js.map +1 -1
- package/dist/react16/react-to-react-dom.d.ts.map +1 -1
- package/dist/react16/react-to-react-dom.js.map +1 -1
- package/dist/react16/replace-create-factory.js +1 -1
- package/dist/react16/replace-create-factory.js.map +1 -1
- package/dist/react16/upgrade-to-react-16.d.ts.map +1 -1
- package/dist/react16/upgrade-to-react-16.js +2 -0
- package/dist/react16/upgrade-to-react-16.js.map +1 -1
- package/dist/react17/rename-unsafe-lifecycles.d.ts.map +1 -1
- package/dist/react17/rename-unsafe-lifecycles.js.map +1 -1
- package/dist/react17/upgrade-to-react-17.d.ts.map +1 -1
- package/dist/react17/upgrade-to-react-17.js +2 -0
- package/dist/react17/upgrade-to-react-17.js.map +1 -1
- package/dist/react18/upgrade-to-react-18.d.ts.map +1 -1
- package/dist/react18/upgrade-to-react-18.js +2 -0
- package/dist/react18/upgrade-to-react-18.js.map +1 -1
- package/dist/react19/find-legacy-context-api.d.ts.map +1 -1
- package/dist/react19/find-legacy-context-api.js +9 -9
- package/dist/react19/find-legacy-context-api.js.map +1 -1
- package/dist/react19/no-implicit-ref-callback-return.d.ts.map +1 -1
- package/dist/react19/no-implicit-ref-callback-return.js +7 -1
- package/dist/react19/no-implicit-ref-callback-return.js.map +1 -1
- package/dist/react19/remove-forward-ref.d.ts.map +1 -1
- package/dist/react19/remove-forward-ref.js.map +1 -1
- package/dist/react19/scoped-jsx-namespace.d.ts.map +1 -1
- package/dist/react19/scoped-jsx-namespace.js +6 -2
- package/dist/react19/scoped-jsx-namespace.js.map +1 -1
- package/dist/refactoring/create-class-to-es6.d.ts.map +1 -1
- package/dist/refactoring/create-class-to-es6.js +122 -58
- package/dist/refactoring/create-class-to-es6.js.map +1 -1
- package/dist/refactoring/pure-render-mixin.d.ts.map +1 -1
- package/dist/refactoring/pure-render-mixin.js +21 -68
- package/dist/refactoring/pure-render-mixin.js.map +1 -1
- package/package.json +2 -2
- package/src/react16/error-boundaries.ts +2 -0
- package/src/react16/find-dom-node.ts +2 -0
- package/src/react16/react-dom-factories.ts +11 -8
- package/src/react16/react-prop-types.ts +2 -0
- package/src/react16/react-to-react-dom.ts +2 -0
- package/src/react16/replace-create-factory.ts +2 -2
- package/src/react16/upgrade-to-react-16.ts +3 -1
- package/src/react17/remove-event-persist.ts +1 -1
- package/src/react17/rename-unsafe-lifecycles.ts +2 -0
- package/src/react17/upgrade-to-react-17.ts +3 -1
- package/src/react18/upgrade-to-react-18.ts +2 -0
- package/src/react19/find-context-consumer.ts +1 -1
- package/src/react19/find-deprecated-reactdom-apis.ts +1 -1
- package/src/react19/find-element-ref.ts +1 -1
- package/src/react19/find-legacy-context-api.ts +10 -17
- package/src/react19/no-implicit-ref-callback-return.ts +17 -9
- package/src/react19/remove-context-provider.ts +1 -1
- package/src/react19/remove-forward-ref.ts +18 -0
- package/src/react19/remove-prop-types.ts +1 -1
- package/src/react19/replace-default-props.ts +1 -1
- package/src/react19/replace-react-shallow-renderer.ts +1 -1
- package/src/react19/replace-reactdom-hydrate.ts +1 -1
- package/src/react19/replace-use-form-state.ts +1 -1
- package/src/react19/scoped-jsx-namespace.ts +7 -3
- package/src/react19/use-ref-required-initial.ts +1 -1
- package/src/refactoring/create-class-to-es6.ts +125 -56
- package/src/refactoring/pure-render-mixin.ts +28 -107
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {ExecutionContext, printer, Recipe, TreeVisitor} from "@openrewrite/rewrite";
|
|
1
|
+
import {emptyMarkers, ExecutionContext, findMarker, printer, randomId, Recipe, TreeVisitor} from "@openrewrite/rewrite";
|
|
2
2
|
import {JavaScriptVisitor, JS, raw, template} from "@openrewrite/rewrite/javascript";
|
|
3
3
|
import {J} from "@openrewrite/rewrite/java";
|
|
4
4
|
|
|
@@ -13,6 +13,9 @@ const LIFECYCLE_METHODS = new Set([
|
|
|
13
13
|
'getDerivedStateFromProps', 'getDerivedStateFromError',
|
|
14
14
|
]);
|
|
15
15
|
|
|
16
|
+
/** The methods a `createClass` component has and a `React.Component` doesn't, as react-codemod lists them. */
|
|
17
|
+
const CREATE_CLASS_METHODS = new Set(['getDOMNode', 'isMounted', 'replaceProps', 'replaceState', 'setProps']);
|
|
18
|
+
|
|
16
19
|
const SEMICOLON_KIND = "org.openrewrite.java.marker.Semicolon";
|
|
17
20
|
|
|
18
21
|
/**
|
|
@@ -24,7 +27,8 @@ const SEMICOLON_KIND = "org.openrewrite.java.marker.Semicolon";
|
|
|
24
27
|
* - Static properties (propTypes, contextTypes) → class statics
|
|
25
28
|
* - Auto-binding of methods → arrow function class fields
|
|
26
29
|
* - Lifecycle methods and render → regular class methods
|
|
27
|
-
* - Skips conversion if mixins are present
|
|
30
|
+
* - Skips conversion if mixins are present, or a member it has no conversion for, such as
|
|
31
|
+
* `render: function() {}` or `statics`
|
|
28
32
|
*
|
|
29
33
|
* @see https://github.com/reactjs/react-codemod#class
|
|
30
34
|
*/
|
|
@@ -82,6 +86,8 @@ export class CreateClassToES6 extends Recipe {
|
|
|
82
86
|
let vd = await super.visitVariableDeclarations(varDecl, ctx) as J.VariableDeclarations;
|
|
83
87
|
|
|
84
88
|
if (!vd.variables || vd.variables.length !== 1) return vd;
|
|
89
|
+
// One of several declarators, as in `const A = React.createClass({...}), b = 1`, is no statement a class can replace
|
|
90
|
+
if (this.cursor.parentTree()?.value.kind === JS.Kind.ScopedVariableDeclarations) return vd;
|
|
85
91
|
|
|
86
92
|
const namedVar = vd.variables[0].element as J.VariableDeclarations.NamedVariable;
|
|
87
93
|
if (!namedVar.initializer) return vd;
|
|
@@ -106,13 +112,17 @@ export class CreateClassToES6 extends Recipe {
|
|
|
106
112
|
|
|
107
113
|
// Check for mixins — skip conversion if present
|
|
108
114
|
if (this.hasMixins(specObj.body)) return vd;
|
|
115
|
+
// A class has no `this.isMounted()` and the like to call
|
|
116
|
+
if (await mentions(specObj.body, CREATE_CLASS_METHODS)) return vd;
|
|
109
117
|
|
|
110
118
|
// Build transformed class members
|
|
111
|
-
const
|
|
112
|
-
|
|
119
|
+
const members = await this.transformMembers(vd, specObj.body, ctx);
|
|
120
|
+
if (!members) return vd;
|
|
113
121
|
|
|
114
122
|
// Construct class declaration using template
|
|
115
|
-
const classDecl =
|
|
123
|
+
const classDecl = this.isExported(vd) ?
|
|
124
|
+
await template`export class ${raw(componentName)} extends React.Component {}`.apply(vd, this.cursor) :
|
|
125
|
+
await template`class ${raw(componentName)} extends React.Component {}`.apply(vd, this.cursor);
|
|
116
126
|
if (!classDecl) return vd;
|
|
117
127
|
|
|
118
128
|
// Splice transformed members into class body
|
|
@@ -143,6 +153,10 @@ export class CreateClassToES6 extends Recipe {
|
|
|
143
153
|
return false;
|
|
144
154
|
}
|
|
145
155
|
|
|
156
|
+
private isExported(vd: J.VariableDeclarations): boolean {
|
|
157
|
+
return vd.modifiers.some(m => m.keyword === 'export');
|
|
158
|
+
}
|
|
159
|
+
|
|
146
160
|
private hasMixins(body: J.Block): boolean {
|
|
147
161
|
for (const stmt of body.statements) {
|
|
148
162
|
const s = stmt.element;
|
|
@@ -162,63 +176,55 @@ export class CreateClassToES6 extends Recipe {
|
|
|
162
176
|
return undefined;
|
|
163
177
|
}
|
|
164
178
|
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
for (const stmt of body.statements) {
|
|
168
|
-
if (stmt.element?.prefix) return stmt.element.prefix;
|
|
169
|
-
}
|
|
170
|
-
return undefined;
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
private async transformMembers(body: J.Block, memberIndent: any, ctx: ExecutionContext): Promise<any[]> {
|
|
179
|
+
/** The members of the class, or `undefined` when the spec holds one that has no conversion here. */
|
|
180
|
+
private async transformMembers(declaration: J.VariableDeclarations, body: J.Block, ctx: ExecutionContext): Promise<any[] | undefined> {
|
|
174
181
|
const members: any[] = [];
|
|
175
182
|
|
|
176
183
|
for (const stmt of body.statements) {
|
|
177
184
|
const s = stmt.element;
|
|
185
|
+
// A converted member keeps the comments and indentation it had in the spec
|
|
186
|
+
const prefix = s.prefix;
|
|
178
187
|
|
|
179
188
|
if (s.kind === J.Kind.MethodDeclaration) {
|
|
180
189
|
const method = s as J.MethodDeclaration;
|
|
181
190
|
const name = method.name.simpleName;
|
|
182
191
|
|
|
183
192
|
if (name === 'getInitialState') {
|
|
184
|
-
const stateInit = await this.convertGetInitialState(method,
|
|
185
|
-
if (stateInit)
|
|
193
|
+
const stateInit = await this.convertGetInitialState(method, prefix, ctx);
|
|
194
|
+
if (!stateInit) return undefined;
|
|
195
|
+
members.push({...stmt, element: stateInit});
|
|
186
196
|
} else if (name === 'getDefaultProps') {
|
|
187
|
-
const defaultProps = await this.convertGetDefaultProps(method,
|
|
188
|
-
if (defaultProps)
|
|
197
|
+
const defaultProps = await this.convertGetDefaultProps(method, declaration, prefix);
|
|
198
|
+
if (!defaultProps) return undefined;
|
|
199
|
+
members.push({...stmt, element: defaultProps});
|
|
189
200
|
} else if (LIFECYCLE_METHODS.has(name)) {
|
|
190
201
|
members.push(stmt);
|
|
191
202
|
} else {
|
|
192
|
-
const arrowProp = await this.convertToArrowProperty(method,
|
|
193
|
-
if (arrowProp)
|
|
194
|
-
|
|
195
|
-
} else {
|
|
196
|
-
members.push(stmt);
|
|
197
|
-
}
|
|
203
|
+
const arrowProp = await this.convertToArrowProperty(method, prefix, ctx);
|
|
204
|
+
if (!arrowProp) return undefined;
|
|
205
|
+
members.push({...stmt, element: arrowProp});
|
|
198
206
|
}
|
|
199
207
|
} else if (s.kind === JS.Kind.PropertyAssignment) {
|
|
200
208
|
const prop = s as JS.PropertyAssignment;
|
|
201
209
|
const keyName = this.getPropertyKeyName(prop);
|
|
202
210
|
|
|
203
211
|
if (keyName === 'propTypes' || keyName === 'contextTypes' || keyName === 'childContextTypes') {
|
|
204
|
-
const staticProp = await this.convertToStaticProperty(prop, keyName,
|
|
205
|
-
if (staticProp)
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
} else if (keyName === 'displayName' || keyName === 'statics' || keyName === 'mixins') {
|
|
211
|
-
// Skip displayName (inferred from class name), statics, mixins
|
|
212
|
-
} else {
|
|
213
|
-
members.push(stmt);
|
|
212
|
+
const staticProp = await this.convertToStaticProperty(prop, keyName, declaration, prefix);
|
|
213
|
+
if (!staticProp) return undefined;
|
|
214
|
+
members.push({...stmt, element: staticProp});
|
|
215
|
+
} else if (keyName !== 'displayName') {
|
|
216
|
+
// `render: function() {}`, `statics` and other properties don't belong in a class body as they are
|
|
217
|
+
return undefined;
|
|
214
218
|
}
|
|
219
|
+
} else {
|
|
220
|
+
return undefined;
|
|
215
221
|
}
|
|
216
222
|
}
|
|
217
223
|
|
|
218
224
|
return members;
|
|
219
225
|
}
|
|
220
226
|
|
|
221
|
-
private async convertGetInitialState(method: J.MethodDeclaration,
|
|
227
|
+
private async convertGetInitialState(method: J.MethodDeclaration, prefix: any, ctx: ExecutionContext): Promise<J | undefined> {
|
|
222
228
|
if (!method.body) return undefined;
|
|
223
229
|
const stmts = method.body.statements;
|
|
224
230
|
if (stmts.length !== 1) return undefined;
|
|
@@ -230,13 +236,14 @@ export class CreateClassToES6 extends Recipe {
|
|
|
230
236
|
if (!returnStmt.expression) return undefined;
|
|
231
237
|
|
|
232
238
|
const result = await template`state = ${returnStmt.expression}`.apply(method, this.cursor);
|
|
233
|
-
|
|
234
|
-
|
|
239
|
+
const field = result && classField(result);
|
|
240
|
+
if (field && prefix) {
|
|
241
|
+
return {...(field as any), prefix};
|
|
235
242
|
}
|
|
236
|
-
return
|
|
243
|
+
return field;
|
|
237
244
|
}
|
|
238
245
|
|
|
239
|
-
private async convertGetDefaultProps(method: J.MethodDeclaration,
|
|
246
|
+
private async convertGetDefaultProps(method: J.MethodDeclaration, declaration: J.VariableDeclarations, prefix: any): Promise<J | undefined> {
|
|
240
247
|
if (!method.body) return undefined;
|
|
241
248
|
const stmts = method.body.statements;
|
|
242
249
|
if (stmts.length !== 1) return undefined;
|
|
@@ -247,15 +254,17 @@ export class CreateClassToES6 extends Recipe {
|
|
|
247
254
|
const returnStmt = firstStmt as J.Return;
|
|
248
255
|
if (!returnStmt.expression) return undefined;
|
|
249
256
|
|
|
250
|
-
|
|
251
|
-
if (result && indent) {
|
|
252
|
-
return {...(result as any), prefix: indent};
|
|
253
|
-
}
|
|
254
|
-
return result;
|
|
257
|
+
return this.staticField('defaultProps', returnStmt.expression, declaration, prefix);
|
|
255
258
|
}
|
|
256
259
|
|
|
257
|
-
private async convertToArrowProperty(method: J.MethodDeclaration,
|
|
260
|
+
private async convertToArrowProperty(method: J.MethodDeclaration, prefix: any, ctx: ExecutionContext): Promise<J | undefined> {
|
|
258
261
|
if (!method.body) return undefined;
|
|
262
|
+
// An arrow function can't be a generator or an accessor, and this one has no place for type parameters or a return type
|
|
263
|
+
if (findMarker(method, JS.Markers.Generator) || method.typeParameters || method.returnTypeExpression) return undefined;
|
|
264
|
+
const isAsync = method.modifiers.some(m => m.keyword === 'async');
|
|
265
|
+
if (method.modifiers.length !== (isAsync ? 1 : 0)) return undefined;
|
|
266
|
+
// Nor can it declare `this`, and a class field can't read `arguments`
|
|
267
|
+
if (declaresThis(method) || await mentions(method.body, new Set(['arguments']))) return undefined;
|
|
259
268
|
|
|
260
269
|
const name = method.name.simpleName;
|
|
261
270
|
|
|
@@ -269,12 +278,14 @@ export class CreateClassToES6 extends Recipe {
|
|
|
269
278
|
}
|
|
270
279
|
|
|
271
280
|
// Create stub with empty body, then splice in original body to preserve whitespace
|
|
272
|
-
const stub =
|
|
281
|
+
const stub = isAsync ?
|
|
282
|
+
await template`${raw(name)} = async (${raw(paramsSource)}) => {}`.apply(method, this.cursor) :
|
|
283
|
+
await template`${raw(name)} = (${raw(paramsSource)}) => {}`.apply(method, this.cursor);
|
|
273
284
|
if (!stub) return undefined;
|
|
274
285
|
|
|
275
286
|
// Navigate into the J.Assignment -> JS.ArrowFunction -> J.Lambda to replace the empty body
|
|
276
287
|
const s = stub as any;
|
|
277
|
-
const result = {
|
|
288
|
+
const result = classField({
|
|
278
289
|
...s,
|
|
279
290
|
assignment: {
|
|
280
291
|
...s.assignment,
|
|
@@ -286,24 +297,82 @@ export class CreateClassToES6 extends Recipe {
|
|
|
286
297
|
}
|
|
287
298
|
}
|
|
288
299
|
}
|
|
289
|
-
};
|
|
290
|
-
if (result &&
|
|
291
|
-
return {...(result as any), prefix
|
|
300
|
+
});
|
|
301
|
+
if (result && prefix) {
|
|
302
|
+
return {...(result as any), prefix};
|
|
292
303
|
}
|
|
293
304
|
return result;
|
|
294
305
|
}
|
|
295
306
|
|
|
296
307
|
|
|
297
308
|
|
|
298
|
-
private async convertToStaticProperty(prop: JS.PropertyAssignment, name: string,
|
|
299
|
-
|
|
309
|
+
private async convertToStaticProperty(prop: JS.PropertyAssignment, name: string, declaration: J.VariableDeclarations, prefix: any): Promise<J | undefined> {
|
|
310
|
+
// A shorthand `propTypes` stands for `propTypes: propTypes`
|
|
311
|
+
return this.staticField(name, prop.initializer ?? prop.name.element, declaration, prefix);
|
|
312
|
+
}
|
|
300
313
|
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
314
|
+
/**
|
|
315
|
+
* The field `static name = value` as the parser makes it, which a template parses only inside a class, so it
|
|
316
|
+
* takes the field from a class laid out where the converted class replaces `declaration`.
|
|
317
|
+
*/
|
|
318
|
+
private async staticField(name: string, value: J, declaration: J.VariableDeclarations, prefix: any): Promise<J | undefined> {
|
|
319
|
+
const cls = await template`class C {
|
|
320
|
+
static ${raw(name)} = ${value}
|
|
321
|
+
}`.apply(declaration, this.cursor);
|
|
322
|
+
const field = cls?.kind === J.Kind.ClassDeclaration ? (cls as J.ClassDeclaration).body.statements[0]?.element : undefined;
|
|
323
|
+
if (field && prefix) {
|
|
324
|
+
return {...(field as any), prefix};
|
|
304
325
|
}
|
|
305
|
-
return
|
|
326
|
+
return field;
|
|
306
327
|
}
|
|
307
328
|
}();
|
|
308
329
|
}
|
|
309
330
|
}
|
|
331
|
+
|
|
332
|
+
/** A class field as the parser makes it, a declaration, from the `name = value` assignment a template makes of it. */
|
|
333
|
+
function classField(tree: J): J {
|
|
334
|
+
if (tree.kind !== J.Kind.Assignment) return tree;
|
|
335
|
+
const assignment = tree as J.Assignment;
|
|
336
|
+
const emptySpace = {kind: 'org.openrewrite.java.tree.Space' as const, whitespace: '', comments: []};
|
|
337
|
+
return {
|
|
338
|
+
kind: J.Kind.VariableDeclarations,
|
|
339
|
+
id: assignment.id,
|
|
340
|
+
prefix: assignment.prefix,
|
|
341
|
+
markers: assignment.markers,
|
|
342
|
+
leadingAnnotations: [],
|
|
343
|
+
modifiers: [],
|
|
344
|
+
variables: [{
|
|
345
|
+
kind: J.Kind.RightPadded,
|
|
346
|
+
element: {
|
|
347
|
+
kind: J.Kind.NamedVariable,
|
|
348
|
+
id: randomId(),
|
|
349
|
+
prefix: emptySpace,
|
|
350
|
+
markers: emptyMarkers,
|
|
351
|
+
name: assignment.variable,
|
|
352
|
+
dimensionsAfterName: [],
|
|
353
|
+
initializer: assignment.assignment,
|
|
354
|
+
},
|
|
355
|
+
after: emptySpace,
|
|
356
|
+
markers: emptyMarkers,
|
|
357
|
+
}],
|
|
358
|
+
} as any as J.VariableDeclarations;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
/** Whether the method declares the TypeScript parameter `this`. */
|
|
362
|
+
function declaresThis(method: J.MethodDeclaration): boolean {
|
|
363
|
+
const first = method.parameters.elements[0]?.element;
|
|
364
|
+
return first?.kind === J.Kind.VariableDeclarations &&
|
|
365
|
+
((first as J.VariableDeclarations).variables[0]?.element.name as J.Identifier | undefined)?.simpleName === 'this';
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
/** Whether an identifier with one of the names appears anywhere in the tree. */
|
|
369
|
+
async function mentions(tree: J, names: Set<string>): Promise<boolean> {
|
|
370
|
+
let found = false;
|
|
371
|
+
await new class extends JavaScriptVisitor<void> {
|
|
372
|
+
protected override async visitIdentifier(identifier: J.Identifier, p: void): Promise<J | undefined> {
|
|
373
|
+
found = found || names.has(identifier.simpleName);
|
|
374
|
+
return identifier;
|
|
375
|
+
}
|
|
376
|
+
}().visit(tree, undefined);
|
|
377
|
+
return found;
|
|
378
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {ExecutionContext, Recipe, TreeVisitor} from "@openrewrite/rewrite";
|
|
2
|
-
import {JavaScriptVisitor, JS, maybeRemoveImport,
|
|
1
|
+
import {Cursor, ExecutionContext, Recipe, TreeVisitor} from "@openrewrite/rewrite";
|
|
2
|
+
import {JavaScriptVisitor, JS, maybeRemoveImport, template} from "@openrewrite/rewrite/javascript";
|
|
3
3
|
import {J} from "@openrewrite/rewrite/java";
|
|
4
4
|
|
|
5
5
|
/**
|
|
@@ -69,27 +69,11 @@ export class PureRenderMixin extends Recipe {
|
|
|
69
69
|
// Get member indent from existing members
|
|
70
70
|
const memberIndent = this.getMemberIndent(specObj.body);
|
|
71
71
|
|
|
72
|
-
//
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
// Create the return expression via template
|
|
78
|
-
const returnExpr = await template`React.addons.shallowCompare(this, nextProps, nextState)`.apply(specObj, this.cursor);
|
|
79
|
-
if (!returnExpr) return m;
|
|
80
|
-
|
|
81
|
-
// Create parameter identifiers via template
|
|
82
|
-
const nextPropsId = await template`nextProps`.apply(specObj, this.cursor);
|
|
83
|
-
const nextStateId = await template`nextState`.apply(specObj, this.cursor);
|
|
84
|
-
if (!nextPropsId || !nextStateId) return m;
|
|
85
|
-
|
|
86
|
-
// Build the shouldComponentUpdate method by cloning structure from existing method
|
|
87
|
-
const scuMethod = this.buildMethodFromTemplate(
|
|
88
|
-
existingMethod,
|
|
89
|
-
'shouldComponentUpdate',
|
|
90
|
-
[nextPropsId, nextStateId],
|
|
91
|
-
returnExpr
|
|
92
|
-
);
|
|
72
|
+
// A spec written with function properties alone gets no method shorthand
|
|
73
|
+
if (!this.hasMethod(specObj.body)) return m;
|
|
74
|
+
|
|
75
|
+
const scuMethod = await this.shouldComponentUpdate(specObj);
|
|
76
|
+
if (!scuMethod) return m;
|
|
93
77
|
|
|
94
78
|
// Build new statements: remove mixins (or PureRenderMixin from it), add shouldComponentUpdate
|
|
95
79
|
const newStatements = this.buildNewStatements(
|
|
@@ -181,91 +165,20 @@ export class PureRenderMixin extends Recipe {
|
|
|
181
165
|
});
|
|
182
166
|
}
|
|
183
167
|
|
|
184
|
-
private
|
|
185
|
-
|
|
186
|
-
if (stmt.element?.kind === J.Kind.MethodDeclaration) {
|
|
187
|
-
return stmt.element as J.MethodDeclaration;
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
return undefined;
|
|
168
|
+
private hasMethod(body: J.Block): boolean {
|
|
169
|
+
return body.statements.some(stmt => stmt.element?.kind === J.Kind.MethodDeclaration);
|
|
191
170
|
}
|
|
192
171
|
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
} as J.Identifier;
|
|
204
|
-
|
|
205
|
-
const emptySpace = {kind: 'org.openrewrite.java.tree.Space' as const, whitespace: '', comments: []};
|
|
206
|
-
const singleSpace = {kind: 'org.openrewrite.java.tree.Space' as const, whitespace: ' ', comments: []};
|
|
207
|
-
const emptyMarkers = existingMethod.markers;
|
|
208
|
-
|
|
209
|
-
// Build parameter list with proper spacing between params
|
|
210
|
-
const paramElements = params.map((param, i) => ({
|
|
211
|
-
kind: 'org.openrewrite.java.tree.JRightPadded' as const,
|
|
212
|
-
element: i > 0 ? {...(param as any), prefix: singleSpace} : param,
|
|
213
|
-
after: emptySpace,
|
|
214
|
-
markers: emptyMarkers,
|
|
215
|
-
}));
|
|
216
|
-
|
|
217
|
-
const bodyPrefix = existingMethod.body?.prefix ?? existingMethod.prefix;
|
|
218
|
-
|
|
219
|
-
// Build return statement by wrapping the expression
|
|
220
|
-
// Add a space prefix to the expression so it renders as "return expr" not "returnexpr"
|
|
221
|
-
const returnExprWithSpace = {...(returnExpr as any), prefix: singleSpace};
|
|
222
|
-
const returnStmt: any = {
|
|
223
|
-
kind: J.Kind.Return,
|
|
224
|
-
id: (returnExpr as any).id,
|
|
225
|
-
prefix: {
|
|
226
|
-
kind: 'org.openrewrite.java.tree.Space' as const,
|
|
227
|
-
whitespace: '\n ',
|
|
228
|
-
comments: []
|
|
229
|
-
},
|
|
230
|
-
markers: emptyMarkers,
|
|
231
|
-
expression: returnExprWithSpace,
|
|
232
|
-
};
|
|
233
|
-
|
|
234
|
-
const body: J.Block = {
|
|
235
|
-
kind: J.Kind.Block,
|
|
236
|
-
id: existingMethod.body?.id ?? (existingMethod as any).id,
|
|
237
|
-
prefix: bodyPrefix,
|
|
238
|
-
markers: existingMethod.markers,
|
|
239
|
-
static: existingMethod.body?.static ?? {
|
|
240
|
-
kind: 'org.openrewrite.java.tree.JRightPadded' as const,
|
|
241
|
-
element: false,
|
|
242
|
-
after: {kind: 'org.openrewrite.java.tree.Space' as const, whitespace: '', comments: []},
|
|
243
|
-
markers: existingMethod.markers,
|
|
244
|
-
},
|
|
245
|
-
statements: [{
|
|
246
|
-
kind: 'org.openrewrite.java.tree.JRightPadded' as const,
|
|
247
|
-
element: returnStmt,
|
|
248
|
-
after: {kind: 'org.openrewrite.java.tree.Space' as const, whitespace: '', comments: []},
|
|
249
|
-
markers: existingMethod.markers,
|
|
250
|
-
}],
|
|
251
|
-
end: {
|
|
252
|
-
kind: 'org.openrewrite.java.tree.Space' as const,
|
|
253
|
-
whitespace: '\n ',
|
|
254
|
-
comments: []
|
|
255
|
-
},
|
|
256
|
-
} as any as J.Block;
|
|
257
|
-
|
|
258
|
-
const parameters = {
|
|
259
|
-
...existingMethod.parameters,
|
|
260
|
-
elements: paramElements,
|
|
261
|
-
} as any;
|
|
262
|
-
|
|
263
|
-
return {
|
|
264
|
-
...existingMethod,
|
|
265
|
-
name: nameId,
|
|
266
|
-
parameters: parameters,
|
|
267
|
-
body: body,
|
|
268
|
-
} as any as J.MethodDeclaration;
|
|
172
|
+
/** The method as the parser makes it, from an object literal laid out where the spec is. */
|
|
173
|
+
private async shouldComponentUpdate(specObj: J.NewClass): Promise<J.MethodDeclaration | undefined> {
|
|
174
|
+
// A cursor on the spec, not the call, so that the template reads as an object literal where the call is a statement
|
|
175
|
+
const obj = await template`{
|
|
176
|
+
shouldComponentUpdate(nextProps, nextState) {
|
|
177
|
+
return React.addons.shallowCompare(this, nextProps, nextState);
|
|
178
|
+
}
|
|
179
|
+
}`.apply(specObj, new Cursor(specObj, this.cursor));
|
|
180
|
+
const method = obj?.kind === J.Kind.NewClass ? (obj as J.NewClass).body?.statements[0]?.element : undefined;
|
|
181
|
+
return method?.kind === J.Kind.MethodDeclaration ? method as J.MethodDeclaration : undefined;
|
|
269
182
|
}
|
|
270
183
|
|
|
271
184
|
private getMemberIndent(body: J.Block): any {
|
|
@@ -294,11 +207,19 @@ export class PureRenderMixin extends Recipe {
|
|
|
294
207
|
const stmt = statements[i];
|
|
295
208
|
const prop = stmt.element as JS.PropertyAssignment;
|
|
296
209
|
const arr = prop.initializer as J.NewArray;
|
|
297
|
-
const
|
|
210
|
+
const elements = arr.initializer!.elements as any[];
|
|
211
|
+
const filteredElements = elements.filter((el: any) => {
|
|
298
212
|
const elem = el.element ?? el;
|
|
299
213
|
return !(elem.kind === J.Kind.Identifier &&
|
|
300
214
|
(elem as J.Identifier).simpleName === 'PureRenderMixin');
|
|
301
215
|
});
|
|
216
|
+
// The mixin that now comes first takes the place of the one removed
|
|
217
|
+
if (filteredElements.length > 0 && filteredElements[0] !== elements[0]) {
|
|
218
|
+
filteredElements[0] = {
|
|
219
|
+
...filteredElements[0],
|
|
220
|
+
element: {...filteredElements[0].element, prefix: elements[0].element.prefix}
|
|
221
|
+
};
|
|
222
|
+
}
|
|
302
223
|
result.push({
|
|
303
224
|
...stmt,
|
|
304
225
|
element: {
|