@openrewrite/rewrite 8.97.0 → 8.97.2

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 (152) hide show
  1. package/README.md +42 -0
  2. package/dist/java/visitor.d.ts +2 -0
  3. package/dist/java/visitor.d.ts.map +1 -1
  4. package/dist/java/visitor.js +7 -3
  5. package/dist/java/visitor.js.map +1 -1
  6. package/dist/javascript/add-import.d.ts +1 -6
  7. package/dist/javascript/add-import.d.ts.map +1 -1
  8. package/dist/javascript/add-import.js +90 -58
  9. package/dist/javascript/add-import.js.map +1 -1
  10. package/dist/javascript/comparator.d.ts +16 -10
  11. package/dist/javascript/comparator.d.ts.map +1 -1
  12. package/dist/javascript/comparator.js +10 -8
  13. package/dist/javascript/comparator.js.map +1 -1
  14. package/dist/javascript/package-json-parser.d.ts +2 -0
  15. package/dist/javascript/package-json-parser.d.ts.map +1 -1
  16. package/dist/javascript/package-json-parser.js +27 -15
  17. package/dist/javascript/package-json-parser.js.map +1 -1
  18. package/dist/javascript/package-manager.d.ts +13 -4
  19. package/dist/javascript/package-manager.d.ts.map +1 -1
  20. package/dist/javascript/package-manager.js +134 -17
  21. package/dist/javascript/package-manager.js.map +1 -1
  22. package/dist/javascript/parser.d.ts +2 -0
  23. package/dist/javascript/parser.d.ts.map +1 -1
  24. package/dist/javascript/parser.js +23 -58
  25. package/dist/javascript/parser.js.map +1 -1
  26. package/dist/javascript/print.d.ts.map +1 -1
  27. package/dist/javascript/print.js +1 -1
  28. package/dist/javascript/print.js.map +1 -1
  29. package/dist/javascript/recipes/change-method-target-to-import.d.ts +4 -0
  30. package/dist/javascript/recipes/change-method-target-to-import.d.ts.map +1 -1
  31. package/dist/javascript/recipes/change-method-target-to-import.js +20 -2
  32. package/dist/javascript/recipes/change-method-target-to-import.js.map +1 -1
  33. package/dist/javascript/recipes/dependencies.d.ts +19 -0
  34. package/dist/javascript/recipes/dependencies.d.ts.map +1 -1
  35. package/dist/javascript/recipes/dependencies.js +8 -0
  36. package/dist/javascript/recipes/dependencies.js.map +1 -1
  37. package/dist/javascript/recipes/order-imports.js +3 -3
  38. package/dist/javascript/recipes/order-imports.js.map +1 -1
  39. package/dist/javascript/remove-import.d.ts.map +1 -1
  40. package/dist/javascript/remove-import.js +115 -33
  41. package/dist/javascript/remove-import.js.map +1 -1
  42. package/dist/javascript/scope.d.ts +7 -0
  43. package/dist/javascript/scope.d.ts.map +1 -1
  44. package/dist/javascript/scope.js +23 -0
  45. package/dist/javascript/scope.js.map +1 -1
  46. package/dist/javascript/templating/bindings.d.ts.map +1 -1
  47. package/dist/javascript/templating/bindings.js +0 -4
  48. package/dist/javascript/templating/bindings.js.map +1 -1
  49. package/dist/javascript/templating/comparator.d.ts +32 -79
  50. package/dist/javascript/templating/comparator.d.ts.map +1 -1
  51. package/dist/javascript/templating/comparator.js +35 -628
  52. package/dist/javascript/templating/comparator.js.map +1 -1
  53. package/dist/javascript/templating/debug.d.ts +143 -0
  54. package/dist/javascript/templating/debug.d.ts.map +1 -0
  55. package/dist/javascript/templating/debug.js +769 -0
  56. package/dist/javascript/templating/debug.js.map +1 -0
  57. package/dist/javascript/templating/engine.d.ts +12 -3
  58. package/dist/javascript/templating/engine.d.ts.map +1 -1
  59. package/dist/javascript/templating/engine.js +135 -76
  60. package/dist/javascript/templating/engine.js.map +1 -1
  61. package/dist/javascript/templating/index.d.ts +1 -1
  62. package/dist/javascript/templating/index.d.ts.map +1 -1
  63. package/dist/javascript/templating/index.js.map +1 -1
  64. package/dist/javascript/templating/pattern.d.ts +44 -35
  65. package/dist/javascript/templating/pattern.d.ts.map +1 -1
  66. package/dist/javascript/templating/pattern.js +224 -238
  67. package/dist/javascript/templating/pattern.js.map +1 -1
  68. package/dist/javascript/templating/rewrite.d.ts +4 -0
  69. package/dist/javascript/templating/rewrite.d.ts.map +1 -1
  70. package/dist/javascript/templating/rewrite.js +5 -1
  71. package/dist/javascript/templating/rewrite.js.map +1 -1
  72. package/dist/javascript/templating/template.js +1 -1
  73. package/dist/javascript/templating/template.js.map +1 -1
  74. package/dist/javascript/templating/types.d.ts +38 -35
  75. package/dist/javascript/templating/types.d.ts.map +1 -1
  76. package/dist/javascript/templating/utils.d.ts.map +1 -1
  77. package/dist/javascript/templating/utils.js +18 -9
  78. package/dist/javascript/templating/utils.js.map +1 -1
  79. package/dist/javascript/tree.d.ts +3 -1
  80. package/dist/javascript/tree.d.ts.map +1 -1
  81. package/dist/javascript/tree.js +5 -0
  82. package/dist/javascript/tree.js.map +1 -1
  83. package/dist/json/tree.d.ts +1 -2
  84. package/dist/json/tree.d.ts.map +1 -1
  85. package/dist/json/tree.js +10 -7
  86. package/dist/json/tree.js.map +1 -1
  87. package/dist/rewrite-javascript-version.txt +1 -1
  88. package/dist/rpc/request/get-object.d.ts.map +1 -1
  89. package/dist/rpc/request/get-object.js +5 -3
  90. package/dist/rpc/request/get-object.js.map +1 -1
  91. package/dist/rpc/request/install-recipes.d.ts +10 -0
  92. package/dist/rpc/request/install-recipes.d.ts.map +1 -1
  93. package/dist/rpc/request/install-recipes.js +125 -51
  94. package/dist/rpc/request/install-recipes.js.map +1 -1
  95. package/dist/rpc/request/prepare-recipe.d.ts +1 -0
  96. package/dist/rpc/request/prepare-recipe.d.ts.map +1 -1
  97. package/dist/rpc/request/prepare-recipe.js +13 -6
  98. package/dist/rpc/request/prepare-recipe.js.map +1 -1
  99. package/dist/rpc/rewrite-rpc.d.ts +1 -1
  100. package/dist/rpc/rewrite-rpc.d.ts.map +1 -1
  101. package/dist/rpc/rewrite-rpc.js +2 -3
  102. package/dist/rpc/rewrite-rpc.js.map +1 -1
  103. package/dist/rpc/worker-transport.d.ts +7 -3
  104. package/dist/rpc/worker-transport.d.ts.map +1 -1
  105. package/dist/rpc/worker-transport.js +54 -41
  106. package/dist/rpc/worker-transport.js.map +1 -1
  107. package/dist/test/rewrite-test.d.ts +1 -16
  108. package/dist/test/rewrite-test.d.ts.map +1 -1
  109. package/dist/test/rewrite-test.js +7 -58
  110. package/dist/test/rewrite-test.js.map +1 -1
  111. package/dist/util.d.ts +16 -0
  112. package/dist/util.d.ts.map +1 -1
  113. package/dist/util.js +53 -0
  114. package/dist/util.js.map +1 -1
  115. package/dist/yaml/parser.js +8 -5
  116. package/dist/yaml/parser.js.map +1 -1
  117. package/dist/yaml/print.js +9 -3
  118. package/dist/yaml/print.js.map +1 -1
  119. package/package.json +1 -1
  120. package/src/java/visitor.ts +8 -3
  121. package/src/javascript/add-import.ts +98 -64
  122. package/src/javascript/comparator.ts +66 -53
  123. package/src/javascript/package-json-parser.ts +25 -16
  124. package/src/javascript/package-manager.ts +121 -15
  125. package/src/javascript/parser.ts +29 -81
  126. package/src/javascript/print.ts +1 -2
  127. package/src/javascript/recipes/change-method-target-to-import.ts +23 -3
  128. package/src/javascript/recipes/dependencies.ts +23 -0
  129. package/src/javascript/recipes/order-imports.ts +5 -5
  130. package/src/javascript/remove-import.ts +113 -41
  131. package/src/javascript/scope.ts +23 -0
  132. package/src/javascript/templating/bindings.ts +1 -6
  133. package/src/javascript/templating/comparator.ts +76 -766
  134. package/src/javascript/templating/debug.ts +806 -0
  135. package/src/javascript/templating/engine.ts +138 -77
  136. package/src/javascript/templating/index.ts +1 -0
  137. package/src/javascript/templating/pattern.ts +267 -270
  138. package/src/javascript/templating/rewrite.ts +5 -1
  139. package/src/javascript/templating/template.ts +1 -1
  140. package/src/javascript/templating/types.ts +40 -37
  141. package/src/javascript/templating/utils.ts +4 -12
  142. package/src/javascript/tree.ts +6 -1
  143. package/src/json/tree.ts +9 -7
  144. package/src/rpc/request/get-object.ts +34 -28
  145. package/src/rpc/request/install-recipes.ts +128 -52
  146. package/src/rpc/request/prepare-recipe.ts +18 -15
  147. package/src/rpc/rewrite-rpc.ts +2 -4
  148. package/src/rpc/worker-transport.ts +63 -43
  149. package/src/test/rewrite-test.ts +2 -59
  150. package/src/util.ts +58 -0
  151. package/src/yaml/parser.ts +9 -5
  152. package/src/yaml/print.ts +9 -3
