@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,13 +1,20 @@
|
|
|
1
|
-
import {ExecutionContext, Recipe, TreeVisitor,
|
|
2
|
-
import {
|
|
3
|
-
import {Expression, isIdentifier, J} from "@openrewrite/rewrite/java";
|
|
4
|
-
|
|
1
|
+
import {check, Cursor, emptyMarkers, ExecutionContext, mapAsync, not, Recipe, TreeVisitor, updateIfChanged} from "@openrewrite/rewrite";
|
|
2
|
+
import {bindingNames, isVendoredOrBundled, JavaScriptVisitor, JS, namesReferencedWithin, referencesOf} from "@openrewrite/rewrite/javascript";
|
|
3
|
+
import {Expression, isIdentifier, isTextComment, J, singleSpace, Statement, TextComment} from "@openrewrite/rewrite/java";
|
|
4
|
+
|
|
5
|
+
const DEFAULT_PROPS = 'defaultProps';
|
|
6
|
+
export const TODO = " TODO: `defaultProps` is ignored on function components in React 19; move these defaults into the parameters, see https://react.dev/blog/2024/04/25/react-19-upgrade-guide#removed-proptypes-and-defaultprops";
|
|
5
7
|
|
|
6
8
|
/**
|
|
7
9
|
* Converts `Component.defaultProps` to ES6 default parameter values.
|
|
8
10
|
*
|
|
9
|
-
* In React 19, `defaultProps` for function components is
|
|
10
|
-
* This recipe moves default values into the function
|
|
11
|
+
* In React 19, `defaultProps` for function components is removed.
|
|
12
|
+
* This recipe moves default values into the parameter destructuring of a function or arrow
|
|
13
|
+
* function component declared in the same scope. The assignment is deleted only when every
|
|
14
|
+
* entry was moved; otherwise the remaining entries stay, marked with a TODO. Class components
|
|
15
|
+
* keep their `defaultProps`. So does an entry whose value uses a name the props pattern also
|
|
16
|
+
* binds: a parameter default would read the prop instead of the import or constant. A component
|
|
17
|
+
* whose `defaultProps` is read elsewhere in the file keeps it too, as the read would find nothing.
|
|
11
18
|
*
|
|
12
19
|
* Before:
|
|
13
20
|
* ```tsx
|
|
@@ -29,188 +36,234 @@ import {create} from "mutative";
|
|
|
29
36
|
export class ReplaceDefaultProps extends Recipe {
|
|
30
37
|
readonly name = "org.openrewrite.react.19.replace-default-props";
|
|
31
38
|
readonly displayName: string = "Replace `defaultProps` with default parameter values";
|
|
32
|
-
readonly description: string = "Converts `Component.defaultProps = {...}` to ES6 default parameter values in function components. `defaultProps` for function components is
|
|
39
|
+
readonly description: string = "Converts `Component.defaultProps = {...}` to ES6 default parameter values in function components declared in the same scope. Entries that can't be moved, including values that use a name a destructured prop also binds, stay in `defaultProps`, marked with a TODO, as does a component whose `defaultProps` is read elsewhere in the file, and class components are left alone. `defaultProps` for function components is removed in React 19.";
|
|
33
40
|
|
|
34
41
|
async editor(): Promise<TreeVisitor<any, ExecutionContext>> {
|
|
35
42
|
return check(not(isVendoredOrBundled()), new class extends JavaScriptVisitor<ExecutionContext> {
|
|
36
|
-
// Map: component name → { propName → default value expression }
|
|
37
|
-
private defaultPropsMap = new Map<string, Map<string, Expression>>();
|
|
38
|
-
// Track which statement IDs to remove
|
|
39
|
-
private statementsToRemove = new Set<string>();
|
|
40
|
-
|
|
41
43
|
override async visitJsCompilationUnit(cu: JS.CompilationUnit, ctx: ExecutionContext): Promise<J | undefined> {
|
|
42
|
-
|
|
43
|
-
this.statementsToRemove.clear();
|
|
44
|
-
|
|
45
|
-
// First pass: collect all defaultProps assignments
|
|
46
|
-
this.collectDefaultProps(cu);
|
|
47
|
-
|
|
48
|
-
if (this.defaultPropsMap.size === 0) {
|
|
44
|
+
if (!cu.statements.some(stmt => asDefaultPropsAssignment(stmt.element))) {
|
|
49
45
|
return cu;
|
|
50
46
|
}
|
|
51
47
|
|
|
52
|
-
|
|
53
|
-
|
|
48
|
+
const assigned = new Set<J>(cu.statements.flatMap(stmt => asDefaultPropsAssignment(stmt.element)?.assignment.variable ?? []));
|
|
49
|
+
const readsDefaultProps = (component: string) => readsDefaultPropsOf(this.cursor, component, assigned);
|
|
54
50
|
|
|
55
|
-
//
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
if (filteredStatements.length !== result.statements.length) {
|
|
63
|
-
result = create(result, draft => {
|
|
64
|
-
draft.statements = filteredStatements as any;
|
|
65
|
-
});
|
|
51
|
+
// Declarations by name; null where a name is declared more than once
|
|
52
|
+
const declarations = new Map<string, J | null>();
|
|
53
|
+
for (const stmt of cu.statements) {
|
|
54
|
+
const name = declaredName(stmt.element);
|
|
55
|
+
if (name !== undefined) {
|
|
56
|
+
declarations.set(name, declarations.has(name) ? null : stmt.element!);
|
|
66
57
|
}
|
|
67
58
|
}
|
|
68
59
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
60
|
+
const replaced = new Map<J, J>();
|
|
61
|
+
const statements = await mapAsync(cu.statements, async stmt => {
|
|
62
|
+
const assignment = asDefaultPropsAssignment(stmt.element);
|
|
63
|
+
const declaration = assignment && declarations.get(assignment.componentName);
|
|
64
|
+
if (!assignment || !declaration || !isFunctionComponent(replaced.get(declaration) ?? declaration)) {
|
|
65
|
+
return stmt;
|
|
66
|
+
}
|
|
76
67
|
|
|
77
|
-
|
|
78
|
-
|
|
68
|
+
if (readsDefaultProps(assignment.componentName)) {
|
|
69
|
+
return hasTodo(assignment.assignment) ? stmt : {...stmt, element: withTodo(assignment.assignment)};
|
|
70
|
+
}
|
|
79
71
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
72
|
+
let component = replaced.get(declaration) ?? declaration;
|
|
73
|
+
const entries = assignment.object.body!.statements;
|
|
74
|
+
const remaining: typeof entries = [];
|
|
75
|
+
for (const entry of entries) {
|
|
76
|
+
const prop = entry.element as J;
|
|
77
|
+
const moved = prop.kind === JS.Kind.PropertyAssignment ? moveDefault(component, prop as JS.PropertyAssignment) : undefined;
|
|
78
|
+
if (moved) {
|
|
79
|
+
component = moved;
|
|
80
|
+
} else {
|
|
81
|
+
remaining.push(entry);
|
|
85
82
|
}
|
|
86
|
-
} else if (s.kind === J.Kind.Assignment) {
|
|
87
|
-
assignment = s as J.Assignment;
|
|
88
83
|
}
|
|
84
|
+
replaced.set(declaration, component);
|
|
89
85
|
|
|
90
|
-
if (
|
|
91
|
-
|
|
92
|
-
// Check for Component.defaultProps = { ... }
|
|
93
|
-
if (assignment.variable.kind !== J.Kind.FieldAccess) continue;
|
|
94
|
-
const fa = assignment.variable as J.FieldAccess;
|
|
95
|
-
if (fa.name.element.simpleName !== 'defaultProps') continue;
|
|
96
|
-
if (!isIdentifier(fa.target)) continue;
|
|
97
|
-
const componentName = (fa.target as J.Identifier).simpleName;
|
|
98
|
-
|
|
99
|
-
// Extract the object literal values
|
|
100
|
-
const rhs = assignment.assignment.element;
|
|
101
|
-
if (!rhs) continue;
|
|
102
|
-
|
|
103
|
-
const defaults = this.extractObjectLiteralDefaults(rhs);
|
|
104
|
-
if (defaults.size > 0) {
|
|
105
|
-
this.defaultPropsMap.set(componentName, defaults);
|
|
106
|
-
this.statementsToRemove.add((s as any).id);
|
|
86
|
+
if (remaining.length === 0) {
|
|
87
|
+
return undefined;
|
|
107
88
|
}
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
private extractObjectLiteralDefaults(expr: Expression): Map<string, Expression> {
|
|
112
|
-
const defaults = new Map<string, Expression>();
|
|
113
|
-
|
|
114
|
-
if (isObjectLiteral(expr) && expr.body) {
|
|
115
|
-
for (const stmt of expr.body.statements) {
|
|
116
|
-
const s = stmt.element;
|
|
117
|
-
if (!s) continue;
|
|
118
|
-
|
|
119
|
-
if (s.kind === JS.Kind.PropertyAssignment) {
|
|
120
|
-
const prop = s as JS.PropertyAssignment;
|
|
121
|
-
const nameExpr = prop.name.element;
|
|
122
|
-
if (isIdentifier(nameExpr) && prop.initializer) {
|
|
123
|
-
defaults.set(nameExpr.simpleName, prop.initializer);
|
|
124
|
-
}
|
|
125
|
-
}
|
|
89
|
+
if (remaining.length === entries.length && hasTodo(assignment.assignment)) {
|
|
90
|
+
return stmt;
|
|
126
91
|
}
|
|
127
|
-
|
|
92
|
+
// The new last entry ends the list the way the old one did, trailing comma included
|
|
93
|
+
const last = entries[entries.length - 1];
|
|
94
|
+
remaining[remaining.length - 1] = {...remaining[remaining.length - 1], markers: last.markers, after: last.after};
|
|
95
|
+
const kept: J.Assignment = {
|
|
96
|
+
...assignment.assignment,
|
|
97
|
+
assignment: {
|
|
98
|
+
...assignment.assignment.assignment,
|
|
99
|
+
element: {...assignment.object, body: {...assignment.object.body!, statements: remaining}} as J.NewClass
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
return {...stmt, element: withTodo(kept)};
|
|
103
|
+
});
|
|
128
104
|
|
|
129
|
-
return
|
|
105
|
+
return updateIfChanged(cu, {
|
|
106
|
+
statements: await mapAsync(statements, async stmt => {
|
|
107
|
+
const to = replaced.get(stmt.element);
|
|
108
|
+
return to && to !== stmt.element ? {...stmt, element: to as Statement} : stmt;
|
|
109
|
+
})
|
|
110
|
+
});
|
|
130
111
|
}
|
|
112
|
+
}());
|
|
113
|
+
}
|
|
114
|
+
}
|
|
131
115
|
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
116
|
+
/**
|
|
117
|
+
* Whether code reads the `defaultProps` of the value `name` binds at `cursor`, other than as the target of
|
|
118
|
+
* one of the `assigned` assignments. References are resolved rather than matched by name, so a local that
|
|
119
|
+
* shadows the component does not count, and a local alias is followed to its own references.
|
|
120
|
+
*/
|
|
121
|
+
function readsDefaultPropsOf(cursor: Cursor, name: string, assigned: ReadonlySet<J>, seen = new Set<J>()): boolean {
|
|
122
|
+
return referencesOf(cursor, name, 'value').some(reference => {
|
|
123
|
+
if (reference.declares) return false;
|
|
124
|
+
const {identifier} = reference;
|
|
125
|
+
const holder = reference.cursor.parent?.value as J | undefined;
|
|
126
|
+
switch (holder?.kind) {
|
|
127
|
+
case J.Kind.FieldAccess: {
|
|
128
|
+
const access = holder as J.FieldAccess;
|
|
129
|
+
return access.target === identifier && access.name.element.simpleName === DEFAULT_PROPS && !assigned.has(access);
|
|
136
130
|
}
|
|
131
|
+
case J.Kind.ArrayAccess: {
|
|
132
|
+
const access = holder as J.ArrayAccess;
|
|
133
|
+
const index = access.dimension.index.element as J.Literal;
|
|
134
|
+
return access.indexed === identifier && (index.kind !== J.Kind.Literal || index.value === DEFAULT_PROPS);
|
|
135
|
+
}
|
|
136
|
+
case J.Kind.NamedVariable: {
|
|
137
|
+
const variable = holder as J.VariableDeclarations.NamedVariable;
|
|
138
|
+
return variable.initializer?.element === identifier && readsThrough(variable.name, reference.cursor.parent!);
|
|
139
|
+
}
|
|
140
|
+
case J.Kind.Assignment: {
|
|
141
|
+
const assignment = holder as J.Assignment;
|
|
142
|
+
// A property such as `module.exports` hands the component to other files, which this check does not cover
|
|
143
|
+
return assignment.assignment.element === identifier && assignment.variable.kind !== J.Kind.FieldAccess &&
|
|
144
|
+
readsThrough(assignment.variable, reference.cursor.parent!);
|
|
145
|
+
}
|
|
146
|
+
default:
|
|
147
|
+
return false;
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
function readsThrough(target: J, holder: Cursor): boolean {
|
|
152
|
+
if (!isIdentifier(target)) {
|
|
153
|
+
const names = bindingNames(target);
|
|
154
|
+
return names.length === 0 || names.some(({member}) => member === undefined || member === DEFAULT_PROPS);
|
|
155
|
+
}
|
|
156
|
+
if (seen.has(target)) return false;
|
|
157
|
+
seen.add(target);
|
|
158
|
+
return readsDefaultPropsOf(new Cursor(target, holder), target.simpleName, assigned, seen);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
137
161
|
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
// The first param element is J.VariableDeclarations (the destructured props)
|
|
153
|
-
const firstParamPadded = paramElements[0];
|
|
154
|
-
const firstParam = (firstParamPadded as any).element ?? firstParamPadded;
|
|
155
|
-
|
|
156
|
-
if (firstParam.kind !== J.Kind.VariableDeclarations) return method;
|
|
157
|
-
|
|
158
|
-
const varDecls = firstParam as J.VariableDeclarations;
|
|
159
|
-
if (!varDecls.variables || varDecls.variables.length === 0) return method;
|
|
162
|
+
type Assignment = { assignment: J.Assignment, componentName: string, object: J.NewClass };
|
|
163
|
+
|
|
164
|
+
function asDefaultPropsAssignment(statement: J | undefined): Assignment | undefined {
|
|
165
|
+
if (statement?.kind !== J.Kind.Assignment) return undefined;
|
|
166
|
+
const assignment = statement as J.Assignment;
|
|
167
|
+
if (assignment.variable.kind !== J.Kind.FieldAccess) return undefined;
|
|
168
|
+
const target = assignment.variable as J.FieldAccess;
|
|
169
|
+
if (target.name.element.simpleName !== DEFAULT_PROPS || !isIdentifier(target.target)) return undefined;
|
|
170
|
+
// Object literals are represented as J.NewClass in the AST
|
|
171
|
+
const object = assignment.assignment.element;
|
|
172
|
+
if (object?.kind !== J.Kind.NewClass || !(object as J.NewClass).body) return undefined;
|
|
173
|
+
return {assignment, componentName: (target.target as J.Identifier).simpleName, object: object as J.NewClass};
|
|
174
|
+
}
|
|
160
175
|
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
176
|
+
function declaredName(statement: J | undefined): string | undefined {
|
|
177
|
+
switch (statement?.kind) {
|
|
178
|
+
case J.Kind.MethodDeclaration:
|
|
179
|
+
return (statement as J.MethodDeclaration).name.simpleName;
|
|
180
|
+
case J.Kind.ClassDeclaration:
|
|
181
|
+
return (statement as J.ClassDeclaration).name.simpleName;
|
|
182
|
+
case J.Kind.VariableDeclarations: {
|
|
183
|
+
const variables = (statement as J.VariableDeclarations).variables;
|
|
184
|
+
const name = variables.length === 1 ? variables[0].element.name : undefined;
|
|
185
|
+
return name && isIdentifier(name) ? name.simpleName : undefined;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
return undefined;
|
|
189
|
+
}
|
|
164
190
|
|
|
165
|
-
|
|
191
|
+
function isFunctionComponent(declaration: J): boolean {
|
|
192
|
+
if (declaration.kind === J.Kind.MethodDeclaration) return true;
|
|
193
|
+
return declaration.kind === J.Kind.VariableDeclarations &&
|
|
194
|
+
(declaration as J.VariableDeclarations).variables[0].element.initializer?.element.kind === JS.Kind.ArrowFunction;
|
|
195
|
+
}
|
|
166
196
|
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
197
|
+
/** The component with the default of `prop` moved into its destructured props, or undefined when it can't be. */
|
|
198
|
+
function moveDefault(component: J, prop: JS.PropertyAssignment): J | undefined {
|
|
199
|
+
const key = prop.name.element;
|
|
200
|
+
if (!isIdentifier(key) || !prop.initializer) return undefined;
|
|
201
|
+
return updateProps(component, pattern => withDefault(pattern, key.simpleName, prop.initializer!));
|
|
202
|
+
}
|
|
170
203
|
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
204
|
+
function updateProps(component: J, update: (pattern: JS.ObjectBindingPattern) => JS.ObjectBindingPattern | undefined): J | undefined {
|
|
205
|
+
if (component.kind === J.Kind.MethodDeclaration) {
|
|
206
|
+
const method = component as J.MethodDeclaration;
|
|
207
|
+
const [first, ...rest] = method.parameters.elements;
|
|
208
|
+
const param = first && updateParam(first.element, update);
|
|
209
|
+
return param && {...method, parameters: {...method.parameters, elements: [{...first, element: param}, ...rest]}} as J.MethodDeclaration;
|
|
210
|
+
}
|
|
211
|
+
const declarations = component as J.VariableDeclarations;
|
|
212
|
+
const variable = declarations.variables[0];
|
|
213
|
+
const arrow = variable.element.initializer!.element as JS.ArrowFunction;
|
|
214
|
+
const [first, ...rest] = arrow.lambda.parameters.parameters;
|
|
215
|
+
const param = first && updateParam(first.element, update);
|
|
216
|
+
if (!param) return undefined;
|
|
217
|
+
const lambda: J.Lambda = {...arrow.lambda, parameters: {...arrow.lambda.parameters, parameters: [{...first, element: param}, ...rest]}};
|
|
218
|
+
const initializer = {...variable.element.initializer!, element: {...arrow, lambda}};
|
|
219
|
+
return {...declarations, variables: [{...variable, element: {...variable.element, initializer}}]} as J.VariableDeclarations;
|
|
220
|
+
}
|
|
177
221
|
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
if (isIdentifier(name) && defaults.has(name.simpleName) && !binding.initializer) {
|
|
188
|
-
const defaultVal = defaults.get(name.simpleName)!;
|
|
189
|
-
changed = true;
|
|
190
|
-
const updatedBinding = create(binding, draft => {
|
|
191
|
-
draft.initializer = {
|
|
192
|
-
kind: J.Kind.LeftPadded,
|
|
193
|
-
before: {kind: 'org.openrewrite.java.tree.Space' as const, whitespace: ' ', comments: []},
|
|
194
|
-
element: defaultVal,
|
|
195
|
-
markers: binding.markers
|
|
196
|
-
} as any;
|
|
197
|
-
});
|
|
198
|
-
if (elem.element) {
|
|
199
|
-
return {...elem, element: updatedBinding};
|
|
200
|
-
}
|
|
201
|
-
return updatedBinding;
|
|
202
|
-
}
|
|
203
|
-
}
|
|
222
|
+
function updateParam<T>(param: T, update: (pattern: JS.ObjectBindingPattern) => JS.ObjectBindingPattern | undefined): T | undefined {
|
|
223
|
+
const declarations = param as unknown as J.VariableDeclarations;
|
|
224
|
+
if (declarations.kind !== J.Kind.VariableDeclarations) return undefined;
|
|
225
|
+
const variable = declarations.variables[0];
|
|
226
|
+
const pattern = variable.element.name as unknown as JS.ObjectBindingPattern;
|
|
227
|
+
if (pattern.kind !== JS.Kind.ObjectBindingPattern) return undefined;
|
|
228
|
+
const updated = update(pattern);
|
|
229
|
+
return updated && {...declarations, variables: [{...variable, element: {...variable.element, name: updated}}, ...declarations.variables.slice(1)]} as unknown as T;
|
|
230
|
+
}
|
|
204
231
|
|
|
205
|
-
|
|
206
|
-
|
|
232
|
+
/** The pattern with `prop` defaulting to `value`, replacing a default it has: `defaultProps` wins at runtime. */
|
|
233
|
+
function withDefault(pattern: JS.ObjectBindingPattern, prop: string, value: Expression): JS.ObjectBindingPattern | undefined {
|
|
234
|
+
const index = pattern.bindings.elements.findIndex(padded => {
|
|
235
|
+
const binding = padded.element as JS.BindingElement;
|
|
236
|
+
const name = binding.propertyName?.element ?? binding.name;
|
|
237
|
+
return binding.kind === JS.Kind.BindingElement && isIdentifier(name) && name.simpleName === prop;
|
|
238
|
+
});
|
|
239
|
+
if (index < 0) return undefined;
|
|
240
|
+
// The value moves from module scope into the parameter scope, where the bindings of the pattern shadow it
|
|
241
|
+
const read = namesReferencedWithin(value);
|
|
242
|
+
if (read.size > 0 && bindingNames(pattern).some(({name}) => read.has(name))) return undefined;
|
|
243
|
+
const padded = pattern.bindings.elements[index];
|
|
244
|
+
const binding = padded.element as JS.BindingElement;
|
|
245
|
+
const initializer = binding.initializer;
|
|
246
|
+
const updated: JS.BindingElement = {
|
|
247
|
+
...binding,
|
|
248
|
+
initializer: {
|
|
249
|
+
kind: J.Kind.LeftPadded,
|
|
250
|
+
before: initializer?.before ?? singleSpace,
|
|
251
|
+
element: {...value, prefix: initializer?.element.prefix ?? singleSpace},
|
|
252
|
+
markers: initializer?.markers ?? emptyMarkers
|
|
253
|
+
}
|
|
254
|
+
};
|
|
255
|
+
const elements = [...pattern.bindings.elements];
|
|
256
|
+
elements[index] = {...padded, element: updated};
|
|
257
|
+
return {...pattern, bindings: {...pattern.bindings, elements}};
|
|
258
|
+
}
|
|
207
259
|
|
|
208
|
-
|
|
260
|
+
function hasTodo(assignment: J.Assignment): boolean {
|
|
261
|
+
return assignment.prefix.comments.some(c => isTextComment(c) && c.text === TODO);
|
|
262
|
+
}
|
|
209
263
|
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
}
|
|
264
|
+
function withTodo(assignment: J.Assignment): J.Assignment {
|
|
265
|
+
if (hasTodo(assignment)) return assignment;
|
|
266
|
+
const lineStart = /\r?\n[ \t]*$/.exec(assignment.prefix.whitespace)?.[0] ?? '\n';
|
|
267
|
+
const todo: TextComment = {kind: J.Kind.TextComment, multiline: false, text: TODO, suffix: lineStart, markers: emptyMarkers};
|
|
268
|
+
return {...assignment, prefix: {...assignment.prefix, comments: [todo, ...assignment.prefix.comments]}};
|
|
216
269
|
}
|
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import {ExecutionContext, Recipe, TreeVisitor, check, not} from "@openrewrite/rewrite";
|
|
1
|
+
import {ExecutionContext, Recipe, TreeVisitor, check, mapAsync, not, updateIfChanged} from "@openrewrite/rewrite";
|
|
2
2
|
import {JavaScriptVisitor, JS, isVendoredOrBundled, withStringValue} from "@openrewrite/rewrite/javascript";
|
|
3
3
|
import {J} from "@openrewrite/rewrite/java";
|
|
4
|
-
import {create} from "mutative";
|
|
5
4
|
|
|
6
5
|
/**
|
|
7
6
|
* Replaces `react-test-renderer/shallow` imports with `react-shallow-renderer`.
|
|
@@ -29,40 +28,18 @@ export class ReplaceReactShallowRenderer extends Recipe {
|
|
|
29
28
|
async editor(): Promise<TreeVisitor<any, ExecutionContext>> {
|
|
30
29
|
return check(not(isVendoredOrBundled()), new class extends JavaScriptVisitor<ExecutionContext> {
|
|
31
30
|
override async visitJsCompilationUnit(cu: JS.CompilationUnit, ctx: ExecutionContext): Promise<J | undefined> {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
if (literal.value !== 'react-test-renderer/shallow') return paddedStmt;
|
|
45
|
-
|
|
46
|
-
const newLiteral = withStringValue(literal, 'react-shallow-renderer');
|
|
47
|
-
|
|
48
|
-
const newModSpec = (imp.moduleSpecifier as any).element
|
|
49
|
-
? {...(imp.moduleSpecifier as any), element: newLiteral}
|
|
50
|
-
: newLiteral;
|
|
51
|
-
|
|
52
|
-
changed = true;
|
|
53
|
-
return {
|
|
54
|
-
...paddedStmt,
|
|
55
|
-
element: {
|
|
56
|
-
...imp,
|
|
57
|
-
moduleSpecifier: newModSpec
|
|
58
|
-
}
|
|
59
|
-
};
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
if (!changed) return result;
|
|
63
|
-
|
|
64
|
-
return create(result, draft => {
|
|
65
|
-
draft.statements = newStatements as any;
|
|
31
|
+
const result = await super.visitJsCompilationUnit(cu, ctx) as JS.CompilationUnit;
|
|
32
|
+
return updateIfChanged(result, {
|
|
33
|
+
statements: await mapAsync(result.statements, async paddedStmt => {
|
|
34
|
+
const imp = paddedStmt.element as JS.Import;
|
|
35
|
+
const moduleSpecifier = imp.kind === JS.Kind.Import ? imp.moduleSpecifier : undefined;
|
|
36
|
+
const literal = moduleSpecifier?.element as J.Literal | undefined;
|
|
37
|
+
if (literal?.kind !== J.Kind.Literal || literal.value !== 'react-test-renderer/shallow') return paddedStmt;
|
|
38
|
+
return {
|
|
39
|
+
...paddedStmt,
|
|
40
|
+
element: {...imp, moduleSpecifier: {...moduleSpecifier!, element: withStringValue(literal, 'react-shallow-renderer')}}
|
|
41
|
+
};
|
|
42
|
+
})
|
|
66
43
|
});
|
|
67
44
|
}
|
|
68
45
|
}());
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import {ExecutionContext, Recipe, TreeVisitor, check, not, randomId} from "@openrewrite/rewrite";
|
|
2
|
-
import {capture, JavaScriptVisitor, pattern, raw, rewrite, template, isVendoredOrBundled, maybeAddImport, maybeRemoveImport} from "@openrewrite/rewrite/javascript";
|
|
2
|
+
import {capture, JavaScriptVisitor, pattern, raw, rewrite, template, isVendoredOrBundled, maybeAddImport, maybeRemoveImport, namesDeclaredWithin, namesReferencedWithin} from "@openrewrite/rewrite/javascript";
|
|
3
3
|
import {J, singleSpace} from "@openrewrite/rewrite/java";
|
|
4
4
|
import {baseTypeArguments, unwrapBaseType} from "../util/react-class";
|
|
5
5
|
|
|
@@ -107,6 +107,8 @@ export class ClassToFunctional extends Recipe {
|
|
|
107
107
|
|
|
108
108
|
const propsParam = this.propsParameter(cd);
|
|
109
109
|
if (propsParam === undefined) return cd;
|
|
110
|
+
// The new `props` parameter would capture a `props` the render body binds or reads from further out
|
|
111
|
+
if (namesDeclaredWithin(renderMethod.body).has('props') || namesReferencedWithin(renderMethod.body).has('props')) return cd;
|
|
110
112
|
|
|
111
113
|
// Keep `export` (and `export default`) so importers still resolve the component.
|
|
112
114
|
const modifiers = cd.modifiers;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import {Cursor, emptyMarkers, ExecutionContext, findMarker, Marker, printer, randomId, Recipe, Tree, TreeVisitor, check, not} from "@openrewrite/rewrite";
|
|
1
|
+
import {Cursor, emptyMarkers, ExecutionContext, findMarker, Marker, printer, randomId, Recipe, Tree, TreeVisitor, check, mapAsync, not, updateIfChanged} from "@openrewrite/rewrite";
|
|
2
2
|
import {compilationUnitOf, isObjectLiteral, JavaScriptVisitor, JS, maybeBind, maybeUnbind, moduleBindings, raw, template, isVendoredOrBundled} from "@openrewrite/rewrite/javascript";
|
|
3
|
-
import {
|
|
3
|
+
import {isTextComment, J, Statement, TextComment} from "@openrewrite/rewrite/java";
|
|
4
|
+
import {reindent, shiftComment, shiftLines} from "../util/reindent";
|
|
4
5
|
|
|
5
6
|
const LIFECYCLE_METHODS = new Set([
|
|
6
7
|
'render',
|
|
@@ -47,45 +48,25 @@ export class CreateClassToES6 extends Recipe {
|
|
|
47
48
|
return check(not(isVendoredOrBundled()), new class extends JavaScriptVisitor<ExecutionContext> {
|
|
48
49
|
private convertedClassIds = new Set<string>();
|
|
49
50
|
|
|
50
|
-
private stripSemicolonFromConverted(statements:
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
const hasSemicolon = stmt.markers?.markers?.some(
|
|
55
|
-
(m: any) => m.kind === SEMICOLON_KIND
|
|
56
|
-
);
|
|
57
|
-
if (hasSemicolon) {
|
|
58
|
-
modified = true;
|
|
59
|
-
return {
|
|
60
|
-
...stmt,
|
|
61
|
-
markers: {
|
|
62
|
-
...stmt.markers,
|
|
63
|
-
markers: stmt.markers.markers.filter(
|
|
64
|
-
(m: any) => m.kind !== SEMICOLON_KIND
|
|
65
|
-
)
|
|
66
|
-
}
|
|
67
|
-
};
|
|
68
|
-
}
|
|
51
|
+
private stripSemicolonFromConverted(statements: J.RightPadded<Statement>[]): Promise<J.RightPadded<Statement>[]> {
|
|
52
|
+
return mapAsync(statements, async stmt => {
|
|
53
|
+
if (!this.convertedClassIds.has(stmt.element.id) || !stmt.markers.markers.some(m => m.kind === SEMICOLON_KIND)) {
|
|
54
|
+
return stmt;
|
|
69
55
|
}
|
|
70
|
-
return stmt;
|
|
56
|
+
return {...stmt, markers: {...stmt.markers, markers: stmt.markers.markers.filter(m => m.kind !== SEMICOLON_KIND)}};
|
|
71
57
|
});
|
|
72
|
-
return { modified, statements: newStatements };
|
|
73
58
|
}
|
|
74
59
|
|
|
75
60
|
override async visitJsCompilationUnit(cu: JS.CompilationUnit, ctx: ExecutionContext): Promise<J | undefined> {
|
|
76
|
-
|
|
61
|
+
const c = await super.visitJsCompilationUnit(cu, ctx) as JS.CompilationUnit;
|
|
77
62
|
if (this.convertedClassIds.size === 0) return c;
|
|
78
|
-
|
|
79
|
-
const result = this.stripSemicolonFromConverted(c.statements as any);
|
|
80
|
-
return result.modified ? {...c, statements: result.statements} as any : c;
|
|
63
|
+
return updateIfChanged(c, {statements: await this.stripSemicolonFromConverted(c.statements)});
|
|
81
64
|
}
|
|
82
65
|
|
|
83
66
|
override async visitBlock(block: J.Block, ctx: ExecutionContext): Promise<J | undefined> {
|
|
84
|
-
|
|
67
|
+
const b = await super.visitBlock(block, ctx) as J.Block;
|
|
85
68
|
if (this.convertedClassIds.size === 0) return b;
|
|
86
|
-
|
|
87
|
-
const result = this.stripSemicolonFromConverted(b.statements as any);
|
|
88
|
-
return result.modified ? {...b, statements: result.statements} as any : b;
|
|
69
|
+
return updateIfChanged(b, {statements: await this.stripSemicolonFromConverted(b.statements)});
|
|
89
70
|
}
|
|
90
71
|
|
|
91
72
|
override async visitVariableDeclarations(varDecl: J.VariableDeclarations, ctx: ExecutionContext): Promise<J | undefined> {
|
|
@@ -498,38 +479,6 @@ function spaceText(space: J.Space): string {
|
|
|
498
479
|
}).join('');
|
|
499
480
|
}
|
|
500
481
|
|
|
501
|
-
/** The tree with each line it spans that is indented by `from` indented by `to` instead. */
|
|
502
|
-
async function reindent<T extends J>(tree: T, from: string, to: string): Promise<T> {
|
|
503
|
-
if (from === to) return tree;
|
|
504
|
-
const shift = (text: string) => shiftLines(text, from, to);
|
|
505
|
-
const shiftSpace = (space: J.Space): J.Space => ({
|
|
506
|
-
...space,
|
|
507
|
-
whitespace: shift(space.whitespace),
|
|
508
|
-
comments: space.comments.map(c => ({...shiftComment(c, shift), suffix: shift(c.suffix)}))
|
|
509
|
-
});
|
|
510
|
-
return await new class extends JavaScriptVisitor<void> {
|
|
511
|
-
override async visitSpace(space: J.Space, p: void): Promise<J.Space> {
|
|
512
|
-
return shiftSpace(space);
|
|
513
|
-
}
|
|
514
|
-
|
|
515
|
-
// A trailing comma keeps the whitespace after it
|
|
516
|
-
protected override async visitMarker<M extends Marker>(marker: M, p: void): Promise<M> {
|
|
517
|
-
const suffix = (marker as any).suffix;
|
|
518
|
-
return suffix?.kind === J.Kind.Space ? {...marker, suffix: shiftSpace(suffix)} : marker;
|
|
519
|
-
}
|
|
520
|
-
}().visit(tree, undefined) as T;
|
|
521
|
-
}
|
|
522
|
-
|
|
523
|
-
/** The text with each line after its first that is indented by `from` indented by `to` instead. */
|
|
524
|
-
function shiftLines(text: string, from: string, to: string): string {
|
|
525
|
-
return text.split('\n').map((line, i) => i > 0 && line.startsWith(from) ? to + line.substring(from.length) : line).join('\n');
|
|
526
|
-
}
|
|
527
|
-
|
|
528
|
-
/** The comment with `shift` applied to the lines its text spans, as only a block comment's can. */
|
|
529
|
-
function shiftComment(comment: Comment, shift: (text: string) => string): Comment {
|
|
530
|
-
return isTextComment(comment) && comment.multiline ? {...comment, text: shift(comment.text)} as TextComment : comment;
|
|
531
|
-
}
|
|
532
|
-
|
|
533
482
|
/** Whether the file binds `name` with an `import type`, as its default or namespace import. */
|
|
534
483
|
function importsTypeOnly(cu: JS.CompilationUnit | undefined, name: string): boolean {
|
|
535
484
|
return cu?.statements.some(({element}) => {
|