@openrewrite/recipes-react 0.17.0 → 0.19.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 +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +9 -5
- package/dist/index.js.map +1 -1
- 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 +3 -5
- package/dist/migration/cra-to-vite/find-cra-config-blockers.js.map +1 -1
- 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 +5 -2
- package/dist/migration/cra-to-vite/update-react-app-env-types.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 +8 -1
- package/dist/react18/replace-unmount-component-at-node.js.map +1 -1
- package/dist/react19/find-legacy-context-api.d.ts.map +1 -1
- package/dist/react19/find-legacy-context-api.js +21 -38
- package/dist/react19/find-legacy-context-api.js.map +1 -1
- package/dist/react19/no-implicit-ref-callback-return.d.ts.map +1 -1
- package/dist/react19/no-implicit-ref-callback-return.js +25 -5
- package/dist/react19/no-implicit-ref-callback-return.js.map +1 -1
- package/dist/{react18 → react19}/remove-unstable-batched-updates.d.ts +2 -2
- package/dist/react19/remove-unstable-batched-updates.d.ts.map +1 -0
- package/dist/react19/remove-unstable-batched-updates.js +295 -0
- package/dist/react19/remove-unstable-batched-updates.js.map +1 -0
- package/dist/react19/replace-default-props.d.ts +1 -0
- package/dist/react19/replace-default-props.d.ts.map +1 -1
- package/dist/react19/replace-default-props.js +198 -151
- 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 +10 -25
- package/dist/react19/replace-react-shallow-renderer.js.map +1 -1
- package/dist/refactoring/class-to-functional.d.ts.map +1 -1
- package/dist/refactoring/class-to-functional.js +2 -0
- package/dist/refactoring/class-to-functional.js.map +1 -1
- package/dist/refactoring/create-class-to-es6.d.ts.map +1 -1
- package/dist/refactoring/create-class-to-es6.js +14 -49
- package/dist/refactoring/create-class-to-es6.js.map +1 -1
- package/dist/search/find-function-component-default-props.d.ts.map +1 -1
- package/dist/search/find-function-component-default-props.js +6 -15
- package/dist/search/find-function-component-default-props.js.map +1 -1
- package/dist/search/find-redux-stores.d.ts +12 -0
- package/dist/search/find-redux-stores.d.ts.map +1 -0
- package/dist/search/find-redux-stores.js +87 -0
- package/dist/search/find-redux-stores.js.map +1 -0
- package/dist/simplify-object-pattern-property.d.ts.map +1 -1
- package/dist/simplify-object-pattern-property.js +14 -21
- package/dist/simplify-object-pattern-property.js.map +1 -1
- package/dist/simplify-react-imports.d.ts.map +1 -1
- package/dist/simplify-react-imports.js +10 -59
- package/dist/simplify-react-imports.js.map +1 -1
- package/dist/testing/enzyme-host-components.d.ts +2 -1
- package/dist/testing/enzyme-host-components.d.ts.map +1 -1
- package/dist/testing/enzyme-host-components.js +33 -2
- package/dist/testing/enzyme-host-components.js.map +1 -1
- package/dist/testing/enzyme-to-react-testing-library.d.ts +10 -1
- package/dist/testing/enzyme-to-react-testing-library.d.ts.map +1 -1
- package/dist/testing/enzyme-to-react-testing-library.js +83 -24
- package/dist/testing/enzyme-to-react-testing-library.js.map +1 -1
- package/dist/testing/enzyme-wrapper-scope.d.ts +4 -1
- package/dist/testing/enzyme-wrapper-scope.d.ts.map +1 -1
- package/dist/testing/enzyme-wrapper-scope.js +36 -7
- package/dist/testing/enzyme-wrapper-scope.js.map +1 -1
- package/dist/util/java-recipe-proxy.d.ts +2 -0
- package/dist/util/java-recipe-proxy.d.ts.map +1 -1
- package/dist/util/java-recipe-proxy.js +14 -2
- package/dist/util/java-recipe-proxy.js.map +1 -1
- package/dist/util/reindent.d.ts +5 -0
- package/dist/util/reindent.d.ts.map +1 -0
- package/dist/util/reindent.js +44 -0
- package/dist/util/reindent.js.map +1 -0
- package/dist/zustand/redux-stores.d.ts +74 -0
- package/dist/zustand/redux-stores.d.ts.map +1 -0
- package/dist/zustand/redux-stores.js +439 -0
- package/dist/zustand/redux-stores.js.map +1 -0
- package/dist/zustand/redux-to-zustand.d.ts +76 -0
- package/dist/zustand/redux-to-zustand.d.ts.map +1 -0
- package/dist/zustand/redux-to-zustand.js +654 -0
- package/dist/zustand/redux-to-zustand.js.map +1 -0
- package/dist/zustand/zustand-plan.d.ts +97 -0
- package/dist/zustand/zustand-plan.d.ts.map +1 -0
- package/dist/zustand/zustand-plan.js +833 -0
- package/dist/zustand/zustand-plan.js.map +1 -0
- package/package.json +1 -1
- package/src/index.ts +7 -3
- package/src/migration/cra-to-vite/find-cra-config-blockers.ts +3 -5
- package/src/migration/cra-to-vite/update-react-app-env-types.ts +3 -3
- package/src/react18/replace-unmount-component-at-node.ts +13 -2
- package/src/react19/find-legacy-context-api.ts +21 -42
- package/src/react19/no-implicit-ref-callback-return.ts +34 -8
- package/src/react19/remove-unstable-batched-updates.ts +301 -0
- package/src/react19/replace-default-props.ts +212 -159
- package/src/react19/replace-react-shallow-renderer.ts +13 -36
- package/src/refactoring/class-to-functional.ts +3 -1
- package/src/refactoring/create-class-to-es6.ts +12 -63
- package/src/search/find-function-component-default-props.ts +8 -10
- package/src/search/find-redux-stores.ts +76 -0
- package/src/simplify-object-pattern-property.ts +22 -49
- package/src/simplify-react-imports.ts +12 -66
- package/src/testing/enzyme-host-components.ts +38 -3
- package/src/testing/enzyme-to-react-testing-library.ts +118 -31
- package/src/testing/enzyme-wrapper-scope.ts +39 -10
- package/src/util/java-recipe-proxy.ts +15 -2
- package/src/util/reindent.ts +35 -0
- package/src/zustand/redux-stores.ts +441 -0
- package/src/zustand/redux-to-zustand.ts +660 -0
- package/src/zustand/zustand-plan.ts +854 -0
- package/dist/react18/remove-unstable-batched-updates.d.ts.map +0 -1
- package/dist/react18/remove-unstable-batched-updates.js +0 -170
- package/dist/react18/remove-unstable-batched-updates.js.map +0 -1
- package/src/react18/remove-unstable-batched-updates.ts +0 -192
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import {Column, DataTable, ExecutionContext, foundSearchResult, Recipe, TreeVisitor, check, not} from "@openrewrite/rewrite";
|
|
2
|
-
import {isVendoredOrBundled, JavaScriptVisitor, JS} from "@openrewrite/rewrite/javascript";
|
|
2
|
+
import {isVendoredOrBundled, JavaScriptVisitor, JS, referencesOf} from "@openrewrite/rewrite/javascript";
|
|
3
3
|
import {J} from "@openrewrite/rewrite/java";
|
|
4
4
|
import {extendsReactComponentByName} from "../util/react-class";
|
|
5
5
|
|
|
@@ -41,25 +41,23 @@ export class FindFunctionComponentDefaultProps extends Recipe {
|
|
|
41
41
|
|
|
42
42
|
return check(not(isVendoredOrBundled()), new class extends JavaScriptVisitor<ExecutionContext> {
|
|
43
43
|
private filePath = "";
|
|
44
|
-
private classComponents = new Set<string>();
|
|
45
44
|
|
|
46
45
|
override async visitJsCompilationUnit(cu: JS.CompilationUnit, ctx: ExecutionContext): Promise<J | undefined> {
|
|
47
46
|
this.filePath = cu.sourcePath ?? "";
|
|
48
|
-
const classes = this.classComponents = new Set<string>();
|
|
49
|
-
await new class extends JavaScriptVisitor<ExecutionContext> {
|
|
50
|
-
protected override async visitClassDeclaration(decl: J.ClassDeclaration, c: ExecutionContext): Promise<J | undefined> {
|
|
51
|
-
if (extendsReactComponentByName(decl)) classes.add(decl.name.simpleName);
|
|
52
|
-
return super.visitClassDeclaration(decl, c);
|
|
53
|
-
}
|
|
54
|
-
}().visit(cu, ctx);
|
|
55
47
|
return super.visitJsCompilationUnit(cu, ctx);
|
|
56
48
|
}
|
|
57
49
|
|
|
50
|
+
/** Resolved rather than matched by name, so a parameter or local that shadows a class component still counts. */
|
|
51
|
+
private isClassComponent(name: string): boolean {
|
|
52
|
+
const declaration = referencesOf(this.cursor, name, 'value').find(r => r.declares)?.cursor.parent?.value;
|
|
53
|
+
return declaration?.kind === J.Kind.ClassDeclaration && extendsReactComponentByName(declaration as J.ClassDeclaration);
|
|
54
|
+
}
|
|
55
|
+
|
|
58
56
|
override async visitFieldAccess(fieldAccess: J.FieldAccess, ctx: ExecutionContext): Promise<J | undefined> {
|
|
59
57
|
const fa = await super.visitFieldAccess(fieldAccess, ctx) as J.FieldAccess;
|
|
60
58
|
if (fa.name.element.simpleName !== 'defaultProps' || fa.target.kind !== J.Kind.Identifier) return fa;
|
|
61
59
|
const receiver = (fa.target as J.Identifier).simpleName;
|
|
62
|
-
if (!/^[A-Z]/.test(receiver) || this.
|
|
60
|
+
if (!/^[A-Z]/.test(receiver) || this.isClassComponent(receiver)) return fa;
|
|
63
61
|
dataTable.insertRow(ctx, {filePath: this.filePath, component: receiver, upgradeStep: DEFAULT_PROPS_STEP});
|
|
64
62
|
return foundSearchResult(fa, 'defaultProps') as J.FieldAccess;
|
|
65
63
|
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import {check, DataTable, ExecutionContext, foundSearchResult, not, ScanningRecipe, TreeVisitor} from "@openrewrite/rewrite";
|
|
2
|
+
import {isVendoredOrBundled, JavaScriptVisitor, JS} from "@openrewrite/rewrite/javascript";
|
|
3
|
+
import {J} from "@openrewrite/rewrite/java";
|
|
4
|
+
import {
|
|
5
|
+
localSlices, REDUX_STORES_TABLE, ReduxStoreRow, SliceModule, storeFindings, storeFunctions, storeName, SUGGESTED_ACTIONS, TEST_SOURCE, ZUSTAND_GUIDE
|
|
6
|
+
} from "../zustand/redux-stores";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Inventories the Redux stores of a repository for a migration to zustand. A store is a candidate when it is a
|
|
10
|
+
* `configureStore` call whose `reducer` is an object of `createSlice` reducers, in the same source or imported from
|
|
11
|
+
* a relative module, and whose options are only `reducer`. Every other store gets a row for each reason it is not,
|
|
12
|
+
* such as a legacy `createStore`, custom middleware or a hand-written reducer. Stores are attributed by what the
|
|
13
|
+
* source imports from `redux` or `@reduxjs/toolkit`; namespace imports and `require` are not followed.
|
|
14
|
+
*
|
|
15
|
+
* Only the store's setup is checked; the components and modules that read or dispatch to it are not.
|
|
16
|
+
*
|
|
17
|
+
* @see https://zustand.docs.pmnd.rs/guides/migrating-to-zustand
|
|
18
|
+
*/
|
|
19
|
+
export class FindReduxStores extends ScanningRecipe<Map<string, SliceModule>> {
|
|
20
|
+
readonly name = "org.openrewrite.react.search.find-redux-stores";
|
|
21
|
+
readonly displayName = "Find Redux stores";
|
|
22
|
+
readonly description = "Finds the Redux stores created with `createStore` or Redux Toolkit's `configureStore` and records whether each one's setup can move to zustand: a `configureStore` call whose `reducer` is an object of `createSlice` reducers without `extraReducers`, and no other option, is a candidate. Every other store is recorded with the reasons it is not, such as a legacy `createStore`, custom middleware, enhancers, preloaded state or a hand-written reducer. The components and modules that use a store are not checked.";
|
|
23
|
+
|
|
24
|
+
private readonly stores = new DataTable<ReduxStoreRow>(
|
|
25
|
+
REDUX_STORES_TABLE, "Redux stores",
|
|
26
|
+
"The Redux stores of the repository and whether each one's setup can move to zustand, with one row for each reason it can't.",
|
|
27
|
+
ReduxStoreRow
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
initialValue(): Map<string, SliceModule> {
|
|
31
|
+
return new Map();
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
async scanner(modules: Map<string, SliceModule>): Promise<TreeVisitor<any, ExecutionContext>> {
|
|
35
|
+
return check(not(isVendoredOrBundled()), new class extends JavaScriptVisitor<ExecutionContext> {
|
|
36
|
+
override async visitJsCompilationUnit(cu: JS.CompilationUnit): Promise<J | undefined> {
|
|
37
|
+
const {reducers, slices} = localSlices(cu);
|
|
38
|
+
// Every source is kept, so that a relative import resolves to the file Node would load
|
|
39
|
+
modules.set(cu.sourcePath, {reducers, slices});
|
|
40
|
+
return cu;
|
|
41
|
+
}
|
|
42
|
+
}());
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
async editorWithData(modules: Map<string, SliceModule>): Promise<TreeVisitor<any, ExecutionContext>> {
|
|
46
|
+
const stores = this.stores;
|
|
47
|
+
return check(not(isVendoredOrBundled()), new class extends JavaScriptVisitor<ExecutionContext> {
|
|
48
|
+
private cu?: JS.CompilationUnit;
|
|
49
|
+
private functions = {legacy: new Set<string>(), configure: new Set<string>()};
|
|
50
|
+
|
|
51
|
+
override async visitJsCompilationUnit(cu: JS.CompilationUnit, ctx: ExecutionContext): Promise<J | undefined> {
|
|
52
|
+
this.functions = storeFunctions(cu);
|
|
53
|
+
if (this.functions.legacy.size === 0 && this.functions.configure.size === 0) return cu;
|
|
54
|
+
this.cu = cu;
|
|
55
|
+
return super.visitJsCompilationUnit(cu, ctx);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
override async visitMethodInvocation(method: J.MethodInvocation, ctx: ExecutionContext): Promise<J | undefined> {
|
|
59
|
+
const visited = await super.visitMethodInvocation(method, ctx) as J.MethodInvocation;
|
|
60
|
+
const found = storeFindings(method, this.cu!, modules, this.functions);
|
|
61
|
+
if (!found) return visited;
|
|
62
|
+
const {api, findings} = found;
|
|
63
|
+
const cu = this.cu!;
|
|
64
|
+
const rows = findings.length === 0 ? [{reason: "SLICE_STORE", detail: ""}] : findings;
|
|
65
|
+
for (const {reason, detail} of rows) {
|
|
66
|
+
stores.insertRow(ctx, {
|
|
67
|
+
sourcePath: cu.sourcePath, store: storeName(this.cursor), api,
|
|
68
|
+
status: findings.length === 0 ? "CANDIDATE" : "REVIEW", reason, detail,
|
|
69
|
+
testSource: TEST_SOURCE.test(cu.sourcePath), suggestedAction: SUGGESTED_ACTIONS[reason]
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
return foundSearchResult(visited, `Redux store: ${rows.map(row => row.reason).join(", ")}. See ${ZUSTAND_GUIDE}`);
|
|
73
|
+
}
|
|
74
|
+
}());
|
|
75
|
+
}
|
|
76
|
+
}
|
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import {ExecutionContext, Recipe, TreeVisitor, check, not} from "@openrewrite/rewrite";
|
|
1
|
+
import {ExecutionContext, Recipe, TreeVisitor, check, mapAsync, not, updateIfChanged} from "@openrewrite/rewrite";
|
|
2
2
|
import {JavaScriptVisitor, JS, isVendoredOrBundled} from "@openrewrite/rewrite/javascript";
|
|
3
3
|
import {J} from "@openrewrite/rewrite/java";
|
|
4
|
-
import {create} from "mutative";
|
|
5
4
|
|
|
6
5
|
/**
|
|
7
6
|
* Simplifies object pattern properties where the key and value have the same name.
|
|
@@ -19,55 +18,29 @@ export class SimplifyObjectPatternProperty extends Recipe {
|
|
|
19
18
|
async editor(): Promise<TreeVisitor<any, ExecutionContext>> {
|
|
20
19
|
return check(not(isVendoredOrBundled()), new class extends JavaScriptVisitor<ExecutionContext> {
|
|
21
20
|
protected async visitObjectBindingPattern(pattern: JS.ObjectBindingPattern, p: ExecutionContext): Promise<J | undefined> {
|
|
22
|
-
// First, recursively visit children
|
|
23
21
|
const visited = await super.visitObjectBindingPattern(pattern, p) as JS.ObjectBindingPattern;
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
if (element.kind === JS.Kind.BindingElement) {
|
|
31
|
-
const binding = element as JS.BindingElement;
|
|
32
|
-
|
|
33
|
-
// Check if the property name is an identifier
|
|
34
|
-
if (binding.propertyName && binding.propertyName.element.kind === J.Kind.Identifier) {
|
|
35
|
-
const propName = (binding.propertyName.element as J.Identifier).simpleName;
|
|
36
|
-
|
|
37
|
-
// Check if the assignment is also an identifier with the same name
|
|
38
|
-
if (binding.name && binding.name.kind === J.Kind.Identifier) {
|
|
39
|
-
const assignmentName = (binding.name as J.Identifier).simpleName;
|
|
40
|
-
|
|
41
|
-
// If they match, simplify to just the identifier
|
|
42
|
-
if (propName === assignmentName) {
|
|
43
|
-
// Create a new binding element with propertyName set to undefined (shorthand syntax)
|
|
44
|
-
// Also need to update the name's prefix to remove extra spacing
|
|
45
|
-
return {
|
|
46
|
-
...right,
|
|
47
|
-
element: {
|
|
48
|
-
...binding,
|
|
49
|
-
propertyName: undefined,
|
|
50
|
-
name: {
|
|
51
|
-
...binding.name,
|
|
52
|
-
prefix: binding.propertyName!.element.prefix
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
} as J.RightPadded<JS.BindingElement>;
|
|
56
|
-
}
|
|
22
|
+
return updateIfChanged(visited, {
|
|
23
|
+
bindings: updateIfChanged(visited.bindings, {
|
|
24
|
+
elements: await mapAsync(visited.bindings.elements, async right => {
|
|
25
|
+
const binding = right.element;
|
|
26
|
+
if (binding.kind !== JS.Kind.BindingElement) {
|
|
27
|
+
return right;
|
|
57
28
|
}
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
29
|
+
const {propertyName, name} = binding as JS.BindingElement;
|
|
30
|
+
if (propertyName?.element.kind !== J.Kind.Identifier || name?.kind !== J.Kind.Identifier ||
|
|
31
|
+
(propertyName.element as J.Identifier).simpleName !== (name as J.Identifier).simpleName) {
|
|
32
|
+
return right;
|
|
33
|
+
}
|
|
34
|
+
return {
|
|
35
|
+
...right,
|
|
36
|
+
element: {
|
|
37
|
+
...binding,
|
|
38
|
+
propertyName: undefined,
|
|
39
|
+
name: {...name, prefix: propertyName.element.prefix}
|
|
40
|
+
}
|
|
41
|
+
} as J.RightPadded<JS.BindingElement>;
|
|
42
|
+
})
|
|
43
|
+
})
|
|
71
44
|
});
|
|
72
45
|
}
|
|
73
46
|
}());
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import {ExecutionContext, Recipe, TreeVisitor, check, not} from "@openrewrite/rewrite";
|
|
2
|
-
import {JavaScriptVisitor, maybeAddImport, isVendoredOrBundled} from "@openrewrite/rewrite/javascript";
|
|
2
|
+
import {JavaScriptVisitor, JS, maybeAddImport, isVendoredOrBundled, namesDeclaredIn} from "@openrewrite/rewrite/javascript";
|
|
3
3
|
import {J} from "@openrewrite/rewrite/java";
|
|
4
|
-
import {
|
|
4
|
+
import {importBindings} from "./util/module-imports";
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Common React hooks and utilities that can be imported directly instead of
|
|
@@ -100,24 +100,20 @@ export class SimplifyReactImports extends Recipe {
|
|
|
100
100
|
|
|
101
101
|
async editor(): Promise<TreeVisitor<any, ExecutionContext>> {
|
|
102
102
|
return check(not(isVendoredOrBundled()), new class extends JavaScriptVisitor<ExecutionContext> {
|
|
103
|
-
// Track which React exports we've used in this file
|
|
104
103
|
private usedExports = new Set<string>();
|
|
105
|
-
|
|
106
|
-
private localIdentifiers = new Set<string>();
|
|
104
|
+
private declaredNames = new Set<string>();
|
|
107
105
|
|
|
108
106
|
override async visitJsCompilationUnit(cu: JS.CompilationUnit, ctx: ExecutionContext): Promise<J | undefined> {
|
|
109
|
-
// Reset tracking for each file
|
|
110
107
|
this.usedExports = new Set<string>();
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
108
|
+
// File-wide rather than in scope, as `maybeAddImport` aliases the import when any binding of the name is declared
|
|
109
|
+
this.declaredNames = new Set(namesDeclaredIn(cu));
|
|
110
|
+
for (const {local, name, module} of importBindings(cu)) {
|
|
111
|
+
if (module === 'react' && local === name) this.declaredNames.delete(local);
|
|
112
|
+
}
|
|
115
113
|
|
|
116
|
-
|
|
117
|
-
let result = await super.visitJsCompilationUnit(cu, ctx) as JS.CompilationUnit;
|
|
114
|
+
const result = await super.visitJsCompilationUnit(cu, ctx) as JS.CompilationUnit;
|
|
118
115
|
|
|
119
|
-
//
|
|
120
|
-
// We set onlyIfReferenced: false since we already know these are used (we just transformed them)
|
|
116
|
+
// Already known to be referenced, since they were just unqualified
|
|
121
117
|
for (const exportName of this.usedExports) {
|
|
122
118
|
maybeAddImport(this, {module: "react", member: exportName, onlyIfReferenced: false});
|
|
123
119
|
}
|
|
@@ -125,54 +121,6 @@ export class SimplifyReactImports extends Recipe {
|
|
|
125
121
|
return result;
|
|
126
122
|
}
|
|
127
123
|
|
|
128
|
-
/**
|
|
129
|
-
* Collect all locally defined identifiers to detect potential naming conflicts.
|
|
130
|
-
* This includes variable declarations, function declarations, and non-React imports.
|
|
131
|
-
*/
|
|
132
|
-
private async collectLocalIdentifiers(cu: JS.CompilationUnit, ctx: ExecutionContext): Promise<void> {
|
|
133
|
-
const collector = new class extends JavaScriptVisitor<ExecutionContext> {
|
|
134
|
-
constructor(private identifiers: Set<string>) {
|
|
135
|
-
super();
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
override async visitVariableDeclarations(varDecl: J.VariableDeclarations, ctx: ExecutionContext): Promise<J | undefined> {
|
|
139
|
-
for (const v of varDecl.variables) {
|
|
140
|
-
const name = v.element.name;
|
|
141
|
-
if (name.kind === J.Kind.Identifier) {
|
|
142
|
-
this.identifiers.add((name as J.Identifier).simpleName);
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
return super.visitVariableDeclarations(varDecl, ctx);
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
override async visitMethodDeclaration(method: J.MethodDeclaration, ctx: ExecutionContext): Promise<J | undefined> {
|
|
149
|
-
this.identifiers.add(method.name.simpleName);
|
|
150
|
-
return super.visitMethodDeclaration(method, ctx);
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
override async visitImportSpecifier(spec: JS.ImportSpecifier, ctx: ExecutionContext): Promise<J | undefined> {
|
|
154
|
-
// Track named imports
|
|
155
|
-
const specifier = spec.specifier;
|
|
156
|
-
|
|
157
|
-
if (specifier.kind === JS.Kind.Alias) {
|
|
158
|
-
// Aliased import: import { useState as myUseState }
|
|
159
|
-
const alias = specifier as JS.Alias;
|
|
160
|
-
const aliasIdent = alias.alias;
|
|
161
|
-
if (aliasIdent.kind === J.Kind.Identifier) {
|
|
162
|
-
this.identifiers.add((aliasIdent as J.Identifier).simpleName);
|
|
163
|
-
}
|
|
164
|
-
} else if (specifier.kind === J.Kind.Identifier) {
|
|
165
|
-
// Direct import: import { useState }
|
|
166
|
-
// We don't track these as conflicts since they're the imports we manage
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
return super.visitImportSpecifier(spec, ctx);
|
|
170
|
-
}
|
|
171
|
-
}(this.localIdentifiers);
|
|
172
|
-
|
|
173
|
-
await collector.visit(cu, ctx);
|
|
174
|
-
}
|
|
175
|
-
|
|
176
124
|
override async visitMethodInvocation(method: J.MethodInvocation, ctx: ExecutionContext): Promise<J | undefined> {
|
|
177
125
|
let m = await super.visitMethodInvocation(method, ctx) as J.MethodInvocation;
|
|
178
126
|
|
|
@@ -181,8 +129,7 @@ export class SimplifyReactImports extends Recipe {
|
|
|
181
129
|
return m;
|
|
182
130
|
}
|
|
183
131
|
|
|
184
|
-
|
|
185
|
-
if (this.localIdentifiers.has(methodName)) {
|
|
132
|
+
if (this.declaredNames.has(methodName)) {
|
|
186
133
|
return m;
|
|
187
134
|
}
|
|
188
135
|
|
|
@@ -220,8 +167,7 @@ export class SimplifyReactImports extends Recipe {
|
|
|
220
167
|
return fa;
|
|
221
168
|
}
|
|
222
169
|
|
|
223
|
-
|
|
224
|
-
if (this.localIdentifiers.has(memberName)) {
|
|
170
|
+
if (this.declaredNames.has(memberName)) {
|
|
225
171
|
return fa;
|
|
226
172
|
}
|
|
227
173
|
|
|
@@ -230,6 +230,41 @@ export function hostComponentExports(cu: JS.CompilationUnit): ReadonlySet<string
|
|
|
230
230
|
return exported.length > 0 ? new Set(exported) : NONE;
|
|
231
231
|
}
|
|
232
232
|
|
|
233
|
+
/**
|
|
234
|
+
* The components a source declares at its top level, exported or not, that render host elements only: each
|
|
235
|
+
* declaration creates no other element, and what it renders is host elements, as for {@link hostComponentExports}.
|
|
236
|
+
* A test that declares a component to render, such as one that throws, renders the same with `shallow` as in full.
|
|
237
|
+
*/
|
|
238
|
+
export function hostComponentsDeclared(cu: JS.CompilationUnit): ReadonlySet<string> {
|
|
239
|
+
const bindings = importBindings(cu);
|
|
240
|
+
if (bindings.some(({local, module}) => HOOK.test(local) && module !== "react")) return NONE;
|
|
241
|
+
// The test's own top-level values may hold an element of a component, so they count as imports do; only a
|
|
242
|
+
// value the rendering test passes in, such as a prop, is known to come from it
|
|
243
|
+
const outside = new Set([...localNames(bindings, cu), ...topLevelNames(cu)]);
|
|
244
|
+
const declared = new Set<string>();
|
|
245
|
+
for (const {element} of cu.statements) {
|
|
246
|
+
const component = declaredComponent(element);
|
|
247
|
+
if (component && hostElementsOnly(element, outside) && returnsAsHost(component.renders, outside)) declared.add(component.name);
|
|
248
|
+
}
|
|
249
|
+
return declared;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/** The names a source declares in its top-level statements: variables, functions, and classes. */
|
|
253
|
+
function topLevelNames(cu: JS.CompilationUnit): ReadonlySet<string> {
|
|
254
|
+
const names = new Set<string>();
|
|
255
|
+
for (const {element} of cu.statements) {
|
|
256
|
+
const statement = element.kind === JS.Kind.ExportDeclaration || element.kind === JS.Kind.ExportAssignment ? undefined : element;
|
|
257
|
+
if (statement?.kind === J.Kind.MethodDeclaration) names.add((statement as J.MethodDeclaration).name.simpleName);
|
|
258
|
+
if (statement?.kind === J.Kind.ClassDeclaration) names.add((statement as J.ClassDeclaration).name.simpleName);
|
|
259
|
+
if (statement?.kind === J.Kind.VariableDeclarations) {
|
|
260
|
+
for (const {element: variable} of (statement as J.VariableDeclarations).variables) {
|
|
261
|
+
if (isIdentifier(variable.name)) names.add(variable.name.simpleName);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
return names;
|
|
266
|
+
}
|
|
267
|
+
|
|
233
268
|
/** Each local name a source binds with an import, including namespace imports. */
|
|
234
269
|
export function importedNames(cu: JS.CompilationUnit): ReadonlySet<string> {
|
|
235
270
|
return localNames(importBindings(cu), cu);
|
|
@@ -268,17 +303,17 @@ export function propsRenderHostElements(props: Expression, testImports: Readonly
|
|
|
268
303
|
|
|
269
304
|
/**
|
|
270
305
|
* Whether Enzyme's `shallow` renders the same host elements for `element` as a full render: it is a
|
|
271
|
-
* component of another source that renders host elements only, and receives no element of a
|
|
306
|
+
* component of another source, or one of `declared` in the test itself, that renders host elements only, and receives no element of a
|
|
272
307
|
* component through its props or children, where `testImports` are the names the test imports.
|
|
273
308
|
*/
|
|
274
309
|
export function rendersHostElements(element: J, imports: ReadonlyMap<string, ImportedExport>, components: HostComponents,
|
|
275
|
-
testImports: ReadonlySet<string>): boolean {
|
|
310
|
+
testImports: ReadonlySet<string>, declared: ReadonlySet<string> = NONE): boolean {
|
|
276
311
|
if (element.kind !== JS.Kind.JsxTag) return false;
|
|
277
312
|
const tag = element as JSX.Tag;
|
|
278
313
|
const name = tag.openName.element;
|
|
279
314
|
if (!isIdentifier(name)) return false;
|
|
280
315
|
const source = imports.get(name.simpleName);
|
|
281
|
-
if (!source || !components.get(source.path)?.has(source.name)) return false;
|
|
316
|
+
if (!declared.has(name.simpleName) && (!source || !components.get(source.path)?.has(source.name))) return false;
|
|
282
317
|
return tag.attributes.every(({element: attribute}) => hostElementsOnly(attribute, testImports) && (
|
|
283
318
|
attribute.kind === JS.Kind.JsxSpreadAttribute ? suppliedByTest((attribute as JSX.SpreadAttribute).expression.element, testImports) :
|
|
284
319
|
attribute.kind !== JS.Kind.JsxAttribute || !(attribute as JSX.Attribute).value || suppliedByTest((attribute as JSX.Attribute).value!.element, testImports))) &&
|