@openrewrite/rewrite 8.62.3 → 8.62.5

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 (170) hide show
  1. package/dist/execution.d.ts.map +1 -1
  2. package/dist/execution.js.map +1 -1
  3. package/dist/java/rpc.d.ts +1 -0
  4. package/dist/java/rpc.d.ts.map +1 -1
  5. package/dist/java/rpc.js +81 -0
  6. package/dist/java/rpc.js.map +1 -1
  7. package/dist/java/tree.d.ts +12 -6
  8. package/dist/java/tree.d.ts.map +1 -1
  9. package/dist/java/tree.js +12 -92
  10. package/dist/java/tree.js.map +1 -1
  11. package/dist/java/type.d.ts +2 -0
  12. package/dist/java/type.d.ts.map +1 -1
  13. package/dist/java/type.js +12 -0
  14. package/dist/java/type.js.map +1 -1
  15. package/dist/java/visitor.d.ts +12 -4
  16. package/dist/java/visitor.d.ts.map +1 -1
  17. package/dist/java/visitor.js +23 -1
  18. package/dist/java/visitor.js.map +1 -1
  19. package/dist/javascript/format.d.ts +2 -2
  20. package/dist/javascript/format.d.ts.map +1 -1
  21. package/dist/javascript/format.js.map +1 -1
  22. package/dist/javascript/index.d.ts +3 -0
  23. package/dist/javascript/index.d.ts.map +1 -1
  24. package/dist/javascript/index.js +3 -0
  25. package/dist/javascript/index.js.map +1 -1
  26. package/dist/javascript/method-matcher.d.ts +16 -0
  27. package/dist/javascript/method-matcher.d.ts.map +1 -0
  28. package/dist/javascript/method-matcher.js +222 -0
  29. package/dist/javascript/method-matcher.js.map +1 -0
  30. package/dist/javascript/parser.d.ts +1 -1
  31. package/dist/javascript/parser.d.ts.map +1 -1
  32. package/dist/javascript/parser.js +27 -5
  33. package/dist/javascript/parser.js.map +1 -1
  34. package/dist/javascript/preconditions.d.ts +6 -0
  35. package/dist/javascript/preconditions.d.ts.map +1 -0
  36. package/dist/javascript/preconditions.js +58 -0
  37. package/dist/javascript/preconditions.js.map +1 -0
  38. package/dist/javascript/print.d.ts +2 -2
  39. package/dist/javascript/print.d.ts.map +1 -1
  40. package/dist/javascript/print.js.map +1 -1
  41. package/dist/javascript/remove-import.d.ts +56 -0
  42. package/dist/javascript/remove-import.d.ts.map +1 -0
  43. package/dist/javascript/remove-import.js +715 -0
  44. package/dist/javascript/remove-import.js.map +1 -0
  45. package/dist/javascript/rpc.js +3 -18
  46. package/dist/javascript/rpc.js.map +1 -1
  47. package/dist/javascript/search/index.d.ts +3 -0
  48. package/dist/javascript/search/index.d.ts.map +1 -0
  49. package/dist/javascript/search/index.js +19 -0
  50. package/dist/javascript/search/index.js.map +1 -0
  51. package/dist/javascript/search/uses-method.d.ts +8 -0
  52. package/dist/javascript/search/uses-method.d.ts.map +1 -0
  53. package/dist/javascript/search/uses-method.js +35 -0
  54. package/dist/javascript/search/uses-method.js.map +1 -0
  55. package/dist/javascript/search/uses-type.d.ts +8 -0
  56. package/dist/javascript/search/uses-type.d.ts.map +1 -0
  57. package/dist/javascript/search/uses-type.js +71 -0
  58. package/dist/javascript/search/uses-type.js.map +1 -0
  59. package/dist/javascript/templating.d.ts +1 -1
  60. package/dist/javascript/templating.d.ts.map +1 -1
  61. package/dist/javascript/templating.js +1 -1
  62. package/dist/javascript/templating.js.map +1 -1
  63. package/dist/javascript/tree.d.ts +3 -3
  64. package/dist/javascript/tree.d.ts.map +1 -1
  65. package/dist/javascript/tree.js +28 -0
  66. package/dist/javascript/tree.js.map +1 -1
  67. package/dist/javascript/type-mapping.d.ts +7 -18
  68. package/dist/javascript/type-mapping.d.ts.map +1 -1
  69. package/dist/javascript/type-mapping.js +290 -203
  70. package/dist/javascript/type-mapping.js.map +1 -1
  71. package/dist/javascript/visitor.d.ts +1 -1
  72. package/dist/javascript/visitor.d.ts.map +1 -1
  73. package/dist/javascript/visitor.js +1 -1
  74. package/dist/javascript/visitor.js.map +1 -1
  75. package/dist/json/print.js.map +1 -1
  76. package/dist/json/rpc.js +46 -17
  77. package/dist/json/rpc.js.map +1 -1
  78. package/dist/json/visitor.d.ts +2 -2
  79. package/dist/json/visitor.d.ts.map +1 -1
  80. package/dist/json/visitor.js.map +1 -1
  81. package/dist/print.d.ts +1 -0
  82. package/dist/print.d.ts.map +1 -1
  83. package/dist/print.js +6 -0
  84. package/dist/print.js.map +1 -1
  85. package/dist/rpc/queue.d.ts +15 -6
  86. package/dist/rpc/queue.d.ts.map +1 -1
  87. package/dist/rpc/queue.js +37 -13
  88. package/dist/rpc/queue.js.map +1 -1
  89. package/dist/rpc/request/generate.d.ts +4 -0
  90. package/dist/rpc/request/generate.d.ts.map +1 -1
  91. package/dist/rpc/request/generate.js +9 -4
  92. package/dist/rpc/request/generate.js.map +1 -1
  93. package/dist/rpc/request/get-object.d.ts +2 -2
  94. package/dist/rpc/request/get-object.d.ts.map +1 -1
  95. package/dist/rpc/request/get-object.js +4 -12
  96. package/dist/rpc/request/get-object.js.map +1 -1
  97. package/dist/rpc/request/parse.d.ts.map +1 -1
  98. package/dist/rpc/request/parse.js.map +1 -1
  99. package/dist/rpc/request/print.d.ts +1 -1
  100. package/dist/rpc/request/print.d.ts.map +1 -1
  101. package/dist/rpc/request/print.js +1 -1
  102. package/dist/rpc/request/print.js.map +1 -1
  103. package/dist/rpc/request/visit.d.ts +3 -2
  104. package/dist/rpc/request/visit.d.ts.map +1 -1
  105. package/dist/rpc/request/visit.js +5 -4
  106. package/dist/rpc/request/visit.js.map +1 -1
  107. package/dist/rpc/rewrite-rpc.d.ts +4 -4
  108. package/dist/rpc/rewrite-rpc.d.ts.map +1 -1
  109. package/dist/rpc/rewrite-rpc.js +16 -17
  110. package/dist/rpc/rewrite-rpc.js.map +1 -1
  111. package/dist/search/index.d.ts +2 -0
  112. package/dist/search/index.d.ts.map +1 -0
  113. package/dist/search/index.js +18 -0
  114. package/dist/search/index.js.map +1 -0
  115. package/dist/search/is-source-file.d.ts +8 -0
  116. package/dist/search/is-source-file.d.ts.map +1 -0
  117. package/dist/search/is-source-file.js +70 -0
  118. package/dist/search/is-source-file.js.map +1 -0
  119. package/dist/test/rewrite-test.d.ts.map +1 -1
  120. package/dist/test/rewrite-test.js +3 -0
  121. package/dist/test/rewrite-test.js.map +1 -1
  122. package/dist/text/rpc.js +37 -40
  123. package/dist/text/rpc.js.map +1 -1
  124. package/dist/util.d.ts +1 -0
  125. package/dist/util.d.ts.map +1 -1
  126. package/dist/util.js +13 -0
  127. package/dist/util.js.map +1 -1
  128. package/dist/version.txt +1 -1
  129. package/dist/visitor.d.ts +1 -1
  130. package/dist/visitor.d.ts.map +1 -1
  131. package/dist/visitor.js +3 -2
  132. package/dist/visitor.js.map +1 -1
  133. package/package.json +3 -1
  134. package/src/execution.ts +0 -2
  135. package/src/java/rpc.ts +68 -0
  136. package/src/java/tree.ts +20 -76
  137. package/src/java/type.ts +14 -0
  138. package/src/java/visitor.ts +32 -12
  139. package/src/javascript/format.ts +2 -2
  140. package/src/javascript/index.ts +4 -0
  141. package/src/javascript/method-matcher.ts +250 -0
  142. package/src/javascript/parser.ts +20 -6
  143. package/src/javascript/preconditions.ts +40 -0
  144. package/src/javascript/print.ts +3 -3
  145. package/src/javascript/remove-import.ts +780 -0
  146. package/src/javascript/rpc.ts +6 -19
  147. package/src/javascript/search/index.ts +2 -0
  148. package/src/javascript/search/uses-method.ts +21 -0
  149. package/src/javascript/search/uses-type.ts +27 -0
  150. package/src/javascript/templating.ts +4 -3
  151. package/src/javascript/tree.ts +47 -3
  152. package/src/javascript/type-mapping.ts +320 -214
  153. package/src/javascript/visitor.ts +126 -126
  154. package/src/json/print.ts +1 -1
  155. package/src/json/rpc.ts +40 -19
  156. package/src/json/visitor.ts +2 -2
  157. package/src/print.ts +9 -3
  158. package/src/rpc/queue.ts +36 -12
  159. package/src/rpc/request/generate.ts +18 -6
  160. package/src/rpc/request/get-object.ts +6 -13
  161. package/src/rpc/request/parse.ts +1 -1
  162. package/src/rpc/request/print.ts +2 -2
  163. package/src/rpc/request/visit.ts +6 -5
  164. package/src/rpc/rewrite-rpc.ts +22 -21
  165. package/src/search/index.ts +1 -0
  166. package/src/search/is-source-file.ts +26 -0
  167. package/src/test/rewrite-test.ts +5 -2
  168. package/src/text/rpc.ts +33 -37
  169. package/src/util.ts +19 -4
  170. package/src/visitor.ts +3 -3