@@ -207,6 +207,19 @@ function isTypeReference(name: string): boolean {
207
207
  return /^[A-Za-z_$][\w$]*(\.[A-Za-z_$][\w$]*)*$/.test(name);
208
208
  }
209
209
 
210
+ /** A type as TypeScript writes it, and how tightly it binds, which is where it needs parentheses. */
211
+ interface WrittenType {
212
+ readonly text: string;
213
+ readonly binding: 'union' | 'intersection' | 'primary';
214
+ }
215
+
216
+ /** The TypeScript type of each primitive keyword: the parser types a bigint as a long, and undefined with none. */
217
+ const primitiveTypes = new Map([
218
+ ['String', 'string'], ['boolean', 'boolean'], ['void', 'void'], ['null', 'null'], ['', 'undefined'],
219
+ ['long', 'bigint'], ['double', 'number'], ['float', 'number'], ['int', 'number'], ['short', 'number'],
220
+ ['byte', 'number']
221
+ ]);
222
+
210
223
  /**
211
224
  * The compiler's diagnostic for code that did not parse, as one line: the marker states it ahead
212
225
  * of a stack, and its positions index the whole parsed text, context statements included.
@@ -481,64 +494,97 @@ export class TemplateEngine {
481
494
  * Converts a Type instance to a TypeScript type string.
482
495
  *
483
496
  * @param type The Type instance
484
- * @param enclosing The types that `type` is a type argument or element type of
497
+ * @param enclosing The types that `type` is a type argument, element type, member or bound of
485
498
  * @returns A TypeScript type string
486
499
  */
