@openrewrite/recipes-react 0.15.0 → 0.16.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/migration/cra-to-vite/find-cra-config-blockers.js +20 -28
- package/dist/migration/cra-to-vite/find-cra-config-blockers.js.map +1 -1
- package/dist/migration/cra-to-vite/replace-react-scripts-dependency.d.ts.map +1 -1
- package/dist/migration/cra-to-vite/replace-react-scripts-dependency.js +13 -33
- package/dist/migration/cra-to-vite/replace-react-scripts-dependency.js.map +1 -1
- package/dist/migration/cra-to-vite/shared.d.ts +1 -14
- package/dist/migration/cra-to-vite/shared.d.ts.map +1 -1
- package/dist/migration/cra-to-vite/shared.js +13 -126
- package/dist/migration/cra-to-vite/shared.js.map +1 -1
- package/dist/migration/cra-to-vite/update-react-scripts-scripts.d.ts.map +1 -1
- package/dist/migration/cra-to-vite/update-react-scripts-scripts.js +16 -21
- package/dist/migration/cra-to-vite/update-react-scripts-scripts.js.map +1 -1
- package/dist/migration/cra-to-vite/update-tsconfig-for-vite.d.ts.map +1 -1
- package/dist/migration/cra-to-vite/update-tsconfig-for-vite.js +4 -13
- package/dist/migration/cra-to-vite/update-tsconfig-for-vite.js.map +1 -1
- package/dist/react17/migrate-deprecated-lifecycles.js +1 -1
- package/dist/react17/migrate-deprecated-lifecycles.js.map +1 -1
- package/dist/react17/use-new-jsx-transform.d.ts.map +1 -1
- package/dist/react17/use-new-jsx-transform.js +30 -78
- package/dist/react17/use-new-jsx-transform.js.map +1 -1
- package/dist/react18/replace-unmount-component-at-node.d.ts.map +1 -1
- package/dist/react18/replace-unmount-component-at-node.js +62 -20
- package/dist/react18/replace-unmount-component-at-node.js.map +1 -1
- package/dist/react19/replace-default-props.d.ts.map +1 -1
- package/dist/react19/replace-default-props.js +10 -13
- package/dist/react19/replace-default-props.js.map +1 -1
- package/dist/react19/replace-react-shallow-renderer.d.ts.map +1 -1
- package/dist/react19/replace-react-shallow-renderer.js +1 -6
- package/dist/react19/replace-react-shallow-renderer.js.map +1 -1
- package/dist/refactoring/create-class-to-es6.d.ts.map +1 -1
- package/dist/refactoring/create-class-to-es6.js +4 -7
- package/dist/refactoring/create-class-to-es6.js.map +1 -1
- package/dist/refactoring/create-element-to-jsx.d.ts.map +1 -1
- package/dist/refactoring/create-element-to-jsx.js +3 -4
- package/dist/refactoring/create-element-to-jsx.js.map +1 -1
- package/dist/refactoring/prop-types-to-typescript.js +2 -3
- package/dist/refactoring/prop-types-to-typescript.js.map +1 -1
- package/dist/refactoring/pure-render-mixin.d.ts.map +1 -1
- package/dist/refactoring/pure-render-mixin.js +3 -6
- package/dist/refactoring/pure-render-mixin.js.map +1 -1
- package/dist/testing/enzyme-host-components.d.ts +14 -0
- package/dist/testing/enzyme-host-components.d.ts.map +1 -0
- package/dist/testing/enzyme-host-components.js +245 -0
- package/dist/testing/enzyme-host-components.js.map +1 -0
- package/dist/testing/enzyme-to-react-testing-library.d.ts +10 -5
- package/dist/testing/enzyme-to-react-testing-library.d.ts.map +1 -1
- package/dist/testing/enzyme-to-react-testing-library.js +138 -113
- package/dist/testing/enzyme-to-react-testing-library.js.map +1 -1
- package/dist/testing/enzyme-wrapper-scope.d.ts +13 -4
- package/dist/testing/enzyme-wrapper-scope.d.ts.map +1 -1
- package/dist/testing/enzyme-wrapper-scope.js +161 -163
- package/dist/testing/enzyme-wrapper-scope.js.map +1 -1
- package/dist/util/module-imports.d.ts +12 -0
- package/dist/util/module-imports.d.ts.map +1 -0
- package/dist/util/module-imports.js +56 -0
- package/dist/util/module-imports.js.map +1 -0
- package/dist/util/react-class.d.ts +1 -0
- package/dist/util/react-class.d.ts.map +1 -1
- package/dist/util/react-class.js +3 -2
- package/dist/util/react-class.js.map +1 -1
- package/package.json +3 -3
- package/src/migration/cra-to-vite/find-cra-config-blockers.ts +21 -27
- package/src/migration/cra-to-vite/replace-react-scripts-dependency.ts +14 -39
- package/src/migration/cra-to-vite/shared.ts +15 -136
- package/src/migration/cra-to-vite/update-react-scripts-scripts.ts +16 -21
- package/src/migration/cra-to-vite/update-tsconfig-for-vite.ts +6 -13
- package/src/react17/migrate-deprecated-lifecycles.ts +2 -2
- package/src/react17/use-new-jsx-transform.ts +30 -72
- package/src/react18/replace-unmount-component-at-node.ts +78 -32
- package/src/react19/replace-default-props.ts +11 -15
- package/src/react19/replace-react-shallow-renderer.ts +2 -7
- package/src/refactoring/create-class-to-es6.ts +6 -9
- package/src/refactoring/create-element-to-jsx.ts +4 -6
- package/src/refactoring/prop-types-to-typescript.ts +4 -4
- package/src/refactoring/pure-render-mixin.ts +4 -7
- package/src/testing/enzyme-host-components.ts +283 -0
- package/src/testing/enzyme-to-react-testing-library.ts +176 -107
- package/src/testing/enzyme-wrapper-scope.ts +144 -137
- package/src/util/module-imports.ts +55 -0
- package/src/util/react-class.ts +2 -1
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
import {JS, JSX, namesReferencedWithin, walk} from "@openrewrite/rewrite/javascript";
|
|
2
|
+
import {Expression, isIdentifier, J, Type} from "@openrewrite/rewrite/java";
|
|
3
|
+
import {COMPONENT_BASES, unwrapBaseType} from "../util/react-class";
|
|
4
|
+
import {importBindings, namespaceImports, resolveRelative} from "../util/module-imports";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Each scanned JavaScript or TypeScript source mapped to the exports, `default` for the default
|
|
8
|
+
* export, of the components it declares that render host elements only. A source declaring none
|
|
9
|
+
* maps to an empty set, so that module resolution still stops at it.
|
|
10
|
+
*/
|
|
11
|
+
export type HostComponents = Map<string, ReadonlySet<string>>;
|
|
12
|
+
|
|
13
|
+
const NONE: ReadonlySet<string> = new Set();
|
|
14
|
+
|
|
15
|
+
/** A local name bound to an export of another source in the same repository. */
|
|
16
|
+
export interface ImportedExport {
|
|
17
|
+
path: string;
|
|
18
|
+
name: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// Each renders what a JSX tag in the same file does not show, or, for the effects and refs, runs only
|
|
22
|
+
// in a full render: Enzyme's shallow renderer does not run `useEffect` or `useLayoutEffect`, attaches
|
|
23
|
+
// no ref, and does not parse `dangerouslySetInnerHTML` into elements that a selector can match.
|
|
24
|
+
const RENDERS_OTHERWISE = new Set(["createElement", "cloneElement", "createPortal", "lazy",
|
|
25
|
+
"useEffect", "useLayoutEffect", "useInsertionEffect", "ref", "dangerouslySetInnerHTML"]);
|
|
26
|
+
|
|
27
|
+
// React's hooks that behave alike in a shallow and a full render. Any other hook, such as a custom
|
|
28
|
+
// one declared elsewhere, may run an effect that this source does not show.
|
|
29
|
+
const SAME_IN_SHALLOW = new Set(["useState", "useReducer", "useMemo", "useCallback", "useRef", "useContext",
|
|
30
|
+
"useId", "useDebugValue", "useTransition", "useDeferredValue", "useSyncExternalStore"]);
|
|
31
|
+
|
|
32
|
+
const HOOK = /^use[A-Z0-9]/;
|
|
33
|
+
|
|
34
|
+
const FRAGMENT = new Set(["Fragment"]);
|
|
35
|
+
|
|
36
|
+
/** A lowercase tag is an intrinsic element and an empty one a fragment, which renders its children. */
|
|
37
|
+
function hostTag(tag: JSX.Tag): boolean {
|
|
38
|
+
const name = tag.openName.element;
|
|
39
|
+
if (name.kind === J.Kind.Empty) return true;
|
|
40
|
+
if (isIdentifier(name)) return /^[a-z]/.test(name.simpleName) || FRAGMENT.has(name.simpleName);
|
|
41
|
+
return reactMember(name, FRAGMENT);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function primitive(type: Type | undefined): boolean {
|
|
45
|
+
if (Type.isPrimitive(type)) return true;
|
|
46
|
+
return Type.isUnion(type) && type.bounds.length > 0 && type.bounds.every(primitive);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function rootName(node: Expression): string | undefined {
|
|
50
|
+
if (isIdentifier(node)) return node.simpleName;
|
|
51
|
+
if (node.kind === J.Kind.FieldAccess) return rootName((node as J.FieldAccess).target);
|
|
52
|
+
return undefined;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Whether an expression a host element renders as a child holds no element of a component: a JSX
|
|
57
|
+
* tag, whose own name is checked separately, a primitive, a branch of either, a `map` whose
|
|
58
|
+
* callback is declared in the source, or a value that does not come from an import, such as a prop or
|
|
59
|
+
* `children`, which the rendering test supplies.
|
|
60
|
+
*/
|
|
61
|
+
function rendersAsHost(node: Expression, imports: ReadonlySet<string>): boolean {
|
|
62
|
+
switch (node.kind) {
|
|
63
|
+
case JS.Kind.JsxTag:
|
|
64
|
+
case J.Kind.Literal:
|
|
65
|
+
case J.Kind.Empty:
|
|
66
|
+
case JS.Kind.TemplateExpression:
|
|
67
|
+
return true;
|
|
68
|
+
case J.Kind.Parentheses:
|
|
69
|
+
return rendersAsHost((node as J.Parentheses<Expression>).tree.element, imports);
|
|
70
|
+
case J.Kind.Ternary: {
|
|
71
|
+
const ternary = node as J.Ternary;
|
|
72
|
+
return rendersAsHost(ternary.truePart.element, imports) && rendersAsHost(ternary.falsePart.element, imports);
|
|
73
|
+
}
|
|
74
|
+
case J.Kind.Binary: {
|
|
75
|
+
const binary = node as J.Binary;
|
|
76
|
+
// `condition && <tag />` renders the condition only where it is falsy
|
|
77
|
+
if (binary.operator.element === "And" as J.Binary.Type) return rendersAsHost(binary.right, imports);
|
|
78
|
+
return rendersAsHost(binary.left, imports) && rendersAsHost(binary.right, imports);
|
|
79
|
+
}
|
|
80
|
+
case JS.Kind.Binary:
|
|
81
|
+
return rendersAsHost((node as JS.Binary).left, imports) && rendersAsHost((node as JS.Binary).right, imports);
|
|
82
|
+
case J.Kind.Identifier:
|
|
83
|
+
case J.Kind.FieldAccess: {
|
|
84
|
+
const name = rootName(node);
|
|
85
|
+
return primitive((node as J.Identifier | J.FieldAccess).type) || (name !== undefined && !imports.has(name));
|
|
86
|
+
}
|
|
87
|
+
case J.Kind.MethodInvocation: {
|
|
88
|
+
const call = node as J.MethodInvocation;
|
|
89
|
+
// A callback declared elsewhere, imported or not, may return any element
|
|
90
|
+
return call.name.simpleName === "map" && call.select !== undefined && call.arguments.elements.every(({element}) =>
|
|
91
|
+
element.kind === JS.Kind.ArrowFunction && returnsAsHost((element as JS.ArrowFunction).lambda.body, imports)) ||
|
|
92
|
+
primitive(call.methodType?.returnType);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
return primitive((node as Expression & {type?: Type}).type);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const FUNCTIONS = new Set<string>([JS.Kind.ArrowFunction, J.Kind.Lambda, J.Kind.MethodDeclaration, J.Kind.ClassDeclaration]);
|
|
99
|
+
|
|
100
|
+
/** Whether what a function returns renders as host elements: its expression body, or each `return` of its own. */
|
|
101
|
+
function returnsAsHost(body: J | undefined, imports: ReadonlySet<string>): boolean {
|
|
102
|
+
if (!body) return false;
|
|
103
|
+
if (body.kind !== J.Kind.Block) return rendersAsHost(body as Expression, imports);
|
|
104
|
+
let host = true;
|
|
105
|
+
walk(body, tree => {
|
|
106
|
+
if (!host || tree !== body && FUNCTIONS.has(tree.kind)) return false;
|
|
107
|
+
if (tree.kind === J.Kind.Return) {
|
|
108
|
+
const returned = (tree as J.Return).expression;
|
|
109
|
+
host = !returned || rendersAsHost(returned, imports);
|
|
110
|
+
}
|
|
111
|
+
return host;
|
|
112
|
+
});
|
|
113
|
+
return host;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Whether every element `node` creates, including any nested within it, is a host element or a
|
|
118
|
+
* fragment, and it calls nothing that creates elements otherwise.
|
|
119
|
+
*/
|
|
120
|
+
export function hostElementsOnly(node: J, imports: ReadonlySet<string>): boolean {
|
|
121
|
+
let host = true;
|
|
122
|
+
walk(node, tree => {
|
|
123
|
+
if (!host) return false;
|
|
124
|
+
if (tree.kind === J.Kind.Identifier && RENDERS_OTHERWISE.has((tree as J.Identifier).simpleName)) host = false;
|
|
125
|
+
if (tree.kind === J.Kind.MethodInvocation && HOOK.test((tree as J.MethodInvocation).name.simpleName) &&
|
|
126
|
+
!SAME_IN_SHALLOW.has((tree as J.MethodInvocation).name.simpleName)) host = false;
|
|
127
|
+
if (tree.kind === JS.Kind.JsxTag) {
|
|
128
|
+
const tag = tree as JSX.Tag;
|
|
129
|
+
host = hostTag(tag) && (tag.children ?? []).every(child =>
|
|
130
|
+
child.kind !== JS.Kind.JsxEmbeddedExpression || rendersAsHost((child as JSX.EmbeddedExpression).expression.element, imports));
|
|
131
|
+
}
|
|
132
|
+
return host;
|
|
133
|
+
});
|
|
134
|
+
return host;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function exportName(declaration: {modifiers: J.Modifier[]}, name: string): string | undefined {
|
|
138
|
+
const keywords = new Set(declaration.modifiers.map(modifier => modifier.keyword));
|
|
139
|
+
return keywords.has("export") ? keywords.has("default") ? "default" : name : undefined;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** Whether `node` is `React.<name>` for one of `names`. */
|
|
143
|
+
function reactMember(node: J | undefined, names: ReadonlySet<string>): boolean {
|
|
144
|
+
if (node?.kind !== J.Kind.FieldAccess) return false;
|
|
145
|
+
const access = node as J.FieldAccess;
|
|
146
|
+
return isIdentifier(access.target) && access.target.simpleName === "React" && names.has(access.name.element.simpleName);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** Whether a class extends `Component` or `PureComponent`, bare or as a member of `React`, so that its `render` is its own. */
|
|
150
|
+
function reactBaseClass(declaration: J.ClassDeclaration): boolean {
|
|
151
|
+
const base = unwrapBaseType(declaration.extends?.element);
|
|
152
|
+
return base !== undefined && (isIdentifier(base) ? COMPONENT_BASES.has(base.simpleName) : reactMember(base, COMPONENT_BASES));
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/** A top-level declaration that can be a component, the export name it has, if any, and the body that renders it. */
|
|
156
|
+
function declaredComponent(statement: J): {name: string, exported?: string, renders?: J} | undefined {
|
|
157
|
+
if (statement.kind === J.Kind.MethodDeclaration) {
|
|
158
|
+
const method = statement as J.MethodDeclaration;
|
|
159
|
+
return {name: method.name.simpleName, renders: method.body, exported: exportName(method, method.name.simpleName)};
|
|
160
|
+
}
|
|
161
|
+
if (statement.kind === J.Kind.ClassDeclaration) {
|
|
162
|
+
const declaration = statement as J.ClassDeclaration;
|
|
163
|
+
if (!reactBaseClass(declaration)) return undefined;
|
|
164
|
+
const render = declaration.body.statements.map(({element}) => element).find(member =>
|
|
165
|
+
member.kind === J.Kind.MethodDeclaration && (member as J.MethodDeclaration).name.simpleName === "render") as J.MethodDeclaration | undefined;
|
|
166
|
+
return {name: declaration.name.simpleName, renders: render?.body, exported: exportName(declaration, declaration.name.simpleName)};
|
|
167
|
+
}
|
|
168
|
+
if (statement.kind === J.Kind.VariableDeclarations) {
|
|
169
|
+
const declaration = statement as J.VariableDeclarations;
|
|
170
|
+
const variable = declaration.variables.length === 1 ? declaration.variables[0].element : undefined;
|
|
171
|
+
if (!variable || !isIdentifier(variable.name) || variable.initializer?.element.kind !== JS.Kind.ArrowFunction) return undefined;
|
|
172
|
+
return {name: variable.name.simpleName, renders: (variable.initializer.element as JS.ArrowFunction).lambda.body,
|
|
173
|
+
exported: exportName(declaration, variable.name.simpleName)};
|
|
174
|
+
}
|
|
175
|
+
return undefined;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** Each export name of a source, with the body that renders it where it can be a component, without looking into any body. */
|
|
179
|
+
function componentExports(cu: JS.CompilationUnit): Map<string, J | undefined> {
|
|
180
|
+
const declared = new Map<string, J | undefined>();
|
|
181
|
+
const exported = new Map<string, J | undefined>();
|
|
182
|
+
for (const {element} of cu.statements) {
|
|
183
|
+
const component = declaredComponent(element);
|
|
184
|
+
if (!component) continue;
|
|
185
|
+
declared.set(component.name, component.renders);
|
|
186
|
+
if (component.exported) exported.set(component.exported, component.renders);
|
|
187
|
+
}
|
|
188
|
+
for (const {element} of cu.statements) {
|
|
189
|
+
if (element.kind === JS.Kind.ExportAssignment) {
|
|
190
|
+
const assignment = element as JS.ExportAssignment;
|
|
191
|
+
const expression = assignment.expression.element;
|
|
192
|
+
if (assignment.exportEquals) continue;
|
|
193
|
+
if (expression.kind === JS.Kind.ArrowFunction) exported.set("default", (expression as JS.ArrowFunction).lambda.body);
|
|
194
|
+
if (isIdentifier(expression) && declared.has(expression.simpleName)) exported.set("default", declared.get(expression.simpleName));
|
|
195
|
+
}
|
|
196
|
+
if (element.kind === JS.Kind.ExportDeclaration) {
|
|
197
|
+
const declaration = element as JS.ExportDeclaration;
|
|
198
|
+
if (declaration.moduleSpecifier || declaration.typeOnly.element || declaration.exportClause?.kind !== JS.Kind.NamedExports) continue;
|
|
199
|
+
for (const {element: specifier} of (declaration.exportClause as JS.NamedExports).elements.elements) {
|
|
200
|
+
if (specifier.kind !== JS.Kind.ExportSpecifier || (specifier as JS.ExportSpecifier).typeOnly.element) continue;
|
|
201
|
+
const named = (specifier as JS.ExportSpecifier).specifier;
|
|
202
|
+
if (isIdentifier(named) && declared.has(named.simpleName)) exported.set(named.simpleName, declared.get(named.simpleName));
|
|
203
|
+
if (named.kind === JS.Kind.Alias) {
|
|
204
|
+
const alias = named as JS.Alias;
|
|
205
|
+
const local = alias.propertyName.element;
|
|
206
|
+
if (isIdentifier(local) && declared.has(local.simpleName) && isIdentifier(alias.alias)) exported.set(alias.alias.simpleName, declared.get(local.simpleName));
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
return exported;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* The exports of a source that are function or class components rendering host elements only, where
|
|
216
|
+
* the whole source creates no other element: a component it declares can then render no other.
|
|
217
|
+
* Components wrapped in a call, such as `memo`, `forwardRef`, or a higher-order component, are left out.
|
|
218
|
+
*/
|
|
219
|
+
export function hostComponentExports(cu: JS.CompilationUnit): ReadonlySet<string> {
|
|
220
|
+
// Most sources export no component, which the top-level statements alone show
|
|
221
|
+
const candidates = componentExports(cu);
|
|
222
|
+
if (candidates.size === 0) return NONE;
|
|
223
|
+
const bindings = importBindings(cu);
|
|
224
|
+
// A hook React declares behaves alike only where it is React's own
|
|
225
|
+
if (bindings.some(({local, module}) => HOOK.test(local) && module !== "react")) return NONE;
|
|
226
|
+
const imports = new Set([...bindings.map(({local}) => local), ...namespaceImports(cu)]);
|
|
227
|
+
if (!hostElementsOnly(cu, imports)) return NONE;
|
|
228
|
+
// What a component returns is checked as a child is, since a helper may return any element
|
|
229
|
+
const exported = [...candidates].filter(([, renders]) => returnsAsHost(renders, imports)).map(([name]) => name);
|
|
230
|
+
return exported.length > 0 ? new Set(exported) : NONE;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/** Each local name a source binds with an import, including namespace imports. */
|
|
234
|
+
export function importedNames(cu: JS.CompilationUnit): ReadonlySet<string> {
|
|
235
|
+
return new Set([...importBindings(cu).map(({local}) => local), ...namespaceImports(cu)]);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/** Each local name a source binds to an export of another scanned source. */
|
|
239
|
+
export function relativeImports(cu: JS.CompilationUnit, components: HostComponents): Map<string, ImportedExport> {
|
|
240
|
+
const imports = new Map<string, ImportedExport>();
|
|
241
|
+
for (const {local, name, module} of importBindings(cu)) {
|
|
242
|
+
const path = resolveRelative(cu.sourcePath, module, components);
|
|
243
|
+
if (path) imports.set(local, {path, name});
|
|
244
|
+
}
|
|
245
|
+
return imports;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* Whether a prop value the test supplies is not an imported value, which may hold an element of a
|
|
250
|
+
* component. Unlike a child in {@link rendersAsHost}, any other expression passes: its own elements are checked by
|
|
251
|
+
* {@link hostElementsOnly}, and a prop the component does not render as a child cannot add an element.
|
|
252
|
+
*/
|
|
253
|
+
function suppliedByTest(node: Expression, testImports: ReadonlySet<string>): boolean {
|
|
254
|
+
const value = node.kind === JS.Kind.JsxEmbeddedExpression ? (node as JSX.EmbeddedExpression).expression.element : node;
|
|
255
|
+
if (value.kind !== J.Kind.Identifier && value.kind !== J.Kind.FieldAccess) return true;
|
|
256
|
+
const name = rootName(value);
|
|
257
|
+
return primitive((value as J.Identifier | J.FieldAccess).type) || name === undefined || !testImports.has(name);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/** Whether props that `setProps` passes a shallow wrapper hold no element of a component, which a full render would show as its DOM. */
|
|
261
|
+
export function propsRenderHostElements(props: Expression, testImports: ReadonlySet<string>): boolean {
|
|
262
|
+
return hostElementsOnly(props, testImports) && ![...namesReferencedWithin(props)].some(name => testImports.has(name));
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* Whether Enzyme's `shallow` renders the same host elements for `element` as a full render: it is a
|
|
267
|
+
* component of another source that renders host elements only, and receives no element of a
|
|
268
|
+
* component through its props or children, where `testImports` are the names the test imports.
|
|
269
|
+
*/
|
|
270
|
+
export function rendersHostElements(element: J, imports: ReadonlyMap<string, ImportedExport>, components: HostComponents,
|
|
271
|
+
testImports: ReadonlySet<string>): boolean {
|
|
272
|
+
if (element.kind !== JS.Kind.JsxTag) return false;
|
|
273
|
+
const tag = element as JSX.Tag;
|
|
274
|
+
const name = tag.openName.element;
|
|
275
|
+
if (!isIdentifier(name)) return false;
|
|
276
|
+
const source = imports.get(name.simpleName);
|
|
277
|
+
if (!source || !components.get(source.path)?.has(source.name)) return false;
|
|
278
|
+
return tag.attributes.every(({element: attribute}) => hostElementsOnly(attribute, testImports) && (
|
|
279
|
+
attribute.kind === JS.Kind.JsxSpreadAttribute ? suppliedByTest((attribute as JSX.SpreadAttribute).expression.element, testImports) :
|
|
280
|
+
attribute.kind !== JS.Kind.JsxAttribute || !(attribute as JSX.Attribute).value || suppliedByTest((attribute as JSX.Attribute).value!.element, testImports))) &&
|
|
281
|
+
(tag.children ?? []).every(child => hostElementsOnly(child, testImports) &&
|
|
282
|
+
(child.kind !== JS.Kind.JsxEmbeddedExpression || rendersAsHost((child as JSX.EmbeddedExpression).expression.element, testImports)));
|
|
283
|
+
}
|