@openrewrite/recipes-react 0.15.0 → 0.16.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/cra-to-vite/find-cra-config-blockers.js +20 -28
- package/dist/migration/cra-to-vite/find-cra-config-blockers.js.map +1 -1
- package/dist/migration/cra-to-vite/replace-react-scripts-dependency.d.ts.map +1 -1
- package/dist/migration/cra-to-vite/replace-react-scripts-dependency.js +13 -33
- package/dist/migration/cra-to-vite/replace-react-scripts-dependency.js.map +1 -1
- package/dist/migration/cra-to-vite/shared.d.ts +1 -14
- package/dist/migration/cra-to-vite/shared.d.ts.map +1 -1
- package/dist/migration/cra-to-vite/shared.js +13 -126
- package/dist/migration/cra-to-vite/shared.js.map +1 -1
- package/dist/migration/cra-to-vite/update-react-scripts-scripts.d.ts.map +1 -1
- package/dist/migration/cra-to-vite/update-react-scripts-scripts.js +16 -21
- package/dist/migration/cra-to-vite/update-react-scripts-scripts.js.map +1 -1
- package/dist/migration/cra-to-vite/update-tsconfig-for-vite.d.ts.map +1 -1
- package/dist/migration/cra-to-vite/update-tsconfig-for-vite.js +4 -13
- package/dist/migration/cra-to-vite/update-tsconfig-for-vite.js.map +1 -1
- package/dist/react17/migrate-deprecated-lifecycles.js +1 -1
- package/dist/react17/migrate-deprecated-lifecycles.js.map +1 -1
- package/dist/react17/use-new-jsx-transform.d.ts.map +1 -1
- package/dist/react17/use-new-jsx-transform.js +30 -78
- package/dist/react17/use-new-jsx-transform.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 +62 -20
- package/dist/react18/replace-unmount-component-at-node.js.map +1 -1
- package/dist/react19/replace-default-props.d.ts.map +1 -1
- package/dist/react19/replace-default-props.js +10 -13
- 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 +1 -6
- package/dist/react19/replace-react-shallow-renderer.js.map +1 -1
- package/dist/refactoring/create-class-to-es6.d.ts.map +1 -1
- package/dist/refactoring/create-class-to-es6.js +4 -7
- 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 +3 -4
- package/dist/refactoring/create-element-to-jsx.js.map +1 -1
- package/dist/refactoring/prop-types-to-typescript.js +2 -3
- package/dist/refactoring/prop-types-to-typescript.js.map +1 -1
- package/dist/refactoring/pure-render-mixin.d.ts.map +1 -1
- package/dist/refactoring/pure-render-mixin.js +3 -6
- package/dist/refactoring/pure-render-mixin.js.map +1 -1
- package/dist/testing/enzyme-host-components.d.ts +14 -0
- package/dist/testing/enzyme-host-components.d.ts.map +1 -0
- package/dist/testing/enzyme-host-components.js +245 -0
- package/dist/testing/enzyme-host-components.js.map +1 -0
- package/dist/testing/enzyme-to-react-testing-library.d.ts +10 -5
- package/dist/testing/enzyme-to-react-testing-library.d.ts.map +1 -1
- package/dist/testing/enzyme-to-react-testing-library.js +138 -113
- package/dist/testing/enzyme-to-react-testing-library.js.map +1 -1
- package/dist/testing/enzyme-wrapper-scope.d.ts +13 -4
- package/dist/testing/enzyme-wrapper-scope.d.ts.map +1 -1
- package/dist/testing/enzyme-wrapper-scope.js +161 -163
- package/dist/testing/enzyme-wrapper-scope.js.map +1 -1
- package/dist/util/module-imports.d.ts +12 -0
- package/dist/util/module-imports.d.ts.map +1 -0
- package/dist/util/module-imports.js +56 -0
- package/dist/util/module-imports.js.map +1 -0
- package/dist/util/react-class.d.ts +1 -0
- package/dist/util/react-class.d.ts.map +1 -1
- package/dist/util/react-class.js +3 -2
- package/dist/util/react-class.js.map +1 -1
- package/package.json +3 -3
- package/src/migration/cra-to-vite/find-cra-config-blockers.ts +21 -27
- package/src/migration/cra-to-vite/replace-react-scripts-dependency.ts +14 -39
- package/src/migration/cra-to-vite/shared.ts +15 -136
- package/src/migration/cra-to-vite/update-react-scripts-scripts.ts +16 -21
- package/src/migration/cra-to-vite/update-tsconfig-for-vite.ts +6 -13
- package/src/react17/migrate-deprecated-lifecycles.ts +2 -2
- package/src/react17/use-new-jsx-transform.ts +30 -72
- package/src/react18/replace-unmount-component-at-node.ts +78 -32
- package/src/react19/replace-default-props.ts +11 -15
- package/src/react19/replace-react-shallow-renderer.ts +2 -7
- package/src/refactoring/create-class-to-es6.ts +6 -9
- package/src/refactoring/create-element-to-jsx.ts +4 -6
- package/src/refactoring/prop-types-to-typescript.ts +4 -4
- package/src/refactoring/pure-render-mixin.ts +4 -7
- package/src/testing/enzyme-host-components.ts +283 -0
- package/src/testing/enzyme-to-react-testing-library.ts +176 -107
- package/src/testing/enzyme-wrapper-scope.ts +144 -137
- package/src/util/module-imports.ts +55 -0
- package/src/util/react-class.ts +2 -1
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import {ExecutionContext, Recipe, TreeVisitor} from "@openrewrite/rewrite";
|
|
2
|
-
import {
|
|
3
|
-
|
|
2
|
+
import {
|
|
3
|
+
elementsOf, findMember, getMemberKeyName, isArray, isLiteral, isObject, Json, JsonVisitor, mapElements, updateMember
|
|
4
|
+
} from "@openrewrite/rewrite/json";
|
|
4
5
|
|
|
5
6
|
type ConfigKind = 'tsconfig' | 'babel' | 'eslint';
|
|
6
7
|
|
|
@@ -9,7 +10,7 @@ const BABEL_JSX_PRESETS = new Set([
|
|
|
9
10
|
'@babel/react',
|
|
10
11
|
'@babel/plugin-transform-react-jsx',
|
|
11
12
|
]);
|
|
12
|
-
const ESLINT_RULES =
|
|
13
|
+
const ESLINT_RULES = ['react/react-in-jsx-scope', 'react/jsx-uses-react'];
|
|
13
14
|
|
|
14
15
|
function configKind(sourcePath: string): ConfigKind | undefined {
|
|
15
16
|
const name = sourcePath.split('/').pop()!;
|
|
@@ -21,33 +22,28 @@ function configKind(sourcePath: string): ConfigKind | undefined {
|
|
|
21
22
|
|
|
22
23
|
function withString(literal: Json.Literal, value: string): Json.Literal {
|
|
23
24
|
const quote = literal.source.startsWith("'") ? "'" : '"';
|
|
24
|
-
return
|
|
25
|
-
draft.source = `${quote}${value}${quote}`;
|
|
26
|
-
draft.value = value;
|
|
27
|
-
});
|
|
25
|
+
return {...literal, source: `${quote}${value}${quote}`, value};
|
|
28
26
|
}
|
|
29
27
|
|
|
30
|
-
function
|
|
31
|
-
|
|
32
|
-
const members = object.members.map(padded => {
|
|
33
|
-
const element = padded.element;
|
|
34
|
-
if (!isMember(element)) return padded;
|
|
35
|
-
const updated = fn(element);
|
|
36
|
-
if (updated === element) return padded;
|
|
37
|
-
changed = true;
|
|
38
|
-
return create(padded, draft => {
|
|
39
|
-
draft.element = updated as any;
|
|
40
|
-
});
|
|
41
|
-
});
|
|
42
|
-
return changed ? create(object, draft => {
|
|
43
|
-
draft.members = members as any;
|
|
44
|
-
}) : object;
|
|
28
|
+
function withValue(member: Json.Member, value: Json.Value): Json.Member {
|
|
29
|
+
return value === member.value ? member : {...member, value};
|
|
45
30
|
}
|
|
46
31
|
|
|
47
|
-
function
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
32
|
+
function updateJsx(value: Json.Value): Json.Value {
|
|
33
|
+
if (!isLiteral(value)) return value;
|
|
34
|
+
if (value.value === 'react') return withString(value, 'react-jsx');
|
|
35
|
+
if (value.value === 'react-dev') return withString(value, 'react-jsxdev');
|
|
36
|
+
return value;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const isOff = (v: unknown) => v === 'off' || v === 0;
|
|
40
|
+
|
|
41
|
+
function turnRuleOff(value: Json.Value): Json.Value {
|
|
42
|
+
if (isLiteral(value)) return isOff(value.value) ? value : withString(value, 'off');
|
|
43
|
+
if (!isArray(value)) return value;
|
|
44
|
+
const [first] = elementsOf(value);
|
|
45
|
+
if (!first || !isLiteral(first) || isOff(first.value)) return value;
|
|
46
|
+
return mapElements(value, element => element === first ? withString(first, 'off') : element);
|
|
51
47
|
}
|
|
52
48
|
|
|
53
49
|
/**
|
|
@@ -87,13 +83,12 @@ export class UseNewJsxTransform extends Recipe {
|
|
|
87
83
|
if (!isObject(visited.value)) return visited;
|
|
88
84
|
const key = getMemberKeyName(visited);
|
|
89
85
|
if (this.kind === 'tsconfig' && key === 'compilerOptions') {
|
|
90
|
-
const names = visited.value.members.map(m => isMember(m.element) ? getMemberKeyName(m.element) : undefined);
|
|
91
86
|
// TS5089: `jsxFactory`/`jsxFragmentFactory` cannot be combined with `react-jsx`
|
|
92
|
-
if (
|
|
93
|
-
return withValue(visited,
|
|
87
|
+
if (findMember(visited.value, 'jsxFactory') || findMember(visited.value, 'jsxFragmentFactory')) return visited;
|
|
88
|
+
return withValue(visited, updateMember(visited.value, 'jsx', updateJsx));
|
|
94
89
|
}
|
|
95
90
|
if (this.kind === 'eslint' && key === 'rules') {
|
|
96
|
-
return withValue(visited,
|
|
91
|
+
return withValue(visited, ESLINT_RULES.reduce((rules, rule) => updateMember(rules, rule, turnRuleOff), visited.value));
|
|
97
92
|
}
|
|
98
93
|
return visited;
|
|
99
94
|
}
|
|
@@ -101,49 +96,12 @@ export class UseNewJsxTransform extends Recipe {
|
|
|
101
96
|
protected override async visitArray(array: Json.Array, ctx: ExecutionContext): Promise<Json | undefined> {
|
|
102
97
|
const visited = await super.visitArray(array, ctx) as Json.Array;
|
|
103
98
|
if (this.kind !== 'babel') return visited;
|
|
104
|
-
const [name, options] = visited
|
|
99
|
+
const [name, options] = elementsOf(visited);
|
|
105
100
|
if (!name || !options || !isLiteral(name) || !BABEL_JSX_PRESETS.has(name.value) || !isObject(options)) return visited;
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
return getMemberKeyName(m) === 'runtime' && isLiteral(value) && value.value === 'classic'
|
|
111
|
-
? withValue(m, withString(value, 'automatic')) : m;
|
|
112
|
-
});
|
|
113
|
-
if (updated === options) return visited;
|
|
114
|
-
return create(visited, draft => {
|
|
115
|
-
draft.values[1] = create(visited.values[1], d => {
|
|
116
|
-
d.element = updated as any;
|
|
117
|
-
}) as any;
|
|
118
|
-
});
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
private updateJsx(member: Json.Member): Json.Member {
|
|
122
|
-
const value = member.value;
|
|
123
|
-
if (getMemberKeyName(member) !== 'jsx' || !isLiteral(value)) return member;
|
|
124
|
-
if (value.value === 'react') return withValue(member, withString(value, 'react-jsx'));
|
|
125
|
-
if (value.value === 'react-dev') return withValue(member, withString(value, 'react-jsxdev'));
|
|
126
|
-
return member;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
private turnRuleOff(member: Json.Member): Json.Member {
|
|
130
|
-
const key = getMemberKeyName(member);
|
|
131
|
-
if (!key || !ESLINT_RULES.has(key)) return member;
|
|
132
|
-
const value = member.value;
|
|
133
|
-
const isOff = (v: any) => v === 'off' || v === 0;
|
|
134
|
-
if (isLiteral(value)) {
|
|
135
|
-
return isOff(value.value) ? member : withValue(member, withString(value, 'off'));
|
|
136
|
-
}
|
|
137
|
-
if (isArray(value)) {
|
|
138
|
-
const first = value.values[0]?.element;
|
|
139
|
-
if (!first || !isLiteral(first) || isOff(first.value)) return member;
|
|
140
|
-
return withValue(member, create(value, draft => {
|
|
141
|
-
draft.values[0] = create(value.values[0], d => {
|
|
142
|
-
d.element = withString(first, 'off') as any;
|
|
143
|
-
}) as any;
|
|
144
|
-
}));
|
|
145
|
-
}
|
|
146
|
-
return member;
|
|
101
|
+
if (findMember(options, 'pragma') || findMember(options, 'pragmaFrag')) return visited;
|
|
102
|
+
const updated = updateMember(options, 'runtime', value =>
|
|
103
|
+
isLiteral(value) && value.value === 'classic' ? withString(value, 'automatic') : value);
|
|
104
|
+
return updated === options ? visited : mapElements(visited, value => value === options ? updated : value);
|
|
147
105
|
}
|
|
148
106
|
};
|
|
149
107
|
}
|
|
@@ -1,68 +1,114 @@
|
|
|
1
|
-
import {ExecutionContext,
|
|
1
|
+
import {and, check, Cursor, ExecutionContext, foundSearchResult, not, Recipe, TreeVisitor} from "@openrewrite/rewrite";
|
|
2
2
|
import {
|
|
3
3
|
capture,
|
|
4
|
+
isEqual,
|
|
4
5
|
JavaScriptVisitor,
|
|
6
|
+
isVendoredOrBundled,
|
|
7
|
+
JS,
|
|
5
8
|
maybeRemoveImport,
|
|
6
9
|
pattern,
|
|
7
|
-
|
|
10
|
+
raw,
|
|
8
11
|
template,
|
|
9
|
-
|
|
12
|
+
usesMethod
|
|
10
13
|
} from "@openrewrite/rewrite/javascript";
|
|
11
|
-
import {Expression, J} from "@openrewrite/rewrite/java";
|
|
14
|
+
import {Expression, isIdentifier, J} from "@openrewrite/rewrite/java";
|
|
15
|
+
|
|
16
|
+
const GUIDE = "https://react.dev/blog/2022/03/08/react-18-upgrade-guide#updates-to-client-rendering-apis";
|
|
17
|
+
const NO_ROOT_MESSAGE = `No \`root\` for this container is in scope. Call \`unmount()\` on the root returned by \`createRoot\`: ${GUIDE}`;
|
|
18
|
+
const VALUE_USED_MESSAGE = `The result of \`unmountComponentAtNode\` is used, but \`root.unmount()\` returns \`void\`: ${GUIDE}`;
|
|
12
19
|
|
|
13
20
|
/**
|
|
14
|
-
* Replaces `
|
|
21
|
+
* Replaces `unmountComponentAtNode(container)` with `root.unmount()` when the
|
|
22
|
+
* `root` returned by `createRoot(container)` for the same container is in scope.
|
|
15
23
|
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
24
|
+
* `createRoot(container).unmount()` would unmount a new, empty root and leave the
|
|
25
|
+
* mounted tree alive, so without a root in scope the call is kept and marked.
|
|
26
|
+
* It is also kept and marked when its `boolean` result is used, because
|
|
27
|
+
* `unmount()` returns `void`.
|
|
18
28
|
*
|
|
19
29
|
* Before:
|
|
20
30
|
* ```tsx
|
|
21
|
-
*
|
|
22
|
-
* unmountComponentAtNode(
|
|
31
|
+
* const root = createRoot(container);
|
|
32
|
+
* unmountComponentAtNode(container);
|
|
23
33
|
* ```
|
|
24
34
|
*
|
|
25
35
|
* After:
|
|
26
36
|
* ```tsx
|
|
27
|
-
*
|
|
28
|
-
*
|
|
37
|
+
* const root = createRoot(container);
|
|
38
|
+
* root.unmount();
|
|
29
39
|
* ```
|
|
30
40
|
*
|
|
31
41
|
* @see https://react.dev/blog/2022/03/08/react-18-upgrade-guide#updates-to-client-rendering-apis
|
|
32
42
|
*/
|
|
33
43
|
export class ReplaceUnmountComponentAtNode extends Recipe {
|
|
34
44
|
readonly name = "org.openrewrite.react.18.replace-unmount-component-at-node";
|
|
35
|
-
readonly displayName: string = "Replace `unmountComponentAtNode` with `
|
|
36
|
-
readonly description: string = "Replaces `ReactDOM.unmountComponentAtNode(container)` with `
|
|
45
|
+
readonly displayName: string = "Replace `unmountComponentAtNode` with `root.unmount()`";
|
|
46
|
+
readonly description: string = "Replaces `ReactDOM.unmountComponentAtNode(container)` with `root.unmount()` when the `root` created by `createRoot(container)` is in scope. Otherwise, and when the call's result is used, the call is kept and marked.";
|
|
37
47
|
|
|
38
48
|
async editor(): Promise<TreeVisitor<any, ExecutionContext>> {
|
|
39
|
-
const
|
|
49
|
+
const container = capture<Expression>();
|
|
50
|
+
const unmountCall = pattern`unmountComponentAtNode(${container})`.configure({
|
|
40
51
|
context: ['import { unmountComponentAtNode } from "react-dom";'],
|
|
41
52
|
dependencies: {'@types/react-dom': '^17.0.0'}
|
|
42
|
-
};
|
|
53
|
+
});
|
|
54
|
+
const rootContainer = capture<Expression>();
|
|
55
|
+
const createRootCall = pattern`createRoot(${rootContainer})`.configure({
|
|
56
|
+
context: ['import { createRoot } from "react-dom/client";'],
|
|
57
|
+
dependencies: {'@types/react-dom': '^18.0.0'}
|
|
58
|
+
});
|
|
43
59
|
|
|
44
|
-
const
|
|
60
|
+
const visitor = new class extends JavaScriptVisitor<ExecutionContext> {
|
|
61
|
+
override async visitMethodInvocation(method: J.MethodInvocation, p: ExecutionContext): Promise<J | undefined> {
|
|
62
|
+
const m = await super.visitMethodInvocation(method, p) as J.MethodInvocation;
|
|
45
63
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
context: ['import { createRoot } from "react-dom/client";'],
|
|
50
|
-
dependencies: {'@types/react-dom': '^18.0.0'}
|
|
51
|
-
})
|
|
52
|
-
}));
|
|
64
|
+
const match = await unmountCall.match(m, this.cursor);
|
|
65
|
+
const containerArg = match?.get(container);
|
|
66
|
+
if (!containerArg) return m;
|
|
53
67
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
68
|
+
if (!isStatement(this.cursor)) {
|
|
69
|
+
return foundSearchResult(m, VALUE_USED_MESSAGE);
|
|
70
|
+
}
|
|
57
71
|
|
|
58
|
-
const
|
|
59
|
-
if (
|
|
60
|
-
|
|
61
|
-
return result;
|
|
72
|
+
const root = await this.findRoot(containerArg);
|
|
73
|
+
if (!root) {
|
|
74
|
+
return foundSearchResult(m, NO_ROOT_MESSAGE);
|
|
62
75
|
}
|
|
63
76
|
|
|
64
|
-
|
|
77
|
+
maybeRemoveImport(this, "react-dom", "unmountComponentAtNode");
|
|
78
|
+
return await template`${raw(root)}.unmount()`.apply(m, this.cursor);
|
|
65
79
|
}
|
|
66
|
-
|
|
80
|
+
|
|
81
|
+
/** The name of a `const root = createRoot(<container>)` in an enclosing scope. */
|
|
82
|
+
private async findRoot(containerArg: Expression): Promise<string | undefined> {
|
|
83
|
+
for (let c: Cursor | undefined = this.cursor; c; c = c.parent) {
|
|
84
|
+
const statements: any[] | undefined = c.value?.kind === J.Kind.Block || c.value?.kind === JS.Kind.CompilationUnit
|
|
85
|
+
? c.value.statements
|
|
86
|
+
: undefined;
|
|
87
|
+
for (const statement of statements ?? []) {
|
|
88
|
+
const decl = statement.element ?? statement;
|
|
89
|
+
if (decl.kind !== J.Kind.VariableDeclarations) continue;
|
|
90
|
+
for (const v of decl.variables) {
|
|
91
|
+
const named = v.element;
|
|
92
|
+
const init = named.initializer?.element;
|
|
93
|
+
if (!init || !isIdentifier(named.name)) continue;
|
|
94
|
+
const rootArg = (await createRootCall.match(init, this.cursor))?.get(rootContainer);
|
|
95
|
+
if (rootArg && await isEqual(rootArg, containerArg)) {
|
|
96
|
+
return named.name.simpleName;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return undefined;
|
|
102
|
+
}
|
|
103
|
+
}();
|
|
104
|
+
|
|
105
|
+
return check(and(usesMethod("react-dom unmountComponentAtNode(..)"), not(isVendoredOrBundled())), visitor);
|
|
67
106
|
}
|
|
68
107
|
}
|
|
108
|
+
|
|
109
|
+
/** Whether the invocation at the cursor is an expression statement, so its value is unused. */
|
|
110
|
+
function isStatement(cursor: Cursor): boolean {
|
|
111
|
+
const parent = cursor.parent?.value;
|
|
112
|
+
const grand = cursor.parent?.parent?.value;
|
|
113
|
+
return parent?.kind === J.Kind.RightPadded && (grand?.kind === J.Kind.Block || grand?.kind === JS.Kind.CompilationUnit);
|
|
114
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import {ExecutionContext, Recipe, TreeVisitor, check, not} from "@openrewrite/rewrite";
|
|
2
|
-
import {JavaScriptVisitor, JS, isVendoredOrBundled} from "@openrewrite/rewrite/javascript";
|
|
2
|
+
import {isObjectLiteral, JavaScriptVisitor, JS, isVendoredOrBundled} from "@openrewrite/rewrite/javascript";
|
|
3
3
|
import {Expression, isIdentifier, J} from "@openrewrite/rewrite/java";
|
|
4
4
|
import {create} from "mutative";
|
|
5
5
|
|
|
@@ -111,20 +111,16 @@ export class ReplaceDefaultProps extends Recipe {
|
|
|
111
111
|
private extractObjectLiteralDefaults(expr: Expression): Map<string, Expression> {
|
|
112
112
|
const defaults = new Map<string, Expression>();
|
|
113
113
|
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
if (
|
|
123
|
-
|
|
124
|
-
const nameExpr = prop.name.element;
|
|
125
|
-
if (isIdentifier(nameExpr) && prop.initializer) {
|
|
126
|
-
defaults.set(nameExpr.simpleName, prop.initializer);
|
|
127
|
-
}
|
|
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);
|
|
128
124
|
}
|
|
129
125
|
}
|
|
130
126
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import {ExecutionContext, Recipe, TreeVisitor, check, not} from "@openrewrite/rewrite";
|
|
2
|
-
import {JavaScriptVisitor, JS, isVendoredOrBundled} from "@openrewrite/rewrite/javascript";
|
|
2
|
+
import {JavaScriptVisitor, JS, isVendoredOrBundled, withStringValue} from "@openrewrite/rewrite/javascript";
|
|
3
3
|
import {J} from "@openrewrite/rewrite/java";
|
|
4
4
|
import {create} from "mutative";
|
|
5
5
|
|
|
@@ -43,12 +43,7 @@ export class ReplaceReactShallowRenderer extends Recipe {
|
|
|
43
43
|
const literal = modSpec as J.Literal;
|
|
44
44
|
if (literal.value !== 'react-test-renderer/shallow') return paddedStmt;
|
|
45
45
|
|
|
46
|
-
const newLiteral =
|
|
47
|
-
draft.value = 'react-shallow-renderer';
|
|
48
|
-
if (draft.valueSource) {
|
|
49
|
-
draft.valueSource = draft.valueSource.replace('react-test-renderer/shallow', 'react-shallow-renderer');
|
|
50
|
-
}
|
|
51
|
-
});
|
|
46
|
+
const newLiteral = withStringValue(literal, 'react-shallow-renderer');
|
|
52
47
|
|
|
53
48
|
const newModSpec = (imp.moduleSpecifier as any).element
|
|
54
49
|
? {...(imp.moduleSpecifier as any), element: newLiteral}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {Cursor, emptyMarkers, ExecutionContext, findMarker, Marker, printer, randomId, Recipe, Tree, TreeVisitor, check, not} from "@openrewrite/rewrite";
|
|
2
|
-
import {compilationUnitOf, JavaScriptVisitor, JS, maybeBind, maybeUnbind, moduleBindings, raw, template, isVendoredOrBundled} from "@openrewrite/rewrite/javascript";
|
|
3
|
-
import {Comment, J, TextComment} from "@openrewrite/rewrite/java";
|
|
2
|
+
import {compilationUnitOf, isObjectLiteral, JavaScriptVisitor, JS, maybeBind, maybeUnbind, moduleBindings, raw, template, isVendoredOrBundled} from "@openrewrite/rewrite/javascript";
|
|
3
|
+
import {Comment, isTextComment, J, TextComment} from "@openrewrite/rewrite/java";
|
|
4
4
|
|
|
5
5
|
const LIFECYCLE_METHODS = new Set([
|
|
6
6
|
'render',
|
|
@@ -110,11 +110,8 @@ export class CreateClassToES6 extends Recipe {
|
|
|
110
110
|
const args = methodInv.arguments.elements;
|
|
111
111
|
if (args.length === 0) return vd;
|
|
112
112
|
|
|
113
|
-
const
|
|
114
|
-
if (
|
|
115
|
-
|
|
116
|
-
const specObj = specArg as J.NewClass;
|
|
117
|
-
if (!specObj.body) return vd;
|
|
113
|
+
const specObj = args[0].element;
|
|
114
|
+
if (!isObjectLiteral(specObj) || !specObj.body) return vd;
|
|
118
115
|
|
|
119
116
|
// Check for mixins — skip conversion if present
|
|
120
117
|
if (this.hasMixins(specObj.body)) return vd;
|
|
@@ -434,7 +431,7 @@ async function inPlaceOf(member: J.RightPadded<J>, field: J, value: J, lineFollo
|
|
|
434
431
|
const next = j < space.comments.length - 1 ? '' : around.after[i + 1]?.whitespace;
|
|
435
432
|
// The last one ends the line where a line comment would take in what follows
|
|
436
433
|
const suffix = next !== undefined ? shift(c.suffix + next) :
|
|
437
|
-
((c
|
|
434
|
+
(isTextComment(c) && c.multiline || lineFollows ? '' : lineStart ?? '\n') + member.after.whitespace;
|
|
438
435
|
return {...shiftComment(c, shift), suffix};
|
|
439
436
|
}));
|
|
440
437
|
return {
|
|
@@ -530,7 +527,7 @@ function shiftLines(text: string, from: string, to: string): string {
|
|
|
530
527
|
|
|
531
528
|
/** The comment with `shift` applied to the lines its text spans, as only a block comment's can. */
|
|
532
529
|
function shiftComment(comment: Comment, shift: (text: string) => string): Comment {
|
|
533
|
-
return (comment
|
|
530
|
+
return isTextComment(comment) && comment.multiline ? {...comment, text: shift(comment.text)} as TextComment : comment;
|
|
534
531
|
}
|
|
535
532
|
|
|
536
533
|
/** Whether the file binds `name` with an `import type`, as its default or namespace import. */
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import {ExecutionContext, printer, Recipe, TreeVisitor, check, not} from "@openrewrite/rewrite";
|
|
2
|
-
import {JavaScriptVisitor, JS, raw, template, isVendoredOrBundled} from "@openrewrite/rewrite/javascript";
|
|
2
|
+
import {isObjectLiteral, JavaScriptVisitor, JS, raw, template, isVendoredOrBundled} from "@openrewrite/rewrite/javascript";
|
|
3
3
|
import {J} from "@openrewrite/rewrite/java";
|
|
4
4
|
|
|
5
5
|
/**
|
|
@@ -116,13 +116,11 @@ export class CreateElementToJsx extends Recipe {
|
|
|
116
116
|
if ((propsExpr as J.Identifier).simpleName === 'undefined') return '';
|
|
117
117
|
}
|
|
118
118
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
const obj = propsExpr as J.NewClass;
|
|
122
|
-
if (!obj.body) return '';
|
|
119
|
+
if (isObjectLiteral(propsExpr)) {
|
|
120
|
+
if (!propsExpr.body) return '';
|
|
123
121
|
|
|
124
122
|
const attrs: string[] = [];
|
|
125
|
-
for (const stmt of
|
|
123
|
+
for (const stmt of propsExpr.body.statements) {
|
|
126
124
|
const s = stmt.element;
|
|
127
125
|
|
|
128
126
|
if (s.kind === JS.Kind.PropertyAssignment) {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import {ExecutionContext, Recipe, TreeVisitor, check, not} from "@openrewrite/rewrite";
|
|
2
|
-
import {isVendoredOrBundled, JavaScriptVisitor, JS, maybeAddImport, raw, template} from "@openrewrite/rewrite/javascript";
|
|
2
|
+
import {isObjectLiteral, isTypeScript, isVendoredOrBundled, JavaScriptVisitor, JS, maybeAddImport, raw, template} from "@openrewrite/rewrite/javascript";
|
|
3
3
|
import {emptySpace, J, singleSpace} from "@openrewrite/rewrite/java";
|
|
4
4
|
|
|
5
5
|
/** `PropTypes.<name>` validators with a fixed TypeScript type, and the `react` type each one needs. */
|
|
@@ -93,8 +93,8 @@ function isPropTypes(expr: J): boolean {
|
|
|
93
93
|
|
|
94
94
|
/** `name: type` / `name?: type` for each property of an object literal, or `undefined` if any is unsupported. */
|
|
95
95
|
function objectMembers(literal: J, reactTypes: Set<string>, names?: Set<string>): string[] | undefined {
|
|
96
|
-
if (literal
|
|
97
|
-
const body =
|
|
96
|
+
if (!isObjectLiteral(literal)) return undefined;
|
|
97
|
+
const body = literal.body;
|
|
98
98
|
if (!body) return undefined;
|
|
99
99
|
const members: string[] = [];
|
|
100
100
|
for (const statement of body.statements) {
|
|
@@ -247,7 +247,7 @@ export class PropTypesToTypeScript extends Recipe {
|
|
|
247
247
|
async editor(): Promise<TreeVisitor<any, ExecutionContext>> {
|
|
248
248
|
return check(not(isVendoredOrBundled()), new class extends JavaScriptVisitor<ExecutionContext> {
|
|
249
249
|
override async visitJsCompilationUnit(cu: JS.CompilationUnit): Promise<J | undefined> {
|
|
250
|
-
if (
|
|
250
|
+
if (!isTypeScript(cu)) return cu;
|
|
251
251
|
|
|
252
252
|
const reactTypes = new Set<string>();
|
|
253
253
|
const plans: { index: number; propsName: string; source: string }[] = [];
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import {Cursor, ExecutionContext, findMarker, Marker, Recipe, TreeVisitor, check, not} from "@openrewrite/rewrite";
|
|
2
|
-
import {JavaScriptVisitor, JS, maybeRemoveImport, template, isVendoredOrBundled} from "@openrewrite/rewrite/javascript";
|
|
2
|
+
import {isObjectLiteral, JavaScriptVisitor, JS, maybeRemoveImport, template, isVendoredOrBundled} from "@openrewrite/rewrite/javascript";
|
|
3
3
|
import {J, TextComment} from "@openrewrite/rewrite/java";
|
|
4
4
|
|
|
5
5
|
/**
|
|
@@ -53,11 +53,8 @@ export class PureRenderMixin extends Recipe {
|
|
|
53
53
|
const args = m.arguments.elements;
|
|
54
54
|
if (args.length === 0) return m;
|
|
55
55
|
|
|
56
|
-
const
|
|
57
|
-
if (
|
|
58
|
-
|
|
59
|
-
const specObj = specArg as J.NewClass;
|
|
60
|
-
if (!specObj.body) return m;
|
|
56
|
+
const specObj = args[0].element;
|
|
57
|
+
if (!isObjectLiteral(specObj) || !specObj.body) return m;
|
|
61
58
|
|
|
62
59
|
// Find the mixins property and check for PureRenderMixin
|
|
63
60
|
const mixinsInfo = this.findPureRenderMixin(specObj.body);
|
|
@@ -173,7 +170,7 @@ export class PureRenderMixin extends Recipe {
|
|
|
173
170
|
return React.addons.shallowCompare(this, nextProps, nextState);
|
|
174
171
|
}
|
|
175
172
|
}`.apply(specObj, new Cursor(specObj, this.cursor));
|
|
176
|
-
const method = obj
|
|
173
|
+
const method = obj && isObjectLiteral(obj) ? obj.body?.statements[0]?.element : undefined;
|
|
177
174
|
return method?.kind === J.Kind.MethodDeclaration ? method as J.MethodDeclaration : undefined;
|
|
178
175
|
}
|
|
179
176
|
|