@openrewrite/recipes-react 0.11.0 → 0.13.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/migration/change-component-prop-value.d.ts.map +1 -1
- package/dist/migration/change-component-prop-value.js +2 -2
- package/dist/migration/change-component-prop-value.js.map +1 -1
- package/dist/react-native/view-prop-types.d.ts.map +1 -1
- package/dist/react-native/view-prop-types.js +2 -2
- package/dist/react-native/view-prop-types.js.map +1 -1
- package/dist/react16/error-boundaries.d.ts.map +1 -1
- package/dist/react16/error-boundaries.js +2 -2
- 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 +2 -2
- 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 +10 -9
- 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 +2 -2
- 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 +2 -2
- package/dist/react16/react-to-react-dom.js.map +1 -1
- package/dist/react16/replace-create-factory.d.ts.map +1 -1
- package/dist/react16/replace-create-factory.js +3 -3
- 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/remove-event-persist.d.ts.map +1 -1
- package/dist/react17/remove-event-persist.js +2 -2
- package/dist/react17/remove-event-persist.js.map +1 -1
- package/dist/react17/rename-unsafe-lifecycles.d.ts.map +1 -1
- package/dist/react17/rename-unsafe-lifecycles.js +2 -2
- package/dist/react17/rename-unsafe-lifecycles.js.map +1 -1
- package/dist/react17/update-react-imports.d.ts.map +1 -1
- package/dist/react17/update-react-imports.js +2 -2
- package/dist/react17/update-react-imports.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/remove-unstable-batched-updates.d.ts.map +1 -1
- package/dist/react18/remove-unstable-batched-updates.js +2 -2
- package/dist/react18/remove-unstable-batched-updates.js.map +1 -1
- package/dist/react18/replace-reactdom-render.d.ts.map +1 -1
- package/dist/react18/replace-reactdom-render.js +2 -2
- package/dist/react18/replace-reactdom-render.js.map +1 -1
- package/dist/react18/replace-render-callback.d.ts.map +1 -1
- package/dist/react18/replace-render-callback.js +2 -2
- package/dist/react18/replace-render-callback.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 +2 -2
- package/dist/react18/replace-unmount-component-at-node.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/deprecated-react-types.d.ts.map +1 -1
- package/dist/react19/deprecated-react-types.js +2 -2
- package/dist/react19/deprecated-react-types.js.map +1 -1
- package/dist/react19/find-context-consumer.d.ts.map +1 -1
- package/dist/react19/find-context-consumer.js +2 -2
- package/dist/react19/find-context-consumer.js.map +1 -1
- package/dist/react19/find-deprecated-reactdom-apis.d.ts.map +1 -1
- package/dist/react19/find-deprecated-reactdom-apis.js +2 -2
- package/dist/react19/find-deprecated-reactdom-apis.js.map +1 -1
- package/dist/react19/find-element-ref.d.ts.map +1 -1
- package/dist/react19/find-element-ref.js +2 -2
- package/dist/react19/find-element-ref.js.map +1 -1
- package/dist/react19/find-legacy-context-api.d.ts.map +1 -1
- package/dist/react19/find-legacy-context-api.js +11 -11
- 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 +9 -3
- package/dist/react19/no-implicit-ref-callback-return.js.map +1 -1
- package/dist/react19/remove-context-provider.d.ts.map +1 -1
- package/dist/react19/remove-context-provider.js +2 -2
- package/dist/react19/remove-context-provider.js.map +1 -1
- package/dist/react19/remove-forward-ref.d.ts.map +1 -1
- package/dist/react19/remove-forward-ref.js +2 -2
- package/dist/react19/remove-forward-ref.js.map +1 -1
- package/dist/react19/remove-prop-types.d.ts.map +1 -1
- package/dist/react19/remove-prop-types.js +2 -2
- package/dist/react19/remove-prop-types.js.map +1 -1
- package/dist/react19/remove-react-fc.d.ts.map +1 -1
- package/dist/react19/remove-react-fc.js +2 -2
- package/dist/react19/remove-react-fc.js.map +1 -1
- package/dist/react19/replace-act-import.d.ts.map +1 -1
- package/dist/react19/replace-act-import.js +1 -1
- package/dist/react19/replace-act-import.js.map +1 -1
- package/dist/react19/replace-default-props.d.ts.map +1 -1
- package/dist/react19/replace-default-props.js +2 -2
- 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 +2 -2
- package/dist/react19/replace-react-shallow-renderer.js.map +1 -1
- package/dist/react19/replace-reactdom-hydrate.d.ts.map +1 -1
- package/dist/react19/replace-reactdom-hydrate.js +2 -2
- package/dist/react19/replace-reactdom-hydrate.js.map +1 -1
- package/dist/react19/replace-string-ref.d.ts.map +1 -1
- package/dist/react19/replace-string-ref.js +2 -2
- package/dist/react19/replace-string-ref.js.map +1 -1
- package/dist/react19/replace-use-form-state.d.ts.map +1 -1
- package/dist/react19/replace-use-form-state.js +2 -2
- package/dist/react19/replace-use-form-state.js.map +1 -1
- package/dist/react19/scoped-jsx-namespace.d.ts.map +1 -1
- package/dist/react19/scoped-jsx-namespace.js +8 -4
- package/dist/react19/scoped-jsx-namespace.js.map +1 -1
- package/dist/react19/use-context-hook.d.ts.map +1 -1
- package/dist/react19/use-context-hook.js +2 -2
- package/dist/react19/use-context-hook.js.map +1 -1
- package/dist/react19/use-ref-required-initial.d.ts.map +1 -1
- package/dist/react19/use-ref-required-initial.js +2 -2
- package/dist/react19/use-ref-required-initial.js.map +1 -1
- package/dist/refactoring/class-to-functional.d.ts.map +1 -1
- package/dist/refactoring/class-to-functional.js +2 -2
- 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 +261 -65
- package/dist/refactoring/create-class-to-es6.js.map +1 -1
- package/dist/refactoring/create-element-to-jsx.d.ts.map +1 -1
- package/dist/refactoring/create-element-to-jsx.js +2 -2
- package/dist/refactoring/create-element-to-jsx.js.map +1 -1
- package/dist/refactoring/manual-bind-to-arrow.d.ts.map +1 -1
- package/dist/refactoring/manual-bind-to-arrow.js +2 -2
- package/dist/refactoring/manual-bind-to-arrow.js.map +1 -1
- package/dist/refactoring/pure-render-mixin.d.ts.map +1 -1
- package/dist/refactoring/pure-render-mixin.js +42 -83
- package/dist/refactoring/pure-render-mixin.js.map +1 -1
- package/dist/refactoring/sort-comp.d.ts.map +1 -1
- package/dist/refactoring/sort-comp.js +2 -2
- package/dist/refactoring/sort-comp.js.map +1 -1
- package/dist/search/find-hook-usage.d.ts.map +1 -1
- package/dist/search/find-hook-usage.js +2 -2
- package/dist/search/find-hook-usage.js.map +1 -1
- package/dist/search/find-prop-usage.d.ts.map +1 -1
- package/dist/search/find-prop-usage.js +2 -2
- package/dist/search/find-prop-usage.js.map +1 -1
- package/dist/search/find-react-component.d.ts.map +1 -1
- package/dist/search/find-react-component.js +2 -2
- package/dist/search/find-react-component.js.map +1 -1
- package/dist/search/find-server-rendering-usage.d.ts.map +1 -1
- package/dist/search/find-server-rendering-usage.js +2 -2
- package/dist/search/find-server-rendering-usage.js.map +1 -1
- package/dist/simplify-object-pattern-property.d.ts.map +1 -1
- package/dist/simplify-object-pattern-property.js +2 -2
- 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 +2 -2
- package/dist/simplify-react-imports.js.map +1 -1
- package/package.json +2 -2
- package/src/migration/change-component-prop-value.ts +4 -4
- package/src/react-native/view-prop-types.ts +4 -4
- package/src/react16/error-boundaries.ts +6 -4
- package/src/react16/find-dom-node.ts +7 -4
- package/src/react16/react-dom-factories.ts +15 -11
- package/src/react16/react-prop-types.ts +6 -5
- package/src/react16/react-to-react-dom.ts +6 -6
- package/src/react16/replace-create-factory.ts +6 -14
- package/src/react16/upgrade-to-react-16.ts +3 -1
- package/src/react17/remove-event-persist.ts +5 -5
- package/src/react17/rename-unsafe-lifecycles.ts +6 -4
- package/src/react17/update-react-imports.ts +4 -4
- package/src/react17/upgrade-to-react-17.ts +3 -1
- package/src/react18/remove-unstable-batched-updates.ts +4 -4
- package/src/react18/replace-reactdom-render.ts +5 -4
- package/src/react18/replace-render-callback.ts +4 -4
- package/src/react18/replace-unmount-component-at-node.ts +5 -4
- package/src/react18/upgrade-to-react-18.ts +2 -0
- package/src/react19/deprecated-react-types.ts +4 -4
- package/src/react19/find-context-consumer.ts +5 -5
- package/src/react19/find-deprecated-reactdom-apis.ts +5 -5
- package/src/react19/find-element-ref.ts +5 -5
- package/src/react19/find-legacy-context-api.ts +14 -21
- package/src/react19/no-implicit-ref-callback-return.ts +21 -13
- package/src/react19/remove-context-provider.ts +6 -5
- package/src/react19/remove-forward-ref.ts +25 -4
- package/src/react19/remove-prop-types.ts +5 -5
- package/src/react19/remove-react-fc.ts +5 -4
- package/src/react19/replace-act-import.ts +3 -3
- package/src/react19/replace-default-props.ts +5 -5
- package/src/react19/replace-react-shallow-renderer.ts +5 -5
- package/src/react19/replace-reactdom-hydrate.ts +6 -5
- package/src/react19/replace-string-ref.ts +4 -4
- package/src/react19/replace-use-form-state.ts +6 -5
- package/src/react19/scoped-jsx-namespace.ts +11 -7
- package/src/react19/use-context-hook.ts +7 -4
- package/src/react19/use-ref-required-initial.ts +5 -5
- package/src/refactoring/class-to-functional.ts +4 -4
- package/src/refactoring/create-class-to-es6.ts +319 -65
- package/src/refactoring/create-element-to-jsx.ts +4 -4
- package/src/refactoring/manual-bind-to-arrow.ts +4 -4
- package/src/refactoring/pure-render-mixin.ts +55 -126
- package/src/refactoring/sort-comp.ts +4 -4
- package/src/search/find-hook-usage.ts +4 -4
- package/src/search/find-prop-usage.ts +4 -4
- package/src/search/find-react-component.ts +4 -4
- package/src/search/find-server-rendering-usage.ts +4 -4
- package/src/simplify-object-pattern-property.ts +4 -4
- package/src/simplify-react-imports.ts +4 -4
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {ExecutionContext, printer, Recipe, TreeVisitor} from "@openrewrite/rewrite";
|
|
2
|
-
import {JavaScriptVisitor, JS, raw, template} from "@openrewrite/rewrite/javascript";
|
|
1
|
+
import {ExecutionContext, printer, Recipe, TreeVisitor, check, not} from "@openrewrite/rewrite";
|
|
2
|
+
import {JavaScriptVisitor, JS, raw, template, isVendoredOrBundled} from "@openrewrite/rewrite/javascript";
|
|
3
3
|
import {J} from "@openrewrite/rewrite/java";
|
|
4
4
|
|
|
5
5
|
/**
|
|
@@ -33,7 +33,7 @@ export class CreateElementToJsx extends Recipe {
|
|
|
33
33
|
readonly description: string = "Converts `React.createElement()` calls to JSX syntax for improved readability.";
|
|
34
34
|
|
|
35
35
|
async editor(): Promise<TreeVisitor<any, ExecutionContext>> {
|
|
36
|
-
return new class extends JavaScriptVisitor<ExecutionContext> {
|
|
36
|
+
return check(not(isVendoredOrBundled()), new class extends JavaScriptVisitor<ExecutionContext> {
|
|
37
37
|
override async visitMethodInvocation(method: J.MethodInvocation, ctx: ExecutionContext): Promise<J | undefined> {
|
|
38
38
|
let m = await super.visitMethodInvocation(method, ctx) as J.MethodInvocation;
|
|
39
39
|
|
|
@@ -195,6 +195,6 @@ export class CreateElementToJsx extends Recipe {
|
|
|
195
195
|
|
|
196
196
|
return parts.join('');
|
|
197
197
|
}
|
|
198
|
-
}();
|
|
198
|
+
}());
|
|
199
199
|
}
|
|
200
200
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {ExecutionContext, Recipe, TreeVisitor} from "@openrewrite/rewrite";
|
|
2
|
-
import {JavaScriptVisitor} from "@openrewrite/rewrite/javascript";
|
|
1
|
+
import {ExecutionContext, Recipe, TreeVisitor, check, not} from "@openrewrite/rewrite";
|
|
2
|
+
import {JavaScriptVisitor, isVendoredOrBundled} from "@openrewrite/rewrite/javascript";
|
|
3
3
|
import {J} from "@openrewrite/rewrite/java";
|
|
4
4
|
|
|
5
5
|
/**
|
|
@@ -34,7 +34,7 @@ export class ManualBindToArrow extends Recipe {
|
|
|
34
34
|
readonly description: string = "Converts `this.method = this.method.bind(this)` in constructors to class field arrow function syntax.";
|
|
35
35
|
|
|
36
36
|
async editor(): Promise<TreeVisitor<any, ExecutionContext>> {
|
|
37
|
-
return new class extends JavaScriptVisitor<ExecutionContext> {
|
|
37
|
+
return check(not(isVendoredOrBundled()), new class extends JavaScriptVisitor<ExecutionContext> {
|
|
38
38
|
private boundMethods = new Set<string>();
|
|
39
39
|
|
|
40
40
|
override async visitClassDeclaration(classDecl: J.ClassDeclaration, ctx: ExecutionContext): Promise<J | undefined> {
|
|
@@ -134,6 +134,6 @@ export class ManualBindToArrow extends Recipe {
|
|
|
134
134
|
|
|
135
135
|
return m;
|
|
136
136
|
}
|
|
137
|
-
}();
|
|
137
|
+
}());
|
|
138
138
|
}
|
|
139
139
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {ExecutionContext, Recipe, TreeVisitor} from "@openrewrite/rewrite";
|
|
2
|
-
import {JavaScriptVisitor, JS, maybeRemoveImport,
|
|
3
|
-
import {J} from "@openrewrite/rewrite/java";
|
|
1
|
+
import {Cursor, ExecutionContext, findMarker, Marker, Recipe, TreeVisitor, check, not} from "@openrewrite/rewrite";
|
|
2
|
+
import {JavaScriptVisitor, JS, maybeRemoveImport, template, isVendoredOrBundled} from "@openrewrite/rewrite/javascript";
|
|
3
|
+
import {J, TextComment} from "@openrewrite/rewrite/java";
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Removes `PureRenderMixin` from `React.createClass` components and replaces it
|
|
@@ -33,7 +33,7 @@ export class PureRenderMixin extends Recipe {
|
|
|
33
33
|
readonly description: string = "Removes `PureRenderMixin` from `React.createClass` mixins and adds an explicit `shouldComponentUpdate` method.";
|
|
34
34
|
|
|
35
35
|
async editor(): Promise<TreeVisitor<any, ExecutionContext>> {
|
|
36
|
-
return new class extends JavaScriptVisitor<ExecutionContext> {
|
|
36
|
+
return check(not(isVendoredOrBundled()), new class extends JavaScriptVisitor<ExecutionContext> {
|
|
37
37
|
private transformed = false;
|
|
38
38
|
|
|
39
39
|
override async visitJsCompilationUnit(cu: JS.CompilationUnit, ctx: ExecutionContext): Promise<J | undefined> {
|
|
@@ -66,37 +66,17 @@ export class PureRenderMixin extends Recipe {
|
|
|
66
66
|
// Check that shouldComponentUpdate doesn't already exist
|
|
67
67
|
if (this.hasShouldComponentUpdate(specObj.body)) return m;
|
|
68
68
|
|
|
69
|
-
//
|
|
70
|
-
|
|
69
|
+
// A spec written with function properties alone gets no method shorthand
|
|
70
|
+
if (!this.hasMethod(specObj.body)) return m;
|
|
71
71
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
const existingMethod = this.findAnyMethod(specObj.body);
|
|
75
|
-
if (!existingMethod) return m;
|
|
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
|
+
const scuMethod = await this.shouldComponentUpdate(specObj);
|
|
73
|
+
if (!scuMethod) return m;
|
|
93
74
|
|
|
94
75
|
// Build new statements: remove mixins (or PureRenderMixin from it), add shouldComponentUpdate
|
|
95
76
|
const newStatements = this.buildNewStatements(
|
|
96
77
|
specObj.body.statements,
|
|
97
78
|
mixinsInfo,
|
|
98
|
-
scuMethod
|
|
99
|
-
memberIndent
|
|
79
|
+
scuMethod
|
|
100
80
|
);
|
|
101
81
|
|
|
102
82
|
this.transformed = true;
|
|
@@ -181,105 +161,26 @@ export class PureRenderMixin extends Recipe {
|
|
|
181
161
|
});
|
|
182
162
|
}
|
|
183
163
|
|
|
184
|
-
private
|
|
185
|
-
|
|
186
|
-
if (stmt.element?.kind === J.Kind.MethodDeclaration) {
|
|
187
|
-
return stmt.element as J.MethodDeclaration;
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
return undefined;
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
private buildMethodFromTemplate(
|
|
194
|
-
existingMethod: J.MethodDeclaration,
|
|
195
|
-
name: string,
|
|
196
|
-
params: J[],
|
|
197
|
-
returnExpr: J
|
|
198
|
-
): J.MethodDeclaration {
|
|
199
|
-
// Clone the existing method structure and replace name, params, and body
|
|
200
|
-
const nameId = {
|
|
201
|
-
...existingMethod.name,
|
|
202
|
-
simpleName: name,
|
|
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;
|
|
164
|
+
private hasMethod(body: J.Block): boolean {
|
|
165
|
+
return body.statements.some(stmt => stmt.element?.kind === J.Kind.MethodDeclaration);
|
|
269
166
|
}
|
|
270
167
|
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
168
|
+
/** The method as the parser makes it, from an object literal laid out where the spec is. */
|
|
169
|
+
private async shouldComponentUpdate(specObj: J.NewClass): Promise<J.MethodDeclaration | undefined> {
|
|
170
|
+
// A cursor on the spec, not the call, so that the template reads as an object literal where the call is a statement
|
|
171
|
+
const obj = await template`{
|
|
172
|
+
shouldComponentUpdate(nextProps, nextState) {
|
|
173
|
+
return React.addons.shallowCompare(this, nextProps, nextState);
|
|
174
|
+
}
|
|
175
|
+
}`.apply(specObj, new Cursor(specObj, this.cursor));
|
|
176
|
+
const method = obj?.kind === J.Kind.NewClass ? (obj as J.NewClass).body?.statements[0]?.element : undefined;
|
|
177
|
+
return method?.kind === J.Kind.MethodDeclaration ? method as J.MethodDeclaration : undefined;
|
|
276
178
|
}
|
|
277
179
|
|
|
278
180
|
private buildNewStatements(
|
|
279
181
|
statements: any[],
|
|
280
182
|
mixinsInfo: { stmtIndex: number; isOnlyMixin: boolean },
|
|
281
|
-
scuMethod: J
|
|
282
|
-
indent: any
|
|
183
|
+
scuMethod: J
|
|
283
184
|
): any[] {
|
|
284
185
|
const result: any[] = [];
|
|
285
186
|
let scuInserted = false;
|
|
@@ -294,11 +195,19 @@ export class PureRenderMixin extends Recipe {
|
|
|
294
195
|
const stmt = statements[i];
|
|
295
196
|
const prop = stmt.element as JS.PropertyAssignment;
|
|
296
197
|
const arr = prop.initializer as J.NewArray;
|
|
297
|
-
const
|
|
198
|
+
const elements = arr.initializer!.elements as any[];
|
|
199
|
+
const filteredElements = elements.filter((el: any) => {
|
|
298
200
|
const elem = el.element ?? el;
|
|
299
201
|
return !(elem.kind === J.Kind.Identifier &&
|
|
300
202
|
(elem as J.Identifier).simpleName === 'PureRenderMixin');
|
|
301
203
|
});
|
|
204
|
+
// The mixin that now comes first takes the place of the one removed
|
|
205
|
+
if (filteredElements.length > 0 && filteredElements[0] !== elements[0]) {
|
|
206
|
+
filteredElements[0] = {
|
|
207
|
+
...filteredElements[0],
|
|
208
|
+
element: {...filteredElements[0].element, prefix: elements[0].element.prefix}
|
|
209
|
+
};
|
|
210
|
+
}
|
|
302
211
|
result.push({
|
|
303
212
|
...stmt,
|
|
304
213
|
element: {
|
|
@@ -322,7 +231,7 @@ export class PureRenderMixin extends Recipe {
|
|
|
322
231
|
(s as J.MethodDeclaration).name.simpleName === 'render') {
|
|
323
232
|
const scuStmt = {
|
|
324
233
|
...statements[i],
|
|
325
|
-
element:
|
|
234
|
+
element: {...scuMethod, prefix: lineOf(s.prefix)}
|
|
326
235
|
};
|
|
327
236
|
result.push(scuStmt);
|
|
328
237
|
scuInserted = true;
|
|
@@ -335,12 +244,32 @@ export class PureRenderMixin extends Recipe {
|
|
|
335
244
|
const lastStmt = statements[statements.length - 1];
|
|
336
245
|
result.push({
|
|
337
246
|
...lastStmt,
|
|
338
|
-
element:
|
|
247
|
+
element: {...scuMethod, prefix: lineOf(lastStmt.element.prefix)}
|
|
339
248
|
});
|
|
340
249
|
}
|
|
341
250
|
|
|
342
|
-
|
|
251
|
+
// The spec's trailing comma follows the member that now comes last, and no other
|
|
252
|
+
const trailingComma = findMarker(statements[statements.length - 1], J.Markers.TrailingComma);
|
|
253
|
+
return result.map((stmt, i) => {
|
|
254
|
+
const wanted = i === result.length - 1 ? trailingComma : undefined;
|
|
255
|
+
if (findMarker(stmt, J.Markers.TrailingComma) === wanted) return stmt;
|
|
256
|
+
const markers = stmt.markers.markers.filter((m: Marker) => m.kind !== J.Markers.TrailingComma);
|
|
257
|
+
return {...stmt, markers: {...stmt.markers, markers: wanted ? [...markers, wanted] : markers}};
|
|
258
|
+
});
|
|
343
259
|
}
|
|
344
|
-
}();
|
|
260
|
+
}());
|
|
345
261
|
}
|
|
346
262
|
}
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* A prefix of its own for a member next to the one `prefix` belongs to: the line break and indentation of the line that
|
|
266
|
+
* member starts on, without its comments.
|
|
267
|
+
*/
|
|
268
|
+
function lineOf(prefix: J.Space): J.Space {
|
|
269
|
+
const text = prefix.whitespace + prefix.comments.map(c => {
|
|
270
|
+
const comment = c as TextComment;
|
|
271
|
+
return (comment.multiline ? `/*${comment.text}*/` : `//${comment.text}`) + c.suffix;
|
|
272
|
+
}).join('');
|
|
273
|
+
const lineStart = /\r?\n[ \t]*(?=[^\n]*$)/.exec(text);
|
|
274
|
+
return {kind: J.Kind.Space, comments: [], whitespace: lineStart ? lineStart[0] : prefix.whitespace};
|
|
275
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {ExecutionContext, Recipe, TreeVisitor} from "@openrewrite/rewrite";
|
|
2
|
-
import {JavaScriptVisitor} from "@openrewrite/rewrite/javascript";
|
|
1
|
+
import {ExecutionContext, Recipe, TreeVisitor, check, not} from "@openrewrite/rewrite";
|
|
2
|
+
import {JavaScriptVisitor, isVendoredOrBundled} from "@openrewrite/rewrite/javascript";
|
|
3
3
|
import {J} from "@openrewrite/rewrite/java";
|
|
4
4
|
|
|
5
5
|
const LIFECYCLE_METHODS = [
|
|
@@ -49,7 +49,7 @@ export class SortComp extends Recipe {
|
|
|
49
49
|
readonly description: string = "Reorders React component methods to follow the recommended lifecycle ordering convention.";
|
|
50
50
|
|
|
51
51
|
async editor(): Promise<TreeVisitor<any, ExecutionContext>> {
|
|
52
|
-
return new class extends JavaScriptVisitor<ExecutionContext> {
|
|
52
|
+
return check(not(isVendoredOrBundled()), new class extends JavaScriptVisitor<ExecutionContext> {
|
|
53
53
|
override async visitClassDeclaration(classDecl: J.ClassDeclaration, ctx: ExecutionContext): Promise<J | undefined> {
|
|
54
54
|
let cd = await super.visitClassDeclaration(classDecl, ctx) as J.ClassDeclaration;
|
|
55
55
|
|
|
@@ -130,6 +130,6 @@ export class SortComp extends Recipe {
|
|
|
130
130
|
// 4: everything else
|
|
131
131
|
return 400;
|
|
132
132
|
}
|
|
133
|
-
}();
|
|
133
|
+
}());
|
|
134
134
|
}
|
|
135
135
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {Column, DataTable, ExecutionContext, foundSearchResult, Recipe, TreeVisitor} from "@openrewrite/rewrite";
|
|
2
|
-
import {JavaScriptVisitor, JS} from "@openrewrite/rewrite/javascript";
|
|
1
|
+
import {Column, DataTable, ExecutionContext, foundSearchResult, Recipe, TreeVisitor, check, not} from "@openrewrite/rewrite";
|
|
2
|
+
import {JavaScriptVisitor, JS, isVendoredOrBundled} from "@openrewrite/rewrite/javascript";
|
|
3
3
|
import {isIdentifier, J} from "@openrewrite/rewrite/java";
|
|
4
4
|
|
|
5
5
|
const BUILT_IN_HOOKS = new Set([
|
|
@@ -72,7 +72,7 @@ export class FindHookUsage extends Recipe {
|
|
|
72
72
|
async editor(): Promise<TreeVisitor<any, ExecutionContext>> {
|
|
73
73
|
const dataTable = this.dataTable;
|
|
74
74
|
|
|
75
|
-
return new class extends JavaScriptVisitor<ExecutionContext> {
|
|
75
|
+
return check(not(isVendoredOrBundled()), new class extends JavaScriptVisitor<ExecutionContext> {
|
|
76
76
|
private filePath = "";
|
|
77
77
|
private componentStack: string[] = [];
|
|
78
78
|
private conditionalDepth = 0;
|
|
@@ -221,6 +221,6 @@ export class FindHookUsage extends Recipe {
|
|
|
221
221
|
}
|
|
222
222
|
return null;
|
|
223
223
|
}
|
|
224
|
-
}();
|
|
224
|
+
}());
|
|
225
225
|
}
|
|
226
226
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {Column, DataTable, ExecutionContext, foundSearchResult, Option, Recipe, TreeVisitor} from "@openrewrite/rewrite";
|
|
2
|
-
import {JavaScriptVisitor, JS, JSX} from "@openrewrite/rewrite/javascript";
|
|
1
|
+
import {Column, DataTable, ExecutionContext, foundSearchResult, Option, Recipe, TreeVisitor, check, not} from "@openrewrite/rewrite";
|
|
2
|
+
import {JavaScriptVisitor, JS, JSX, isVendoredOrBundled} from "@openrewrite/rewrite/javascript";
|
|
3
3
|
import {Expression, isIdentifier, J} from "@openrewrite/rewrite/java";
|
|
4
4
|
|
|
5
5
|
class FindPropUsageRow {
|
|
@@ -70,7 +70,7 @@ export class FindPropUsage extends Recipe {
|
|
|
70
70
|
const filterProp = this.propName;
|
|
71
71
|
const dataTable = this.dataTable;
|
|
72
72
|
|
|
73
|
-
return new class extends JavaScriptVisitor<ExecutionContext> {
|
|
73
|
+
return check(not(isVendoredOrBundled()), new class extends JavaScriptVisitor<ExecutionContext> {
|
|
74
74
|
private filePath = "";
|
|
75
75
|
|
|
76
76
|
override async visitJsCompilationUnit(cu: JS.CompilationUnit, ctx: ExecutionContext): Promise<J | undefined> {
|
|
@@ -171,6 +171,6 @@ export class FindPropUsage extends Recipe {
|
|
|
171
171
|
}
|
|
172
172
|
return "";
|
|
173
173
|
}
|
|
174
|
-
}();
|
|
174
|
+
}());
|
|
175
175
|
}
|
|
176
176
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {Column, DataTable, ExecutionContext, foundSearchResult, Option, Recipe, TreeVisitor} from "@openrewrite/rewrite";
|
|
2
|
-
import {JavaScriptVisitor, JS, JSX} from "@openrewrite/rewrite/javascript";
|
|
1
|
+
import {Column, DataTable, ExecutionContext, foundSearchResult, Option, Recipe, TreeVisitor, check, not} from "@openrewrite/rewrite";
|
|
2
|
+
import {JavaScriptVisitor, JS, JSX, isVendoredOrBundled} from "@openrewrite/rewrite/javascript";
|
|
3
3
|
import {Expression, isIdentifier, J} from "@openrewrite/rewrite/java";
|
|
4
4
|
|
|
5
5
|
class FindReactComponentRow {
|
|
@@ -69,7 +69,7 @@ export class FindReactComponent extends Recipe {
|
|
|
69
69
|
const importPathFilter = this.importPath;
|
|
70
70
|
const dataTable = this.dataTable;
|
|
71
71
|
|
|
72
|
-
return new class extends JavaScriptVisitor<ExecutionContext> {
|
|
72
|
+
return check(not(isVendoredOrBundled()), new class extends JavaScriptVisitor<ExecutionContext> {
|
|
73
73
|
// Track component aliases within the current file
|
|
74
74
|
private aliases = new Map<string, { importPath: string; isDefault: boolean; alias: string }>();
|
|
75
75
|
private filePath = "";
|
|
@@ -249,6 +249,6 @@ export class FindReactComponent extends Recipe {
|
|
|
249
249
|
const regex = new RegExp("^" + pattern.replace(/\*/g, ".*") + "$");
|
|
250
250
|
return regex.test(actual);
|
|
251
251
|
}
|
|
252
|
-
}();
|
|
252
|
+
}());
|
|
253
253
|
}
|
|
254
254
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {Column, DataTable, ExecutionContext, foundSearchResult, Recipe, TreeVisitor} from "@openrewrite/rewrite";
|
|
2
|
-
import {JavaScriptVisitor, JS} from "@openrewrite/rewrite/javascript";
|
|
1
|
+
import {Column, DataTable, ExecutionContext, foundSearchResult, Recipe, TreeVisitor, check, not} from "@openrewrite/rewrite";
|
|
2
|
+
import {JavaScriptVisitor, JS, isVendoredOrBundled} from "@openrewrite/rewrite/javascript";
|
|
3
3
|
import {isIdentifier, J} from "@openrewrite/rewrite/java";
|
|
4
4
|
|
|
5
5
|
class ServerRenderingRow {
|
|
@@ -46,7 +46,7 @@ export class FindServerRenderingUsage extends Recipe {
|
|
|
46
46
|
async editor(): Promise<TreeVisitor<any, ExecutionContext>> {
|
|
47
47
|
const dataTable = this.dataTable;
|
|
48
48
|
|
|
49
|
-
return new class extends JavaScriptVisitor<ExecutionContext> {
|
|
49
|
+
return check(not(isVendoredOrBundled()), new class extends JavaScriptVisitor<ExecutionContext> {
|
|
50
50
|
private filePath = "";
|
|
51
51
|
private importedSsrApis = new Set<string>();
|
|
52
52
|
|
|
@@ -117,6 +117,6 @@ export class FindServerRenderingUsage extends Recipe {
|
|
|
117
117
|
|
|
118
118
|
return m;
|
|
119
119
|
}
|
|
120
|
-
}();
|
|
120
|
+
}());
|
|
121
121
|
}
|
|
122
122
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {ExecutionContext, Recipe, TreeVisitor} from "@openrewrite/rewrite";
|
|
2
|
-
import {JavaScriptVisitor, JS} from "@openrewrite/rewrite/javascript";
|
|
1
|
+
import {ExecutionContext, Recipe, TreeVisitor, check, not} from "@openrewrite/rewrite";
|
|
2
|
+
import {JavaScriptVisitor, JS, isVendoredOrBundled} from "@openrewrite/rewrite/javascript";
|
|
3
3
|
import {J} from "@openrewrite/rewrite/java";
|
|
4
4
|
import {create} from "mutative";
|
|
5
5
|
|
|
@@ -17,7 +17,7 @@ export class SimplifyObjectPatternProperty extends Recipe {
|
|
|
17
17
|
readonly description: string = "Simplifies object destructuring patterns where the property name and variable name are the same (e.g., `{ x: x }` becomes `{ x }`).";
|
|
18
18
|
|
|
19
19
|
async editor(): Promise<TreeVisitor<any, ExecutionContext>> {
|
|
20
|
-
return new class extends JavaScriptVisitor<ExecutionContext> {
|
|
20
|
+
return check(not(isVendoredOrBundled()), new class extends JavaScriptVisitor<ExecutionContext> {
|
|
21
21
|
protected async visitObjectBindingPattern(pattern: JS.ObjectBindingPattern, p: ExecutionContext): Promise<J | undefined> {
|
|
22
22
|
// First, recursively visit children
|
|
23
23
|
const visited = await super.visitObjectBindingPattern(pattern, p) as JS.ObjectBindingPattern;
|
|
@@ -70,6 +70,6 @@ export class SimplifyObjectPatternProperty extends Recipe {
|
|
|
70
70
|
draft.bindings.elements = simplifiedBindings;
|
|
71
71
|
});
|
|
72
72
|
}
|
|
73
|
-
}();
|
|
73
|
+
}());
|
|
74
74
|
}
|
|
75
75
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {ExecutionContext, Recipe, TreeVisitor} from "@openrewrite/rewrite";
|
|
2
|
-
import {JavaScriptVisitor, maybeAddImport} from "@openrewrite/rewrite/javascript";
|
|
1
|
+
import {ExecutionContext, Recipe, TreeVisitor, check, not} from "@openrewrite/rewrite";
|
|
2
|
+
import {JavaScriptVisitor, maybeAddImport, isVendoredOrBundled} from "@openrewrite/rewrite/javascript";
|
|
3
3
|
import {J} from "@openrewrite/rewrite/java";
|
|
4
4
|
import {JS} from "@openrewrite/rewrite/javascript";
|
|
5
5
|
|
|
@@ -99,7 +99,7 @@ export class SimplifyReactImports extends Recipe {
|
|
|
99
99
|
readonly description: string = "Converts `React.useState`, `React.useEffect`, and other React namespace accesses to direct named imports.";
|
|
100
100
|
|
|
101
101
|
async editor(): Promise<TreeVisitor<any, ExecutionContext>> {
|
|
102
|
-
return new class extends JavaScriptVisitor<ExecutionContext> {
|
|
102
|
+
return check(not(isVendoredOrBundled()), new class extends JavaScriptVisitor<ExecutionContext> {
|
|
103
103
|
// Track which React exports we've used in this file
|
|
104
104
|
private usedExports = new Set<string>();
|
|
105
105
|
// Track locally defined identifiers to avoid naming conflicts
|
|
@@ -236,6 +236,6 @@ export class SimplifyReactImports extends Recipe {
|
|
|
236
236
|
|
|
237
237
|
return identifier;
|
|
238
238
|
}
|
|
239
|
-
}();
|
|
239
|
+
}());
|
|
240
240
|
}
|
|
241
241
|
}
|