@@ -18,7 +18,7 @@ import {asRef, RpcCodec, RpcCodecs, RpcReceiveQueue, RpcSendQueue} from "../rpc"
18
18
  import {isJavaScript, JS, JSX} from "./tree";
19
19
  import {Expression, J, Statement, Type, TypedTree, TypeTree} from "../java";
20
20
  import {createDraft, finishDraft} from "immer";
21
- import {JavaReceiver, JavaSender} from "../java/rpc";
21
+ import {JavaReceiver, JavaSender, registerJLanguageCodecs} from "../java/rpc";
22
22
  import {Cursor, Tree} from "../tree";
23
23
  import ComputedPropertyName = JS.ComputedPropertyName;
24
24
 
@@ -105,7 +105,7 @@ class JavaScriptSender extends JavaScriptVisitor<RpcSendQueue> {
105
105
  await q.getAndSend(functionCall, m => m.function, f => this.visitRightPadded(f, q));
106
106
  await q.getAndSend(functionCall, m => m.typeParameters, params => this.visitContainer(params, q));
107
107
  await q.getAndSend(functionCall, m => m.arguments, args => this.visitContainer(args, q));
108
- await q.getAndSend(functionCall, m => asRef(m.functionType), type => this.visitType(type, q));
108
+ await q.getAndSend(functionCall, m => asRef(m.methodType), type => this.visitType(type, q));
109
109
  return functionCall;
110
110
  }
