@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.
Files changed (80) hide show
  1. package/dist/migration/cra-to-vite/find-cra-config-blockers.js +20 -28
  2. package/dist/migration/cra-to-vite/find-cra-config-blockers.js.map +1 -1
  3. package/dist/migration/cra-to-vite/replace-react-scripts-dependency.d.ts.map +1 -1
  4. package/dist/migration/cra-to-vite/replace-react-scripts-dependency.js +13 -33
  5. package/dist/migration/cra-to-vite/replace-react-scripts-dependency.js.map +1 -1
  6. package/dist/migration/cra-to-vite/shared.d.ts +1 -14
  7. package/dist/migration/cra-to-vite/shared.d.ts.map +1 -1
  8. package/dist/migration/cra-to-vite/shared.js +13 -126
  9. package/dist/migration/cra-to-vite/shared.js.map +1 -1
  10. package/dist/migration/cra-to-vite/update-react-scripts-scripts.d.ts.map +1 -1
  11. package/dist/migration/cra-to-vite/update-react-scripts-scripts.js +16 -21
  12. package/dist/migration/cra-to-vite/update-react-scripts-scripts.js.map +1 -1
  13. package/dist/migration/cra-to-vite/update-tsconfig-for-vite.d.ts.map +1 -1
  14. package/dist/migration/cra-to-vite/update-tsconfig-for-vite.js +4 -13
  15. package/dist/migration/cra-to-vite/update-tsconfig-for-vite.js.map +1 -1
  16. package/dist/react17/migrate-deprecated-lifecycles.js +1 -1
  17. package/dist/react17/migrate-deprecated-lifecycles.js.map +1 -1
  18. package/dist/react17/use-new-jsx-transform.d.ts.map +1 -1
  19. package/dist/react17/use-new-jsx-transform.js +30 -78
  20. package/dist/react17/use-new-jsx-transform.js.map +1 -1
  21. package/dist/react18/replace-unmount-component-at-node.d.ts.map +1 -1
  22. package/dist/react18/replace-unmount-component-at-node.js +62 -20
  23. package/dist/react18/replace-unmount-component-at-node.js.map +1 -1
  24. package/dist/react19/replace-default-props.d.ts.map +1 -1
  25. package/dist/react19/replace-default-props.js +10 -13
  26. package/dist/react19/replace-default-props.js.map +1 -1
  27. package/dist/react19/replace-react-shallow-renderer.d.ts.map +1 -1
  28. package/dist/react19/replace-react-shallow-renderer.js +1 -6
  29. package/dist/react19/replace-react-shallow-renderer.js.map +1 -1
  30. package/dist/refactoring/create-class-to-es6.d.ts.map +1 -1
  31. package/dist/refactoring/create-class-to-es6.js +4 -7
  32. package/dist/refactoring/create-class-to-es6.js.map +1 -1
  33. package/dist/refactoring/create-element-to-jsx.d.ts.map +1 -1
  34. package/dist/refactoring/create-element-to-jsx.js +3 -4
  35. package/dist/refactoring/create-element-to-jsx.js.map +1 -1
  36. package/dist/refactoring/prop-types-to-typescript.js +2 -3
  37. package/dist/refactoring/prop-types-to-typescript.js.map +1 -1
  38. package/dist/refactoring/pure-render-mixin.d.ts.map +1 -1
  39. package/dist/refactoring/pure-render-mixin.js +3 -6
  40. package/dist/refactoring/pure-render-mixin.js.map +1 -1
  41. package/dist/testing/enzyme-host-components.d.ts +14 -0
  42. package/dist/testing/enzyme-host-components.d.ts.map +1 -0
  43. package/dist/testing/enzyme-host-components.js +245 -0
  44. package/dist/testing/enzyme-host-components.js.map +1 -0
  45. package/dist/testing/enzyme-to-react-testing-library.d.ts +10 -5
  46. package/dist/testing/enzyme-to-react-testing-library.d.ts.map +1 -1
  47. package/dist/testing/enzyme-to-react-testing-library.js +138 -113
  48. package/dist/testing/enzyme-to-react-testing-library.js.map +1 -1
  49. package/dist/testing/enzyme-wrapper-scope.d.ts +13 -4
  50. package/dist/testing/enzyme-wrapper-scope.d.ts.map +1 -1
  51. package/dist/testing/enzyme-wrapper-scope.js +161 -163
  52. package/dist/testing/enzyme-wrapper-scope.js.map +1 -1
  53. package/dist/util/module-imports.d.ts +12 -0
  54. package/dist/util/module-imports.d.ts.map +1 -0
  55. package/dist/util/module-imports.js +56 -0
  56. package/dist/util/module-imports.js.map +1 -0
  57. package/dist/util/react-class.d.ts +1 -0
  58. package/dist/util/react-class.d.ts.map +1 -1
  59. package/dist/util/react-class.js +3 -2
  60. package/dist/util/react-class.js.map +1 -1
  61. package/package.json +3 -3
  62. package/src/migration/cra-to-vite/find-cra-config-blockers.ts +21 -27
  63. package/src/migration/cra-to-vite/replace-react-scripts-dependency.ts +14 -39
  64. package/src/migration/cra-to-vite/shared.ts +15 -136
  65. package/src/migration/cra-to-vite/update-react-scripts-scripts.ts +16 -21
  66. package/src/migration/cra-to-vite/update-tsconfig-for-vite.ts +6 -13
  67. package/src/react17/migrate-deprecated-lifecycles.ts +2 -2
  68. package/src/react17/use-new-jsx-transform.ts +30 -72
  69. package/src/react18/replace-unmount-component-at-node.ts +78 -32
  70. package/src/react19/replace-default-props.ts +11 -15
  71. package/src/react19/replace-react-shallow-renderer.ts +2 -7
  72. package/src/refactoring/create-class-to-es6.ts +6 -9
  73. package/src/refactoring/create-element-to-jsx.ts +4 -6
  74. package/src/refactoring/prop-types-to-typescript.ts +4 -4
  75. package/src/refactoring/pure-render-mixin.ts +4 -7
  76. package/src/testing/enzyme-host-components.ts +283 -0
  77. package/src/testing/enzyme-to-react-testing-library.ts +176 -107
  78. package/src/testing/enzyme-wrapper-scope.ts +144 -137
  79. package/src/util/module-imports.ts +55 -0
  80. package/src/util/react-class.ts +2 -1
