@openrewrite/recipes-react 0.13.0 → 0.15.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 +12 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +70 -17
- package/dist/index.js.map +1 -1
- package/dist/migration/cra-to-vite/add-vite-config.d.ts +9 -0
- package/dist/migration/cra-to-vite/add-vite-config.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/add-vite-config.js +49 -0
- package/dist/migration/cra-to-vite/add-vite-config.js.map +1 -0
- package/dist/migration/cra-to-vite/cra-blockers-table.d.ts +10 -0
- package/dist/migration/cra-to-vite/cra-blockers-table.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/cra-blockers-table.js +31 -0
- package/dist/migration/cra-to-vite/cra-blockers-table.js.map +1 -0
- package/dist/migration/cra-to-vite/cra-to-vite.d.ts +14 -0
- package/dist/migration/cra-to-vite/cra-to-vite.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/cra-to-vite.js +73 -0
- package/dist/migration/cra-to-vite/cra-to-vite.js.map +1 -0
- package/dist/migration/cra-to-vite/find-cra-config-blockers.d.ts +14 -0
- package/dist/migration/cra-to-vite/find-cra-config-blockers.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/find-cra-config-blockers.js +185 -0
- package/dist/migration/cra-to-vite/find-cra-config-blockers.js.map +1 -0
- package/dist/migration/cra-to-vite/find-cra-env-file-blockers.d.ts +10 -0
- package/dist/migration/cra-to-vite/find-cra-env-file-blockers.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/find-cra-env-file-blockers.js +67 -0
- package/dist/migration/cra-to-vite/find-cra-env-file-blockers.js.map +1 -0
- package/dist/migration/cra-to-vite/find-cra-source-blockers.d.ts +10 -0
- package/dist/migration/cra-to-vite/find-cra-source-blockers.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/find-cra-source-blockers.js +168 -0
- package/dist/migration/cra-to-vite/find-cra-source-blockers.js.map +1 -0
- package/dist/migration/cra-to-vite/find-cra-to-vite-blockers.d.ts +8 -0
- package/dist/migration/cra-to-vite/find-cra-to-vite-blockers.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/find-cra-to-vite-blockers.js +31 -0
- package/dist/migration/cra-to-vite/find-cra-to-vite-blockers.js.map +1 -0
- package/dist/migration/cra-to-vite/index.d.ts +16 -0
- package/dist/migration/cra-to-vite/index.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/index.js +35 -0
- package/dist/migration/cra-to-vite/index.js.map +1 -0
- package/dist/migration/cra-to-vite/move-public-index-html.d.ts +11 -0
- package/dist/migration/cra-to-vite/move-public-index-html.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/move-public-index-html.js +124 -0
- package/dist/migration/cra-to-vite/move-public-index-html.js.map +1 -0
- package/dist/migration/cra-to-vite/rename-process-env-react-app.d.ts +9 -0
- package/dist/migration/cra-to-vite/rename-process-env-react-app.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/rename-process-env-react-app.js +92 -0
- package/dist/migration/cra-to-vite/rename-process-env-react-app.js.map +1 -0
- package/dist/migration/cra-to-vite/rename-react-app-env-vars-in-env-files.d.ts +9 -0
- package/dist/migration/cra-to-vite/rename-react-app-env-vars-in-env-files.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/rename-react-app-env-vars-in-env-files.js +65 -0
- package/dist/migration/cra-to-vite/rename-react-app-env-vars-in-env-files.js.map +1 -0
- package/dist/migration/cra-to-vite/replace-react-scripts-dependency.d.ts +14 -0
- package/dist/migration/cra-to-vite/replace-react-scripts-dependency.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/replace-react-scripts-dependency.js +98 -0
- package/dist/migration/cra-to-vite/replace-react-scripts-dependency.js.map +1 -0
- package/dist/migration/cra-to-vite/scan-cra-projects.d.ts +21 -0
- package/dist/migration/cra-to-vite/scan-cra-projects.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/scan-cra-projects.js +87 -0
- package/dist/migration/cra-to-vite/scan-cra-projects.js.map +1 -0
- package/dist/migration/cra-to-vite/shared.d.ts +37 -0
- package/dist/migration/cra-to-vite/shared.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/shared.js +218 -0
- package/dist/migration/cra-to-vite/shared.js.map +1 -0
- package/dist/migration/cra-to-vite/update-react-app-env-types.d.ts +9 -0
- package/dist/migration/cra-to-vite/update-react-app-env-types.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/update-react-app-env-types.js +47 -0
- package/dist/migration/cra-to-vite/update-react-app-env-types.js.map +1 -0
- package/dist/migration/cra-to-vite/update-react-scripts-scripts.d.ts +9 -0
- package/dist/migration/cra-to-vite/update-react-scripts-scripts.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/update-react-scripts-scripts.js +67 -0
- package/dist/migration/cra-to-vite/update-react-scripts-scripts.js.map +1 -0
- package/dist/migration/cra-to-vite/update-tsconfig-for-vite.d.ts +10 -0
- package/dist/migration/cra-to-vite/update-tsconfig-for-vite.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/update-tsconfig-for-vite.js +53 -0
- package/dist/migration/cra-to-vite/update-tsconfig-for-vite.js.map +1 -0
- package/dist/react17/migrate-deprecated-lifecycles.d.ts +8 -0
- package/dist/react17/migrate-deprecated-lifecycles.d.ts.map +1 -0
- package/dist/react17/migrate-deprecated-lifecycles.js +258 -0
- package/dist/react17/migrate-deprecated-lifecycles.js.map +1 -0
- package/dist/react17/use-new-jsx-transform.d.ts +8 -0
- package/dist/react17/use-new-jsx-transform.d.ts.map +1 -0
- package/dist/react17/use-new-jsx-transform.js +169 -0
- package/dist/react17/use-new-jsx-transform.js.map +1 -0
- package/dist/react18/add-children-to-props.d.ts +8 -0
- package/dist/react18/add-children-to-props.d.ts.map +1 -0
- package/dist/react18/add-children-to-props.js +277 -0
- package/dist/react18/add-children-to-props.js.map +1 -0
- package/dist/react18/replace-reactdom-render.d.ts.map +1 -1
- package/dist/react18/replace-reactdom-render.js +109 -12
- package/dist/react18/replace-reactdom-render.js.map +1 -1
- package/dist/refactoring/class-to-functional.d.ts.map +1 -1
- package/dist/refactoring/class-to-functional.js +81 -7
- package/dist/refactoring/class-to-functional.js.map +1 -1
- package/dist/refactoring/fix-async-effect.d.ts +8 -0
- package/dist/refactoring/fix-async-effect.d.ts.map +1 -0
- package/dist/refactoring/fix-async-effect.js +71 -0
- package/dist/refactoring/fix-async-effect.js.map +1 -0
- package/dist/refactoring/prop-types-to-typescript.d.ts +8 -0
- package/dist/refactoring/prop-types-to-typescript.d.ts.map +1 -0
- package/dist/refactoring/prop-types-to-typescript.js +342 -0
- package/dist/refactoring/prop-types-to-typescript.js.map +1 -0
- package/dist/search/find-deprecated-lifecycles.d.ts +12 -0
- package/dist/search/find-deprecated-lifecycles.d.ts.map +1 -0
- package/dist/search/find-deprecated-lifecycles.js +86 -0
- package/dist/search/find-deprecated-lifecycles.js.map +1 -0
- package/dist/search/find-effects-without-cleanup.d.ts +9 -0
- package/dist/search/find-effects-without-cleanup.d.ts.map +1 -0
- package/dist/search/find-effects-without-cleanup.js +133 -0
- package/dist/search/find-effects-without-cleanup.js.map +1 -0
- package/dist/search/find-enzyme-usage.d.ts +9 -0
- package/dist/search/find-enzyme-usage.d.ts.map +1 -0
- package/dist/search/find-enzyme-usage.js +155 -0
- package/dist/search/find-enzyme-usage.js.map +1 -0
- package/dist/search/find-function-component-default-props.d.ts +11 -0
- package/dist/search/find-function-component-default-props.d.ts.map +1 -0
- package/dist/search/find-function-component-default-props.js +96 -0
- package/dist/search/find-function-component-default-props.js.map +1 -0
- package/dist/search/find-legacy-react-apis.d.ts +8 -0
- package/dist/search/find-legacy-react-apis.d.ts.map +1 -0
- package/dist/search/find-legacy-react-apis.js +41 -0
- package/dist/search/find-legacy-react-apis.js.map +1 -0
- package/dist/search/find-missing-use-client-directive.d.ts +9 -0
- package/dist/search/find-missing-use-client-directive.d.ts.map +1 -0
- package/dist/search/find-missing-use-client-directive.js +127 -0
- package/dist/search/find-missing-use-client-directive.js.map +1 -0
- package/dist/search/find-reactdom-root-apis.d.ts +14 -0
- package/dist/search/find-reactdom-root-apis.d.ts.map +1 -0
- package/dist/search/find-reactdom-root-apis.js +122 -0
- package/dist/search/find-reactdom-root-apis.js.map +1 -0
- package/dist/testing/enzyme-to-react-testing-library.d.ts +36 -0
- package/dist/testing/enzyme-to-react-testing-library.d.ts.map +1 -0
- package/dist/testing/enzyme-to-react-testing-library.js +547 -0
- package/dist/testing/enzyme-to-react-testing-library.js.map +1 -0
- package/dist/testing/enzyme-usage.d.ts +22 -0
- package/dist/testing/enzyme-usage.d.ts.map +1 -0
- package/dist/testing/enzyme-usage.js +107 -0
- package/dist/testing/enzyme-usage.js.map +1 -0
- package/dist/testing/enzyme-wrapper-scope.d.ts +42 -0
- package/dist/testing/enzyme-wrapper-scope.d.ts.map +1 -0
- package/dist/testing/enzyme-wrapper-scope.js +422 -0
- package/dist/testing/enzyme-wrapper-scope.js.map +1 -0
- package/dist/util/react-class.d.ts +6 -0
- package/dist/util/react-class.d.ts.map +1 -0
- package/dist/util/react-class.js +41 -0
- package/dist/util/react-class.js.map +1 -0
- package/dist/util/tree-search.d.ts +6 -0
- package/dist/util/tree-search.d.ts.map +1 -0
- package/dist/util/tree-search.js +51 -0
- package/dist/util/tree-search.js.map +1 -0
- package/package.json +7 -4
- package/src/index.ts +53 -12
- package/src/migration/cra-to-vite/add-vite-config.ts +47 -0
- package/src/migration/cra-to-vite/cra-blockers-table.ts +27 -0
- package/src/migration/cra-to-vite/cra-to-vite.ts +76 -0
- package/src/migration/cra-to-vite/find-cra-config-blockers.ts +179 -0
- package/src/migration/cra-to-vite/find-cra-env-file-blockers.ts +60 -0
- package/src/migration/cra-to-vite/find-cra-source-blockers.ts +138 -0
- package/src/migration/cra-to-vite/find-cra-to-vite-blockers.ts +21 -0
- package/src/migration/cra-to-vite/index.ts +26 -0
- package/src/migration/cra-to-vite/move-public-index-html.ts +132 -0
- package/src/migration/cra-to-vite/rename-process-env-react-app.ts +93 -0
- package/src/migration/cra-to-vite/rename-react-app-env-vars-in-env-files.ts +61 -0
- package/src/migration/cra-to-vite/replace-react-scripts-dependency.ts +96 -0
- package/src/migration/cra-to-vite/scan-cra-projects.ts +86 -0
- package/src/migration/cra-to-vite/shared.ts +244 -0
- package/src/migration/cra-to-vite/update-react-app-env-types.ts +40 -0
- package/src/migration/cra-to-vite/update-react-scripts-scripts.ts +69 -0
- package/src/migration/cra-to-vite/update-tsconfig-for-vite.ts +47 -0
- package/src/react17/migrate-deprecated-lifecycles.ts +265 -0
- package/src/react17/use-new-jsx-transform.ts +150 -0
- package/src/react18/add-children-to-props.ts +235 -0
- package/src/react18/replace-reactdom-render.ts +109 -20
- package/src/refactoring/class-to-functional.ts +101 -13
- package/src/refactoring/fix-async-effect.ts +88 -0
- package/src/refactoring/prop-types-to-typescript.ts +341 -0
- package/src/search/find-deprecated-lifecycles.ts +64 -0
- package/src/search/find-effects-without-cleanup.ts +109 -0
- package/src/search/find-enzyme-usage.ts +117 -0
- package/src/search/find-function-component-default-props.ts +68 -0
- package/src/search/find-legacy-react-apis.ts +41 -0
- package/src/search/find-missing-use-client-directive.ts +99 -0
- package/src/search/find-reactdom-root-apis.ts +99 -0
- package/src/testing/enzyme-to-react-testing-library.ts +553 -0
- package/src/testing/enzyme-usage.ts +94 -0
- package/src/testing/enzyme-wrapper-scope.ts +449 -0
- package/src/util/react-class.ts +34 -0
- package/src/util/tree-search.ts +32 -0
- package/dist/react16/upgrade-to-react-16.d.ts +0 -12
- package/dist/react16/upgrade-to-react-16.d.ts.map +0 -1
- package/dist/react16/upgrade-to-react-16.js +0 -66
- package/dist/react16/upgrade-to-react-16.js.map +0 -1
- package/dist/react17/upgrade-to-react-17.d.ts +0 -12
- package/dist/react17/upgrade-to-react-17.d.ts.map +0 -1
- package/dist/react17/upgrade-to-react-17.js +0 -62
- package/dist/react17/upgrade-to-react-17.js.map +0 -1
- package/dist/react18/upgrade-to-react-18.d.ts +0 -12
- package/dist/react18/upgrade-to-react-18.d.ts.map +0 -1
- package/dist/react18/upgrade-to-react-18.js +0 -64
- package/dist/react18/upgrade-to-react-18.js.map +0 -1
- package/dist/react19/upgrade-to-react-19.d.ts +0 -8
- package/dist/react19/upgrade-to-react-19.d.ts.map +0 -1
- package/dist/react19/upgrade-to-react-19.js +0 -68
- package/dist/react19/upgrade-to-react-19.js.map +0 -1
- package/src/react16/upgrade-to-react-16.ts +0 -59
- package/src/react17/upgrade-to-react-17.ts +0 -52
- package/src/react18/upgrade-to-react-18.ts +0 -55
- package/src/react19/upgrade-to-react-19.ts +0 -75
|
@@ -0,0 +1,449 @@
|
|
|
1
|
+
import {Cursor, ExecutionContext} from "@openrewrite/rewrite";
|
|
2
|
+
import {JavaScriptVisitor, JS, JSX} from "@openrewrite/rewrite/javascript";
|
|
3
|
+
import {emptySpace, Expression, isIdentifier, J, Type} from "@openrewrite/rewrite/java";
|
|
4
|
+
import {create} from "mutative";
|
|
5
|
+
import {enzymeMethod, enzymeWrapper} from "./enzyme-usage";
|
|
6
|
+
|
|
7
|
+
const HTML_TAGS = new Set(["a", "abbr", "address", "area", "article", "aside", "audio", "b", "bdi", "bdo",
|
|
8
|
+
"blockquote", "body", "br", "button", "canvas", "caption", "cite", "code", "col", "colgroup", "data",
|
|
9
|
+
"datalist", "dd", "del", "details", "dfn", "dialog", "div", "dl", "dt", "em", "embed", "fieldset",
|
|
10
|
+
"figcaption", "figure", "footer", "form", "h1", "h2", "h3", "h4", "h5", "h6", "header", "hgroup", "hr", "i",
|
|
11
|
+
"iframe", "img", "input", "ins", "kbd", "label", "legend", "li", "main", "map", "mark", "menu", "meter",
|
|
12
|
+
"nav", "noscript", "object", "ol", "optgroup", "option", "output", "p", "picture", "pre", "progress", "q",
|
|
13
|
+
"rp", "rt", "ruby", "s", "samp", "section", "select", "slot", "small", "source", "span", "strong", "sub",
|
|
14
|
+
"summary", "sup", "table", "tbody", "td", "template", "textarea", "tfoot", "th", "thead", "time", "tr",
|
|
15
|
+
"track", "u", "ul", "var", "video", "wbr"]);
|
|
16
|
+
const SVG_TAGS = new Set(["circle", "clipPath", "defs", "desc", "ellipse", "foreignObject", "g", "image", "line",
|
|
17
|
+
"linearGradient", "marker", "mask", "path", "pattern", "polygon", "polyline", "radialGradient", "rect", "stop",
|
|
18
|
+
"svg", "symbol", "text", "textPath", "tspan", "use"]);
|
|
19
|
+
|
|
20
|
+
// React writes these props through as identically named string attributes. Props such as
|
|
21
|
+
// `htmlFor`, `className`, `value`, or `checked` differ from their DOM attributes and stay excluded.
|
|
22
|
+
const ATTRIBUTE = String.raw`(?:data-[a-z0-9]+(?:-[a-z0-9]+)*|aria-[a-z]+|id|name|type|role|href|title|alt|placeholder)`;
|
|
23
|
+
const SIMPLE = String.raw`(?:\.-?[_a-zA-Z][\w-]*|#-?[_a-zA-Z][\w-]*|\[${ATTRIBUTE}(?:="[^"\\]*"|='[^'\\]*')?\])`;
|
|
24
|
+
const COMPOUND = new RegExp(String.raw`([a-z][a-zA-Z0-9]*)?(?:${SIMPLE})*`, "y");
|
|
25
|
+
const ATTRIBUTE_VALUE = /="([^"\\]*)"\]|='([^'\\]*)'\]/g;
|
|
26
|
+
|
|
27
|
+
/** The tag of each compound in a selector of compounds separated by single spaces, or undefined if it is not one. */
|
|
28
|
+
function compoundTags(value: string): (string | undefined)[] | undefined {
|
|
29
|
+
const tags: (string | undefined)[] = [];
|
|
30
|
+
for (let i = 0; ;) {
|
|
31
|
+
COMPOUND.lastIndex = i;
|
|
32
|
+
const match = COMPOUND.exec(value);
|
|
33
|
+
if (!match || match[0] === "") return undefined;
|
|
34
|
+
tags.push(match[1]);
|
|
35
|
+
i += match[0].length;
|
|
36
|
+
if (i === value.length) return tags;
|
|
37
|
+
if (value[i++] !== " ") return undefined;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Accepts a selector whose Enzyme and DOM meanings agree for host elements: a compound of an HTML
|
|
43
|
+
* or SVG tag, classes, IDs, and quoted pass-through attributes, or several such compounds combined as
|
|
44
|
+
* descendants. Enzyme compares an attribute value strictly against the
|
|
45
|
+
* prop, while the DOM sees it stringified, so values that a number or boolean prop renders to are
|
|
46
|
+
* excluded. Enzyme additionally matches composite elements whose props satisfy a class, ID, or
|
|
47
|
+
* attribute compound, so it finds every element the DOM finds and possibly more: counts can only
|
|
48
|
+
* drop, which a converted assertion shows, while positions can shift, which {@link positional}
|
|
49
|
+
* guards. A composite element between a parent and its child breaks a child combinator, and other
|
|
50
|
+
* combinators and pseudo-classes are excluded.
|
|
51
|
+
*/
|
|
52
|
+
export function domSelector(value: unknown): value is string {
|
|
53
|
+
if (typeof value !== "string" || value === "") return false;
|
|
54
|
+
const tags = compoundTags(value);
|
|
55
|
+
return !!tags && tags.every(tag => tag === undefined || HTML_TAGS.has(tag) || SVG_TAGS.has(tag)) &&
|
|
56
|
+
[...value.matchAll(ATTRIBUTE_VALUE)].every(([, double, single]) => onlyStringPropsRenderTo(double ?? single));
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function onlyStringPropsRenderTo(value: string): boolean {
|
|
60
|
+
return value !== "true" && value !== "false" && String(Number(value)) !== value;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function selectorLiteral(node: J | undefined): node is J.Literal {
|
|
64
|
+
return node?.kind === J.Kind.Literal && domSelector((node as J.Literal).value);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** The tag of the elements a selector matches. */
|
|
68
|
+
function selectorTag(selector: J.Literal): string | undefined {
|
|
69
|
+
const tags = compoundTags(selector.value as string);
|
|
70
|
+
return tags?.[tags.length - 1];
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Whether Enzyme finds exactly the host elements the DOM does, in the same order, which a position
|
|
75
|
+
* into the matches depends on: where every compound names a tag, or `hostNodes()` keeps the host
|
|
76
|
+
* elements a single compound matches.
|
|
77
|
+
*/
|
|
78
|
+
function positional(selector: J.Literal, hostNodes: boolean): boolean {
|
|
79
|
+
const tags = compoundTags(selector.value as string)!;
|
|
80
|
+
return tags.every(tag => tag !== undefined) || (hostNodes && tags.length === 1);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Enzyme's `simulate` calls the React handler directly, while `fireEvent` dispatches a DOM event that
|
|
84
|
+
// React 17 receives through its root container. A dispatched click also runs the default action, such
|
|
85
|
+
// as toggling a checkbox or following a link, so clicks on those elements stay excluded.
|
|
86
|
+
const SIMULATED_EVENTS = new Set(["click", "doubleClick", "mouseDown", "mouseUp", "mouseEnter", "mouseLeave",
|
|
87
|
+
"mouseOver", "mouseOut", "mouseMove", "focus", "blur", "submit", "keyDown", "keyUp", "change", "touchStart",
|
|
88
|
+
"touchMove", "touchEnd", "wheel"]);
|
|
89
|
+
// Enzyme also accepts the DOM's lowercase names of these events, and `dblclick` for a double click
|
|
90
|
+
const NATIVE_EVENT_NAMES = new Map([...[...SIMULATED_EVENTS].map(event => [event.toLowerCase(), event] as const), ["dblclick", "doubleClick"]]);
|
|
91
|
+
const CLICK_DEFAULT_ACTIONS = new Set(["a", "input", "label", "option", "select", "summary", "textarea"]);
|
|
92
|
+
|
|
93
|
+
export function simpleChain(node: Expression): boolean {
|
|
94
|
+
if (node.markers.markers.some(marker => marker.kind === JS.Markers.Optional)) return false;
|
|
95
|
+
if (node.kind === J.Kind.FieldAccess) {
|
|
96
|
+
const field = node as J.FieldAccess;
|
|
97
|
+
return field.name.before.comments.length === 0 && field.name.element.prefix.comments.length === 0 && simpleChain(field.target);
|
|
98
|
+
}
|
|
99
|
+
if (node.kind === J.Kind.MethodInvocation) {
|
|
100
|
+
const call = node as J.MethodInvocation;
|
|
101
|
+
return !call.typeParameters && call.name.prefix.comments.length === 0 &&
|
|
102
|
+
call.arguments.before.comments.length === 0 && call.arguments.elements.every(arg =>
|
|
103
|
+
arg.after.comments.length === 0 && (arg.element.kind !== J.Kind.Empty || arg.element.prefix.comments.length === 0)) &&
|
|
104
|
+
(!call.select || (call.select.after.comments.length === 0 && simpleChain(call.select.element)));
|
|
105
|
+
}
|
|
106
|
+
return isIdentifier(node);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export type WrapperUse = "length" | "exists" | "positionExists" | "nodes" | "selectorText" | "html" | "text" | "hasClass" | "simulate" | "setProps";
|
|
110
|
+
|
|
111
|
+
/** `first` and `last` index like Enzyme's methods of the same name; a literal like `at(n)`. */
|
|
112
|
+
export type ElementIndex = J.Literal | "first" | "last";
|
|
113
|
+
|
|
114
|
+
export interface WrapperRead {
|
|
115
|
+
use: WrapperUse;
|
|
116
|
+
wrapper: J.Identifier;
|
|
117
|
+
selector?: J.Literal;
|
|
118
|
+
index?: ElementIndex;
|
|
119
|
+
/** A `find` within the positioned element. */
|
|
120
|
+
within?: J.Literal;
|
|
121
|
+
/** Found nodes asserted only to be truthy, which any wrapper and any node list are. */
|
|
122
|
+
vacuous?: boolean;
|
|
123
|
+
/** A read outside an assertion, which throws where nothing is found, as Enzyme's single-node methods do. */
|
|
124
|
+
unasserted?: boolean;
|
|
125
|
+
className?: J.Literal;
|
|
126
|
+
event?: string;
|
|
127
|
+
eventInit?: Expression;
|
|
128
|
+
/** The properties a `{nativeEvent}` payload gives the event the handler receives. */
|
|
129
|
+
nativeEvent?: [string, Expression][];
|
|
130
|
+
props?: Expression;
|
|
131
|
+
/** The variable holding the rendered element, which `setProps` clones. */
|
|
132
|
+
element?: string;
|
|
133
|
+
/** Whether each `setProps` assigns its clone to the element, for a later one to clone. */
|
|
134
|
+
reassigned?: boolean;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export interface WrapperScope {
|
|
138
|
+
declaration: J.VariableDeclarations;
|
|
139
|
+
name: string;
|
|
140
|
+
uses: Map<string, WrapperRead>;
|
|
141
|
+
removedStatements: Set<string>;
|
|
142
|
+
unmounts: Set<string>;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// The declaration is an Enzyme `mount`, so a reference is its wrapper unless another binding of the
|
|
146
|
+
// name shadows it; a project without Enzyme's type declarations leaves the wrapper untyped.
|
|
147
|
+
function untyped(type: Type | undefined): boolean {
|
|
148
|
+
return !type || type.kind === Type.Kind.Unknown;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function noArguments(call: J.MethodInvocation): boolean {
|
|
152
|
+
return call.arguments.elements.every(arg => arg.element.kind === J.Kind.Empty);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function call(node: J | undefined, name: string): boolean {
|
|
156
|
+
return node?.kind === J.Kind.MethodInvocation && (node as J.MethodInvocation).name.simpleName === name;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function selectOf(node: J | undefined, target: J): boolean {
|
|
160
|
+
return node?.kind === J.Kind.MethodInvocation && (node as J.MethodInvocation).select?.element === target;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function statement(cursor: Cursor): J | undefined {
|
|
164
|
+
const parent = cursor.parentTree();
|
|
165
|
+
const value = parent?.value as J | undefined;
|
|
166
|
+
if (value?.kind === JS.Kind.ExpressionStatement) return parent!.parentTree()?.value.kind === J.Kind.Block ? value : undefined;
|
|
167
|
+
return value?.kind === J.Kind.Block ? cursor.value as J : undefined;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
export function statementPosition(cursor: Cursor): boolean {
|
|
171
|
+
return statement(cursor) !== undefined;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/** The argument `mount` renders, which `render` accepts as it is. */
|
|
175
|
+
export function mountedElement(mount: J.MethodInvocation): boolean {
|
|
176
|
+
const [element] = mount.arguments.elements;
|
|
177
|
+
return mount.arguments.elements.length === 1 && element.element.kind !== J.Kind.Empty && element.element.kind !== JS.Kind.Spread;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/** The matcher a node list can take the place of found nodes for, as `expect`'s subject. */
|
|
181
|
+
function nodesMatcher(expectCursor: Cursor): "length" | "truthy" | undefined {
|
|
182
|
+
const expect = expectCursor.value as J;
|
|
183
|
+
let matcherCursor = expectCursor.parentTree();
|
|
184
|
+
let negated = false;
|
|
185
|
+
const parent = matcherCursor?.value as J | undefined;
|
|
186
|
+
if (parent?.kind === J.Kind.FieldAccess && (parent as J.FieldAccess).target === expect &&
|
|
187
|
+
(parent as J.FieldAccess).name.element.simpleName === "not") {
|
|
188
|
+
negated = true;
|
|
189
|
+
matcherCursor = matcherCursor!.parentTree();
|
|
190
|
+
}
|
|
191
|
+
const matcher = matcherCursor?.value as J | undefined;
|
|
192
|
+
if (matcher?.kind !== J.Kind.MethodInvocation || (matcher as J.MethodInvocation).select?.element !== (negated ? parent : expect)) return undefined;
|
|
193
|
+
if (call(matcher, "toHaveLength")) return "length";
|
|
194
|
+
return call(matcher, "toBeTruthy") && !negated && noArguments(matcher as J.MethodInvocation) ? "truthy" : undefined;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
function stringLiteral(node: J | undefined): node is J.Literal {
|
|
198
|
+
return node?.kind === J.Kind.Literal && typeof (node as J.Literal).value === "string";
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
function objectProperties(node: J | undefined): Map<string, Expression> | undefined {
|
|
202
|
+
if (node?.kind !== J.Kind.NewClass) return undefined;
|
|
203
|
+
const object = node as J.NewClass;
|
|
204
|
+
if (object.class || !object.body) return undefined;
|
|
205
|
+
const properties = new Map<string, Expression>();
|
|
206
|
+
for (const statement of object.body.statements) {
|
|
207
|
+
const property = statement.element as JS.PropertyAssignment;
|
|
208
|
+
if (property.kind !== JS.Kind.PropertyAssignment || property.assigmentToken !== "Colon" ||
|
|
209
|
+
!isIdentifier(property.name.element) || !property.initializer) return undefined;
|
|
210
|
+
properties.set(property.name.element.simpleName, property.initializer);
|
|
211
|
+
}
|
|
212
|
+
return properties;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* `{target: {value}}` sets the value React's change tracking compares, so an empty value, which an
|
|
217
|
+
* input usually starts with, would not call `onChange`. Keyboard events may name their `key`.
|
|
218
|
+
*/
|
|
219
|
+
function eventInit(event: string, init: J | undefined): boolean {
|
|
220
|
+
const properties = objectProperties(init);
|
|
221
|
+
if (event === "change") {
|
|
222
|
+
const target = objectProperties(properties?.get("target"));
|
|
223
|
+
const value = target?.get("value");
|
|
224
|
+
return properties?.size === 1 && target?.size === 1 && value !== undefined && !(stringLiteral(value) && value.value === "");
|
|
225
|
+
}
|
|
226
|
+
if (init === undefined) return true;
|
|
227
|
+
return (event === "keyDown" || event === "keyUp") && properties?.size === 1 && stringLiteral(properties.get("key"));
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Enzyme replaces the native event of the synthetic event with a `{nativeEvent}` payload, which a real
|
|
232
|
+
* event whose own properties shadow the same names reproduces for the handler.
|
|
233
|
+
*/
|
|
234
|
+
function nativeEventInit(event: string, init: J | undefined): [string, Expression][] | undefined {
|
|
235
|
+
const properties = objectProperties(init);
|
|
236
|
+
const nativeEvent = objectProperties(properties?.get("nativeEvent"));
|
|
237
|
+
return event !== "change" && properties?.size === 1 && nativeEvent ? [...nativeEvent] : undefined;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
function expectArgument(cursor: Cursor, node: J): boolean {
|
|
241
|
+
const parent = cursor.parentTree()?.value as J | undefined;
|
|
242
|
+
if (!call(parent, "expect")) return false;
|
|
243
|
+
const expect = parent as J.MethodInvocation;
|
|
244
|
+
return !expect.select && expect.arguments.elements.length === 1 && expect.arguments.elements[0].element === node;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
export function wrapperDeclaration(statement: J | undefined): {declaration: J.VariableDeclarations, name: string, mount: J.MethodInvocation} | undefined {
|
|
248
|
+
if (statement?.kind !== J.Kind.VariableDeclarations) return undefined;
|
|
249
|
+
const declaration = statement as J.VariableDeclarations;
|
|
250
|
+
if (declaration.variables.length !== 1 || declaration.typeExpression ||
|
|
251
|
+
!declaration.modifiers.some(modifier => modifier.keyword === "const") ||
|
|
252
|
+
declaration.leadingAnnotations.length > 0) return undefined;
|
|
253
|
+
const variable = declaration.variables[0].element;
|
|
254
|
+
const initializer = variable.initializer?.element;
|
|
255
|
+
if (!isIdentifier(variable.name) || initializer?.kind !== J.Kind.MethodInvocation) return undefined;
|
|
256
|
+
const mount = initializer as J.MethodInvocation;
|
|
257
|
+
if (enzymeMethod(mount) !== "mount" || mount.typeParameters || !mountedElement(mount)) return undefined;
|
|
258
|
+
return {declaration, name: variable.name.simpleName, mount};
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* Proves that every reference to a block-scoped wrapper is a supported DOM read, so the
|
|
263
|
+
* declaration and all its uses can be converted together. Any other reference, including
|
|
264
|
+
* shadowing declarations, property names, arguments, and returns of the same name, rejects
|
|
265
|
+
* the whole scope.
|
|
266
|
+
*/
|
|
267
|
+
export async function analyzeWrapperScope(block: J.Block, declaration: J.VariableDeclarations, name: string,
|
|
268
|
+
events: boolean, untaggedPositions: boolean, cursor: Cursor,
|
|
269
|
+
ctx: ExecutionContext): Promise<WrapperScope | undefined> {
|
|
270
|
+
const uses = new Map<string, WrapperRead>();
|
|
271
|
+
const removedStatements = new Set<string>();
|
|
272
|
+
const unmounts = new Set<string>();
|
|
273
|
+
let declared = false;
|
|
274
|
+
let supported = true;
|
|
275
|
+
let unmounted = false;
|
|
276
|
+
const variable = declaration.variables[0].element;
|
|
277
|
+
await new class extends JavaScriptVisitor<ExecutionContext> {
|
|
278
|
+
override async visitIdentifier(identifier: J.Identifier, ctx: ExecutionContext): Promise<J | undefined> {
|
|
279
|
+
if (identifier.simpleName !== name || !supported) return identifier;
|
|
280
|
+
if (identifier === variable.name) {
|
|
281
|
+
declared = true;
|
|
282
|
+
return identifier;
|
|
283
|
+
}
|
|
284
|
+
const use = unmounted ? undefined : this.use(identifier);
|
|
285
|
+
if (!use) supported = false;
|
|
286
|
+
return identifier;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
private use(identifier: J.Identifier): boolean {
|
|
290
|
+
if (!enzymeWrapper(identifier.type) && !untyped(identifier.type)) return false;
|
|
291
|
+
const ancestors: Cursor[] = [];
|
|
292
|
+
for (let c: Cursor | undefined = this.cursor.parentTree(); c && ancestors.length < 5; c = c.parentTree()) ancestors.push(c);
|
|
293
|
+
const first = ancestors[0]?.value as J | undefined;
|
|
294
|
+
if (!selectOf(first, identifier)) return false;
|
|
295
|
+
const method = first as J.MethodInvocation;
|
|
296
|
+
if (call(method, "find")) {
|
|
297
|
+
const args = method.arguments.elements;
|
|
298
|
+
if (args.length !== 1 || !selectorLiteral(args[0].element)) return false;
|
|
299
|
+
const selector = args[0].element;
|
|
300
|
+
let k = 1;
|
|
301
|
+
let inner: J = method;
|
|
302
|
+
let hostNodes = false;
|
|
303
|
+
if (call(ancestors[k]?.value, "hostNodes") && selectOf(ancestors[k].value, inner) && noArguments(ancestors[k].value)) {
|
|
304
|
+
inner = ancestors[k++].value;
|
|
305
|
+
hostNodes = true;
|
|
306
|
+
}
|
|
307
|
+
const index = this.index(ancestors[k]?.value, inner);
|
|
308
|
+
let within: J.Literal | undefined;
|
|
309
|
+
if (index) {
|
|
310
|
+
if (!untaggedPositions && !positional(selector, hostNodes)) return false;
|
|
311
|
+
inner = ancestors[k++].value;
|
|
312
|
+
const nested = ancestors[k]?.value as J | undefined;
|
|
313
|
+
if (call(nested, "find") && selectOf(nested, inner)) {
|
|
314
|
+
const [argument] = (nested as J.MethodInvocation).arguments.elements;
|
|
315
|
+
if ((nested as J.MethodInvocation).arguments.elements.length !== 1 || !selectorLiteral(argument.element)) return false;
|
|
316
|
+
within = argument.element;
|
|
317
|
+
inner = ancestors[k++].value;
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
const outer = ancestors[k]?.value as J | undefined;
|
|
321
|
+
const outerCursor = ancestors[k];
|
|
322
|
+
const read = {wrapper: identifier, selector, index, within};
|
|
323
|
+
if ((!index || within) && outer?.kind === J.Kind.FieldAccess && (outer as J.FieldAccess).target === inner &&
|
|
324
|
+
(outer as J.FieldAccess).name.element.simpleName === "length") {
|
|
325
|
+
return this.record(outer as J.FieldAccess, {...read, use: "length"});
|
|
326
|
+
}
|
|
327
|
+
if (!index && expectArgument(ancestors[k - 1], inner)) {
|
|
328
|
+
const matcher = nodesMatcher(outerCursor);
|
|
329
|
+
return matcher !== undefined && this.record(inner as Expression, {...read, use: "nodes", vacuous: matcher === "truthy"});
|
|
330
|
+
}
|
|
331
|
+
if (!selectOf(outer, inner)) return false;
|
|
332
|
+
const terminal = outer as J.MethodInvocation;
|
|
333
|
+
const terminalArgs = terminal.arguments.elements.filter(arg => arg.element.kind !== J.Kind.Empty).map(arg => arg.element);
|
|
334
|
+
if (call(terminal, "exists") && terminalArgs.length === 0) return this.record(terminal, {...read, use: !index || within ? "exists" : "positionExists"});
|
|
335
|
+
if (within) return false;
|
|
336
|
+
if ((call(terminal, "text") || call(terminal, "html")) && terminalArgs.length === 0) {
|
|
337
|
+
return this.record(terminal, {...read, use: call(terminal, "text") ? "selectorText" : "html",
|
|
338
|
+
unasserted: !expectArgument(outerCursor, terminal)});
|
|
339
|
+
}
|
|
340
|
+
if (call(terminal, "hasClass") && terminalArgs.length === 1 && stringLiteral(terminalArgs[0]) &&
|
|
341
|
+
/^-?[_a-zA-Z][\w-]*$/.test(terminalArgs[0].value as string)) {
|
|
342
|
+
return this.record(terminal, {...read, use: "hasClass", className: terminalArgs[0]});
|
|
343
|
+
}
|
|
344
|
+
if (call(terminal, "simulate") && events && statement(outerCursor) && terminalArgs.length >= 1 && terminalArgs.length <= 2 &&
|
|
345
|
+
stringLiteral(terminalArgs[0])) {
|
|
346
|
+
const name = terminalArgs[0].value as string;
|
|
347
|
+
const event = SIMULATED_EVENTS.has(name) ? name : NATIVE_EVENT_NAMES.get(name);
|
|
348
|
+
if (!event) return false;
|
|
349
|
+
const nativeEvent = nativeEventInit(event, terminalArgs[1]);
|
|
350
|
+
if (!nativeEvent && !eventInit(event, terminalArgs[1])) return false;
|
|
351
|
+
if (event === "click" && CLICK_DEFAULT_ACTIONS.has(selectorTag(selector) ?? "")) return false;
|
|
352
|
+
return this.record(terminal, {...read, use: "simulate", event, ...nativeEvent ? {nativeEvent} : {eventInit: terminalArgs[1]}});
|
|
353
|
+
}
|
|
354
|
+
return false;
|
|
355
|
+
}
|
|
356
|
+
const [argument] = method.arguments.elements;
|
|
357
|
+
if (call(method, "exists") && method.arguments.elements.length === 1 && selectorLiteral(argument.element)) {
|
|
358
|
+
return this.record(method, {wrapper: identifier, selector: argument.element, use: "exists"});
|
|
359
|
+
}
|
|
360
|
+
if (call(method, "setProps") && method.arguments.elements.length === 1 && statement(ancestors[0]) &&
|
|
361
|
+
argument.element.kind !== J.Kind.Empty && argument.element.kind !== JS.Kind.Spread) {
|
|
362
|
+
return this.record(method, {wrapper: identifier, use: "setProps", props: argument.element as Expression});
|
|
363
|
+
}
|
|
364
|
+
if (!noArguments(method)) return false;
|
|
365
|
+
if (call(method, "text")) return this.record(method, {wrapper: identifier, use: "text", unasserted: !expectArgument(ancestors[0], method)});
|
|
366
|
+
if (call(method, "update") && statement(ancestors[0])?.prefix.comments.length === 0 && method.prefix.comments.length === 0) {
|
|
367
|
+
removedStatements.add(method.id);
|
|
368
|
+
return simpleChain(method);
|
|
369
|
+
}
|
|
370
|
+
if (call(method, "unmount") && statement(ancestors[0])) {
|
|
371
|
+
unmounted = true;
|
|
372
|
+
unmounts.add(method.id);
|
|
373
|
+
return simpleChain(method);
|
|
374
|
+
}
|
|
375
|
+
return false;
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
private index(node: J | undefined, inner: J): ElementIndex | undefined {
|
|
379
|
+
if (!selectOf(node, inner)) return undefined;
|
|
380
|
+
const method = node as J.MethodInvocation;
|
|
381
|
+
if ((call(method, "first") || call(method, "last")) && noArguments(method)) return method.name.simpleName as "first" | "last";
|
|
382
|
+
const [position] = method.arguments.elements;
|
|
383
|
+
if (call(method, "at") && method.arguments.elements.length === 1 && position.element.kind === J.Kind.Literal &&
|
|
384
|
+
Number.isInteger((position.element as J.Literal).value) && ((position.element as J.Literal).value as number) >= 0) {
|
|
385
|
+
return position.element as J.Literal;
|
|
386
|
+
}
|
|
387
|
+
return undefined;
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
private record(node: Expression, read: WrapperRead): boolean {
|
|
391
|
+
if (!simpleChain(node)) return false;
|
|
392
|
+
uses.set(node.id, read);
|
|
393
|
+
return true;
|
|
394
|
+
}
|
|
395
|
+
}().visit(block, ctx, cursor);
|
|
396
|
+
return declared && supported ? {declaration, name, uses, removedStatements, unmounts} : undefined;
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
export async function idsWithin(node: J, ids: Set<string>, ctx: ExecutionContext): Promise<string[]> {
|
|
400
|
+
const found: string[] = [];
|
|
401
|
+
await new class extends JavaScriptVisitor<ExecutionContext> {
|
|
402
|
+
protected override async preVisit(tree: J): Promise<J | undefined> {
|
|
403
|
+
if (ids.has(tree.id)) found.push(tree.id);
|
|
404
|
+
return tree;
|
|
405
|
+
}
|
|
406
|
+
}().visit(node, ctx);
|
|
407
|
+
return found;
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
/**
|
|
411
|
+
* A converted wrapper keeps its name, but no longer holds an Enzyme wrapper. Drop the stale
|
|
412
|
+
* Enzyme attribution so that the inventory does not report converted code.
|
|
413
|
+
*/
|
|
414
|
+
export async function withoutEnzymeTypes<T extends J>(node: T, ctx: ExecutionContext): Promise<T> {
|
|
415
|
+
return await new class extends JavaScriptVisitor<ExecutionContext> {
|
|
416
|
+
override async visitIdentifier(identifier: J.Identifier, ctx: ExecutionContext): Promise<J | undefined> {
|
|
417
|
+
const visited = await super.visitIdentifier(identifier, ctx) as J.Identifier;
|
|
418
|
+
if (!enzymeWrapper(visited.type)) return visited;
|
|
419
|
+
return {...visited, type: undefined, fieldType: undefined} as J.Identifier;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
override async visitMethodInvocation(method: J.MethodInvocation, ctx: ExecutionContext): Promise<J | undefined> {
|
|
423
|
+
const visited = await super.visitMethodInvocation(method, ctx) as J.MethodInvocation;
|
|
424
|
+
if (!enzymeMethod(visited)) return visited;
|
|
425
|
+
return {...visited, methodType: undefined} as J.MethodInvocation;
|
|
426
|
+
}
|
|
427
|
+
}().visit(node, ctx) as T;
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
/**
|
|
431
|
+
* Keeps a multi-line `mount(\n element\n)` call's closing line break before the closing
|
|
432
|
+
* parenthesis, rather than before the render options added after the element argument.
|
|
433
|
+
*/
|
|
434
|
+
export async function withRenderOptionsOnElementLine<T extends J>(node: T, ctx: ExecutionContext): Promise<T> {
|
|
435
|
+
return await new class extends JavaScriptVisitor<ExecutionContext> {
|
|
436
|
+
override async visitMethodInvocation(method: J.MethodInvocation, ctx: ExecutionContext): Promise<J | undefined> {
|
|
437
|
+
const [element, options] = method.arguments.elements;
|
|
438
|
+
if (method.arguments.elements.length !== 2 || !objectProperties(options.element)?.has("container")) {
|
|
439
|
+
return super.visitMethodInvocation(method, ctx);
|
|
440
|
+
}
|
|
441
|
+
if (!element.after.whitespace.includes("\n") || element.after.comments.length > 0 ||
|
|
442
|
+
options.after.whitespace !== "" || options.after.comments.length > 0) return method;
|
|
443
|
+
return create(method, draft => {
|
|
444
|
+
draft.arguments.elements[1].after = element.after as any;
|
|
445
|
+
draft.arguments.elements[0].after = emptySpace as any;
|
|
446
|
+
});
|
|
447
|
+
}
|
|
448
|
+
}().visit(node, ctx) as T;
|
|
449
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import {J} from "@openrewrite/rewrite/java";
|
|
2
|
+
import {JS} from "@openrewrite/rewrite/javascript";
|
|
3
|
+
|
|
4
|
+
const COMPONENT_BASES = new Set(['Component', 'PureComponent']);
|
|
5
|
+
|
|
6
|
+
/** `Component<P, S>` is a parameterized type (possibly wrapped in a type-tree expression) around the plain name; this returns that name. */
|
|
7
|
+
export function unwrapBaseType(type: J | undefined): J | undefined {
|
|
8
|
+
if (type?.kind === JS.Kind.TypeTreeExpression) return unwrapBaseType((type as JS.TypeTreeExpression).expression);
|
|
9
|
+
if (type?.kind === J.Kind.ParameterizedType) return (type as J.ParameterizedType).class;
|
|
10
|
+
if (type?.kind === JS.Kind.ExpressionWithTypeArguments) return (type as JS.ExpressionWithTypeArguments).clazz;
|
|
11
|
+
return type;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** The type arguments of `Component<P, S>`, however the parser represents the parameterized base. */
|
|
15
|
+
export function baseTypeArguments(type: J | undefined): J.Container<J> | undefined {
|
|
16
|
+
if (type?.kind === JS.Kind.TypeTreeExpression) return baseTypeArguments((type as JS.TypeTreeExpression).expression);
|
|
17
|
+
if (type?.kind === J.Kind.ParameterizedType) return (type as J.ParameterizedType).typeParameters;
|
|
18
|
+
if (type?.kind === JS.Kind.ExpressionWithTypeArguments) return (type as JS.ExpressionWithTypeArguments).typeArguments;
|
|
19
|
+
return undefined;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** The simple name of the class a declaration extends (`Component` for both `Component` and `React.Component`). */
|
|
23
|
+
export function extendedClassName(cd: J.ClassDeclaration): string | undefined {
|
|
24
|
+
const base = unwrapBaseType(cd.extends?.element);
|
|
25
|
+
if (base?.kind === J.Kind.Identifier) return (base as J.Identifier).simpleName;
|
|
26
|
+
if (base?.kind === J.Kind.FieldAccess) return (base as J.FieldAccess).name.element.simpleName;
|
|
27
|
+
return undefined;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Whether the class extends `Component` or `PureComponent`, qualified or not. Matches by name only. */
|
|
31
|
+
export function extendsReactComponentByName(cd: J.ClassDeclaration): boolean {
|
|
32
|
+
const name = extendedClassName(cd);
|
|
33
|
+
return !!name && COMPONENT_BASES.has(name);
|
|
34
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import {ExecutionContext} from "@openrewrite/rewrite";
|
|
2
|
+
import {JavaScriptVisitor} from "@openrewrite/rewrite/javascript";
|
|
3
|
+
import {J} from "@openrewrite/rewrite/java";
|
|
4
|
+
|
|
5
|
+
/** The first non-undefined result of `pick` over the subtree's nodes, in visit order. Stops walking at the first match. */
|
|
6
|
+
export async function firstResult<T>(tree: J | undefined, pick: (node: J) => T | undefined): Promise<T | undefined> {
|
|
7
|
+
if (!tree) return undefined;
|
|
8
|
+
let result: T | undefined;
|
|
9
|
+
await new class extends JavaScriptVisitor<ExecutionContext> {
|
|
10
|
+
override async visit<R extends J>(t: any, ctx: ExecutionContext, parent?: any): Promise<R | undefined> {
|
|
11
|
+
if (result !== undefined) return t;
|
|
12
|
+
if (t && typeof t === 'object' && t.kind) {
|
|
13
|
+
result = pick(t);
|
|
14
|
+
if (result !== undefined) return t;
|
|
15
|
+
}
|
|
16
|
+
return super.visit(t, ctx, parent);
|
|
17
|
+
}
|
|
18
|
+
}().visit(tree, {} as ExecutionContext);
|
|
19
|
+
return result;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Whether any node in the subtree satisfies `test`. */
|
|
23
|
+
export async function anyNode(tree: J | undefined, test: (node: J) => boolean): Promise<boolean> {
|
|
24
|
+
return await firstResult(tree, n => test(n) ? true : undefined) === true;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Whether the node is an identifier spelled `name`. */
|
|
28
|
+
export const isIdentifierNamed = (node: J, name: string): boolean =>
|
|
29
|
+
node.kind === J.Kind.Identifier && (node as J.Identifier).simpleName === name;
|
|
30
|
+
|
|
31
|
+
/** Whether the expression is `this`. */
|
|
32
|
+
export const isThis = (expr: J | undefined): boolean => !!expr && isIdentifierNamed(expr, 'this');
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { Recipe } from "@openrewrite/rewrite";
|
|
2
|
-
export declare class UpgradeToReact16 extends Recipe {
|
|
3
|
-
readonly name = "org.openrewrite.react.migrate.upgrade-to-react-16";
|
|
4
|
-
readonly displayName = "Upgrade to React 16";
|
|
5
|
-
readonly description = "Migrate deprecated APIs for React 16 compatibility. Includes PropTypes extraction, ReactDOM split, DOM factory replacement, createFactory replacement, and error boundary API updates.";
|
|
6
|
-
upgradeDependencies?: boolean;
|
|
7
|
-
constructor(options?: {
|
|
8
|
-
upgradeDependencies?: boolean;
|
|
9
|
-
});
|
|
10
|
-
recipeList(): Promise<Recipe[]>;
|
|
11
|
-
}
|
|
12
|
-
//# sourceMappingURL=upgrade-to-react-16.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"upgrade-to-react-16.d.ts","sourceRoot":"","sources":["../../src/react16/upgrade-to-react-16.ts"],"names":[],"mappings":"AAAA,OAAO,EAAS,MAAM,EAAC,MAAM,sBAAsB,CAAC;AAsBpD,qBAAa,gBAAiB,SAAQ,MAAM;IACxC,QAAQ,CAAC,IAAI,uDAAuD;IACpE,QAAQ,CAAC,WAAW,yBAAyB;IAC7C,QAAQ,CAAC,WAAW,4LAA4L;IAOhN,mBAAmB,CAAC,EAAE,OAAO,CAAC;gBAElB,OAAO,CAAC,EAAE;QAAE,mBAAmB,CAAC,EAAE,OAAO,CAAA;KAAE;IAIjD,UAAU,IAAI,OAAO,CAAC,MAAM,EAAE,CAAC;CAoBxC"}
|
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
-
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
-
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
-
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
-
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
-
};
|
|
8
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
9
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
10
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
11
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
12
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
13
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
14
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
15
|
-
});
|
|
16
|
-
};
|
|
17
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
-
exports.UpgradeToReact16 = void 0;
|
|
19
|
-
const rewrite_1 = require("@openrewrite/rewrite");
|
|
20
|
-
const javascript_1 = require("@openrewrite/rewrite/javascript");
|
|
21
|
-
const error_boundaries_1 = require("./error-boundaries");
|
|
22
|
-
const find_dom_node_1 = require("./find-dom-node");
|
|
23
|
-
const react_prop_types_1 = require("./react-prop-types");
|
|
24
|
-
const react_dom_factories_1 = require("./react-dom-factories");
|
|
25
|
-
const react_to_react_dom_1 = require("./react-to-react-dom");
|
|
26
|
-
const replace_create_factory_1 = require("./replace-create-factory");
|
|
27
|
-
class UpgradeToReact16 extends rewrite_1.Recipe {
|
|
28
|
-
constructor(options) {
|
|
29
|
-
super(options);
|
|
30
|
-
this.name = "org.openrewrite.react.migrate.upgrade-to-react-16";
|
|
31
|
-
this.displayName = "Upgrade to React 16";
|
|
32
|
-
this.description = "Migrate deprecated APIs for React 16 compatibility. Includes PropTypes extraction, ReactDOM split, DOM factory replacement, createFactory replacement, and error boundary API updates.";
|
|
33
|
-
}
|
|
34
|
-
recipeList() {
|
|
35
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
36
|
-
const apiRecipes = [
|
|
37
|
-
new react_prop_types_1.ReactPropTypes(),
|
|
38
|
-
new react_dom_factories_1.ReactDomFactories(),
|
|
39
|
-
new react_to_react_dom_1.ReactToReactDom(),
|
|
40
|
-
new find_dom_node_1.FindDomNode(),
|
|
41
|
-
new error_boundaries_1.ErrorBoundaries(),
|
|
42
|
-
new replace_create_factory_1.ReplaceCreateFactory(),
|
|
43
|
-
];
|
|
44
|
-
if (this.upgradeDependencies === false)
|
|
45
|
-
return apiRecipes;
|
|
46
|
-
return [
|
|
47
|
-
...apiRecipes,
|
|
48
|
-
yield (0, javascript_1.upgradeDependencyVersion)({ packageName: "react", newVersion: "16.x" }),
|
|
49
|
-
yield (0, javascript_1.upgradeDependencyVersion)({ packageName: "react-dom", newVersion: "16.x" }),
|
|
50
|
-
yield (0, javascript_1.upgradeDependencyVersion)({ packageName: "@types/react", newVersion: "16.x" }),
|
|
51
|
-
yield (0, javascript_1.upgradeDependencyVersion)({ packageName: "@types/react-dom", newVersion: "16.x" }),
|
|
52
|
-
yield (0, javascript_1.upgradeDependencyVersion)({ packageName: "react-test-renderer", newVersion: "16.x" }),
|
|
53
|
-
yield (0, javascript_1.upgradeDependencyVersion)({ packageName: "@types/react-test-renderer", newVersion: "16.x" }),
|
|
54
|
-
];
|
|
55
|
-
});
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
exports.UpgradeToReact16 = UpgradeToReact16;
|
|
59
|
-
__decorate([
|
|
60
|
-
(0, rewrite_1.Option)({
|
|
61
|
-
displayName: "Upgrade dependency versions",
|
|
62
|
-
description: "Upgrade React dependency versions as well as APIs. Set to false when composing this migration into a later React upgrade.",
|
|
63
|
-
required: false
|
|
64
|
-
})
|
|
65
|
-
], UpgradeToReact16.prototype, "upgradeDependencies", void 0);
|
|
66
|
-
//# sourceMappingURL=upgrade-to-react-16.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"upgrade-to-react-16.js","sourceRoot":"","sources":["../../src/react16/upgrade-to-react-16.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAAA,kDAAoD;AACpD,gEAAyE;AACzE,yDAAmD;AACnD,mDAA4C;AAC5C,yDAAkD;AAClD,+DAAwD;AACxD,6DAAqD;AACrD,qEAA8D;AAe9D,MAAa,gBAAiB,SAAQ,gBAAM;IAYxC,YAAY,OAA2C;QACnD,KAAK,CAAC,OAAO,CAAC,CAAC;QAZV,SAAI,GAAG,mDAAmD,CAAC;QAC3D,gBAAW,GAAG,qBAAqB,CAAC;QACpC,gBAAW,GAAG,wLAAwL,CAAC;IAWhN,CAAC;IAEK,UAAU;;YACZ,MAAM,UAAU,GAAa;gBACzB,IAAI,iCAAc,EAAE;gBACpB,IAAI,uCAAiB,EAAE;gBACvB,IAAI,oCAAe,EAAE;gBACrB,IAAI,2BAAW,EAAE;gBACjB,IAAI,kCAAe,EAAE;gBACrB,IAAI,6CAAoB,EAAE;aAC7B,CAAC;YACF,IAAI,IAAI,CAAC,mBAAmB,KAAK,KAAK;gBAAE,OAAO,UAAU,CAAC;YAC1D,OAAO;gBACH,GAAG,UAAU;gBACb,MAAM,IAAA,qCAAwB,EAAC,EAAC,WAAW,EAAE,OAAO,EAAE,UAAU,EAAE,MAAM,EAAC,CAAC;gBAC1E,MAAM,IAAA,qCAAwB,EAAC,EAAC,WAAW,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,EAAC,CAAC;gBAC9E,MAAM,IAAA,qCAAwB,EAAC,EAAC,WAAW,EAAE,cAAc,EAAE,UAAU,EAAE,MAAM,EAAC,CAAC;gBACjF,MAAM,IAAA,qCAAwB,EAAC,EAAC,WAAW,EAAE,kBAAkB,EAAE,UAAU,EAAE,MAAM,EAAC,CAAC;gBACrF,MAAM,IAAA,qCAAwB,EAAC,EAAC,WAAW,EAAE,qBAAqB,EAAE,UAAU,EAAE,MAAM,EAAC,CAAC;gBACxF,MAAM,IAAA,qCAAwB,EAAC,EAAC,WAAW,EAAE,4BAA4B,EAAE,UAAU,EAAE,MAAM,EAAC,CAAC;aAClG,CAAC;QACN,CAAC;KAAA;CACJ;AApCD,4CAoCC;AA1BG;IALC,IAAA,gBAAM,EAAC;QACJ,WAAW,EAAE,6BAA6B;QAC1C,WAAW,EAAE,2HAA2H;QACxI,QAAQ,EAAE,KAAK;KAClB,CAAC;6DAC4B"}
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { Recipe } from "@openrewrite/rewrite";
|
|
2
|
-
export declare class UpgradeToReact17 extends Recipe {
|
|
3
|
-
readonly name = "org.openrewrite.react.migrate.upgrade-to-react-17";
|
|
4
|
-
readonly displayName = "Upgrade to React 17";
|
|
5
|
-
readonly description = "Migrate deprecated APIs for React 17 compatibility. Includes all React 16 migrations plus lifecycle method prefixing, import cleanup, and event.persist() removal.";
|
|
6
|
-
upgradeDependencies?: boolean;
|
|
7
|
-
constructor(options?: {
|
|
8
|
-
upgradeDependencies?: boolean;
|
|
9
|
-
});
|
|
10
|
-
recipeList(): Promise<Recipe[]>;
|
|
11
|
-
}
|
|
12
|
-
//# sourceMappingURL=upgrade-to-react-17.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"upgrade-to-react-17.d.ts","sourceRoot":"","sources":["../../src/react17/upgrade-to-react-17.ts"],"names":[],"mappings":"AAAA,OAAO,EAAS,MAAM,EAAC,MAAM,sBAAsB,CAAC;AAiBpD,qBAAa,gBAAiB,SAAQ,MAAM;IACxC,QAAQ,CAAC,IAAI,uDAAuD;IACpE,QAAQ,CAAC,WAAW,yBAAyB;IAC7C,QAAQ,CAAC,WAAW,wKAAwK;IAO5L,mBAAmB,CAAC,EAAE,OAAO,CAAC;gBAElB,OAAO,CAAC,EAAE;QAAE,mBAAmB,CAAC,EAAE,OAAO,CAAA;KAAE;IAIjD,UAAU,IAAI,OAAO,CAAC,MAAM,EAAE,CAAC;CAkBxC"}
|