@openrewrite/recipes-react 0.27.0 → 0.29.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/change-react-type.d.ts +14 -0
- package/dist/change-react-type.d.ts.map +1 -0
- package/dist/change-react-type.js +41 -0
- package/dist/change-react-type.js.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +31 -2
- package/dist/index.js.map +1 -1
- package/dist/react18/add-children-to-props.d.ts.map +1 -1
- package/dist/react18/add-children-to-props.js +24 -6
- package/dist/react18/add-children-to-props.js.map +1 -1
- package/dist/react19/deprecated-prop-types-types.d.ts +23 -0
- package/dist/react19/deprecated-prop-types-types.d.ts.map +1 -0
- package/dist/react19/deprecated-prop-types-types.js +191 -0
- package/dist/react19/deprecated-prop-types-types.js.map +1 -0
- package/dist/react19/deprecated-react-child.d.ts +8 -0
- package/dist/react19/deprecated-react-child.d.ts.map +1 -0
- package/dist/react19/deprecated-react-child.js +91 -0
- package/dist/react19/deprecated-react-child.js.map +1 -0
- package/dist/react19/deprecated-react-fragment.d.ts +8 -0
- package/dist/react19/deprecated-react-fragment.d.ts.map +1 -0
- package/dist/react19/deprecated-react-fragment.js +92 -0
- package/dist/react19/deprecated-react-fragment.js.map +1 -0
- package/dist/react19/deprecated-react-node-array.d.ts +8 -0
- package/dist/react19/deprecated-react-node-array.d.ts.map +1 -0
- package/dist/react19/deprecated-react-node-array.js +92 -0
- package/dist/react19/deprecated-react-node-array.js.map +1 -0
- package/dist/react19/deprecated-react-text.d.ts +8 -0
- package/dist/react19/deprecated-react-text.d.ts.map +1 -0
- package/dist/react19/deprecated-react-text.js +91 -0
- package/dist/react19/deprecated-react-text.js.map +1 -0
- package/dist/react19/deprecated-react-types.d.ts.map +1 -1
- package/dist/react19/deprecated-react-types.js +3 -104
- package/dist/react19/deprecated-react-types.js.map +1 -1
- package/dist/react19/refobject-defaults.d.ts +13 -0
- package/dist/react19/refobject-defaults.d.ts.map +1 -0
- package/dist/react19/refobject-defaults.js +152 -0
- package/dist/react19/refobject-defaults.js.map +1 -0
- package/dist/react19/remove-react-fc.js +3 -2
- package/dist/react19/remove-react-fc.js.map +1 -1
- package/dist/types/imports.d.ts +31 -0
- package/dist/types/imports.d.ts.map +1 -0
- package/dist/types/imports.js +385 -0
- package/dist/types/imports.js.map +1 -0
- package/dist/types/index.d.ts +11 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +33 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/module.d.ts +6 -0
- package/dist/types/module.d.ts.map +1 -0
- package/dist/types/module.js +5 -0
- package/dist/types/module.js.map +1 -0
- package/dist/types/re-export.d.ts +5 -0
- package/dist/types/re-export.d.ts.map +1 -0
- package/dist/types/re-export.js +12 -0
- package/dist/types/re-export.js.map +1 -0
- package/dist/types/reference.d.ts +20 -0
- package/dist/types/reference.d.ts.map +1 -0
- package/dist/types/reference.js +161 -0
- package/dist/types/reference.js.map +1 -0
- package/dist/types/rename.d.ts +10 -0
- package/dist/types/rename.d.ts.map +1 -0
- package/dist/types/rename.js +136 -0
- package/dist/types/rename.js.map +1 -0
- package/dist/types/replace.d.ts +8 -0
- package/dist/types/replace.d.ts.map +1 -0
- package/dist/types/replace.js +122 -0
- package/dist/types/replace.js.map +1 -0
- package/dist/util/java-recipe-proxy.d.ts +1 -0
- package/dist/util/java-recipe-proxy.d.ts.map +1 -1
- package/dist/util/java-recipe-proxy.js +3 -1
- package/dist/util/java-recipe-proxy.js.map +1 -1
- package/package.json +2 -2
- package/src/change-react-type.ts +32 -0
- package/src/index.ts +21 -0
- package/src/react18/add-children-to-props.ts +22 -7
- package/src/react19/deprecated-prop-types-types.ts +170 -0
- package/src/react19/deprecated-react-child.ts +67 -0
- package/src/react19/deprecated-react-fragment.ts +74 -0
- package/src/react19/deprecated-react-node-array.ts +74 -0
- package/src/react19/deprecated-react-text.ts +66 -0
- package/src/react19/deprecated-react-types.ts +4 -95
- package/src/react19/refobject-defaults.ts +120 -0
- package/src/react19/remove-react-fc.ts +3 -3
- package/src/types/imports.ts +396 -0
- package/src/types/index.ts +21 -0
- package/src/types/module.ts +10 -0
- package/src/types/re-export.ts +14 -0
- package/src/types/reference.ts +186 -0
- package/src/types/rename.ts +112 -0
- package/src/types/replace.ts +123 -0
- package/src/util/java-recipe-proxy.ts +3 -0
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import {Cursor, emptyMarkers, ExecutionContext, randomId} from "@openrewrite/rewrite";
|
|
2
|
+
import {emptySpace, isIdentifier, J} from "@openrewrite/rewrite/java";
|
|
3
|
+
import {compilationUnitOf, JavaScriptVisitor, JS, maybeBind, maybeRebind} from "@openrewrite/rewrite/javascript";
|
|
4
|
+
import {dropImport, editBlockImports, importsMember, importsOf, localNameOf} from "./imports";
|
|
5
|
+
import {Module} from "./module";
|
|
6
|
+
import {TypeReference, typeReferenceOf} from "./reference";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Renames `from`'s types, `SFC` → `FC` for `new Map([['SFC', 'FC']])`. Core's `maybeRebind` moves each
|
|
10
|
+
* import, used or not, with what reads it, a namespace's `React.SFC`, `import('react').SFC` and
|
|
11
|
+
* re-exports. This visitor renames what no import of the file binds: `React.SFC` through a default
|
|
12
|
+
* import or the UMD global, and the imports of `declare module` blocks.
|
|
13
|
+
*/
|
|
14
|
+
export function renamingTypes(from: Module, renames: ReadonlyMap<string, string>): JavaScriptVisitor<ExecutionContext> {
|
|
15
|
+
return new class extends JavaScriptVisitor<ExecutionContext> {
|
|
16
|
+
private joining = new Set<string>();
|
|
17
|
+
|
|
18
|
+
override async visitJsCompilationUnit(cu: JS.CompilationUnit, ctx: ExecutionContext): Promise<J | undefined> {
|
|
19
|
+
const imports = importsOf(cu, from);
|
|
20
|
+
const bound = new Set<string>();
|
|
21
|
+
this.joining = new Set();
|
|
22
|
+
for (const [member, renamed] of renames) {
|
|
23
|
+
const imported = localNameOf(imports, member) !== undefined;
|
|
24
|
+
if (imported && (bound.has(renamed) || localNameOf(imports, renamed) !== undefined)) {
|
|
25
|
+
this.joining.add(member);
|
|
26
|
+
continue;
|
|
27
|
+
}
|
|
28
|
+
if (imported) bound.add(renamed);
|
|
29
|
+
maybeRebind(this, {from: {module: from.module, member}, to: {module: from.module, member: renamed}});
|
|
30
|
+
}
|
|
31
|
+
const visited = await super.visitJsCompilationUnit(cu, ctx);
|
|
32
|
+
for (const member of this.joining) dropImport(this, from, member);
|
|
33
|
+
return visited;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
override async visitIdentifier(identifier: J.Identifier, ctx: ExecutionContext): Promise<J | undefined> {
|
|
37
|
+
return this.rename(await super.visitIdentifier(identifier, ctx) as J.Identifier);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
override async visitFieldAccess(fieldAccess: J.FieldAccess, ctx: ExecutionContext): Promise<J | undefined> {
|
|
41
|
+
return typeReferenceOf(this.cursor, fieldAccess, from) ? this.rename(fieldAccess) : super.visitFieldAccess(fieldAccess, ctx);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
override async visitParameterizedType(type: J.ParameterizedType, ctx: ExecutionContext): Promise<J | undefined> {
|
|
45
|
+
const pt = await super.visitParameterizedType(type, ctx) as J.ParameterizedType;
|
|
46
|
+
if (pt.class !== type.class) return pt;
|
|
47
|
+
const renamed = this.rename(pt.class as J.Identifier | J.FieldAccess, new Cursor(pt.class, this.cursor));
|
|
48
|
+
return renamed === pt.class ? pt : {...pt, class: renamed} as J.ParameterizedType;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
private rename(name: J.Identifier | J.FieldAccess, cursor = this.cursor): J {
|
|
52
|
+
const ref = typeReferenceOf(cursor, name, from);
|
|
53
|
+
const renamed = ref && renames.get(ref.member);
|
|
54
|
+
if (!renamed) return name;
|
|
55
|
+
const cu = compilationUnitOf(cursor);
|
|
56
|
+
const rebound = !ref.qualifier && !ref.importScope && cu !== undefined && isIdentifier(name) &&
|
|
57
|
+
!this.joining.has(ref.member) && localNameOf(importsOf(cu, from), ref.member) === name.simpleName;
|
|
58
|
+
return rebound ? name : renameType(this, cursor, ref, name, renamed);
|
|
59
|
+
}
|
|
60
|
+
}();
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* How to write `member` of the module `like` refers to, binding it if needed.
|
|
65
|
+
* `ReactElement` beside `React.ReactChild` → `React.ReactElement`; beside `ReactChild` → `ReactElement`, imported
|
|
66
|
+
*/
|
|
67
|
+
export function typeName(visitor: JavaScriptVisitor<any>, cursor: Cursor, like: TypeReference, member: string): string {
|
|
68
|
+
if (like.qualifier) return `${like.qualifier}.${member}`;
|
|
69
|
+
const scope = like.importScope;
|
|
70
|
+
if (scope) {
|
|
71
|
+
const local = localNameOf(importsOf(scope, like.from), member);
|
|
72
|
+
if (!local) editBlockImports(visitor, scope, like.from, {add: member});
|
|
73
|
+
return local ?? member;
|
|
74
|
+
}
|
|
75
|
+
const cu = compilationUnitOf(cursor);
|
|
76
|
+
const imported = cu !== undefined && importsMember(cu, like.from, member);
|
|
77
|
+
const typeOnlyDefault = cu && importsOf(cu, like.from).typeOnlyDefault;
|
|
78
|
+
if (like.typeOnly && !imported && typeOnlyDefault) return `${typeOnlyDefault}.${member}`;
|
|
79
|
+
return maybeBind(visitor, {module: like.from.module, member, typeOnly: like.typeOnly || imported, onlyIfReferenced: false}) ?? member;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Writes `member` where `name` referred to `ref`, binding it and dropping the old import once unused.
|
|
84
|
+
* `SFC<P>` → `FC<P>`; `React.SFC` → `React.FC`; `S` with `import {SFC as S}` → `FC`
|
|
85
|
+
*/
|
|
86
|
+
export function renameType(visitor: JavaScriptVisitor<any>, cursor: Cursor, ref: TypeReference,
|
|
87
|
+
name: J.Identifier | J.FieldAccess, member: string): J.Identifier | J.FieldAccess {
|
|
88
|
+
if (name.kind === J.Kind.FieldAccess) {
|
|
89
|
+
const fa = name as J.FieldAccess;
|
|
90
|
+
return {...fa, name: {...fa.name, element: {...fa.name.element, simpleName: member}}} as J.FieldAccess;
|
|
91
|
+
}
|
|
92
|
+
if (ref.qualifier) return {...name, simpleName: member} as J.Identifier;
|
|
93
|
+
const id = name as J.Identifier;
|
|
94
|
+
const local = typeName(visitor, cursor, ref, member);
|
|
95
|
+
unbind(visitor, ref);
|
|
96
|
+
const dot = local.indexOf('.');
|
|
97
|
+
if (dot < 0) return {...id, simpleName: local};
|
|
98
|
+
const part = (simpleName: string): J.Identifier =>
|
|
99
|
+
({...id, id: randomId(), prefix: emptySpace, markers: emptyMarkers, simpleName, type: undefined});
|
|
100
|
+
return {
|
|
101
|
+
kind: J.Kind.FieldAccess, id: randomId(), prefix: id.prefix, markers: id.markers,
|
|
102
|
+
target: part(local.slice(0, dot)),
|
|
103
|
+
name: {kind: J.Kind.LeftPadded, before: emptySpace, element: part(local.slice(dot + 1)), markers: emptyMarkers},
|
|
104
|
+
type: undefined
|
|
105
|
+
} as J.FieldAccess;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** Drops the import `ref` came through once nothing else uses it, in its `declare module` block or the file. */
|
|
109
|
+
export function unbind(visitor: JavaScriptVisitor<any>, ref: TypeReference): void {
|
|
110
|
+
if (ref.importScope) editBlockImports(visitor, ref.importScope, ref.from, {drop: ref.member});
|
|
111
|
+
else dropImport(visitor, ref.from, ref.member);
|
|
112
|
+
}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import {Cursor, randomId} from "@openrewrite/rewrite";
|
|
2
|
+
import {emptySpace, J, singleSpace} from "@openrewrite/rewrite/java";
|
|
3
|
+
import {JavaScriptParser, JavaScriptVisitor, JS} from "@openrewrite/rewrite/javascript";
|
|
4
|
+
import {TypeReference} from "./reference";
|
|
5
|
+
import {unbind} from "./rename";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Puts the type `code` where `tree` referred to `ref`, parenthesized where its parent binds tighter.
|
|
9
|
+
* `ReactChild[]`, `'ReactElement | number | string'` → `(ReactElement | number | string)[]`
|
|
10
|
+
*
|
|
11
|
+
* A union replacing a member of a union stays nested until {@link flattenUnion} joins it.
|
|
12
|
+
*/
|
|
13
|
+
export async function replaceType(visitor: JavaScriptVisitor<any>, cursor: Cursor, ref: TypeReference, tree: J, code: string): Promise<J> {
|
|
14
|
+
const parent = cursor.parentTree()?.value as J | undefined;
|
|
15
|
+
const optional = tree.markers.markers.some(m => m.kind === JS.Markers.Optional);
|
|
16
|
+
const heritage = parent?.kind === J.Kind.ClassDeclaration &&
|
|
17
|
+
((parent as J.ClassDeclaration).implements?.elements.some(e => e.element.id === tree.id) ?? false);
|
|
18
|
+
let replacement = await parseType(code, heritage);
|
|
19
|
+
if (looseTypes.has(replacement.kind) && (optional || needsParentheses(parent, tree))) {
|
|
20
|
+
replacement = await parseType(`(${code})`);
|
|
21
|
+
}
|
|
22
|
+
if (!ref.qualifier) unbind(visitor, ref);
|
|
23
|
+
const comments = innerComments(tree);
|
|
24
|
+
const prefix = comments.length ? {...tree.prefix, comments: [...tree.prefix.comments, ...comments]} : tree.prefix;
|
|
25
|
+
return {...replacement, prefix, markers: tree.markers} as J;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Joins a union that replaced a member of a union, as the parser reads `A | B | C` as one union.
|
|
30
|
+
* `ReactChild | null` → `ReactElement | number | string | null`, not `(ReactElement | number | string) | null`
|
|
31
|
+
*/
|
|
32
|
+
export function flattenUnion(union: JS.Union): JS.Union {
|
|
33
|
+
return union.types.some(t => t.element.kind === JS.Kind.Union) ? {...union, types: union.types.flatMap(unionMembers)} : union;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Adds the type `member` to `type`'s union, or makes one, as the parser reads it.
|
|
38
|
+
* `View`, `'null'` → `View | null`; `A | B` → `A | B | null`; `() => void` → `(() => void) | null`
|
|
39
|
+
*/
|
|
40
|
+
export async function unionWith(type: J, member: string): Promise<J> {
|
|
41
|
+
const added = await parseType(member);
|
|
42
|
+
if (type.kind === JS.Kind.Union) {
|
|
43
|
+
const union = type as JS.Union;
|
|
44
|
+
const last = union.types[union.types.length - 1];
|
|
45
|
+
return {
|
|
46
|
+
...union,
|
|
47
|
+
types: [...union.types.slice(0, -1), {...last, after: singleSpace}, {...last, element: {...added, prefix: singleSpace}}]
|
|
48
|
+
} as JS.Union;
|
|
49
|
+
}
|
|
50
|
+
const parenthesized = type.kind === JS.Kind.FunctionType || type.kind === JS.Kind.ConditionalType;
|
|
51
|
+
const union = await parseType(`${parenthesized ? '(T)' : 'T'} | ${member}`) as JS.Union;
|
|
52
|
+
const [first, ...rest] = union.types;
|
|
53
|
+
const inner = {...type, prefix: emptySpace} as J;
|
|
54
|
+
const element = parenthesized ? withParenthesized(first.element as J.ParenthesizedTypeTree, inner) : inner;
|
|
55
|
+
return {...union, prefix: type.prefix, types: [{...first, element}, ...rest]} as JS.Union;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function withParenthesized(tree: J.ParenthesizedTypeTree, inner: J): J.ParenthesizedTypeTree {
|
|
59
|
+
const parentheses = tree.parenthesizedType;
|
|
60
|
+
return {...tree, parenthesizedType: {...parentheses, tree: {...parentheses.tree, element: inner}}} as J.ParenthesizedTypeTree;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const looseTypes = new Set<string>([JS.Kind.Union, JS.Kind.Intersection, JS.Kind.FunctionType, JS.Kind.ConditionalType,
|
|
64
|
+
JS.Kind.TypeOperator, JS.Kind.InferType]);
|
|
65
|
+
|
|
66
|
+
function needsParentheses(parent: J | undefined, child: J): boolean {
|
|
67
|
+
switch (parent?.kind) {
|
|
68
|
+
case J.Kind.ArrayType:
|
|
69
|
+
case JS.Kind.Intersection:
|
|
70
|
+
case JS.Kind.TypeOperator:
|
|
71
|
+
return true;
|
|
72
|
+
case JS.Kind.IndexedAccessType:
|
|
73
|
+
return (parent as JS.IndexedAccessType).objectType.id === child.id;
|
|
74
|
+
default:
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function innerComments(tree: J): J.Space["comments"][number][] {
|
|
80
|
+
const found: J.Space["comments"][number][] = [];
|
|
81
|
+
const collect = (node: unknown, top: boolean) => {
|
|
82
|
+
if (!node || typeof node !== 'object') return;
|
|
83
|
+
if (Array.isArray(node)) return node.forEach(n => collect(n, false));
|
|
84
|
+
for (const [key, value] of Object.entries(node)) {
|
|
85
|
+
if (key === 'type' || key === 'markers' || top && key === 'prefix') continue;
|
|
86
|
+
if ((value as {kind?: string} | undefined)?.kind === J.Kind.Space) found.push(...(value as J.Space).comments);
|
|
87
|
+
else collect(value, false);
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
collect(tree, true);
|
|
91
|
+
return found;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const parser = new JavaScriptParser();
|
|
95
|
+
|
|
96
|
+
async function parseType(code: string, heritage = false): Promise<J> {
|
|
97
|
+
const text = heritage ? `interface T extends ${code} {}` : `type T = ${code};`;
|
|
98
|
+
const statement = (await parser.parseOnly({text, sourcePath: 'type.ts'}) as JS.CompilationUnit).statements[0].element;
|
|
99
|
+
const type = heritage ? (statement as J.ClassDeclaration).implements!.elements[0].element :
|
|
100
|
+
(statement as JS.TypeDeclaration).initializer.element;
|
|
101
|
+
return {...withFreshIds(type), prefix: emptySpace};
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function unionMembers(member: J.RightPadded<J>): J.RightPadded<J>[] {
|
|
105
|
+
if (member.element.kind !== JS.Kind.Union) return [member];
|
|
106
|
+
const types = (member.element as JS.Union).types;
|
|
107
|
+
return types.map((t, i) => ({
|
|
108
|
+
...t,
|
|
109
|
+
element: i === 0 ? {...t.element, prefix: member.element.prefix} : t.element,
|
|
110
|
+
after: i === types.length - 1 ? member.after : t.after
|
|
111
|
+
}));
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function withFreshIds<T>(tree: T): T {
|
|
115
|
+
if (Array.isArray(tree)) return tree.map(t => withFreshIds(t)) as T;
|
|
116
|
+
if (tree === null || typeof tree !== 'object') return tree;
|
|
117
|
+
const copy: any = {};
|
|
118
|
+
for (const [key, value] of Object.entries(tree)) {
|
|
119
|
+
copy[key] = key === 'type' ? value : withFreshIds(value);
|
|
120
|
+
}
|
|
121
|
+
if (typeof copy.id === 'string' && copy.kind) copy.id = randomId();
|
|
122
|
+
return copy;
|
|
123
|
+
}
|
|
@@ -13,6 +13,9 @@ const NODE_INPUTS = /(^|\/)(\.npmrc|\.yarnrc(?:\.yml)?|bunfig\.toml|\.?pnpmfile\
|
|
|
13
13
|
|
|
14
14
|
const manifestTrees = () => or(...NODE_MANIFEST_TREES.map(treesOfType));
|
|
15
15
|
|
|
16
|
+
/** The host's gate for the manifests a dependency step reads and the JavaScript sources that decide where it adds. */
|
|
17
|
+
export const manifestAndJavaScriptTrees = () => or(...[...NODE_MANIFEST_TREES, "org.openrewrite.javascript.tree.JS"].map(treesOfType));
|
|
18
|
+
|
|
16
19
|
export function isNodeDependencyInput(path: string, includeInputs = false): boolean {
|
|
17
20
|
return NODE_MANIFESTS.test(path) || includeInputs && NODE_INPUTS.test(path);
|
|
18
21
|
}
|