@@ -1,6 +1,6 @@
1
1
  import {Cursor, ExecutionContext} from "@openrewrite/rewrite";
2
- import {JavaScriptVisitor, JS, JSX} from "@openrewrite/rewrite/javascript";
3
- import {emptySpace, Expression, isIdentifier, J, Type} from "@openrewrite/rewrite/java";
2
+ import {isObjectLiteral, JavaScriptVisitor, JS, referencesOf, walk} from "@openrewrite/rewrite/javascript";
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
6
 
@@ -142,12 +142,6 @@ export interface WrapperScope {
142
142
  unmounts: Set<string>;
143
143
  }
144
144
 
145
- // The declaration is an Enzyme `mount`, so a reference is its wrapper unless another binding of the
146
- // name shadows it; a project without Enzyme's type declarations leaves the wrapper untyped.
147
- function untyped(type: Type | undefined): boolean {
148
- return !type || type.kind === Type.Kind.Unknown;
149
- }
150
-
151
145
  function noArguments(call: J.MethodInvocation): boolean {
152
146
  return call.arguments.elements.every(arg => arg.element.kind === J.Kind.Empty);
153
147
  }
@@ -199,11 +193,9 @@ function stringLiteral(node: J | undefined): node is J.Literal {
199
193
  }
200
194
 