487
500
  private static typeToString(type: Type, enclosing: Type[] = []): string {
501
+ return this.writeType(type, enclosing).text;
502
+ }
503
+
504
+ /** {@link typeToString}, with how tightly the type it writes binds, to parenthesize it by. */
505
+ private static writeType(type: Type, enclosing: Type[]): WrittenType {
488
506
  // A recursive type, such as `type A = Array<A>`, is any where it holds itself
489
507
  if (enclosing.includes(type)) {
490
- return 'any';
508
+ return {text: 'any', binding: 'primary'};
491
509
  }
492
-
493
- // Handle Type.Class and Type.ShallowClass - return their fully qualified names
494
- if (type.kind === Type.Kind.Class || type.kind === Type.Kind.ShallowClass) {
495
- const classType = type as Type.Class;
496
- // A module qualifies its types by where they come from — `src/a.Foo` — and a path that
497
- // is no type reference cannot be declared for. Naming a module's type takes a `context`
498
- // statement of the caller's own, which `type` then names.
499
- return isTypeReference(classType.fullyQualifiedName) ? classType.fullyQualifiedName : 'any';
500
- }
501
-
502
- // Handle Type.Primitive - map to TypeScript primitive types
503
- if (type.kind === Type.Kind.Primitive) {
504
- const primitiveType = type as Type.Primitive;
505
- switch (primitiveType.keyword) {
506
- case 'String':
507
- return 'string';
508
- case 'boolean':
509
- return 'boolean';
510
- case 'double':
511
- case 'float':
512
- case 'int':
513
- case 'long':
514
- case 'short':
515
- case 'byte':
516
- return 'number';
517
- case 'void':
518
- return 'void';
519
- default:
520
- return 'any';
510
+ const nested = [...enclosing, type];
511
+ const primary = (text: string): WrittenType => ({text, binding: 'primary'});
512
+ const typeArguments = (args: Type[]) => args.map(t => this.typeToString(t, nested)).join(', ');
513
+
514
+ switch (type.kind) {
515
+ case Type.Kind.Class:
516
+ case Type.Kind.ShallowClass: {
517
+ const classType = type as Type.Class;
518
+ const name = this.className(classType);
519
+ // A generic type the parser leaves without its arguments, as it does `Array<T>` where `T`
520
+ // is a type variable, takes what each of its own type variables stands for
521
+ return primary(!name ? 'any' :
522
+ classType.typeParameters?.length ? `${name}<${typeArguments(classType.typeParameters)}>` : name);
521
523
  }
524
+ case Type.Kind.Parameterized: {
525
+ // An array, a map or any other generic type, with its type arguments
526
+ const {type: base, typeParameters} = type as Type.Parameterized;
527
+ if (typeParameters.length === 0) {
528
+ return this.writeType(base, nested);
529
+ }
530
+ const name = this.className(base);
531
+ return primary(name ? `${name}<${typeArguments(typeParameters)}>` : 'any');
532
+ }
533
+ case Type.Kind.Primitive:
534
+ return primary(primitiveTypes.get((type as Type.Primitive).keyword) ?? 'any');
535
+ case Type.Kind.Array: {
536
+ const elemType = this.writeType((type as Type.Array).elemType, nested);
537
+ return primary(`${elemType.binding === 'primary' ? elemType.text : `(${elemType.text})`}[]`);
538
+ }
539
+ case Type.Kind.Union: {
540
+ // A member that could be anything makes the union so too
541
+ const members = this.writeMembers((type as Type.Union).bounds, nested);
542
+ const top = members.find(m => m.text === 'any') ?? members.find(m => m.text === 'unknown');
543
+ return top ?? (members.length === 1 ? members[0] :
544
+ {text: members.map(m => m.text).join(' | '), binding: 'union'});
545
+ }
546
+ case Type.Kind.Intersection: {
547
+ // A value of an intersection is a value of each member, so the members that can be written
548
+ // still type it, where the rest would make it any
549
+ const all = this.writeMembers((type as Type.Intersection).bounds, nested);
550
+ const members = all.filter(m => m.text !== 'any' && m.text !== 'unknown');
551
+ return members.length === 0 ? primary(all.some(m => m.text === 'any') ? 'any' : 'unknown') :
552
+ members.length === 1 ? members[0] : {
553
+ text: members.map(m => m.binding === 'union' ? `(${m.text})` : m.text).join(' & '),
554
+ binding: 'intersection'
555
+ };
556
+ }
557
+ case Type.Kind.GenericTypeVariable: {
558
+ // No declaration can name a type variable of the code being edited, so it is what bounds
559
+ // it, or unknown, as TypeScript reads an unconstrained one
560
+ const bounds = (type as Type.GenericTypeVariable).bounds;
561
+ return bounds.length === 0 ? primary('unknown') : this.writeType(bounds.length === 1 ? bounds[0] :
562
+ {kind: Type.Kind.Intersection, bounds} as Type.Intersection, nested);
563
+ }
564
+ default:
565
+ // A method, a variable, an annotation or an unknown type
566
+ return primary('any');
522
567
  }
568
+ }
523
569
 
524
- // Handle Type.Array - render component type plus []
525
- if (type.kind === Type.Kind.Array) {
526
- const arrayType = type as Type.Array;
527
- const componentTypeString = this.typeToString(arrayType.elemType, [...enclosing, type]);
528
- return `${componentTypeString}[]`;
529
- }
530
-
531
- // An array, a map or any other generic type, with its type arguments
532
- if (type.kind === Type.Kind.Parameterized) {
533
- const parameterized = type as Type.Parameterized;
534
- const base = this.typeToString(parameterized.type);
535
- return base === 'any' || parameterized.typeParameters.length === 0 ? base :
536
- `${base}<${parameterized.typeParameters.map(t => this.typeToString(t, [...enclosing, type])).join(', ')}>`;
570
+ /** The distinct members of a union or an intersection as TypeScript writes them. */
571
+ private static writeMembers(bounds: Type[], enclosing: Type[]): WrittenType[] {
572
+ const members = new Map<string, WrittenType>();
573
+ for (const bound of bounds) {
574
+ const member = this.writeType(bound, enclosing);
575
+ members.set(member.text, member);
537
576
  }
577
+ return [...members.values()];
578
+ }
538
579
 
539
- // For other types, return 'any' as a fallback
540
- // TODO: Implement proper Type to string conversion for other Type.Kind values
541
- return 'any';
580
+ /**
581
+ * The name of a class, where it is a type reference. A module qualifies its types by where they come from
582
+ * — `src/a.Foo` — and naming one takes a `context` statement of the caller's own, which `type` then names.
583
+ */
584
+ private static className(type: Type): string | undefined {
585
+ const name = (type.kind === Type.Kind.Class || type.kind === Type.Kind.ShallowClass) ?
586
+ (type as Type.Class).fullyQualifiedName : undefined;
587
+ return name !== undefined && isTypeReference(name) ? name : undefined;
542
588
  }
543
589
 
544
590
  /**
@@ -794,16 +840,44 @@ export function opensWithBrace(templateParts: TemplateStringsArray): boolean {
794
840
  }
795
841
 
796
842
  /** Whether code opening with `{` that replaces `tree`, which `cursor` points at, is an object literal. */
797
- export function replacedByObjectLiteral(tree: J, cursor: Cursor): boolean {
843
+ export async function replacedByObjectLiteral(tree: J, cursor: Cursor): Promise<boolean> {
798
844
  if (tree.kind === J.Kind.Block) {
799
845
  return false;
800
846
  } else if (!isStatement(tree)) {
801
847
  return true;
802
848
  }
803
- const parentTree = cursor.parentTree()?.value;
849
+ const parentTree = await replacedParent(tree, cursor);
804
850
  return parentTree !== undefined && !expectsStatement(parentTree, tree.id);
805
851
  }
806
852
 
853
+ /** The trees templates made, each in place of the tree it replaced, even where it keeps the id of a capture. */
854
+ const templateResults = new WeakSet<J>();
855
+
856
+ /**
857
+ * The tree that holds `tree`. The cursor points at `tree`, at what it stands in for, or at a tree that
858
+ * holds it, as where a recipe applies a template to a child of the node it visits.
859
+ */
860
+ async function replacedParent(tree: J, cursor: Cursor | undefined): Promise<J | undefined> {
861
+ const owner = cursor?.value;
862
+ let holder: J | undefined;
863
+ if (isTree(owner) && owner.id !== tree.id) {
864
+ // A chained rule's result stands in for the owner, though it can keep the id of a capture inside it
865
+ const held = (t: J) => t === tree || (t.id === tree.id && !templateResults.has(tree));
866
+ await new class extends JavaScriptVisitor<undefined> {
867
+ protected override async preVisit(t: J, p: undefined): Promise<J | undefined> {
868
+ if (holder === undefined && held(t)) {
869
+ holder = this.cursor.parentTree()?.value;
870
+ }
871
+ if (holder !== undefined) {
872
+ this.stopAfterPreVisit();
873
+ }
874
+ return t;
875
+ }
876
+ }().visit(owner, undefined);
877
+ }
878
+ return holder ?? cursor?.parentTree()?.value;
879
+ }
880
+
807
881
  /**
808
882
  * Helper class for applying a template to an AST.
809
883
  */
@@ -849,46 +923,35 @@ export class TemplateApplier {
849
923
  }
850
924
 
851
925
  const originalTree = tree as J;
852
- let resultToUse = this.wrapTree(originalTree, this.ast);
926
+ const parentTree = await replacedParent(originalTree, this.cursor);
927
+ let resultToUse = this.wrapTree(originalTree, this.ast, parentTree);
853
928
  // A replaced node spliced back in as a capture carries its own trailing markers
854
929
  const trailing = trailingMarkers(originalTree);
855
930
  const slotMarkers = trailing && !(await treeIds(this.ast)).has(originalTree.id) ? trailing : undefined;
856
931
  if (slotMarkers) {
857
932
  resultToUse = {...resultToUse, markers: withMarkers(resultToUse.markers, slotMarkers)};
858
933
  }
859
- const slot = this.replacedSlot(originalTree);
934
+ const slot = this.replacedSlot(originalTree, parentTree);
860
935
  // `format` substitutes the target's prefix, so decide against the prefix that will print
861
936
  resultToUse = maybeParenthesize(slot?.[0], slot?.[1] ?? originalTree.id,
862
937
  {...resultToUse, prefix: originalTree.prefix}, slotMarkers);
863
- return this.format(resultToUse, originalTree);
938
+ const result = await this.format(resultToUse, originalTree);
939
+ templateResults.add(result);
940
+ return result;
864
941
  }
865
942
 
866
- /** The node holding the tree being replaced and the id it holds it under, per the cursor. */
867
- private replacedSlot(originalTree: J): [J, string] | undefined {
868
- const frames: J[] = [];
869
- for (let c: Cursor | undefined = this.cursor; c && frames.length < 2; c = c.parent) {
870
- if (isTree(c.value)) {
871
- frames.push(c.value as J);
872
- }
873
- }
874
- if (frames.length === 0) {
943
+ /** The node holding the tree being replaced and the id it holds it under. */
944
+ private replacedSlot(originalTree: J, parentTree: J | undefined): [J, string] | undefined {
945
+ if (parentTree === undefined) {
875
946
  return undefined;
876
- }
877
-
878
- // The visitor may sit on the node holding what is being replaced rather than on it
879
- if (requiredPrecedence(frames[0], originalTree.id) !== undefined) {
880
- return [frames[0], originalTree.id];
881
- }
882
- if (frames.length < 2) {
883
- return undefined;
884
- }
885
- if (requiredPrecedence(frames[1], originalTree.id) !== undefined) {
886
- return [frames[1], originalTree.id];
947
+ } else if (requiredPrecedence(parentTree, originalTree.id) !== undefined) {
948
+ return [parentTree, originalTree.id];
887
949
  }
888
950
  // A chained rule rewrites the previous rule's detached result, leaving the cursor on the node
889
951
  // that result stands in for; a wrong guess here only ever adds parentheses, never drops them
890
- return frames[0].id !== originalTree.id && requiredPrecedence(frames[1], frames[0].id) !== undefined ?
891
- [frames[1], frames[0].id] : undefined;
952
+ const standIn = this.cursor?.value;
953
+ return isTree(standIn) && standIn.id !== originalTree.id &&
954
+ requiredPrecedence(parentTree, standIn.id) !== undefined ? [parentTree, standIn.id] : undefined;
892
955
  }
893
956
 
894
957
  private async format(resultToUse: J, originalTree: J) {
@@ -929,9 +992,7 @@ export class TemplateApplier {
929
992
  return {...laidOut!, id: resultToUse.id};
930
993
  }
931
994
 
932
- private wrapTree(originalTree: J, resultToUse: J) {
933
- const parentTree = this.cursor?.parentTree()?.value;
934
-
995
+ private wrapTree(originalTree: J, resultToUse: J, parentTree: J | undefined) {
935
996
  // A `for` head holds each expression of its init and update in an expression statement, as the parser does
936
997
  if (parentTree?.kind === J.Kind.ForLoopControl &&
937
998
  (parentTree as J.ForLoop.Control).condition?.element.id !== originalTree.id) {
@@ -28,6 +28,7 @@ export type {
28
28
  RewriteRule,
29
29
  TryOnOptions,
30
30
  RewriteConfig,
31
+ DebugSetting,
31
32
  DebugOptions,
32
33
  DebugLogEntry,
33
34
  MatchExplanation,