@openrewrite/recipes-react 0.28.0 → 0.31.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 +16 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +67 -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/react18/deprecated-react-type.d.ts +8 -0
- package/dist/react18/deprecated-react-type.d.ts.map +1 -0
- package/dist/react18/deprecated-react-type.js +29 -0
- package/dist/react18/deprecated-react-type.js.map +1 -0
- package/dist/react18/deprecated-sfc-element.d.ts +8 -0
- package/dist/react18/deprecated-sfc-element.d.ts.map +1 -0
- package/dist/react18/deprecated-sfc-element.js +29 -0
- package/dist/react18/deprecated-sfc-element.js.map +1 -0
- package/dist/react18/deprecated-sfc.d.ts +8 -0
- package/dist/react18/deprecated-sfc.d.ts.map +1 -0
- package/dist/react18/deprecated-sfc.js +29 -0
- package/dist/react18/deprecated-sfc.js.map +1 -0
- package/dist/react18/deprecated-stateless-component.d.ts +8 -0
- package/dist/react18/deprecated-stateless-component.d.ts.map +1 -0
- package/dist/react18/deprecated-stateless-component.js +29 -0
- package/dist/react18/deprecated-stateless-component.js.map +1 -0
- package/dist/react19/deprecated-legacy-ref.d.ts +8 -0
- package/dist/react19/deprecated-legacy-ref.d.ts.map +1 -0
- package/dist/react19/deprecated-legacy-ref.js +29 -0
- package/dist/react19/deprecated-legacy-ref.js.map +1 -0
- 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/deprecated-void-function-component.d.ts +8 -0
- package/dist/react19/deprecated-void-function-component.d.ts.map +1 -0
- package/dist/react19/deprecated-void-function-component.js +32 -0
- package/dist/react19/deprecated-void-function-component.js.map +1 -0
- 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/react19/replace-act-import.d.ts +8 -0
- package/dist/react19/replace-act-import.d.ts.map +1 -0
- package/dist/react19/replace-act-import.js +29 -0
- package/dist/react19/replace-act-import.js.map +1 -0
- package/dist/react19/replace-react-shallow-renderer.d.ts +8 -0
- package/dist/react19/replace-react-shallow-renderer.d.ts.map +1 -0
- package/dist/react19/replace-react-shallow-renderer.js +29 -0
- package/dist/react19/replace-react-shallow-renderer.js.map +1 -0
- package/dist/react19/replace-use-form-state.d.ts +8 -0
- package/dist/react19/replace-use-form-state.d.ts.map +1 -0
- package/dist/react19/replace-use-form-state.js +29 -0
- package/dist/react19/replace-use-form-state.js.map +1 -0
- 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 +48 -0
- package/src/migration/cra-to-vite/add-vite-config.ts +0 -1
- package/src/react18/add-children-to-props.ts +22 -7
- package/src/react18/deprecated-react-type.ts +19 -0
- package/src/react18/deprecated-sfc-element.ts +19 -0
- package/src/react18/deprecated-sfc.ts +19 -0
- package/src/react18/deprecated-stateless-component.ts +19 -0
- package/src/react19/deprecated-legacy-ref.ts +19 -0
- 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/deprecated-void-function-component.ts +22 -0
- package/src/react19/refobject-defaults.ts +120 -0
- package/src/react19/remove-react-fc.ts +3 -3
- package/src/react19/replace-act-import.ts +19 -0
- package/src/react19/replace-react-shallow-renderer.ts +19 -0
- package/src/react19/replace-use-form-state.ts +19 -0
- 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,186 @@
|
|
|
1
|
+
import {Cursor} from "@openrewrite/rewrite";
|
|
2
|
+
import {isIdentifier, J} from "@openrewrite/rewrite/java";
|
|
3
|
+
import {JS, JSX, resolve, scopeOf} from "@openrewrite/rewrite/javascript";
|
|
4
|
+
import {boundIn, declaresTopLevelValue, exportName, importScopesOf, importsOf, ImportScope} from "./imports";
|
|
5
|
+
import {Module} from "./module";
|
|
6
|
+
|
|
7
|
+
/** A name referring to a module's exported type, imported by name or through a namespace, default or UMD global. */
|
|
8
|
+
export interface TypeReference {
|
|
9
|
+
readonly from: Module;
|
|
10
|
+
/** `ReactChild` for `import {ReactChild as RC} from 'react'`. */
|
|
11
|
+
readonly member: string;
|
|
12
|
+
/** `React` in `React.ReactChild`. */
|
|
13
|
+
readonly qualifier?: string;
|
|
14
|
+
/** Whether its named import is type-only. */
|
|
15
|
+
readonly typeOnly: boolean;
|
|
16
|
+
/** The `declare module` block whose own import binds it, when that isn't the file's. */
|
|
17
|
+
readonly importScope?: J.Block;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Matches by the import that binds a name, so a local type spelled the same is left alone.
|
|
22
|
+
* `VFC` with `import {VFC} from 'react'` → `{member: 'VFC', typeOnly: false}`; with `type VFC = …` → `undefined`
|
|
23
|
+
*/
|
|
24
|
+
export function typeReferenceOf(cursor: Cursor, name: J, from: Module): TypeReference | undefined {
|
|
25
|
+
const parent = cursor.parentTree()?.value as J | undefined;
|
|
26
|
+
if (parent?.kind === JS.Kind.ExportAssignment) return undefined;
|
|
27
|
+
if (parent?.kind === JS.Kind.ExportSpecifier || parent?.kind === JS.Kind.Alias &&
|
|
28
|
+
cursor.parentTree()?.parentTree()?.value.kind === JS.Kind.ExportSpecifier) return undefined;
|
|
29
|
+
if (parent?.kind === JS.Kind.ImportType) {
|
|
30
|
+
return (parent as JS.ImportType).qualifier?.element === name ? importTypeReferenceOf(parent as JS.ImportType, from) : undefined;
|
|
31
|
+
}
|
|
32
|
+
if (isIdentifier(name)) {
|
|
33
|
+
const imported = namedImportOf(cursor, name, from, true);
|
|
34
|
+
if (!imported) return undefined;
|
|
35
|
+
const genericClass = parent?.kind === J.Kind.ParameterizedType && (parent as J.ParameterizedType).class === name;
|
|
36
|
+
if (imported.typeOnly && cursor.value === name && !genericClass && !inTypePosition(cursor) && !inInterfaceHeritage(cursor) &&
|
|
37
|
+
declaresTopLevelValue(imported.scope, name.simpleName)) {
|
|
38
|
+
return undefined;
|
|
39
|
+
}
|
|
40
|
+
return reference(from, imported.member, imported.typeOnly, imported.scope);
|
|
41
|
+
}
|
|
42
|
+
if (name.kind === J.Kind.FieldAccess) {
|
|
43
|
+
const fa = name as J.FieldAccess;
|
|
44
|
+
if (!isIdentifier(fa.target)) return undefined;
|
|
45
|
+
const qualifier = fa.target.simpleName;
|
|
46
|
+
const member = fa.name.element.simpleName;
|
|
47
|
+
const scopes = importScopesOf(cursor);
|
|
48
|
+
const scope = scopes.find(s => importsOf(s, from).qualifiers.has(qualifier));
|
|
49
|
+
if (scope) {
|
|
50
|
+
return boundIn(cursor, fa.target, scope) ? {from, member, qualifier, typeOnly: false} : undefined;
|
|
51
|
+
}
|
|
52
|
+
if (qualifier === from.global && !scopes.some(s => importsOf(s, from).bindsGlobal) && resolve(cursor, fa.target) === undefined) {
|
|
53
|
+
return {from, member, qualifier, typeOnly: false};
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return undefined;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* The member an `import('…')` type names, which no import statement binds.
|
|
61
|
+
* `import('react').ReactChild` → `{member: 'ReactChild', qualifier: "import('react')"}`; `typeof import('react').X` → `undefined`
|
|
62
|
+
*/
|
|
63
|
+
export function importTypeReferenceOf(importType: JS.ImportType, from: Module): TypeReference | undefined {
|
|
64
|
+
const argument = importType.argumentAndAttributes.elements[0]?.element as JS.LiteralType | undefined;
|
|
65
|
+
const qualifier = importType.qualifier?.element;
|
|
66
|
+
if (importType.hasTypeof.element || argument?.kind !== JS.Kind.LiteralType || !isIdentifier(qualifier)) return undefined;
|
|
67
|
+
return (argument.literal as J.Literal).value === from.module ?
|
|
68
|
+
{from, member: qualifier.simpleName, qualifier: `import('${from.module}')`, typeOnly: false} : undefined;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* The type an `export {…}` specifier re-exports, and whether it comes straight from the module.
|
|
73
|
+
* `export type {SFC} from 'react'` → `fromModule: true`; `import {SFC} from 'react'; export {SFC}` → `fromModule: false`
|
|
74
|
+
*/
|
|
75
|
+
export function reExportedTypeOf(cursor: Cursor, specifier: JS.ExportSpecifier, from: Module):
|
|
76
|
+
{ ref: TypeReference, fromModule: boolean } | undefined {
|
|
77
|
+
const declaration = cursor.parentTree()?.parentTree()?.value as JS.ExportDeclaration | undefined;
|
|
78
|
+
if (declaration?.kind !== JS.Kind.ExportDeclaration) return undefined;
|
|
79
|
+
const s = specifier.specifier;
|
|
80
|
+
const local = s.kind === JS.Kind.Alias ? (s as JS.Alias).propertyName.element as J : s;
|
|
81
|
+
const source = declaration.moduleSpecifier?.element;
|
|
82
|
+
if (source) {
|
|
83
|
+
const member = exportName(local);
|
|
84
|
+
return member !== undefined && source.kind === J.Kind.Literal && (source as J.Literal).value === from.module ?
|
|
85
|
+
{ref: {from, member, typeOnly: declaration.typeOnly.element || specifier.typeOnly.element}, fromModule: true} : undefined;
|
|
86
|
+
}
|
|
87
|
+
if (!isIdentifier(local)) return undefined;
|
|
88
|
+
const imported = namedImportOf(cursor, local, from, false);
|
|
89
|
+
return imported ? {ref: reference(from, imported.member, imported.typeOnly, imported.scope), fromModule: false} : undefined;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* A type expression can replace a name only where a type is expected.
|
|
94
|
+
* `x: ReactChild`, `T extends ReactChild` → `true`; `ReactChild.length`, `typeof ReactChild` → `false`
|
|
95
|
+
*/
|
|
96
|
+
export function inTypePosition(cursor: Cursor): boolean {
|
|
97
|
+
const name = cursor.value as J;
|
|
98
|
+
const parentCursor = cursor.parentTree();
|
|
99
|
+
const parent = parentCursor?.value as J | undefined;
|
|
100
|
+
const holds = (container?: J.Container<J>) => container?.elements.some(e => e.element === name) ?? false;
|
|
101
|
+
const grandparentKind = () => (parentCursor?.parentTree()?.value as J | undefined)?.kind;
|
|
102
|
+
switch (parent?.kind) {
|
|
103
|
+
case JS.Kind.TypeInfo:
|
|
104
|
+
case JS.Kind.Union:
|
|
105
|
+
case JS.Kind.Intersection:
|
|
106
|
+
case JS.Kind.Tuple:
|
|
107
|
+
case JS.Kind.TypeOperator:
|
|
108
|
+
case JS.Kind.IndexedAccessType:
|
|
109
|
+
case JS.Kind.MappedType:
|
|
110
|
+
case J.Kind.ArrayType:
|
|
111
|
+
return true;
|
|
112
|
+
case JS.Kind.MappedTypeParameter:
|
|
113
|
+
return (parent as JS.MappedType.Parameter).iterateType.element === name;
|
|
114
|
+
case JS.Kind.TemplateExpressionSpan: {
|
|
115
|
+
const template = parentCursor!.parentTree();
|
|
116
|
+
return (parent as JS.TemplateExpression.Span).expression === name && template !== undefined && inTypePosition(template);
|
|
117
|
+
}
|
|
118
|
+
case JS.Kind.TypeDeclaration:
|
|
119
|
+
return (parent as JS.TypeDeclaration).initializer.element === name;
|
|
120
|
+
case JS.Kind.FunctionType:
|
|
121
|
+
return (parent as JS.FunctionType).returnType.element === name;
|
|
122
|
+
case JS.Kind.ConditionalType:
|
|
123
|
+
return (parent as JS.ConditionalType).checkType === name;
|
|
124
|
+
case J.Kind.Ternary:
|
|
125
|
+
return grandparentKind() === JS.Kind.ConditionalType;
|
|
126
|
+
case JS.Kind.TypePredicate:
|
|
127
|
+
return (parent as JS.TypePredicate).expression?.element === name;
|
|
128
|
+
case JS.Kind.IndexSignatureDeclaration:
|
|
129
|
+
return (parent as JS.IndexSignatureDeclaration).typeExpression.element === name;
|
|
130
|
+
case J.Kind.TypeParameter:
|
|
131
|
+
return holds((parent as J.TypeParameter).bounds);
|
|
132
|
+
case JS.Kind.As:
|
|
133
|
+
return (parent as JS.As).right === name;
|
|
134
|
+
case JS.Kind.SatisfiesExpression:
|
|
135
|
+
return (parent as JS.SatisfiesExpression).satisfiesType.element === name;
|
|
136
|
+
case J.Kind.ParameterizedType:
|
|
137
|
+
return holds((parent as J.ParameterizedType).typeParameters);
|
|
138
|
+
case J.Kind.MethodInvocation:
|
|
139
|
+
return holds((parent as J.MethodInvocation).typeParameters);
|
|
140
|
+
case JS.Kind.ExpressionWithTypeArguments:
|
|
141
|
+
return holds((parent as JS.ExpressionWithTypeArguments).typeArguments);
|
|
142
|
+
case JS.Kind.JsxTag:
|
|
143
|
+
return holds((parent as JSX.Tag).typeArguments);
|
|
144
|
+
case JS.Kind.TaggedTemplateExpression:
|
|
145
|
+
return holds((parent as JS.TaggedTemplateExpression).typeArguments);
|
|
146
|
+
case J.Kind.ControlParentheses:
|
|
147
|
+
return grandparentKind() === J.Kind.TypeCast;
|
|
148
|
+
case J.Kind.ClassDeclaration:
|
|
149
|
+
return holds((parent as J.ClassDeclaration).implements);
|
|
150
|
+
case J.Kind.Parentheses:
|
|
151
|
+
return grandparentKind() === J.Kind.ParenthesizedTypeTree;
|
|
152
|
+
default:
|
|
153
|
+
return false;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* A local type of the same name would capture the global one.
|
|
159
|
+
* `ReadonlyArray` → `ReadonlyArray`, or `globalThis.ReadonlyArray` beside `type ReadonlyArray = …`
|
|
160
|
+
*/
|
|
161
|
+
export function globalTypeName(cursor: Cursor, name: string): string {
|
|
162
|
+
return scopeOf(cursor).declares(name, 'type') ? `globalThis.${name}` : name;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function reference(from: Module, member: string, typeOnly: boolean, scope: ImportScope): TypeReference {
|
|
166
|
+
return {from, member, typeOnly, ...(scope.kind === J.Kind.Block ? {importScope: scope} : {})};
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
function namedImportOf(cursor: Cursor, local: J.Identifier, from: Module, resolved: boolean):
|
|
170
|
+
{ member: string, typeOnly: boolean, scope: ImportScope } | undefined {
|
|
171
|
+
for (const scope of importScopesOf(cursor)) {
|
|
172
|
+
const imported = importsOf(scope, from).named.get(local.simpleName);
|
|
173
|
+
if (imported && (!resolved || boundIn(cursor, local, scope))) return {...imported, scope};
|
|
174
|
+
}
|
|
175
|
+
return undefined;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function inInterfaceHeritage(cursor: Cursor): boolean {
|
|
179
|
+
let parent = cursor.parentTree();
|
|
180
|
+
while (parent && (parent.value.kind === JS.Kind.ExpressionWithTypeArguments || parent.value.kind === JS.Kind.TypeTreeExpression ||
|
|
181
|
+
parent.value.kind === J.Kind.ParameterizedType)) {
|
|
182
|
+
parent = parent.parentTree();
|
|
183
|
+
}
|
|
184
|
+
const declaration = parent?.value as J | undefined;
|
|
185
|
+
return declaration?.kind === J.Kind.ClassDeclaration && (declaration as J.ClassDeclaration).classKind.type as string === 'Interface';
|
|
186
|
+
}
|
|
@@ -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
|
}
|