201
195
  function objectProperties(node: J | undefined): Map<string, Expression> | undefined {
202
- if (node?.kind !== J.Kind.NewClass) return undefined;
203
- const object = node as J.NewClass;
204
- if (object.class || !object.body) return undefined;
196
+ if (!node || !isObjectLiteral(node) || !node.body) return undefined;
205
197
  const properties = new Map<string, Expression>();
206
- for (const statement of object.body.statements) {
198
+ for (const statement of node.body.statements) {
207
199
  const property = statement.element as JS.PropertyAssignment;
208
200
  if (property.kind !== JS.Kind.PropertyAssignment || property.assigmentToken !== "Colon" ||
209
201
  !isIdentifier(property.name.element) || !property.initializer) return undefined;
@@ -244,7 +236,18 @@ function expectArgument(cursor: Cursor, node: J): boolean {
244
236
  return !expect.select && expect.arguments.elements.length === 1 && expect.arguments.elements[0].element === node;
245
237
  }
246
238
 
247
- export function wrapperDeclaration(statement: J | undefined): {declaration: J.VariableDeclarations, name: string, mount: J.MethodInvocation} | undefined {
239
+ export interface WrapperDeclaration {
240
+ declaration: J.VariableDeclarations;
241
+ name: string;
242
+ mount: J.MethodInvocation;
243
+ }
244
+
245
+ export function variableCursor(block: Cursor, declaration: J.VariableDeclarations): Cursor {
246
+ return new Cursor(declaration.variables[0].element, new Cursor(declaration, block));
247
+ }
248
+
249
+ /** A `const` declaration of a wrapper that `classify` accepts the render of, with what it says of the render. */
250
+ export function wrapperDeclaration<R>(statement: J | undefined, classify: (render: J.MethodInvocation) => R | undefined): (WrapperDeclaration & {render: R}) | undefined {
248
251
  if (statement?.kind !== J.Kind.VariableDeclarations) return undefined;
249
252
  const declaration = statement as J.VariableDeclarations;
250
253
  if (declaration.variables.length !== 1 || declaration.typeExpression ||
@@ -254,156 +257,160 @@ export function wrapperDeclaration(statement: J | undefined): {declaration: J.Va
254
257
  const initializer = variable.initializer?.element;
255
258
  if (!isIdentifier(variable.name) || initializer?.kind !== J.Kind.MethodInvocation) return undefined;
256
259
  const mount = initializer as J.MethodInvocation;
257
- if (enzymeMethod(mount) !== "mount" || mount.typeParameters || !mountedElement(mount)) return undefined;
258
- return {declaration, name: variable.name.simpleName, mount};
260
+ if (mount.typeParameters || !mountedElement(mount)) return undefined;
261
+ const render = classify(mount);
262
+ return render === undefined ? undefined : {declaration, name: variable.name.simpleName, mount, render};
259
263
  }
260
264
 
261
265
  /**
262
266
  * Proves that every reference to a block-scoped wrapper is a supported DOM read, so the
263
267
  * declaration and all its uses can be converted together. Any other reference, including
264
- * shadowing declarations, property names, arguments, and returns of the same name, rejects
265
- * the whole scope.
268
+ * arguments, returns, and shorthand properties, rejects the whole scope. `events` admits `simulate`
269
+ * and `html` admits `html()`, neither of which a shallow wrapper reads as the DOM does.
266
270
  */
267
- export async function analyzeWrapperScope(block: J.Block, declaration: J.VariableDeclarations, name: string,
268
- events: boolean, untaggedPositions: boolean, cursor: Cursor,
269
- ctx: ExecutionContext): Promise<WrapperScope | undefined> {
271
+ export function analyzeWrapperScope(block: Cursor, {declaration, name}: WrapperDeclaration,
272
+ {events, html, untaggedPositions}: {events: boolean, html: boolean, untaggedPositions: boolean}): WrapperScope | undefined {
270
273
  const uses = new Map<string, WrapperRead>();
271
274
  const removedStatements = new Set<string>();
272
275
  const unmounts = new Set<string>();
273
- let declared = false;
274
- let supported = true;
275
276
  let unmounted = false;
276
277
  const variable = declaration.variables[0].element;
277
- await new class extends JavaScriptVisitor<ExecutionContext> {
278
- override async visitIdentifier(identifier: J.Identifier, ctx: ExecutionContext): Promise<J | undefined> {
279
- if (identifier.simpleName !== name || !supported) return identifier;
280
- if (identifier === variable.name) {
281
- declared = true;
282
- return identifier;
283
- }
284
- const use = unmounted ? undefined : this.use(identifier);
285
- if (!use) supported = false;
286
- return identifier;
287
- }
288
278
 
289
- private use(identifier: J.Identifier): boolean {
290
- if (!enzymeWrapper(identifier.type) && !untyped(identifier.type)) return false;
291
- const ancestors: Cursor[] = [];
292
- for (let c: Cursor | undefined = this.cursor.parentTree(); c && ancestors.length < 5; c = c.parentTree()) ancestors.push(c);
293
- const first = ancestors[0]?.value as J | undefined;
294
- if (!selectOf(first, identifier)) return false;
295
- const method = first as J.MethodInvocation;
296
- if (call(method, "find")) {
297
- const args = method.arguments.elements;
298
- if (args.length !== 1 || !selectorLiteral(args[0].element)) return false;
299
- const selector = args[0].element;
300
- let k = 1;
301
- let inner: J = method;
302
- let hostNodes = false;
303
- if (call(ancestors[k]?.value, "hostNodes") && selectOf(ancestors[k].value, inner) && noArguments(ancestors[k].value)) {
304
- inner = ancestors[k++].value;
305
- hostNodes = true;
306
- }
307
- const index = this.index(ancestors[k]?.value, inner);
308
- let within: J.Literal | undefined;
309
- if (index) {
310
- if (!untaggedPositions && !positional(selector, hostNodes)) return false;
279
+ const record = (node: Expression, read: WrapperRead): boolean => {
280
+ if (!simpleChain(node)) return false;
281
+ uses.set(node.id, read);
282
+ return true;
283
+ };
284
+
285
+ const elementIndex = (node: J | undefined, inner: J): ElementIndex | undefined => {
286
+ if (!selectOf(node, inner)) return undefined;
287
+ const method = node as J.MethodInvocation;
288
+ if ((call(method, "first") || call(method, "last")) && noArguments(method)) return method.name.simpleName as "first" | "last";
289
+ const [position] = method.arguments.elements;
290
+ if (call(method, "at") && method.arguments.elements.length === 1 && position.element.kind === J.Kind.Literal &&
291
+ Number.isInteger((position.element as J.Literal).value) && ((position.element as J.Literal).value as number) >= 0) {
292
+ return position.element as J.Literal;
293
+ }
294
+ return undefined;
295
+ };
296
+
297
+ const use = (identifier: J.Identifier, reference: Cursor): boolean => {
298
+ const ancestors: Cursor[] = [];
299
+ for (let c: Cursor | undefined = reference.parentTree(); c && ancestors.length < 5; c = c.parentTree()) ancestors.push(c);
300
+ const first = ancestors[0]?.value as J | undefined;
301
+ if (!selectOf(first, identifier)) return false;
302
+ const method = first as J.MethodInvocation;
303
+ if (call(method, "find")) {
304
+ const args = method.arguments.elements;
305
+ if (args.length !== 1 || !selectorLiteral(args[0].element)) return false;
306
+ const selector = args[0].element;
307
+ let k = 1;
308
+ let inner: J = method;
309
+ let hostNodes = false;
310
+ if (call(ancestors[k]?.value, "hostNodes") && selectOf(ancestors[k].value, inner) && noArguments(ancestors[k].value)) {
311
+ inner = ancestors[k++].value;
312
+ hostNodes = true;
313
+ }
314
+ const index = elementIndex(ancestors[k]?.value, inner);
315
+ let within: J.Literal | undefined;
316
+ if (index) {
317
+ if (!untaggedPositions && !positional(selector, hostNodes)) return false;
318
+ inner = ancestors[k++].value;
319
+ const nested = ancestors[k]?.value as J | undefined;
320
+ if (call(nested, "find") && selectOf(nested, inner)) {
321
+ const [argument] = (nested as J.MethodInvocation).arguments.elements;
322
+ if ((nested as J.MethodInvocation).arguments.elements.length !== 1 || !selectorLiteral(argument.element)) return false;
323
+ within = argument.element;
311
324
  inner = ancestors[k++].value;
312
- const nested = ancestors[k]?.value as J | undefined;
313
- if (call(nested, "find") && selectOf(nested, inner)) {
314
- const [argument] = (nested as J.MethodInvocation).arguments.elements;
315
- if ((nested as J.MethodInvocation).arguments.elements.length !== 1 || !selectorLiteral(argument.element)) return false;
316
- within = argument.element;
317
- inner = ancestors[k++].value;
318
- }
319
- }
320
- const outer = ancestors[k]?.value as J | undefined;
321
- const outerCursor = ancestors[k];
322
- const read = {wrapper: identifier, selector, index, within};
323
- if ((!index || within) && outer?.kind === J.Kind.FieldAccess && (outer as J.FieldAccess).target === inner &&
324
- (outer as J.FieldAccess).name.element.simpleName === "length") {
325
- return this.record(outer as J.FieldAccess, {...read, use: "length"});
326
325
  }
327
- if (!index && expectArgument(ancestors[k - 1], inner)) {
328
- const matcher = nodesMatcher(outerCursor);
329
- return matcher !== undefined && this.record(inner as Expression, {...read, use: "nodes", vacuous: matcher === "truthy"});
330
- }
331
- if (!selectOf(outer, inner)) return false;
332
- const terminal = outer as J.MethodInvocation;
333
- const terminalArgs = terminal.arguments.elements.filter(arg => arg.element.kind !== J.Kind.Empty).map(arg => arg.element);
334
- if (call(terminal, "exists") && terminalArgs.length === 0) return this.record(terminal, {...read, use: !index || within ? "exists" : "positionExists"});
335
- if (within) return false;
336
- if ((call(terminal, "text") || call(terminal, "html")) && terminalArgs.length === 0) {
337
- return this.record(terminal, {...read, use: call(terminal, "text") ? "selectorText" : "html",
338
- unasserted: !expectArgument(outerCursor, terminal)});
339
- }
340
- if (call(terminal, "hasClass") && terminalArgs.length === 1 && stringLiteral(terminalArgs[0]) &&
341
- /^-?[_a-zA-Z][\w-]*$/.test(terminalArgs[0].value as string)) {
342
- return this.record(terminal, {...read, use: "hasClass", className: terminalArgs[0]});
343
- }
344
- if (call(terminal, "simulate") && events && statement(outerCursor) && terminalArgs.length >= 1 && terminalArgs.length <= 2 &&
345
- stringLiteral(terminalArgs[0])) {
346
- const name = terminalArgs[0].value as string;
347
- const event = SIMULATED_EVENTS.has(name) ? name : NATIVE_EVENT_NAMES.get(name);
348
- if (!event) return false;
349
- const nativeEvent = nativeEventInit(event, terminalArgs[1]);
350
- if (!nativeEvent && !eventInit(event, terminalArgs[1])) return false;
351
- if (event === "click" && CLICK_DEFAULT_ACTIONS.has(selectorTag(selector) ?? "")) return false;
352
- return this.record(terminal, {...read, use: "simulate", event, ...nativeEvent ? {nativeEvent} : {eventInit: terminalArgs[1]}});
353
- }
354
- return false;
355
326
  }
356
- const [argument] = method.arguments.elements;
357
- if (call(method, "exists") && method.arguments.elements.length === 1 && selectorLiteral(argument.element)) {
358
- return this.record(method, {wrapper: identifier, selector: argument.element, use: "exists"});
327
+ const outer = ancestors[k]?.value as J | undefined;
328
+ const outerCursor = ancestors[k];
329
+ const read = {wrapper: identifier, selector, index, within};
330
+ if ((!index || within) && outer?.kind === J.Kind.FieldAccess && (outer as J.FieldAccess).target === inner &&
331
+ (outer as J.FieldAccess).name.element.simpleName === "length") {
332
+ return record(outer as J.FieldAccess, {...read, use: "length"});
359
333
  }
360
- if (call(method, "setProps") && method.arguments.elements.length === 1 && statement(ancestors[0]) &&
361
- argument.element.kind !== J.Kind.Empty && argument.element.kind !== JS.Kind.Spread) {
362
- return this.record(method, {wrapper: identifier, use: "setProps", props: argument.element as Expression});
334
+ if (!index && expectArgument(ancestors[k - 1], inner)) {
335
+ const matcher = nodesMatcher(outerCursor);
336
+ return matcher !== undefined && record(inner as Expression, {...read, use: "nodes", vacuous: matcher === "truthy"});
363
337
  }
364
- if (!noArguments(method)) return false;
365
- if (call(method, "text")) return this.record(method, {wrapper: identifier, use: "text", unasserted: !expectArgument(ancestors[0], method)});
366
- if (call(method, "update") && statement(ancestors[0])?.prefix.comments.length === 0 && method.prefix.comments.length === 0) {
367
- removedStatements.add(method.id);
368
- return simpleChain(method);
338
+ if (!selectOf(outer, inner)) return false;
339
+ const terminal = outer as J.MethodInvocation;
340
+ const terminalArgs = terminal.arguments.elements.filter(arg => arg.element.kind !== J.Kind.Empty).map(arg => arg.element);
341
+ if (call(terminal, "exists") && terminalArgs.length === 0) return record(terminal, {...read, use: !index || within ? "exists" : "positionExists"});
342
+ if (within) return false;
343
+ if ((call(terminal, "text") || html && call(terminal, "html")) && terminalArgs.length === 0) {
344
+ return record(terminal, {...read, use: call(terminal, "text") ? "selectorText" : "html",
345
+ unasserted: !expectArgument(outerCursor, terminal)});
369
346
  }
370
- if (call(method, "unmount") && statement(ancestors[0])) {
371
- unmounted = true;
372
- unmounts.add(method.id);
373
- return simpleChain(method);
347
+ if (call(terminal, "hasClass") && terminalArgs.length === 1 && stringLiteral(terminalArgs[0]) &&
348
+ /^-?[_a-zA-Z][\w-]*$/.test(terminalArgs[0].value as string)) {
349
+ return record(terminal, {...read, use: "hasClass", className: terminalArgs[0]});
350
+ }
351
+ if (call(terminal, "simulate") && events && statement(outerCursor) && terminalArgs.length >= 1 && terminalArgs.length <= 2 &&
352
+ stringLiteral(terminalArgs[0])) {
353
+ const name = terminalArgs[0].value as string;
354
+ const event = SIMULATED_EVENTS.has(name) ? name : NATIVE_EVENT_NAMES.get(name);
355
+ if (!event) return false;
356
+ const nativeEvent = nativeEventInit(event, terminalArgs[1]);
357
+ if (!nativeEvent && !eventInit(event, terminalArgs[1])) return false;
358
+ if (event === "click" && CLICK_DEFAULT_ACTIONS.has(selectorTag(selector) ?? "")) return false;
359
+ return record(terminal, {...read, use: "simulate", event, ...nativeEvent ? {nativeEvent} : {eventInit: terminalArgs[1]}});
374
360
  }
375
361
  return false;
376
362
  }
377
-
378
- private index(node: J | undefined, inner: J): ElementIndex | undefined {
379
- if (!selectOf(node, inner)) return undefined;
380
- const method = node as J.MethodInvocation;
381
- if ((call(method, "first") || call(method, "last")) && noArguments(method)) return method.name.simpleName as "first" | "last";
382
- const [position] = method.arguments.elements;
383
- if (call(method, "at") && method.arguments.elements.length === 1 && position.element.kind === J.Kind.Literal &&
384
- Number.isInteger((position.element as J.Literal).value) && ((position.element as J.Literal).value as number) >= 0) {
385
- return position.element as J.Literal;
386
- }
387
- return undefined;
363
+ const [argument] = method.arguments.elements;
364
+ if (call(method, "exists") && method.arguments.elements.length === 1 && selectorLiteral(argument.element)) {
365
+ return record(method, {wrapper: identifier, selector: argument.element, use: "exists"});
388
366
  }
389
-
390
- private record(node: Expression, read: WrapperRead): boolean {
391
- if (!simpleChain(node)) return false;
392
- uses.set(node.id, read);
393
- return true;
367
+ if (call(method, "setProps") && method.arguments.elements.length === 1 && statement(ancestors[0]) &&
368
+ argument.element.kind !== J.Kind.Empty && argument.element.kind !== JS.Kind.Spread) {
369
+ return record(method, {wrapper: identifier, use: "setProps", props: argument.element as Expression});
394
370
  }
395
- }().visit(block, ctx, cursor);
396
- return declared && supported ? {declaration, name, uses, removedStatements, unmounts} : undefined;
371
+ if (!noArguments(method)) return false;
372
+ if (call(method, "text")) return record(method, {wrapper: identifier, use: "text", unasserted: !expectArgument(ancestors[0], method)});
373
+ if (call(method, "update") && statement(ancestors[0])?.prefix.comments.length === 0 && method.prefix.comments.length === 0) {
374
+ removedStatements.add(method.id);
375
+ return simpleChain(method);
376
+ }
377
+ if (call(method, "unmount") && statement(ancestors[0])) {
378
+ unmounted = true;
379
+ unmounts.add(method.id);
380
+ return simpleChain(method);
381
+ }
382
+ return false;
383
+ };
384
+
385
+ const declared = new Cursor(variable.name, variableCursor(block, declaration));
386
+ const references = referencesOf(declared, name, "value");
387
+ if (!references.some(reference => reference.identifier === variable.name)) return undefined;
388
+ for (const reference of references) {
389
+ if (reference.identifier === variable.name) continue;
390
+ if (reference.declares || unmounted || !use(reference.identifier, reference.cursor)) return undefined;
391
+ }
392
+ return {declaration, name, uses, removedStatements, unmounts};
397
393
  }
398
394
 
399
- export async function idsWithin(node: J, ids: Set<string>, ctx: ExecutionContext): Promise<string[]> {
395
+ /** The call a chain of calls and field accesses starts with, such as `mount(...)` in `mount(...).find(...).length`. */
396
+ export function innermostCall(node: J): J.MethodInvocation | undefined {
397
+ let call: J.MethodInvocation | undefined;
398
+ for (let current: J | undefined = node; current;) {
399
+ if (current.kind === J.Kind.FieldAccess) current = (current as J.FieldAccess).target;
400
+ else if (current.kind === J.Kind.MethodInvocation) {
401
+ call = current as J.MethodInvocation;
402
+ current = call.select?.element;
403
+ } else break;
404
+ }
405
+ return call;
406
+ }
407
+
408
+ export function idsWithin(node: J, ids: Set<string>): string[] {
400
409
  const found: string[] = [];
401
- await new class extends JavaScriptVisitor<ExecutionContext> {
402
- protected override async preVisit(tree: J): Promise<J | undefined> {
403
- if (ids.has(tree.id)) found.push(tree.id);
404
- return tree;
405
- }
406
- }().visit(node, ctx);
410
+ walk(node, tree => {
411
+ if (ids.has(tree.id)) found.push(tree.id);
412
+ return true;
413
+ });
407
414
  return found;
408
415
  }
409
416
 
@@ -0,0 +1,55 @@
1
+ import {JS, moduleNameOf} from "@openrewrite/rewrite/javascript";
2
+ import {isIdentifier, J} from "@openrewrite/rewrite/java";
3
+ import {posix} from "path";
4
+
5
+ /** A value an ES module import binds: its local name, the export it names (`default` for a default import), and the module. */
6
+ export interface ImportBinding {
7
+ local: string;
8
+ name: string;
9
+ module: string;
10
+ }
11
+
12
+ /** The values a source binds with top-level ES module imports, leaving out type-only and namespace imports. */
13
+ export function importBindings(cu: JS.CompilationUnit): ImportBinding[] {
14
+ const bindings: ImportBinding[] = [];
15
+ for (const {element} of cu.statements) {
16
+ if (element.kind !== JS.Kind.Import) continue;
17
+ const declaration = element as JS.Import;
18
+ const specifier = declaration.moduleSpecifier?.element;
19
+ const clause = declaration.importClause;
20
+ if (specifier?.kind !== J.Kind.Literal || !clause || clause.typeOnly) continue;
21
+ const module = moduleNameOf(specifier as J.Literal);
22
+ if (clause.name) bindings.push({local: clause.name.element.simpleName, name: "default", module});
23
+ if (clause.namedBindings?.kind !== JS.Kind.NamedImports) continue;
24
+ for (const {element: imported} of (clause.namedBindings as JS.NamedImports).elements.elements) {
25
+ if (imported.kind !== JS.Kind.ImportSpecifier || imported.importType.element) continue;
26
+ const named = imported.specifier;
27
+ if (isIdentifier(named)) bindings.push({local: named.simpleName, name: named.simpleName, module});
28
+ if (named.kind === JS.Kind.Alias && isIdentifier(named.alias)) {
29
+ const property = named.propertyName.element;
30
+ bindings.push({local: named.alias.simpleName, name: isIdentifier(property) ? property.simpleName : String((property as J.Literal).value), module});
31
+ }
32
+ }
33
+ }
34
+ return bindings;
35
+ }
36
+
37
+ /** The local names of a source's namespace imports, `import * as name`, which bind a whole module. */
38
+ export function namespaceImports(cu: JS.CompilationUnit): string[] {
39
+ const names: string[] = [];
40
+ for (const {element} of cu.statements) {
41
+ const bindings = element.kind === JS.Kind.Import ? (element as JS.Import).importClause?.namedBindings : undefined;
42
+ if (bindings?.kind === JS.Kind.Alias && isIdentifier((bindings as JS.Alias).alias)) names.push(((bindings as JS.Alias).alias as J.Identifier).simpleName);
43
+ }
44
+ return names;
45
+ }
46
+
47
+ // Node resolves a relative specifier to the file before the directory's index
48
+ const RESOLVED = ["", ".js", ".jsx", ".ts", ".tsx", "/index.js", "/index.jsx", "/index.ts", "/index.tsx"];
49
+
50
+ /** The source a relative module specifier resolves to from the source at `from`, as Node resolves it among `sources`. */
51
+ export function resolveRelative(from: string, specifier: string, sources: {has(path: string): boolean}): string | undefined {
52
+ if (!/^\.\.?(?:\/|$)/.test(specifier)) return undefined;
53
+ const base = posix.normalize(posix.join(posix.dirname(from), specifier));
54
+ return RESOLVED.map(suffix => base + suffix).find(path => sources.has(path));
55
+ }
@@ -1,7 +1,8 @@
1
1
  import {J} from "@openrewrite/rewrite/java";
2
2
  import {JS} from "@openrewrite/rewrite/javascript";
3
3
 
4
- const COMPONENT_BASES = new Set(['Component', 'PureComponent']);
4
+ /** The React base classes of a class component. */
5
+ export const COMPONENT_BASES: ReadonlySet<string> = new Set(['Component', 'PureComponent']);
5
6
 
6
7
  /** `Component<P, S>` is a parameterized type (possibly wrapped in a type-tree expression) around the plain name; this returns that name. */
7
8
  export function unwrapBaseType(type: J | undefined): J | undefined {