111
111
 
@@ -643,7 +643,7 @@ class JavaScriptReceiver extends JavaScriptVisitor<RpcReceiveQueue> {
643
643
  draft.function = await q.receive(functionCall.function, select => this.visitRightPadded(select, q));
644
644
  draft.typeParameters = await q.receive(functionCall.typeParameters, typeParams => this.visitContainer(typeParams, q));
645
645
  draft.arguments = await q.receive(functionCall.arguments, args => this.visitContainer(args, q));
646
- draft.functionType = await q.receive(functionCall.functionType, type => this.visitType(type, q) as unknown as Type.Method);
646
+ draft.methodType = await q.receive(functionCall.methodType, type => this.visitType(type, q) as unknown as Type.Method);
647
647
 
648
648
  return finishDraft(draft);
649
649
  }
@@ -1106,11 +1106,11 @@ class JavaScriptReceiver extends JavaScriptVisitor<RpcReceiveQueue> {
1106
1106
  return this.javaReceiverDelegate.visitRightPadded(right, q)
1107
1107
  }
1108
1108
 
1109
- protected async visitLeftPadded<T extends J | J.Space | number | string | boolean>(left: J.LeftPadded<T>, q: RpcReceiveQueue): Promise<J.LeftPadded<T>> {
1109
+ async visitLeftPadded<T extends J | J.Space | number | string | boolean>(left: J.LeftPadded<T>, q: RpcReceiveQueue): Promise<J.LeftPadded<T>> {
1110
1110
  return this.javaReceiverDelegate.visitLeftPadded(left, q);
1111
1111
  }
1112
1112
 
1113
- protected async visitContainer<T extends J>(container: J.Container<T>, q: RpcReceiveQueue): Promise<J.Container<T>> {
1113
+ async visitContainer<T extends J>(container: J.Container<T>, q: RpcReceiveQueue): Promise<J.Container<T>> {
1114
1114
  return this.javaReceiverDelegate.visitContainer(container, q);
1115
1115
  }
1116
1116
 
@@ -1139,17 +1139,4 @@ class JavaScriptReceiverDelegate extends JavaReceiver {
1139
1139
  }
1140
1140
  }
1141
1141
 
1142
- const javaScriptCodec: RpcCodec<JS> = {
1143
- async rpcReceive(before: JS, q: RpcReceiveQueue): Promise<JS> {
1144
- return (await new JavaScriptReceiver().visit(before, q))! as JS;
1145
- },
1146
-
1147
- async rpcSend(after: JS, q: RpcSendQueue): Promise<void> {
1148
- await new JavaScriptSender().visit(after, q);
1149
- }
1150
- }
1151
-
1152
- // Register codec for all JavaScript AST node types
1153
- Object.values(JS.Kind).forEach(kind => {
1154
- RpcCodecs.registerCodec(kind, javaScriptCodec);
1155
- });
1142
+ registerJLanguageCodecs(JS.Kind.CompilationUnit, new JavaScriptReceiver(), new JavaScriptSender(), JS.Kind);
@@ -0,0 +1,2 @@
1
+ export * from './uses-method';
2
+ export * from './uses-type';
@@ -0,0 +1,21 @@
1
+ import {Expression, J, JavaVisitor} from "../../java";
2
+ import {ExecutionContext} from "../../execution";
3
+ import {MethodMatcher} from "../method-matcher";
4
+ import {JS} from "../tree";
5
+ import {foundSearchResult} from "../../markers";
6
+
7
+ export class UsesMethod extends JavaVisitor<ExecutionContext> {
8
+ private readonly matcher;
9
+
10
+ constructor(pattern: string) {
11
+ super();
12
+ this.matcher = new MethodMatcher(pattern);
13
+ }
14
+
15
+ protected async visitExpression(expression: Expression, p: ExecutionContext): Promise<J | undefined> {
16
+ if (JS.isMethodCall(expression) && this.matcher.matches(expression.methodType)) {
17
+ return foundSearchResult(expression);
18
+ }
19
+ return super.visitExpression(expression, p);
20
+ }
21
+ }
@@ -0,0 +1,27 @@
1
+ import {J, JavaVisitor, Type} from "../../java";
2
+ import {ExecutionContext} from "../../execution";
3
+ import * as picomatch from "picomatch";
4
+ import {foundSearchResult} from "../../markers";
5
+
6
+ export class UsesType extends JavaVisitor<ExecutionContext> {
7
+ private readonly matcher: picomatch.Matcher;
8
+
9
+ constructor(typePattern: string) {
10
+ super();
11
+ // Create a picomatch matcher for the pattern
12
+ this.matcher = picomatch.default ? picomatch.default(typePattern) : (picomatch as any)(typePattern);
13
+ }
14
+
15
+ protected async preVisit(tree: J, _: ExecutionContext): Promise<J | undefined> {
16
+ if (J.hasType(tree)) {
17
+ const type = tree.type;
18
+ if (Type.isFullyQualified(type)) {
19
+ const fullyQualifiedName = Type.FullyQualified.getFullyQualifiedName(type);
20
+ if (this.matcher(fullyQualifiedName)) {
21
+ return foundSearchResult(tree);
22
+ }
23
+ }
24
+ }
25
+ return tree;
26
+ }
27
+ }
@@ -92,7 +92,8 @@ export class Pattern {
92
92
  export class MatchResult implements Pick<Map<string, J>, "get"> {
93
93
  constructor(
94
94
  private readonly bindings: Map<string, J> = new Map()
95
- ) {}
95
+ ) {
96
+ }
96
97
 
97
98
  get(capture: Capture | string): J | undefined {
98
99
  const name = typeof capture === "string" ? capture : capture.name;
@@ -577,7 +578,7 @@ class TemplateApplier {
577
578
  * @returns A Promise resolving to the modified AST
578
579
  */
579
580
  async apply(): Promise<J | undefined> {
580
- const {tree, loc, mode} = this.coordinates;
581
+ const {loc} = this.coordinates;
581
582
 
582
583
  // Apply the template based on the location and mode
583
584
  switch (loc || 'EXPRESSION_PREFIX') {
@@ -771,7 +772,7 @@ class RewriteRuleImpl implements RewriteRule {
771
772
  * after: template`${"left"} === ${"right"}`
772
773
  * }));
773
774
  */
774
- export function rewrite<T extends J>(
775
+ export function rewrite(
775
776
  builderFn: () => RewriteConfig
776
777
  ): RewriteRule {
777
778
  const config = builderFn();
@@ -17,7 +17,20 @@
17
17
  */
18
18
 
19
19
  import {SourceFile} from "../tree";
20
- import {Expression, J, NameTree, Statement, Type, TypedTree, TypeTree, VariableDeclarator,} from "../java";
20
+ import {
21
+ Expression,
22
+ J,
23
+ JavaVisitor,
24
+ MethodCall,
25
+ NameTree,
26
+ Statement,
27
+ Type,
28
+ TypedTree,
29
+ TypeTree,
30
+ VariableDeclarator,
31
+ registerJavaExtensionKinds
32
+ } from "../java";
33
+ import {JavaScriptVisitor} from "./visitor";
21
34
 
22
35
  export interface JS extends J {
23
36
  }
@@ -94,6 +107,10 @@ export namespace JS {
94
107
  WithStatement: "org.openrewrite.javascript.tree.JS$WithStatement",
95
108
  } as const;
96
109
 
110
+ export function isMethodCall(e?: Expression): e is MethodCall {
111
+ return J.isMethodCall(e) || e?.kind === JS.Kind.FunctionCall;
112
+ }
113
+
97
114
  /**
98
115
  * Represents the root of a JavaScript AST (compilation unit).
99
116
  * @example // file.js as AST root
@@ -650,12 +667,11 @@ export namespace JS {
650
667
  * @example data["key"](5, 0, 4)
651
668
  * @example (() => { return 3 + 5 })()
652
669
  */
653
- export interface FunctionCall extends JS, Expression {
670
+ export interface FunctionCall extends JS, MethodCall {
654
671
  readonly kind: typeof Kind.FunctionCall;
655
672
  readonly function?: J.RightPadded<Expression>;
656
673
  readonly typeParameters?: J.Container<Expression>;
657
674
  readonly arguments: J.Container<Expression>;
658
- readonly functionType?: Type.Method;
659
675
  }
660
676
 
661
677
  /**
@@ -901,6 +917,34 @@ export namespace JSX {
901
917
 
902
918
  const KindValues = new Set(Object.values(JS.Kind));
903
919
 
920
+ function javascriptVisitorAdapter<P>(visitor: JavaVisitor<P>): JavaVisitor<P> {
921
+ const adaptedVisitor = new JavaScriptVisitor<P>();
922
+
923
+ // Walk up the prototype chain of the visitor to get all overridden methods
924
+ let proto = Object.getPrototypeOf(visitor);
925
+ while (proto && proto !== JavaVisitor.prototype) {
926
+ Object.getOwnPropertyNames(proto).forEach(name => {
927
+ const descriptor = Object.getOwnPropertyDescriptor(proto, name);
928
+ if (descriptor && typeof descriptor.value === 'function' && name !== 'constructor') {
929
+ // Copy the method, binding it to jsVisitor
930
+ (adaptedVisitor as any)[name] = descriptor.value.bind(adaptedVisitor);
931
+ }
932
+ });
933
+ proto = Object.getPrototypeOf(proto);
934
+ }
935
+
936
+ // Also copy any instance properties
937
+ Object.keys(visitor).forEach(key => {
938
+ if (!(key in adaptedVisitor)) {
939
+ (adaptedVisitor as any)[key] = (visitor as any)[key];
940
+ }
941
+ });
942
+
943
+ return adaptedVisitor;
944
+ }
945
+
946
+ registerJavaExtensionKinds(Object.values(JS.Kind), javascriptVisitorAdapter);
947
+
904
948
  export function isJavaScript(tree: any): tree is JS {
905
949
  return KindValues.has(tree["kind"]);
906
950
  }