@openrewrite/recipes-react 0.25.0 → 0.26.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.
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"remove-event-persist.d.ts","sourceRoot":"","sources":["../../src/react17/remove-event-persist.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"remove-event-persist.d.ts","sourceRoot":"","sources":["../../src/react17/remove-event-persist.ts"],"names":[],"mappings":"AAAA,OAAO,EAAS,gBAAgB,EAAE,MAAM,EAAQ,WAAW,EAAa,MAAM,sBAAsB,CAAC;AAkKrG,qBAAa,kBAAmB,SAAQ,MAAM;IAC1C,QAAQ,CAAC,IAAI,mDAAmD;IAChE,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAoC;IAChE,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAkO;IAExP,MAAM,IAAI,OAAO,CAAC,WAAW,CAAC,GAAG,EAAE,gBAAgB,CAAC,CAAC;CA2C9D"}
|
|
@@ -14,12 +14,116 @@ const rewrite_1 = require("@openrewrite/rewrite");
|
|
|
14
14
|
const javascript_1 = require("@openrewrite/rewrite/javascript");
|
|
15
15
|
const java_1 = require("@openrewrite/rewrite/java");
|
|
16
16
|
const mutative_1 = require("mutative");
|
|
17
|
+
const REACT = "react";
|
|
18
|
+
const SYNTHETIC_EVENT_PERSIST = [
|
|
19
|
+
new javascript_1.MethodMatcher("React.BaseSyntheticEvent persist()"),
|
|
20
|
+
new javascript_1.MethodMatcher("React.SyntheticEvent persist()")
|
|
21
|
+
];
|
|
22
|
+
function requestedModule(n) {
|
|
23
|
+
var _a, _b;
|
|
24
|
+
if (n.kind === javascript_1.JS.Kind.Import)
|
|
25
|
+
return (_a = n.moduleSpecifier) === null || _a === void 0 ? void 0 : _a.element;
|
|
26
|
+
if (n.kind !== java_1.J.Kind.MethodInvocation)
|
|
27
|
+
return undefined;
|
|
28
|
+
const call = n;
|
|
29
|
+
return !call.select && call.name.simpleName === "require" ? (_b = call.arguments.elements[0]) === null || _b === void 0 ? void 0 : _b.element : undefined;
|
|
30
|
+
}
|
|
31
|
+
function importsReact(cu) {
|
|
32
|
+
let found = false;
|
|
33
|
+
(0, javascript_1.walk)(cu, n => {
|
|
34
|
+
const module = requestedModule(n);
|
|
35
|
+
found || (found = (module === null || module === void 0 ? void 0 : module.kind) === java_1.J.Kind.Literal && (0, javascript_1.moduleNameOf)(module) === REACT);
|
|
36
|
+
return !found;
|
|
37
|
+
});
|
|
38
|
+
return found;
|
|
39
|
+
}
|
|
40
|
+
function readsParameter(cursor, receiver) {
|
|
41
|
+
const name = receiver.simpleName;
|
|
42
|
+
const fn = (0, javascript_1.resolve)(cursor, receiver);
|
|
43
|
+
const parameter = parametersOf(fn).map(p => p.element).find(p => { var _a; return ((_a = (0, javascript_1.identifierOf)(p)) === null || _a === void 0 ? void 0 : _a.simpleName) === name; });
|
|
44
|
+
const annotation = (parameter === null || parameter === void 0 ? void 0 : parameter.kind) === java_1.J.Kind.VariableDeclarations ? parameter.typeExpression : undefined;
|
|
45
|
+
if (parameter === undefined || (annotation && !namesEventType(annotation)))
|
|
46
|
+
return false;
|
|
47
|
+
const references = (0, javascript_1.referencesOf)(cursor, name);
|
|
48
|
+
return references.filter(r => r.declares || r.writes).length === 1 &&
|
|
49
|
+
(annotation !== undefined && !namesAny(annotation) || isJsxEventProp(cursor, fn) || readsEventMember(references));
|
|
50
|
+
}
|
|
51
|
+
const EVENT_MEMBERS = new Set([
|
|
52
|
+
"target", "currentTarget", "nativeEvent", "preventDefault", "stopPropagation", "isDefaultPrevented",
|
|
53
|
+
"isPropagationStopped"
|
|
54
|
+
]);
|
|
55
|
+
function readsEventMember(references) {
|
|
56
|
+
return references.some(r => {
|
|
57
|
+
var _a, _b;
|
|
58
|
+
if (r.declares)
|
|
59
|
+
return false;
|
|
60
|
+
const parent = (_a = r.cursor.parentTree()) === null || _a === void 0 ? void 0 : _a.value;
|
|
61
|
+
switch (parent === null || parent === void 0 ? void 0 : parent.kind) {
|
|
62
|
+
case java_1.J.Kind.FieldAccess:
|
|
63
|
+
return EVENT_MEMBERS.has(parent.name.element.simpleName);
|
|
64
|
+
case java_1.J.Kind.MethodInvocation:
|
|
65
|
+
return ((_b = parent.select) === null || _b === void 0 ? void 0 : _b.element) === r.identifier && EVENT_MEMBERS.has(parent.name.simpleName);
|
|
66
|
+
default:
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
function isJsxEventProp(cursor, fn) {
|
|
72
|
+
let at = cursor;
|
|
73
|
+
while (at && at.value !== fn)
|
|
74
|
+
at = at.parent;
|
|
75
|
+
for (let parent = at === null || at === void 0 ? void 0 : at.parentTree(); parent; parent = parent.parentTree()) {
|
|
76
|
+
const node = parent.value;
|
|
77
|
+
if (node.kind === javascript_1.JS.Kind.JsxEmbeddedExpression || node.kind === java_1.J.Kind.Parentheses)
|
|
78
|
+
continue;
|
|
79
|
+
if (node.kind !== javascript_1.JS.Kind.JsxAttribute)
|
|
80
|
+
return false;
|
|
81
|
+
const key = node.key;
|
|
82
|
+
return key.kind === java_1.J.Kind.Identifier && /^on[A-Z]/.test(key.simpleName);
|
|
83
|
+
}
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
function namesAny(type) {
|
|
87
|
+
const typeIdentifier = type.kind === javascript_1.JS.Kind.TypeInfo ? type.typeIdentifier : type;
|
|
88
|
+
return typeIdentifier.kind === java_1.J.Kind.Identifier && typeIdentifier.simpleName === "any";
|
|
89
|
+
}
|
|
90
|
+
function namesEventType(type) {
|
|
91
|
+
switch (type.kind) {
|
|
92
|
+
case javascript_1.JS.Kind.TypeInfo:
|
|
93
|
+
return namesEventType(type.typeIdentifier);
|
|
94
|
+
case javascript_1.JS.Kind.Union:
|
|
95
|
+
return type.types.some(t => namesEventType(t.element));
|
|
96
|
+
case java_1.J.Kind.ParameterizedType:
|
|
97
|
+
return namesEventType(type.class);
|
|
98
|
+
case java_1.J.Kind.FieldAccess:
|
|
99
|
+
return namesEventType(type.name.element);
|
|
100
|
+
case java_1.J.Kind.Identifier: {
|
|
101
|
+
const simpleName = type.simpleName;
|
|
102
|
+
return simpleName === "any" || simpleName.endsWith("Event");
|
|
103
|
+
}
|
|
104
|
+
default:
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
function parametersOf(fn) {
|
|
109
|
+
switch (fn === null || fn === void 0 ? void 0 : fn.kind) {
|
|
110
|
+
case java_1.J.Kind.MethodDeclaration:
|
|
111
|
+
case javascript_1.JS.Kind.ComputedPropertyMethodDeclaration:
|
|
112
|
+
return fn.parameters.elements;
|
|
113
|
+
case javascript_1.JS.Kind.ArrowFunction:
|
|
114
|
+
return fn.lambda.parameters.parameters;
|
|
115
|
+
case java_1.J.Kind.Lambda:
|
|
116
|
+
return fn.parameters.parameters;
|
|
117
|
+
default:
|
|
118
|
+
return [];
|
|
119
|
+
}
|
|
120
|
+
}
|
|
17
121
|
class RemoveEventPersist extends rewrite_1.Recipe {
|
|
18
122
|
constructor() {
|
|
19
123
|
super(...arguments);
|
|
20
124
|
this.name = "org.openrewrite.react.17.remove-event-persist";
|
|
21
125
|
this.displayName = "Remove `event.persist()` calls";
|
|
22
|
-
this.description = "Removes `
|
|
126
|
+
this.description = "Removes `persist()` calls on React synthetic events, which do nothing since React 17 removed event pooling. Without type attribution, only a call on an event handler's parameter in a file that imports `react` is removed.";
|
|
23
127
|
}
|
|
24
128
|
editor() {
|
|
25
129
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -29,25 +133,8 @@ class RemoveEventPersist extends rewrite_1.Recipe {
|
|
|
29
133
|
visitJsCompilationUnit: { get: () => super.visitJsCompilationUnit }
|
|
30
134
|
});
|
|
31
135
|
return __awaiter(this, void 0, void 0, function* () {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
for (const stmt of result.statements) {
|
|
35
|
-
const s = stmt.element;
|
|
36
|
-
if (!s)
|
|
37
|
-
continue;
|
|
38
|
-
if (this.isPersistCall(s)) {
|
|
39
|
-
statementsToRemove.add(s.id);
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
if (statementsToRemove.size === 0)
|
|
43
|
-
return result;
|
|
44
|
-
const filteredStatements = result.statements.filter(stmt => {
|
|
45
|
-
const s = stmt.element;
|
|
46
|
-
return !s || !statementsToRemove.has(s.id);
|
|
47
|
-
});
|
|
48
|
-
return (0, mutative_1.create)(result, draft => {
|
|
49
|
-
draft.statements = filteredStatements;
|
|
50
|
-
});
|
|
136
|
+
this.reactImported = undefined;
|
|
137
|
+
return this.withoutPersistCalls(yield _super.visitJsCompilationUnit.call(this, cu, ctx));
|
|
51
138
|
});
|
|
52
139
|
}
|
|
53
140
|
visitBlock(block, ctx) {
|
|
@@ -55,56 +142,34 @@ class RemoveEventPersist extends rewrite_1.Recipe {
|
|
|
55
142
|
visitBlock: { get: () => super.visitBlock }
|
|
56
143
|
});
|
|
57
144
|
return __awaiter(this, void 0, void 0, function* () {
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
}
|
|
68
|
-
if (statementsToRemove.size === 0)
|
|
69
|
-
return b;
|
|
70
|
-
const filteredStatements = b.statements.filter(stmt => {
|
|
71
|
-
const s = stmt.element;
|
|
72
|
-
return !s || !statementsToRemove.has(s.id);
|
|
73
|
-
});
|
|
74
|
-
return (0, mutative_1.create)(b, draft => {
|
|
75
|
-
draft.statements = filteredStatements;
|
|
76
|
-
});
|
|
145
|
+
return this.withoutPersistCalls(yield _super.visitBlock.call(this, block, ctx));
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
withoutPersistCalls(node) {
|
|
149
|
+
const statements = node.statements.filter(stmt => !stmt.element || !this.isPersistCall(stmt.element));
|
|
150
|
+
if (statements.length === node.statements.length)
|
|
151
|
+
return node;
|
|
152
|
+
return (0, mutative_1.create)(node, draft => {
|
|
153
|
+
draft.statements = statements;
|
|
77
154
|
});
|
|
78
155
|
}
|
|
79
156
|
isPersistCall(stmt) {
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
}
|
|
84
|
-
else if (stmt.kind === java_1.J.Kind.MethodInvocation) {
|
|
85
|
-
expr = stmt;
|
|
86
|
-
}
|
|
87
|
-
else {
|
|
88
|
-
return false;
|
|
89
|
-
}
|
|
90
|
-
if (!expr || expr.kind !== java_1.J.Kind.MethodInvocation)
|
|
157
|
+
var _a, _b;
|
|
158
|
+
const expr = stmt.kind === javascript_1.JS.Kind.ExpressionStatement ? stmt.expression : stmt;
|
|
159
|
+
if ((expr === null || expr === void 0 ? void 0 : expr.kind) !== java_1.J.Kind.MethodInvocation)
|
|
91
160
|
return false;
|
|
92
161
|
const method = expr;
|
|
93
|
-
if (!method.select)
|
|
94
|
-
return false;
|
|
95
|
-
if (method.name.simpleName !== 'persist')
|
|
162
|
+
if (!method.select || method.name.simpleName !== 'persist' || (0, javascript_1.present)(method.arguments.elements).length > 0)
|
|
96
163
|
return false;
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
var _a;
|
|
101
|
-
const element = (_a = el.element) !== null && _a !== void 0 ? _a : el;
|
|
102
|
-
return element.kind !== java_1.J.Kind.Empty;
|
|
103
|
-
});
|
|
104
|
-
if (hasRealArgs)
|
|
105
|
-
return false;
|
|
164
|
+
const declaringType = (_a = method.methodType) === null || _a === void 0 ? void 0 : _a.declaringType;
|
|
165
|
+
if (declaringType && declaringType.kind !== java_1.Type.Kind.Unknown) {
|
|
166
|
+
return SYNTHETIC_EVENT_PERSIST.some(matcher => matcher.matches(method.methodType));
|
|
106
167
|
}
|
|
107
|
-
|
|
168
|
+
const receiver = method.select.element;
|
|
169
|
+
const statement = new rewrite_1.Cursor(stmt, this.cursor);
|
|
170
|
+
return receiver.kind === java_1.J.Kind.Identifier &&
|
|
171
|
+
((_b = this.reactImported) !== null && _b !== void 0 ? _b : (this.reactImported = importsReact((0, javascript_1.compilationUnitOf)(this)))) &&
|
|
172
|
+
readsParameter(stmt === method ? statement : new rewrite_1.Cursor(method, statement), receiver);
|
|
108
173
|
}
|
|
109
174
|
}());
|
|
110
175
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"remove-event-persist.js","sourceRoot":"","sources":["../../src/react17/remove-event-persist.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,
|
|
1
|
+
{"version":3,"file":"remove-event-persist.js","sourceRoot":"","sources":["../../src/react17/remove-event-persist.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,kDAAqG;AACrG,gEAGyC;AACzC,oDAAkD;AAClD,uCAAgC;AAEhC,MAAM,KAAK,GAAG,OAAO,CAAC;AAEtB,MAAM,uBAAuB,GAAG;IAC5B,IAAI,0BAAa,CAAC,oCAAoC,CAAC;IACvD,IAAI,0BAAa,CAAC,gCAAgC,CAAC;CACtD,CAAC;AAGF,SAAS,eAAe,CAAC,CAAO;;IAC5B,IAAI,CAAC,CAAC,IAAI,KAAK,eAAE,CAAC,IAAI,CAAC,MAAM;QAAE,OAAO,MAAC,CAAe,CAAC,eAAe,0CAAE,OAAO,CAAC;IAChF,IAAI,CAAC,CAAC,IAAI,KAAK,QAAC,CAAC,IAAI,CAAC,gBAAgB;QAAE,OAAO,SAAS,CAAC;IACzD,MAAM,IAAI,GAAG,CAAuB,CAAC;IACrC,OAAO,CAAC,IAAI,CAAC,MAAM,IAAI,IAAI,CAAC,IAAI,CAAC,UAAU,KAAK,SAAS,CAAC,CAAC,CAAC,MAAA,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAC,CAAC,0CAAE,OAAO,CAAC,CAAC,CAAC,SAAS,CAAC;AAChH,CAAC;AAGD,SAAS,YAAY,CAAC,EAAsB;IACxC,IAAI,KAAK,GAAG,KAAK,CAAC;IAClB,IAAA,iBAAI,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
1
|
+
import {Cursor, ExecutionContext, Recipe, Tree, TreeVisitor, check, not} from "@openrewrite/rewrite";
|
|
2
|
+
import {
|
|
3
|
+
JavaScriptVisitor, JS, JSX, MethodMatcher, Reference, compilationUnitOf, identifierOf, isVendoredOrBundled,
|
|
4
|
+
moduleNameOf, present, referencesOf, resolve, walk
|
|
5
|
+
} from "@openrewrite/rewrite/javascript";
|
|
6
|
+
import {J, Type} from "@openrewrite/rewrite/java";
|
|
4
7
|
import {create} from "mutative";
|
|
5
8
|
|
|
9
|
+
const REACT = "react";
|
|
10
|
+
// `@types/react` declares `persist()` on `BaseSyntheticEvent` since 16.7.18, and on `SyntheticEvent` before
|
|
11
|
+
const SYNTHETIC_EVENT_PERSIST = [
|
|
12
|
+
new MethodMatcher("React.BaseSyntheticEvent persist()"),
|
|
13
|
+
new MethodMatcher("React.SyntheticEvent persist()")
|
|
14
|
+
];
|
|
15
|
+
|
|
16
|
+
/** The module an `import` or a `require(...)` call names, as written. */
|
|
17
|
+
function requestedModule(n: Tree): J | undefined {
|
|
18
|
+
if (n.kind === JS.Kind.Import) return (n as JS.Import).moduleSpecifier?.element;
|
|
19
|
+
if (n.kind !== J.Kind.MethodInvocation) return undefined;
|
|
20
|
+
const call = n as J.MethodInvocation;
|
|
21
|
+
return !call.select && call.name.simpleName === "require" ? call.arguments.elements[0]?.element : undefined;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Whether the file imports or requires `react`, in any form: default, namespace, named or type-only. */
|
|
25
|
+
function importsReact(cu: JS.CompilationUnit): boolean {
|
|
26
|
+
let found = false;
|
|
27
|
+
walk(cu, n => {
|
|
28
|
+
const module = requestedModule(n);
|
|
29
|
+
found ||= module?.kind === J.Kind.Literal && moduleNameOf(module as J.Literal) === REACT;
|
|
30
|
+
return !found;
|
|
31
|
+
});
|
|
32
|
+
return found;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Whether `receiver` reads a parameter of an event handler, the way a handler reads its event: a parameter named
|
|
37
|
+
* outright, not one destructured from an argument, not shadowed by a local of the same name, and neither
|
|
38
|
+
* assigned to nor redeclared with `var`, either of which can make it hold something else. A parameter
|
|
39
|
+
* annotated with a type has to name an event type, or `any`, since `persistor: Persistor` is no event.
|
|
40
|
+
* Being a parameter is not enough, since `stores.forEach(store => store.persist())` passes no event: the
|
|
41
|
+
* parameter also has to be annotated with an event type, belong to a function passed to a JSX `on*` prop, or
|
|
42
|
+
* be read as an event is, as `e.target` or `e.preventDefault()` read it. What the function is named says
|
|
43
|
+
* nothing, since `onStoreReady(store)` takes no event.
|
|
44
|
+
*/
|
|
45
|
+
function readsParameter(cursor: Cursor, receiver: J.Identifier): boolean {
|
|
46
|
+
const name = receiver.simpleName;
|
|
47
|
+
const fn = resolve(cursor, receiver);
|
|
48
|
+
const parameter = parametersOf(fn).map(p => p.element).find(p => identifierOf(p)?.simpleName === name);
|
|
49
|
+
const annotation = parameter?.kind === J.Kind.VariableDeclarations ? (parameter as J.VariableDeclarations).typeExpression : undefined;
|
|
50
|
+
if (parameter === undefined || (annotation && !namesEventType(annotation))) return false;
|
|
51
|
+
const references = referencesOf(cursor, name);
|
|
52
|
+
return references.filter(r => r.declares || r.writes).length === 1 &&
|
|
53
|
+
(annotation !== undefined && !namesAny(annotation) || isJsxEventProp(cursor, fn!) || readsEventMember(references));
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// Members of a synthetic event, and of a DOM event, that little else has
|
|
57
|
+
const EVENT_MEMBERS = new Set([
|
|
58
|
+
"target", "currentTarget", "nativeEvent", "preventDefault", "stopPropagation", "isDefaultPrevented",
|
|
59
|
+
"isPropagationStopped"
|
|
60
|
+
]);
|
|
61
|
+
|
|
62
|
+
/** Whether a reference reads a member only an event is likely to have, as `e.target` or `e.preventDefault()` do. */
|
|
63
|
+
function readsEventMember(references: Reference[]): boolean {
|
|
64
|
+
return references.some(r => {
|
|
65
|
+
if (r.declares) return false;
|
|
66
|
+
const parent = r.cursor.parentTree()?.value as J | undefined;
|
|
67
|
+
switch (parent?.kind) {
|
|
68
|
+
case J.Kind.FieldAccess:
|
|
69
|
+
return EVENT_MEMBERS.has((parent as J.FieldAccess).name.element.simpleName);
|
|
70
|
+
case J.Kind.MethodInvocation:
|
|
71
|
+
return (parent as J.MethodInvocation).select?.element === r.identifier && EVENT_MEMBERS.has((parent as J.MethodInvocation).name.simpleName);
|
|
72
|
+
default:
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** Whether `fn` is passed straight to a JSX `on*` prop, as `onChange={e => …}` passes it. */
|
|
79
|
+
function isJsxEventProp(cursor: Cursor, fn: J): boolean {
|
|
80
|
+
let at: Cursor | undefined = cursor;
|
|
81
|
+
while (at && at.value !== fn) at = at.parent;
|
|
82
|
+
for (let parent = at?.parentTree(); parent; parent = parent.parentTree()) {
|
|
83
|
+
const node = parent.value as J;
|
|
84
|
+
if (node.kind === JS.Kind.JsxEmbeddedExpression || node.kind === J.Kind.Parentheses) continue;
|
|
85
|
+
if (node.kind !== JS.Kind.JsxAttribute) return false;
|
|
86
|
+
const key = (node as JSX.Attribute).key;
|
|
87
|
+
return key.kind === J.Kind.Identifier && /^on[A-Z]/.test(key.simpleName);
|
|
88
|
+
}
|
|
89
|
+
return false;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** Whether a type annotation is `any`, which says nothing of what the parameter holds. */
|
|
93
|
+
function namesAny(type: J): boolean {
|
|
94
|
+
const typeIdentifier = type.kind === JS.Kind.TypeInfo ? (type as JS.TypeInfo).typeIdentifier : type;
|
|
95
|
+
return typeIdentifier.kind === J.Kind.Identifier && (typeIdentifier as J.Identifier).simpleName === "any";
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** Whether a type annotation names an event type, such as `React.ChangeEvent<T>`, or `any`, in any member of a union. */
|
|
99
|
+
function namesEventType(type: J): boolean {
|
|
100
|
+
switch (type.kind) {
|
|
101
|
+
case JS.Kind.TypeInfo:
|
|
102
|
+
return namesEventType((type as JS.TypeInfo).typeIdentifier);
|
|
103
|
+
case JS.Kind.Union:
|
|
104
|
+
return (type as JS.Union).types.some(t => namesEventType(t.element));
|
|
105
|
+
case J.Kind.ParameterizedType:
|
|
106
|
+
return namesEventType((type as J.ParameterizedType).class);
|
|
107
|
+
case J.Kind.FieldAccess:
|
|
108
|
+
return namesEventType((type as J.FieldAccess).name.element);
|
|
109
|
+
case J.Kind.Identifier: {
|
|
110
|
+
const simpleName = (type as J.Identifier).simpleName;
|
|
111
|
+
return simpleName === "any" || simpleName.endsWith("Event");
|
|
112
|
+
}
|
|
113
|
+
default:
|
|
114
|
+
return false;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function parametersOf(fn: J | undefined): J.RightPadded<J>[] {
|
|
119
|
+
switch (fn?.kind) {
|
|
120
|
+
case J.Kind.MethodDeclaration:
|
|
121
|
+
case JS.Kind.ComputedPropertyMethodDeclaration:
|
|
122
|
+
return (fn as J.MethodDeclaration | JS.ComputedPropertyMethodDeclaration).parameters.elements;
|
|
123
|
+
case JS.Kind.ArrowFunction:
|
|
124
|
+
return (fn as JS.ArrowFunction).lambda.parameters.parameters;
|
|
125
|
+
case J.Kind.Lambda:
|
|
126
|
+
return (fn as J.Lambda).parameters.parameters;
|
|
127
|
+
default:
|
|
128
|
+
return [];
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
6
132
|
/**
|
|
7
133
|
* Removes `event.persist()` calls.
|
|
8
134
|
*
|
|
@@ -24,97 +150,61 @@ import {create} from "mutative";
|
|
|
24
150
|
* }
|
|
25
151
|
* ```
|
|
26
152
|
*
|
|
153
|
+
* Only `persist()` on a React synthetic event is removed. Without type attribution, the receiver has to
|
|
154
|
+
* be a parameter of an event handler, in a file that imports or requires `react`: annotated with an event
|
|
155
|
+
* type, of a function passed to a JSX `on*` prop, or read as an event is, as `e.target` or
|
|
156
|
+
* `e.preventDefault()` read it. `persistor.persist()`, `navigator.storage.persist()` or `store.persist()`
|
|
157
|
+
* in a `forEach` callback does real work and is kept.
|
|
158
|
+
* A `persist()` left behind is harmless, since React 17 keeps it as a no-op. Run it before a recipe that
|
|
159
|
+
* removes unused `react` imports, such as `UpdateReactImports`.
|
|
160
|
+
*
|
|
27
161
|
* @see https://legacy.reactjs.org/blog/2020/08/10/react-v17-rc.html#no-event-pooling
|
|
28
162
|
*/
|
|
29
163
|
export class RemoveEventPersist extends Recipe {
|
|
30
164
|
readonly name = "org.openrewrite.react.17.remove-event-persist";
|
|
31
165
|
readonly displayName: string = "Remove `event.persist()` calls";
|
|
32
|
-
readonly description: string = "Removes `
|
|
166
|
+
readonly description: string = "Removes `persist()` calls on React synthetic events, which do nothing since React 17 removed event pooling. Without type attribution, only a call on an event handler's parameter in a file that imports `react` is removed.";
|
|
33
167
|
|
|
34
168
|
async editor(): Promise<TreeVisitor<any, ExecutionContext>> {
|
|
35
169
|
return check(not(isVendoredOrBundled()), new class extends JavaScriptVisitor<ExecutionContext> {
|
|
36
|
-
|
|
37
|
-
let result = await super.visitJsCompilationUnit(cu, ctx) as JS.CompilationUnit;
|
|
38
|
-
|
|
39
|
-
const statementsToRemove = new Set<string>();
|
|
40
|
-
|
|
41
|
-
for (const stmt of result.statements) {
|
|
42
|
-
const s = stmt.element;
|
|
43
|
-
if (!s) continue;
|
|
44
|
-
if (this.isPersistCall(s)) {
|
|
45
|
-
statementsToRemove.add((s as any).id);
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
if (statementsToRemove.size === 0) return result;
|
|
170
|
+
private reactImported?: boolean;
|
|
50
171
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
return create(result, draft => {
|
|
57
|
-
draft.statements = filteredStatements as any;
|
|
58
|
-
});
|
|
172
|
+
override async visitJsCompilationUnit(cu: JS.CompilationUnit, ctx: ExecutionContext): Promise<J | undefined> {
|
|
173
|
+
this.reactImported = undefined;
|
|
174
|
+
return this.withoutPersistCalls(await super.visitJsCompilationUnit(cu, ctx) as JS.CompilationUnit);
|
|
59
175
|
}
|
|
60
176
|
|
|
61
177
|
override async visitBlock(block: J.Block, ctx: ExecutionContext): Promise<J | undefined> {
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
const statementsToRemove = new Set<string>();
|
|
65
|
-
|
|
66
|
-
for (const stmt of b.statements) {
|
|
67
|
-
const s = stmt.element;
|
|
68
|
-
if (!s) continue;
|
|
69
|
-
if (this.isPersistCall(s)) {
|
|
70
|
-
statementsToRemove.add((s as any).id);
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
if (statementsToRemove.size === 0) return b;
|
|
75
|
-
|
|
76
|
-
const filteredStatements = b.statements.filter(stmt => {
|
|
77
|
-
const s = stmt.element;
|
|
78
|
-
return !s || !statementsToRemove.has((s as any).id);
|
|
79
|
-
});
|
|
178
|
+
return this.withoutPersistCalls(await super.visitBlock(block, ctx) as J.Block);
|
|
179
|
+
}
|
|
80
180
|
|
|
81
|
-
|
|
82
|
-
|
|
181
|
+
private withoutPersistCalls<T extends JS.CompilationUnit | J.Block>(node: T): T {
|
|
182
|
+
const statements = node.statements.filter(stmt => !stmt.element || !this.isPersistCall(stmt.element));
|
|
183
|
+
if (statements.length === node.statements.length) return node;
|
|
184
|
+
return create(node, draft => {
|
|
185
|
+
draft.statements = statements as any;
|
|
83
186
|
});
|
|
84
187
|
}
|
|
85
188
|
|
|
86
|
-
private isPersistCall(stmt:
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
if (stmt.kind === JS.Kind.ExpressionStatement) {
|
|
90
|
-
expr = (stmt as JS.ExpressionStatement).expression;
|
|
91
|
-
} else if (stmt.kind === J.Kind.MethodInvocation) {
|
|
92
|
-
expr = stmt;
|
|
93
|
-
} else {
|
|
94
|
-
return false;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
if (!expr || expr.kind !== J.Kind.MethodInvocation) return false;
|
|
189
|
+
private isPersistCall(stmt: J): boolean {
|
|
190
|
+
const expr = stmt.kind === JS.Kind.ExpressionStatement ? (stmt as JS.ExpressionStatement).expression : stmt;
|
|
191
|
+
if (expr?.kind !== J.Kind.MethodInvocation) return false;
|
|
98
192
|
|
|
99
193
|
const method = expr as J.MethodInvocation;
|
|
100
194
|
|
|
101
|
-
|
|
102
|
-
if (!method.select) return false;
|
|
195
|
+
if (!method.select || method.name.simpleName !== 'persist' || present(method.arguments.elements).length > 0) return false;
|
|
103
196
|
|
|
104
|
-
|
|
105
|
-
if (
|
|
106
|
-
|
|
107
|
-
// Must have no real arguments (may contain J.Empty placeholder)
|
|
108
|
-
if (method.arguments) {
|
|
109
|
-
const args = method.arguments.elements;
|
|
110
|
-
const hasRealArgs = args.some((el: any) => {
|
|
111
|
-
const element = el.element ?? el;
|
|
112
|
-
return element.kind !== J.Kind.Empty;
|
|
113
|
-
});
|
|
114
|
-
if (hasRealArgs) return false;
|
|
197
|
+
const declaringType = method.methodType?.declaringType;
|
|
198
|
+
if (declaringType && declaringType.kind !== Type.Kind.Unknown) {
|
|
199
|
+
return SYNTHETIC_EVENT_PERSIST.some(matcher => matcher.matches(method.methodType));
|
|
115
200
|
}
|
|
116
|
-
|
|
117
|
-
|
|
201
|
+
// Without types, a parameter is only taken for an event in a file that uses React
|
|
202
|
+
const receiver = method.select.element;
|
|
203
|
+
// `resolve` takes a cursor on the call, under the statement that holds it
|
|
204
|
+
const statement = new Cursor(stmt, this.cursor);
|
|
205
|
+
return receiver.kind === J.Kind.Identifier &&
|
|
206
|
+
(this.reactImported ??= importsReact(compilationUnitOf(this)!)) &&
|
|
207
|
+
readsParameter(stmt === method ? statement : new Cursor(method, statement), receiver as J.Identifier);
|
|
118
208
|
}
|
|
119
209
|
}());
|
|
120
210
|
}
|