@openrewrite/recipes-react 0.16.0 → 0.17.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.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/migration/cra-to-vite/add-vite-config.d.ts +7 -0
- package/dist/migration/cra-to-vite/add-vite-config.d.ts.map +1 -1
- package/dist/migration/cra-to-vite/add-vite-config.js +58 -1
- package/dist/migration/cra-to-vite/add-vite-config.js.map +1 -1
- package/dist/migration/cra-to-vite/cra-file-gate.d.ts +3 -0
- package/dist/migration/cra-to-vite/cra-file-gate.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/cra-file-gate.js +10 -0
- package/dist/migration/cra-to-vite/cra-file-gate.js.map +1 -0
- package/dist/migration/cra-to-vite/cra-runtime.d.ts +2 -0
- package/dist/migration/cra-to-vite/cra-runtime.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/cra-runtime.js +24 -0
- package/dist/migration/cra-to-vite/cra-runtime.js.map +1 -0
- package/dist/migration/cra-to-vite/cra-script.d.ts +8 -0
- package/dist/migration/cra-to-vite/cra-script.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/cra-script.js +82 -0
- package/dist/migration/cra-to-vite/cra-script.js.map +1 -0
- package/dist/migration/cra-to-vite/cra-to-vite.d.ts +2 -1
- package/dist/migration/cra-to-vite/cra-to-vite.d.ts.map +1 -1
- package/dist/migration/cra-to-vite/cra-to-vite.js +23 -16
- package/dist/migration/cra-to-vite/cra-to-vite.js.map +1 -1
- package/dist/migration/cra-to-vite/cra-typecheck.d.ts +4 -0
- package/dist/migration/cra-to-vite/cra-typecheck.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/cra-typecheck.js +35 -0
- package/dist/migration/cra-to-vite/cra-typecheck.js.map +1 -0
- package/dist/migration/cra-to-vite/find-cra-config-blockers.d.ts.map +1 -1
- package/dist/migration/cra-to-vite/find-cra-config-blockers.js +2 -1
- package/dist/migration/cra-to-vite/find-cra-config-blockers.js.map +1 -1
- package/dist/migration/cra-to-vite/index.d.ts +8 -2
- package/dist/migration/cra-to-vite/index.d.ts.map +1 -1
- package/dist/migration/cra-to-vite/index.js +17 -2
- package/dist/migration/cra-to-vite/index.js.map +1 -1
- package/dist/migration/cra-to-vite/migrate-cra-package.d.ts +17 -0
- package/dist/migration/cra-to-vite/migrate-cra-package.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/migrate-cra-package.js +181 -0
- package/dist/migration/cra-to-vite/migrate-cra-package.js.map +1 -0
- package/dist/migration/cra-to-vite/migrate-cra-svg-imports.d.ts +9 -0
- package/dist/migration/cra-to-vite/migrate-cra-svg-imports.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/migrate-cra-svg-imports.js +66 -0
- package/dist/migration/cra-to-vite/migrate-cra-svg-imports.js.map +1 -0
- package/dist/migration/cra-to-vite/migrate-cra-tests-to-vitest.d.ts +49 -0
- package/dist/migration/cra-to-vite/migrate-cra-tests-to-vitest.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/migrate-cra-tests-to-vitest.js +506 -0
- package/dist/migration/cra-to-vite/migrate-cra-tests-to-vitest.js.map +1 -0
- package/dist/migration/cra-to-vite/move-public-index-html.d.ts +2 -0
- package/dist/migration/cra-to-vite/move-public-index-html.d.ts.map +1 -1
- package/dist/migration/cra-to-vite/move-public-index-html.js +12 -7
- package/dist/migration/cra-to-vite/move-public-index-html.js.map +1 -1
- package/dist/migration/cra-to-vite/plan-cra-to-vite.d.ts +30 -0
- package/dist/migration/cra-to-vite/plan-cra-to-vite.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/plan-cra-to-vite.js +428 -0
- package/dist/migration/cra-to-vite/plan-cra-to-vite.js.map +1 -0
- package/dist/migration/cra-to-vite/prepare-cra-package.d.ts +15 -0
- package/dist/migration/cra-to-vite/prepare-cra-package.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/prepare-cra-package.js +100 -0
- package/dist/migration/cra-to-vite/prepare-cra-package.js.map +1 -0
- package/dist/migration/cra-to-vite/remove-cra-configuration.d.ts +12 -0
- package/dist/migration/cra-to-vite/remove-cra-configuration.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/remove-cra-configuration.js +85 -0
- package/dist/migration/cra-to-vite/remove-cra-configuration.js.map +1 -0
- package/dist/migration/cra-to-vite/rename-jsx-files.d.ts +20 -0
- package/dist/migration/cra-to-vite/rename-jsx-files.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/rename-jsx-files.js +220 -0
- package/dist/migration/cra-to-vite/rename-jsx-files.js.map +1 -0
- package/dist/migration/cra-to-vite/rename-process-env-react-app.d.ts.map +1 -1
- package/dist/migration/cra-to-vite/rename-process-env-react-app.js +6 -3
- package/dist/migration/cra-to-vite/rename-process-env-react-app.js.map +1 -1
- package/dist/migration/cra-to-vite/rename-react-app-env-vars-in-env-files.js +1 -1
- package/dist/migration/cra-to-vite/rename-react-app-env-vars-in-env-files.js.map +1 -1
- package/dist/migration/cra-to-vite/replace-react-scripts-dependency.d.ts +11 -4
- package/dist/migration/cra-to-vite/replace-react-scripts-dependency.d.ts.map +1 -1
- package/dist/migration/cra-to-vite/replace-react-scripts-dependency.js +47 -30
- package/dist/migration/cra-to-vite/replace-react-scripts-dependency.js.map +1 -1
- package/dist/migration/cra-to-vite/resolved-cra-dependency.d.ts +3 -0
- package/dist/migration/cra-to-vite/resolved-cra-dependency.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/resolved-cra-dependency.js +42 -0
- package/dist/migration/cra-to-vite/resolved-cra-dependency.js.map +1 -0
- package/dist/migration/cra-to-vite/scan-cra-projects.d.ts +13 -0
- package/dist/migration/cra-to-vite/scan-cra-projects.d.ts.map +1 -1
- package/dist/migration/cra-to-vite/scan-cra-projects.js +52 -4
- package/dist/migration/cra-to-vite/scan-cra-projects.js.map +1 -1
- package/dist/migration/cra-to-vite/shared.d.ts +1 -1
- package/dist/migration/cra-to-vite/shared.d.ts.map +1 -1
- package/dist/migration/cra-to-vite/shared.js +19 -4
- package/dist/migration/cra-to-vite/shared.js.map +1 -1
- package/dist/migration/cra-to-vite/svg-component.d.ts +5 -0
- package/dist/migration/cra-to-vite/svg-component.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/svg-component.js +60 -0
- package/dist/migration/cra-to-vite/svg-component.js.map +1 -0
- package/dist/migration/cra-to-vite/update-react-app-env-types.d.ts.map +1 -1
- package/dist/migration/cra-to-vite/update-react-app-env-types.js +8 -2
- package/dist/migration/cra-to-vite/update-react-app-env-types.js.map +1 -1
- package/dist/migration/cra-to-vite/update-react-scripts-scripts.d.ts +3 -2
- package/dist/migration/cra-to-vite/update-react-scripts-scripts.d.ts.map +1 -1
- package/dist/migration/cra-to-vite/update-react-scripts-scripts.js +18 -14
- package/dist/migration/cra-to-vite/update-react-scripts-scripts.js.map +1 -1
- package/dist/react18/replace-reactdom-render.d.ts.map +1 -1
- package/dist/react18/replace-reactdom-render.js +16 -8
- package/dist/react18/replace-reactdom-render.js.map +1 -1
- package/dist/testing/enzyme-constant-strings.d.ts +12 -0
- package/dist/testing/enzyme-constant-strings.d.ts.map +1 -0
- package/dist/testing/enzyme-constant-strings.js +101 -0
- package/dist/testing/enzyme-constant-strings.js.map +1 -0
- package/dist/testing/enzyme-host-components.d.ts.map +1 -1
- package/dist/testing/enzyme-host-components.js +5 -2
- package/dist/testing/enzyme-host-components.js.map +1 -1
- package/dist/testing/enzyme-portals.d.ts +38 -0
- package/dist/testing/enzyme-portals.d.ts.map +1 -0
- package/dist/testing/enzyme-portals.js +226 -0
- package/dist/testing/enzyme-portals.js.map +1 -0
- package/dist/testing/enzyme-selector-names.d.ts +9 -0
- package/dist/testing/enzyme-selector-names.d.ts.map +1 -0
- package/dist/testing/enzyme-selector-names.js +50 -0
- package/dist/testing/enzyme-selector-names.js.map +1 -0
- package/dist/testing/enzyme-to-react-testing-library.d.ts +71 -8
- package/dist/testing/enzyme-to-react-testing-library.d.ts.map +1 -1
- package/dist/testing/enzyme-to-react-testing-library.js +321 -34
- package/dist/testing/enzyme-to-react-testing-library.js.map +1 -1
- package/dist/testing/enzyme-usage.d.ts.map +1 -1
- package/dist/testing/enzyme-usage.js +27 -4
- package/dist/testing/enzyme-usage.js.map +1 -1
- package/dist/testing/enzyme-wrapper-scope.d.ts +12 -6
- package/dist/testing/enzyme-wrapper-scope.d.ts.map +1 -1
- package/dist/testing/enzyme-wrapper-scope.js +140 -16
- package/dist/testing/enzyme-wrapper-scope.js.map +1 -1
- package/dist/util/java-recipe-proxy.d.ts +16 -0
- package/dist/util/java-recipe-proxy.d.ts.map +1 -0
- package/dist/util/java-recipe-proxy.js +85 -0
- package/dist/util/java-recipe-proxy.js.map +1 -0
- package/dist/util/module-imports.d.ts +1 -0
- package/dist/util/module-imports.d.ts.map +1 -1
- package/dist/util/module-imports.js +5 -1
- package/dist/util/module-imports.js.map +1 -1
- package/package.json +2 -2
- package/src/index.ts +2 -1
- package/src/migration/cra-to-vite/add-vite-config.ts +53 -1
- package/src/migration/cra-to-vite/cra-file-gate.ts +8 -0
- package/src/migration/cra-to-vite/cra-runtime.ts +16 -0
- package/src/migration/cra-to-vite/cra-script.ts +61 -0
- package/src/migration/cra-to-vite/cra-to-vite.ts +25 -36
- package/src/migration/cra-to-vite/cra-typecheck.ts +33 -0
- package/src/migration/cra-to-vite/find-cra-config-blockers.ts +2 -1
- package/src/migration/cra-to-vite/index.ts +10 -2
- package/src/migration/cra-to-vite/migrate-cra-package.ts +127 -0
- package/src/migration/cra-to-vite/migrate-cra-svg-imports.ts +41 -0
- package/src/migration/cra-to-vite/migrate-cra-tests-to-vitest.ts +376 -0
- package/src/migration/cra-to-vite/move-public-index-html.ts +12 -7
- package/src/migration/cra-to-vite/plan-cra-to-vite.ts +283 -0
- package/src/migration/cra-to-vite/prepare-cra-package.ts +86 -0
- package/src/migration/cra-to-vite/remove-cra-configuration.ts +64 -0
- package/src/migration/cra-to-vite/rename-jsx-files.ts +155 -0
- package/src/migration/cra-to-vite/rename-process-env-react-app.ts +6 -3
- package/src/migration/cra-to-vite/rename-react-app-env-vars-in-env-files.ts +1 -1
- package/src/migration/cra-to-vite/replace-react-scripts-dependency.ts +52 -33
- package/src/migration/cra-to-vite/resolved-cra-dependency.ts +41 -0
- package/src/migration/cra-to-vite/scan-cra-projects.ts +57 -6
- package/src/migration/cra-to-vite/shared.ts +20 -10
- package/src/migration/cra-to-vite/svg-component.ts +39 -0
- package/src/migration/cra-to-vite/update-react-app-env-types.ts +11 -4
- package/src/migration/cra-to-vite/update-react-scripts-scripts.ts +22 -28
- package/src/react18/replace-reactdom-render.ts +17 -8
- package/src/testing/enzyme-constant-strings.ts +95 -0
- package/src/testing/enzyme-host-components.ts +6 -2
- package/src/testing/enzyme-portals.ts +279 -0
- package/src/testing/enzyme-selector-names.ts +59 -0
- package/src/testing/enzyme-to-react-testing-library.ts +425 -50
- package/src/testing/enzyme-usage.ts +29 -4
- package/src/testing/enzyme-wrapper-scope.ts +181 -30
- package/src/util/java-recipe-proxy.ts +81 -0
- package/src/util/module-imports.ts +6 -1
|
@@ -47,9 +47,8 @@ export function migrationReason(api: string): {reason: string, suggestedAction:
|
|
|
47
47
|
if (api === "shallow" || api === "dive") {
|
|
48
48
|
return {reason: "SHALLOW_RENDERING", suggestedAction: "Review child rendering and effects before replacing shallow rendering with RTL render."};
|
|
49
49
|
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
}
|
|
50
|
+
const internal = INTERNAL_STATE_ACTIONS[api];
|
|
51
|
+
if (internal) return {reason: "INTERNAL_STATE", suggestedAction: internal};
|
|
53
52
|
if (api === "simulate") {
|
|
54
53
|
return {reason: "UNSUPPORTED_EVENT", suggestedAction: "Review event payloads and async control flow before selecting user-event or fireEvent."};
|
|
55
54
|
}
|
|
@@ -65,9 +64,35 @@ export function migrationReason(api: string): {reason: string, suggestedAction:
|
|
|
65
64
|
if (api === "configure") {
|
|
66
65
|
return {reason: "ENZYME_SETUP", suggestedAction: "Keep Enzyme setup until no tests or helpers in the owning package need it."};
|
|
67
66
|
}
|
|
68
|
-
return {reason: "WRAPPER_API", suggestedAction:
|
|
67
|
+
return {reason: "WRAPPER_API", suggestedAction: WRAPPER_API_ACTIONS[api] ??
|
|
68
|
+
"Review this Enzyme wrapper API for an equivalent RTL interaction or DOM assertion."};
|
|
69
69
|
}
|
|
70
70
|
|
|
71
|
+
// What to write by hand for each, in RTL's terms: RTL has no component tree, so each becomes a DOM read or a user action
|
|
72
|
+
const INTERNAL_STATE_ACTIONS: Readonly<Record<string, string>> = {
|
|
73
|
+
state: "Assert what the state renders, such as text, a class, or an `aria-*` attribute, with `screen.getByText` or `getByRole`, instead of reading component state.",
|
|
74
|
+
setState: "Reach the state as a user does: fire the event that sets it with `fireEvent` or `userEvent`, or `rerender` with the props that lead to it.",
|
|
75
|
+
instance: "Trigger the method through the UI that calls it and assert the result in the DOM, or move its logic into a function and test that directly.",
|
|
76
|
+
invoke: "Fire the DOM event that calls the handler, with `fireEvent` or `userEvent`, instead of invoking the prop."
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
const WRAPPER_API_ACTIONS: Readonly<Record<string, string>> = {
|
|
80
|
+
prop: "Assert what the prop produces in the DOM, such as text, an attribute, or a role. For a callback prop, fire the event that calls it and assert on a `jest.fn()`.",
|
|
81
|
+
props: "Assert what the props produce in the DOM. To check what a child component receives, mock that component with `jest.mock` and assert on the mock's calls.",
|
|
82
|
+
setProps: "Call the `rerender` that RTL's `render` returns, with the element and its new props.",
|
|
83
|
+
text: "Read `textContent` of the queried element, or find it with `screen.getByText`.",
|
|
84
|
+
html: "Assert on `container.innerHTML` or the queried element's `outerHTML`; Enzyme's `html()` of a component is its rendered markup.",
|
|
85
|
+
at: "Index the `querySelectorAll` or `getAllBy*` result; for a component selector, query the DOM element the component renders instead.",
|
|
86
|
+
first: "Take the first element of the `querySelectorAll` or `getAllBy*` result, or use `querySelector` or `getBy*` for a single match.",
|
|
87
|
+
last: "Take the last element of the `querySelectorAll` or `getAllBy*` result.",
|
|
88
|
+
children: "Read the queried element's `children`, or query its descendants with `within(element)`.",
|
|
89
|
+
exists: "Use `queryBy*` with `toBeNull()` or `not.toBeNull()`, or test the length of a `querySelectorAll`.",
|
|
90
|
+
hasClass: "Use `element.classList.contains`, or `toHaveClass` from `@testing-library/jest-dom`.",
|
|
91
|
+
getDOMNode: "Query the element directly with `screen.getBy*` or `container.querySelector`; RTL already returns DOM nodes.",
|
|
92
|
+
update: "Remove it: RTL renders the latest state on every query. Await `findBy*` or `waitFor` where the change is asynchronous.",
|
|
93
|
+
unmount: "Call the `unmount` that RTL's `render` returns, or rely on RTL's automatic cleanup after each test."
|
|
94
|
+
};
|
|
95
|
+
|
|
71
96
|
export function enclosingScope(start: Cursor): string {
|
|
72
97
|
for (let cursor = start.parent; cursor; cursor = cursor.parent) {
|
|
73
98
|
const node = cursor.value as J;
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import {Cursor, ExecutionContext} from "@openrewrite/rewrite";
|
|
2
|
-
import {isObjectLiteral, JavaScriptVisitor, JS, referencesOf, walk} from "@openrewrite/rewrite/javascript";
|
|
2
|
+
import {compilationUnitOf, isObjectLiteral, JavaScriptVisitor, JS, JSX, referencesOf, walk} from "@openrewrite/rewrite/javascript";
|
|
3
3
|
import {emptySpace, Expression, isIdentifier, J} from "@openrewrite/rewrite/java";
|
|
4
4
|
import {create} from "mutative";
|
|
5
5
|
import {enzymeMethod, enzymeWrapper} from "./enzyme-usage";
|
|
6
|
+
import {importBindings} from "../util/module-imports";
|
|
6
7
|
|
|
7
8
|
const HTML_TAGS = new Set(["a", "abbr", "address", "area", "article", "aside", "audio", "b", "bdi", "bdo",
|
|
8
9
|
"blockquote", "body", "br", "button", "canvas", "caption", "cite", "code", "col", "colgroup", "data",
|
|
@@ -44,10 +45,10 @@ function compoundTags(value: string): (string | undefined)[] | undefined {
|
|
|
44
45
|
* descendants. Enzyme compares an attribute value strictly against the
|
|
45
46
|
* prop, while the DOM sees it stringified, so values that a number or boolean prop renders to are
|
|
46
47
|
* excluded. Enzyme additionally matches composite elements whose props satisfy a class, ID, or
|
|
47
|
-
* attribute compound
|
|
48
|
-
*
|
|
49
|
-
*
|
|
50
|
-
* combinators and pseudo-classes are excluded.
|
|
48
|
+
* attribute compound. In the last compound that only adds matches, so counts can only drop, which a
|
|
49
|
+
* converted assertion shows, while positions can shift, which {@link positional} guards. In an ancestor
|
|
50
|
+
* compound it can also lose them, which {@link selectorLiteral} guards. A composite element between a
|
|
51
|
+
* parent and its child breaks a child combinator, and other combinators and pseudo-classes are excluded.
|
|
51
52
|
*/
|
|
52
53
|
export function domSelector(value: unknown): value is string {
|
|
53
54
|
if (typeof value !== "string" || value === "") return false;
|
|
@@ -60,13 +61,45 @@ function onlyStringPropsRenderTo(value: string): boolean {
|
|
|
60
61
|
return value !== "true" && value !== "false" && String(Number(value)) !== value;
|
|
61
62
|
}
|
|
62
63
|
|
|
63
|
-
|
|
64
|
-
|
|
64
|
+
/**
|
|
65
|
+
* A selector literal {@link domSelector} accepts, whose ancestor compounds name a tag or only names that
|
|
66
|
+
* `hostNames` finds on host elements alone. A component that receives an ancestor compound's class or
|
|
67
|
+
* attribute passes it to the element it renders, as `<Button className="close">` does to its `button`:
|
|
68
|
+
* Enzyme then finds `.close button` and the DOM, where that `button` has the class itself, does not.
|
|
69
|
+
*/
|
|
70
|
+
export function selectorLiteral(node: J | undefined, hostNames: HostNames): node is J.Literal {
|
|
71
|
+
return node?.kind === J.Kind.Literal && acceptedSelector((node as J.Literal).value, hostNames);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** A selector string {@link selectorLiteral} accepts, however it is written. */
|
|
75
|
+
function acceptedSelector(value: unknown, hostNames: HostNames): value is string {
|
|
76
|
+
if (!domSelector(value)) return false;
|
|
77
|
+
const names = untaggedAncestorNames(value as string);
|
|
78
|
+
return names !== undefined && (names.length === 0 || hostNames(names));
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** Whether only host elements carry each of the classes, IDs, and attribute values. */
|
|
82
|
+
export type HostNames = (names: readonly string[]) => boolean;
|
|
83
|
+
|
|
84
|
+
const NAME = new RegExp(String.raw`[.#](-?[_a-zA-Z][\w-]*)|\[${ATTRIBUTE}(?:="([^"\\]*)"|='([^'\\]*)')?\]`, "g");
|
|
85
|
+
|
|
86
|
+
/** The names in the ancestor compounds of a selector that name no tag, or undefined where one tests an attribute's presence. */
|
|
87
|
+
function untaggedAncestorNames(selector: string): string[] | undefined {
|
|
88
|
+
const compounds = selector.split(" ").slice(0, -1).filter(compound => !/^[a-z]/.test(compound));
|
|
89
|
+
const names: string[] = [];
|
|
90
|
+
for (const compound of compounds) {
|
|
91
|
+
for (const [, name, double, single] of compound.matchAll(NAME)) {
|
|
92
|
+
const value = name ?? double ?? single;
|
|
93
|
+
if (value === undefined) return undefined;
|
|
94
|
+
names.push(value);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
return names;
|
|
65
98
|
}
|
|
66
99
|
|
|
67
100
|
/** The tag of the elements a selector matches. */
|
|
68
|
-
function selectorTag(selector:
|
|
69
|
-
const tags = compoundTags(selector
|
|
101
|
+
function selectorTag(selector: string): string | undefined {
|
|
102
|
+
const tags = compoundTags(selector);
|
|
70
103
|
return tags?.[tags.length - 1];
|
|
71
104
|
}
|
|
72
105
|
|
|
@@ -75,15 +108,27 @@ function selectorTag(selector: J.Literal): string | undefined {
|
|
|
75
108
|
* into the matches depends on: where every compound names a tag, or `hostNodes()` keeps the host
|
|
76
109
|
* elements a single compound matches.
|
|
77
110
|
*/
|
|
78
|
-
function positional(selector:
|
|
79
|
-
const tags = compoundTags(selector
|
|
111
|
+
function positional(selector: string, hostNodes: boolean): boolean {
|
|
112
|
+
const tags = compoundTags(selector)!;
|
|
80
113
|
return tags.every(tag => tag !== undefined) || (hostNodes && tags.length === 1);
|
|
81
114
|
}
|
|
82
115
|
|
|
116
|
+
/**
|
|
117
|
+
* Whether a `find(within)` from an element that `selector` matches finds only elements below it. Enzyme's
|
|
118
|
+
* `find` also tests the element it is called on, as `querySelectorAll` does not, and matches the compounds of
|
|
119
|
+
* a selector among the element and what is below it, where `querySelectorAll` matches the ancestor compounds
|
|
120
|
+
* anywhere in the document. So `within` is a single compound naming a tag other than the element's.
|
|
121
|
+
*/
|
|
122
|
+
function onlyBelow(selector: string, within: string): boolean {
|
|
123
|
+
const tag = selectorTag(selector);
|
|
124
|
+
const tags = compoundTags(within);
|
|
125
|
+
return tag !== undefined && tags?.length === 1 && tags[0] !== undefined && tags[0] !== tag;
|
|
126
|
+
}
|
|
127
|
+
|
|
83
128
|
// Enzyme's `simulate` calls the React handler directly, while `fireEvent` dispatches a DOM event that
|
|
84
129
|
// React 17 receives through its root container. A dispatched click also runs the default action, such
|
|
85
130
|
// 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",
|
|
131
|
+
export const SIMULATED_EVENTS: ReadonlySet<string> = new Set(["click", "doubleClick", "mouseDown", "mouseUp", "mouseEnter", "mouseLeave",
|
|
87
132
|
"mouseOver", "mouseOut", "mouseMove", "focus", "blur", "submit", "keyDown", "keyUp", "change", "touchStart",
|
|
88
133
|
"touchMove", "touchEnd", "wheel"]);
|
|
89
134
|
// Enzyme also accepts the DOM's lowercase names of these events, and `dblclick` for a double click
|
|
@@ -106,7 +151,7 @@ export function simpleChain(node: Expression): boolean {
|
|
|
106
151
|
return isIdentifier(node);
|
|
107
152
|
}
|
|
108
153
|
|
|
109
|
-
export type WrapperUse = "length" | "exists" | "positionExists" | "nodes" | "selectorText" | "html" | "text" | "hasClass" | "simulate" | "setProps";
|
|
154
|
+
export type WrapperUse = "length" | "exists" | "positionExists" | "nodes" | "selectorText" | "html" | "rootHtml" | "node" | "text" | "hasClass" | "simulate" | "setProps" | "snapshot";
|
|
110
155
|
|
|
111
156
|
/** `first` and `last` index like Enzyme's methods of the same name; a literal like `at(n)`. */
|
|
112
157
|
export type ElementIndex = J.Literal | "first" | "last";
|
|
@@ -114,10 +159,11 @@ export type ElementIndex = J.Literal | "first" | "last";
|
|
|
114
159
|
export interface WrapperRead {
|
|
115
160
|
use: WrapperUse;
|
|
116
161
|
wrapper: J.Identifier;
|
|
117
|
-
selector
|
|
162
|
+
/** The selector as the test writes it, a string literal or a template of constant strings. */
|
|
163
|
+
selector?: Expression;
|
|
118
164
|
index?: ElementIndex;
|
|
119
165
|
/** A `find` within the positioned element. */
|
|
120
|
-
within?:
|
|
166
|
+
within?: Expression;
|
|
121
167
|
/** Found nodes asserted only to be truthy, which any wrapper and any node list are. */
|
|
122
168
|
vacuous?: boolean;
|
|
123
169
|
/** A read outside an assertion, which throws where nothing is found, as Enzyme's single-node methods do. */
|
|
@@ -171,6 +217,24 @@ export function mountedElement(mount: J.MethodInvocation): boolean {
|
|
|
171
217
|
return mount.arguments.elements.length === 1 && element.element.kind !== J.Kind.Empty && element.element.kind !== JS.Kind.Spread;
|
|
172
218
|
}
|
|
173
219
|
|
|
220
|
+
/**
|
|
221
|
+
* Whether the element a mount renders passes a `ref` anywhere in it. A bare mount statement keeps no wrapper, so
|
|
222
|
+
* such a test renders to take the nodes or instances the ref receives. Enzyme never unmounts them, while RTL's
|
|
223
|
+
* cleanup unmounts every render after the test, which throws where the test moved them, as a test of a library
|
|
224
|
+
* that reparents fibers does.
|
|
225
|
+
*/
|
|
226
|
+
export function passesRef(mount: J.MethodInvocation): boolean {
|
|
227
|
+
let ref = false;
|
|
228
|
+
walk(mount.arguments.elements[0].element, tree => {
|
|
229
|
+
if (tree.kind === JS.Kind.JsxAttribute) {
|
|
230
|
+
const key = (tree as JSX.Attribute).key;
|
|
231
|
+
if (isIdentifier(key) && key.simpleName === "ref") ref = true;
|
|
232
|
+
}
|
|
233
|
+
return !ref;
|
|
234
|
+
});
|
|
235
|
+
return ref;
|
|
236
|
+
}
|
|
237
|
+
|
|
174
238
|
/** The matcher a node list can take the place of found nodes for, as `expect`'s subject. */
|
|
175
239
|
function nodesMatcher(expectCursor: Cursor): "length" | "truthy" | undefined {
|
|
176
240
|
const expect = expectCursor.value as J;
|
|
@@ -229,6 +293,41 @@ function nativeEventInit(event: string, init: J | undefined): [string, Expressio
|
|
|
229
293
|
return event !== "change" && properties?.size === 1 && nativeEvent ? [...nativeEvent] : undefined;
|
|
230
294
|
}
|
|
231
295
|
|
|
296
|
+
/**
|
|
297
|
+
* `expect(wrapper.html())` with a matcher RTL's `container.innerHTML` meets exactly where Enzyme's `html()` does. The two
|
|
298
|
+
* differ only where nothing renders, `null` against `""`, which a snapshot shows and a non-empty string fails either way.
|
|
299
|
+
*/
|
|
300
|
+
function innerHtmlAssertion(read: Cursor, html: J): boolean {
|
|
301
|
+
if (!expectArgument(read, html)) return false;
|
|
302
|
+
const expect = read.parentTree()!;
|
|
303
|
+
const matcherCursor = expect.parentTree();
|
|
304
|
+
const matcher = matcherCursor?.value as J | undefined;
|
|
305
|
+
if (!selectOf(matcher, expect.value) || !statement(matcherCursor!)) return false;
|
|
306
|
+
const args = (matcher as J.MethodInvocation).arguments.elements.filter(arg => arg.element.kind !== J.Kind.Empty).map(arg => arg.element);
|
|
307
|
+
if (call(matcher, "toMatchSnapshot")) return args.length === 0 || args.length === 1 && stringLiteral(args[0]);
|
|
308
|
+
return (call(matcher, "toBe") || call(matcher, "toEqual") || call(matcher, "toContain")) &&
|
|
309
|
+
args.length === 1 && stringLiteral(args[0]) && (args[0] as J.Literal).value !== "";
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/** `expect(() => wrapper.unmount()).not.toThrow();`, which unmounts where it stands, as the statement `wrapper.unmount();` does. */
|
|
313
|
+
function unmountsWithoutThrowing(unmount: Cursor): boolean {
|
|
314
|
+
const lambdaCursor = unmount.parentTree();
|
|
315
|
+
const lambda = lambdaCursor?.value as J.Lambda | undefined;
|
|
316
|
+
if (lambda?.kind !== J.Kind.Lambda || lambda.body !== unmount.value ||
|
|
317
|
+
lambda.parameters.parameters.some(parameter => parameter.element.kind !== J.Kind.Empty)) return false;
|
|
318
|
+
const arrowCursor = lambdaCursor!.parentTree();
|
|
319
|
+
const arrow = arrowCursor?.value as JS.ArrowFunction | undefined;
|
|
320
|
+
if (arrow?.kind !== JS.Kind.ArrowFunction || arrow.lambda !== lambda || !expectArgument(arrowCursor!, arrow)) return false;
|
|
321
|
+
const expectCursor = arrowCursor!.parentTree()!;
|
|
322
|
+
const not = expectCursor.parentTree()?.value as J | undefined;
|
|
323
|
+
if (not?.kind !== J.Kind.FieldAccess || (not as J.FieldAccess).target !== expectCursor.value ||
|
|
324
|
+
(not as J.FieldAccess).name.element.simpleName !== "not") return false;
|
|
325
|
+
const matcherCursor = expectCursor.parentTree()!.parentTree();
|
|
326
|
+
const matcher = matcherCursor?.value as J | undefined;
|
|
327
|
+
return call(matcher, "toThrow") && (matcher as J.MethodInvocation).select?.element === not &&
|
|
328
|
+
noArguments(matcher as J.MethodInvocation) && statement(matcherCursor!) !== undefined;
|
|
329
|
+
}
|
|
330
|
+
|
|
232
331
|
function expectArgument(cursor: Cursor, node: J): boolean {
|
|
233
332
|
const parent = cursor.parentTree()?.value as J | undefined;
|
|
234
333
|
if (!call(parent, "expect")) return false;
|
|
@@ -265,17 +364,28 @@ export function wrapperDeclaration<R>(statement: J | undefined, classify: (rende
|
|
|
265
364
|
/**
|
|
266
365
|
* Proves that every reference to a block-scoped wrapper is a supported DOM read, so the
|
|
267
366
|
* declaration and all its uses can be converted together. Any other reference, including
|
|
268
|
-
* arguments, returns, and shorthand properties, rejects the whole scope. `events`
|
|
269
|
-
* and `html` admits `html()
|
|
367
|
+
* arguments, returns, and shorthand properties, rejects the whole scope. `events` are those `simulate`
|
|
368
|
+
* may dispatch, and `html` admits `html()` and the DOM node of a host element, none of which a shallow wrapper reads as the DOM does. `snapshots`
|
|
369
|
+
* admits `expect(wrapper).toMatchSnapshot()` and `expect(toJson(wrapper)).toMatchSnapshot()`, whose
|
|
370
|
+
* snapshot RTL records as the DOM rather than Enzyme's tree. `hostNames` decides the selectors {@link selectorLiteral} accepts.
|
|
270
371
|
*/
|
|
271
372
|
export function analyzeWrapperScope(block: Cursor, {declaration, name}: WrapperDeclaration,
|
|
272
|
-
{events, html, untaggedPositions}: {events:
|
|
373
|
+
{events, html, untaggedPositions, hostNames, snapshots = false, constant = () => undefined}: {events: ReadonlySet<string>, html: boolean, untaggedPositions: boolean, hostNames: HostNames, snapshots?: boolean, constant?: (node: J) => string | undefined}): WrapperScope | undefined {
|
|
374
|
+
let serializers: ReadonlySet<string> | undefined;
|
|
273
375
|
const uses = new Map<string, WrapperRead>();
|
|
274
376
|
const removedStatements = new Set<string>();
|
|
275
377
|
const unmounts = new Set<string>();
|
|
276
378
|
let unmounted = false;
|
|
277
379
|
const variable = declaration.variables[0].element;
|
|
278
380
|
|
|
381
|
+
// The string a selector argument holds where Enzyme and the DOM read it alike, written as a literal or built from constants
|
|
382
|
+
const selectorValue = (node: J | undefined): string | undefined => {
|
|
383
|
+
if (!node) return undefined;
|
|
384
|
+
if (selectorLiteral(node, hostNames)) return node.value as string;
|
|
385
|
+
const value = node.kind === JS.Kind.TemplateExpression ? constant(node) : undefined;
|
|
386
|
+
return acceptedSelector(value, hostNames) ? value : undefined;
|
|
387
|
+
};
|
|
388
|
+
|
|
279
389
|
const record = (node: Expression, read: WrapperRead): boolean => {
|
|
280
390
|
if (!simpleChain(node)) return false;
|
|
281
391
|
uses.set(node.id, read);
|
|
@@ -298,12 +408,18 @@ export function analyzeWrapperScope(block: Cursor, {declaration, name}: WrapperD
|
|
|
298
408
|
const ancestors: Cursor[] = [];
|
|
299
409
|
for (let c: Cursor | undefined = reference.parentTree(); c && ancestors.length < 5; c = c.parentTree()) ancestors.push(c);
|
|
300
410
|
const first = ancestors[0]?.value as J | undefined;
|
|
411
|
+
if (snapshots) {
|
|
412
|
+
serializers ??= snapshotSerializers(block);
|
|
413
|
+
const assertion = snapshotAssertion(identifier, ancestors, serializers);
|
|
414
|
+
if (assertion) return record(assertion, {wrapper: identifier, use: "snapshot"});
|
|
415
|
+
}
|
|
301
416
|
if (!selectOf(first, identifier)) return false;
|
|
302
417
|
const method = first as J.MethodInvocation;
|
|
303
418
|
if (call(method, "find")) {
|
|
304
419
|
const args = method.arguments.elements;
|
|
305
|
-
|
|
306
|
-
|
|
420
|
+
const selectorText = args.length === 1 ? selectorValue(args[0].element) : undefined;
|
|
421
|
+
if (selectorText === undefined) return false;
|
|
422
|
+
const selector = args[0].element as Expression;
|
|
307
423
|
let k = 1;
|
|
308
424
|
let inner: J = method;
|
|
309
425
|
let hostNodes = false;
|
|
@@ -312,15 +428,16 @@ export function analyzeWrapperScope(block: Cursor, {declaration, name}: WrapperD
|
|
|
312
428
|
hostNodes = true;
|
|
313
429
|
}
|
|
314
430
|
const index = elementIndex(ancestors[k]?.value, inner);
|
|
315
|
-
let within:
|
|
431
|
+
let within: Expression | undefined;
|
|
316
432
|
if (index) {
|
|
317
|
-
if (!untaggedPositions && !positional(
|
|
433
|
+
if (!untaggedPositions && !positional(selectorText, hostNodes)) return false;
|
|
318
434
|
inner = ancestors[k++].value;
|
|
319
435
|
const nested = ancestors[k]?.value as J | undefined;
|
|
320
436
|
if (call(nested, "find") && selectOf(nested, inner)) {
|
|
321
437
|
const [argument] = (nested as J.MethodInvocation).arguments.elements;
|
|
322
|
-
|
|
323
|
-
|
|
438
|
+
const withinText = (nested as J.MethodInvocation).arguments.elements.length === 1 ? selectorValue(argument.element) : undefined;
|
|
439
|
+
if (withinText === undefined || !onlyBelow(selectorText, withinText)) return false;
|
|
440
|
+
within = argument.element as Expression;
|
|
324
441
|
inner = ancestors[k++].value;
|
|
325
442
|
}
|
|
326
443
|
}
|
|
@@ -340,6 +457,10 @@ export function analyzeWrapperScope(block: Cursor, {declaration, name}: WrapperD
|
|
|
340
457
|
const terminalArgs = terminal.arguments.elements.filter(arg => arg.element.kind !== J.Kind.Empty).map(arg => arg.element);
|
|
341
458
|
if (call(terminal, "exists") && terminalArgs.length === 0) return record(terminal, {...read, use: !index || within ? "exists" : "positionExists"});
|
|
342
459
|
if (within) return false;
|
|
460
|
+
// A mounted host element's instance is its DOM node, as is the node `getDOMNode` returns; a shallow one has neither
|
|
461
|
+
if (html && (call(terminal, "instance") || call(terminal, "getDOMNode")) && terminalArgs.length === 0 && positional(selectorText, hostNodes)) {
|
|
462
|
+
return record(terminal, {...read, use: "node"});
|
|
463
|
+
}
|
|
343
464
|
if ((call(terminal, "text") || html && call(terminal, "html")) && terminalArgs.length === 0) {
|
|
344
465
|
return record(terminal, {...read, use: call(terminal, "text") ? "selectorText" : "html",
|
|
345
466
|
unasserted: !expectArgument(outerCursor, terminal)});
|
|
@@ -348,33 +469,34 @@ export function analyzeWrapperScope(block: Cursor, {declaration, name}: WrapperD
|
|
|
348
469
|
/^-?[_a-zA-Z][\w-]*$/.test(terminalArgs[0].value as string)) {
|
|
349
470
|
return record(terminal, {...read, use: "hasClass", className: terminalArgs[0]});
|
|
350
471
|
}
|
|
351
|
-
if (call(terminal, "simulate") && events && statement(outerCursor) && terminalArgs.length >= 1 && terminalArgs.length <= 2 &&
|
|
472
|
+
if (call(terminal, "simulate") && events.size > 0 && statement(outerCursor) && terminalArgs.length >= 1 && terminalArgs.length <= 2 &&
|
|
352
473
|
stringLiteral(terminalArgs[0])) {
|
|
353
474
|
const name = terminalArgs[0].value as string;
|
|
354
475
|
const event = SIMULATED_EVENTS.has(name) ? name : NATIVE_EVENT_NAMES.get(name);
|
|
355
|
-
if (!event) return false;
|
|
476
|
+
if (!event || !events.has(event)) return false;
|
|
356
477
|
const nativeEvent = nativeEventInit(event, terminalArgs[1]);
|
|
357
478
|
if (!nativeEvent && !eventInit(event, terminalArgs[1])) return false;
|
|
358
|
-
if (event === "click" && CLICK_DEFAULT_ACTIONS.has(selectorTag(
|
|
479
|
+
if (event === "click" && CLICK_DEFAULT_ACTIONS.has(selectorTag(selectorText) ?? "")) return false;
|
|
359
480
|
return record(terminal, {...read, use: "simulate", event, ...nativeEvent ? {nativeEvent} : {eventInit: terminalArgs[1]}});
|
|
360
481
|
}
|
|
361
482
|
return false;
|
|
362
483
|
}
|
|
363
484
|
const [argument] = method.arguments.elements;
|
|
364
|
-
if (call(method, "exists") && method.arguments.elements.length === 1 &&
|
|
365
|
-
return record(method, {wrapper: identifier, selector: argument.element, use: "exists"});
|
|
485
|
+
if (call(method, "exists") && method.arguments.elements.length === 1 && selectorValue(argument.element) !== undefined) {
|
|
486
|
+
return record(method, {wrapper: identifier, selector: argument.element as Expression, use: "exists"});
|
|
366
487
|
}
|
|
367
488
|
if (call(method, "setProps") && method.arguments.elements.length === 1 && statement(ancestors[0]) &&
|
|
368
489
|
argument.element.kind !== J.Kind.Empty && argument.element.kind !== JS.Kind.Spread) {
|
|
369
490
|
return record(method, {wrapper: identifier, use: "setProps", props: argument.element as Expression});
|
|
370
491
|
}
|
|
371
492
|
if (!noArguments(method)) return false;
|
|
493
|
+
if (html && call(method, "html") && innerHtmlAssertion(ancestors[0], method)) return record(method, {wrapper: identifier, use: "rootHtml"});
|
|
372
494
|
if (call(method, "text")) return record(method, {wrapper: identifier, use: "text", unasserted: !expectArgument(ancestors[0], method)});
|
|
373
495
|
if (call(method, "update") && statement(ancestors[0])?.prefix.comments.length === 0 && method.prefix.comments.length === 0) {
|
|
374
496
|
removedStatements.add(method.id);
|
|
375
497
|
return simpleChain(method);
|
|
376
498
|
}
|
|
377
|
-
if (call(method, "unmount") && statement(ancestors[0])) {
|
|
499
|
+
if (call(method, "unmount") && (statement(ancestors[0]) || unmountsWithoutThrowing(ancestors[0]))) {
|
|
378
500
|
unmounted = true;
|
|
379
501
|
unmounts.add(method.id);
|
|
380
502
|
return simpleChain(method);
|
|
@@ -392,6 +514,35 @@ export function analyzeWrapperScope(block: Cursor, {declaration, name}: WrapperD
|
|
|
392
514
|
return {declaration, name, uses, removedStatements, unmounts};
|
|
393
515
|
}
|
|
394
516
|
|
|
517
|
+
/** The local names a source binds to `enzyme-to-json`'s serializers, such as its default export `toJson`. */
|
|
518
|
+
function snapshotSerializers(cursor: Cursor): ReadonlySet<string> {
|
|
519
|
+
const cu = compilationUnitOf(cursor);
|
|
520
|
+
return new Set(cu ? importBindings(cu).filter(({module}) => module === "enzyme-to-json").map(({local}) => local) : []);
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
/**
|
|
524
|
+
* The `expect(wrapper)` or `expect(toJson(wrapper))` call of a `toMatchSnapshot()` assertion of the
|
|
525
|
+
* wrapper, with no property matcher and at most a snapshot name.
|
|
526
|
+
*/
|
|
527
|
+
function snapshotAssertion(identifier: J.Identifier, ancestors: Cursor[], serializers: ReadonlySet<string>): J.MethodInvocation | undefined {
|
|
528
|
+
let k = 0;
|
|
529
|
+
const onlyArgument = (call: J | undefined, argument: J) => call?.kind === J.Kind.MethodInvocation && !(call as J.MethodInvocation).select &&
|
|
530
|
+
(call as J.MethodInvocation).arguments.elements.length === 1 && (call as J.MethodInvocation).arguments.elements[0].element === argument;
|
|
531
|
+
let subject: J = identifier;
|
|
532
|
+
const serialized = ancestors[k]?.value as J | undefined;
|
|
533
|
+
if (onlyArgument(serialized, subject) && serializers.has((serialized as J.MethodInvocation).name.simpleName)) {
|
|
534
|
+
subject = serialized!;
|
|
535
|
+
k++;
|
|
536
|
+
}
|
|
537
|
+
const expectCall = ancestors[k]?.value as J | undefined;
|
|
538
|
+
if (!onlyArgument(expectCall, subject) || (expectCall as J.MethodInvocation).name.simpleName !== "expect") return undefined;
|
|
539
|
+
const matcher = ancestors[k + 1]?.value as J | undefined;
|
|
540
|
+
if (!call(matcher, "toMatchSnapshot") || !selectOf(matcher, expectCall!) || !statement(ancestors[k + 1])) return undefined;
|
|
541
|
+
const args = (matcher as J.MethodInvocation).arguments.elements.filter(arg => arg.element.kind !== J.Kind.Empty);
|
|
542
|
+
if (args.length > 1 || (args.length === 1 && !stringLiteral(args[0].element))) return undefined;
|
|
543
|
+
return expectCall as J.MethodInvocation;
|
|
544
|
+
}
|
|
545
|
+
|
|
395
546
|
/** The call a chain of calls and field accesses starts with, such as `mount(...)` in `mount(...).find(...).length`. */
|
|
396
547
|
export function innermostCall(node: J): J.MethodInvocation | undefined {
|
|
397
548
|
let call: J.MethodInvocation | undefined;
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import {check, Cursor, ExecutionContext, or, RecipeRef, ScanningRecipe, SourceFile, Tree, TreeVisitor} from "@openrewrite/rewrite";
|
|
2
|
+
import {DelegatingRecipe} from "@openrewrite/rewrite/rpc";
|
|
3
|
+
|
|
4
|
+
/** The host's own tree-type gate, the one it derives for a JsonVisitor or JavaScriptVisitor. */
|
|
5
|
+
export const treesOfType = (type: string) => new RecipeRef("org.openrewrite.rpc.internal.FindTreesOfType", {type});
|
|
6
|
+
|
|
7
|
+
// The files the dependency delegates normally send to Java.
|
|
8
|
+
const NODE_MANIFESTS = /(^|\/)(package\.json|package-lock\.json|npm-shrinkwrap\.json|yarn\.lock|pnpm-lock\.yaml|pnpm-workspace\.yaml|bun\.lock)$/;
|
|
9
|
+
const NODE_MANIFEST_TREES = ["org.openrewrite.json.tree.Json", "org.openrewrite.yaml.tree.Yaml", "org.openrewrite.text.PlainText"];
|
|
10
|
+
|
|
11
|
+
// Native lock transactions also need package-manager policy, patches and hooks they must refuse.
|
|
12
|
+
const NODE_INPUTS = /(^|\/)(\.npmrc|\.yarnrc(?:\.yml)?|bunfig\.toml|\.?pnpmfile\.cjs)$|\.patch$/;
|
|
13
|
+
|
|
14
|
+
export function isNodeDependencyInput(path: string, includeInputs = false): boolean {
|
|
15
|
+
return NODE_MANIFESTS.test(path) || includeInputs && NODE_INPUTS.test(path);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Runs a Java npm dependency recipe through this side's RPC connection rather than as a stand-in the host
|
|
20
|
+
* builds itself, so the host prepares it from its class with every option.
|
|
21
|
+
*/
|
|
22
|
+
export abstract class JavaDependencyRecipe extends ScanningRecipe<number> {
|
|
23
|
+
// HACK: not a `DelegatingRecipe` itself, which the host would build from its marketplace listing. A listing
|
|
24
|
+
// older than the recipe drops the options it does not name: Moderne's lists rewrite-javascript 8.94's
|
|
25
|
+
// `AddDependency`, without `onlyIfDeclaring` and `onlyIfResolving`.
|
|
26
|
+
// An ordinary field: `PrepareRecipe` copies a recipe whose scanner it gates, and a copy has no `#private` field
|
|
27
|
+
protected readonly java: DelegatingRecipe;
|
|
28
|
+
|
|
29
|
+
protected constructor(javaRecipeName: string, options: object, private readonly includePackageManagerInputs = false) {
|
|
30
|
+
super();
|
|
31
|
+
this.java = new DelegatingRecipe(javaRecipeName, {...options});
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
initialValue(): number {
|
|
35
|
+
return 0;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
async scanner(acc: number): Promise<TreeVisitor<any, ExecutionContext>> {
|
|
39
|
+
return manifestsOnly(async () => this.java.scanner(acc), manifest => this.scanned(manifest), this.includePackageManagerInputs);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Called with each manifest as it is scanned; one it returns false for is kept from the Java recipe, which then edits nothing for it. */
|
|
43
|
+
protected scanned(_manifest: SourceFile): boolean {
|
|
44
|
+
return true;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
async editorWithData(acc: number): Promise<TreeVisitor<any, ExecutionContext>> {
|
|
48
|
+
return manifestsOnly(async () => this.java.editorWithData(acc), undefined, this.includePackageManagerInputs);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async generate(acc: number, ctx: ExecutionContext): Promise<SourceFile[]> {
|
|
52
|
+
return this.java.generate(acc, ctx);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
async onComplete(ctx: ExecutionContext): Promise<void> {
|
|
56
|
+
await this.java.onComplete(ctx);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// The remote visitor is prepared on first use, since the host prepares this recipe's visitors before a run
|
|
61
|
+
async function manifestsOnly(remote: () => Promise<TreeVisitor<any, ExecutionContext>>, admit?: (manifest: SourceFile) => boolean, includeInputs = false): Promise<TreeVisitor<any, ExecutionContext>> {
|
|
62
|
+
const accepts = (path: string) => isNodeDependencyInput(path, includeInputs);
|
|
63
|
+
let visitor: Promise<TreeVisitor<any, ExecutionContext>> | undefined;
|
|
64
|
+
const manifests = new class extends TreeVisitor<any, ExecutionContext> {
|
|
65
|
+
override async isAcceptable(sourceFile: SourceFile): Promise<boolean> {
|
|
66
|
+
return accepts(sourceFile.sourcePath);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
override async visit<R extends Tree>(tree: Tree, ctx: ExecutionContext, parent?: Cursor): Promise<R | undefined> {
|
|
70
|
+
if (!accepts((tree as SourceFile).sourcePath ?? "")) return tree as R;
|
|
71
|
+
if (admit && !admit(tree as SourceFile)) return tree as R;
|
|
72
|
+
visitor ??= remote();
|
|
73
|
+
return await (await visitor).visit<R>(tree, ctx, parent);
|
|
74
|
+
}
|
|
75
|
+
}();
|
|
76
|
+
// HACK: the host infers which trees to send only from a typed visitor, and sends a plain TreeVisitor every
|
|
77
|
+
// tree, so this names the types for it. It reads the gate of a scanner only (openrewrite/rewrite#9734).
|
|
78
|
+
// Config and hook files can have any parser type; a plain visitor avoids requiring optional Java parser modules.
|
|
79
|
+
if (includeInputs) return manifests;
|
|
80
|
+
return await check(or(...NODE_MANIFEST_TREES.map(treesOfType)), manifests);
|
|
81
|
+
}
|
|
@@ -47,9 +47,14 @@ export function namespaceImports(cu: JS.CompilationUnit): string[] {
|
|
|
47
47
|
// Node resolves a relative specifier to the file before the directory's index
|
|
48
48
|
const RESOLVED = ["", ".js", ".jsx", ".ts", ".tsx", "/index.js", "/index.jsx", "/index.ts", "/index.tsx"];
|
|
49
49
|
|
|
50
|
+
/** Whether a module specifier names a file by a path relative to the importing source. */
|
|
51
|
+
export function isRelativeSpecifier(specifier: string): boolean {
|
|
52
|
+
return /^\.\.?(?:\/|$)/.test(specifier);
|
|
53
|
+
}
|
|
54
|
+
|
|
50
55
|
/** The source a relative module specifier resolves to from the source at `from`, as Node resolves it among `sources`. */
|
|
51
56
|
export function resolveRelative(from: string, specifier: string, sources: {has(path: string): boolean}): string | undefined {
|
|
52
|
-
if (
|
|
57
|
+
if (!isRelativeSpecifier(specifier)) return undefined;
|
|
53
58
|
const base = posix.normalize(posix.join(posix.dirname(from), specifier));
|
|
54
59
|
return RESOLVED.map(suffix => base + suffix).find(path => sources.has(path));
|
|
55
60
|
}
|