@openrewrite/rewrite 8.92.16 → 8.92.18

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 (254) hide show
  1. package/README.md +48 -0
  2. package/dist/index.d.ts.map +1 -1
  3. package/dist/index.js +2 -1
  4. package/dist/index.js.map +1 -1
  5. package/dist/java/formatting-utils.d.ts +2 -1
  6. package/dist/java/formatting-utils.d.ts.map +1 -1
  7. package/dist/java/formatting-utils.js +4 -3
  8. package/dist/java/formatting-utils.js.map +1 -1
  9. package/dist/java/rpc.d.ts.map +1 -1
  10. package/dist/java/rpc.js +2 -0
  11. package/dist/java/rpc.js.map +1 -1
  12. package/dist/java/tree.d.ts +8 -0
  13. package/dist/java/tree.d.ts.map +1 -1
  14. package/dist/java/tree.js +11 -1
  15. package/dist/java/tree.js.map +1 -1
  16. package/dist/java/type.d.ts +7 -1
  17. package/dist/java/type.d.ts.map +1 -1
  18. package/dist/java/type.js +16 -1
  19. package/dist/java/type.js.map +1 -1
  20. package/dist/javascript/add-import.d.ts +27 -21
  21. package/dist/javascript/add-import.d.ts.map +1 -1
  22. package/dist/javascript/add-import.js +303 -143
  23. package/dist/javascript/add-import.js.map +1 -1
  24. package/dist/javascript/amd.d.ts.map +1 -1
  25. package/dist/javascript/amd.js +25 -10
  26. package/dist/javascript/amd.js.map +1 -1
  27. package/dist/javascript/autodetect.js +36 -45
  28. package/dist/javascript/autodetect.js.map +1 -1
  29. package/dist/javascript/binding.d.ts +6 -4
  30. package/dist/javascript/binding.d.ts.map +1 -1
  31. package/dist/javascript/binding.js +54 -18
  32. package/dist/javascript/binding.js.map +1 -1
  33. package/dist/javascript/comparator.d.ts +22 -5
  34. package/dist/javascript/comparator.d.ts.map +1 -1
  35. package/dist/javascript/comparator.js +120 -38
  36. package/dist/javascript/comparator.js.map +1 -1
  37. package/dist/javascript/format/format.d.ts +2 -0
  38. package/dist/javascript/format/format.d.ts.map +1 -1
  39. package/dist/javascript/format/format.js +11 -1
  40. package/dist/javascript/format/format.js.map +1 -1
  41. package/dist/javascript/format/tabs-and-indents-visitor.d.ts.map +1 -1
  42. package/dist/javascript/format/tabs-and-indents-visitor.js +31 -4
  43. package/dist/javascript/format/tabs-and-indents-visitor.js.map +1 -1
  44. package/dist/javascript/index.d.ts +4 -2
  45. package/dist/javascript/index.d.ts.map +1 -1
  46. package/dist/javascript/index.js +10 -1
  47. package/dist/javascript/index.js.map +1 -1
  48. package/dist/javascript/markers.d.ts +12 -0
  49. package/dist/javascript/markers.d.ts.map +1 -1
  50. package/dist/javascript/markers.js +81 -2
  51. package/dist/javascript/markers.js.map +1 -1
  52. package/dist/javascript/method-matcher.d.ts +13 -2
  53. package/dist/javascript/method-matcher.d.ts.map +1 -1
  54. package/dist/javascript/method-matcher.js +32 -19
  55. package/dist/javascript/method-matcher.js.map +1 -1
  56. package/dist/javascript/node-resolution-result.js +3 -2
  57. package/dist/javascript/node-resolution-result.js.map +1 -1
  58. package/dist/javascript/package-name.d.ts +6 -0
  59. package/dist/javascript/package-name.d.ts.map +1 -0
  60. package/dist/javascript/package-name.js +47 -0
  61. package/dist/javascript/package-name.js.map +1 -0
  62. package/dist/javascript/parser.d.ts +9 -0
  63. package/dist/javascript/parser.d.ts.map +1 -1
  64. package/dist/javascript/parser.js +185 -88
  65. package/dist/javascript/parser.js.map +1 -1
  66. package/dist/javascript/preconditions.d.ts +4 -1
  67. package/dist/javascript/preconditions.d.ts.map +1 -1
  68. package/dist/javascript/preconditions.js +7 -4
  69. package/dist/javascript/preconditions.js.map +1 -1
  70. package/dist/javascript/print.d.ts +10 -1
  71. package/dist/javascript/print.d.ts.map +1 -1
  72. package/dist/javascript/print.js +120 -77
  73. package/dist/javascript/print.js.map +1 -1
  74. package/dist/javascript/recipes/change-method-target-to-import.d.ts +29 -0
  75. package/dist/javascript/recipes/change-method-target-to-import.d.ts.map +1 -0
  76. package/dist/javascript/recipes/change-method-target-to-import.js +164 -0
  77. package/dist/javascript/recipes/change-method-target-to-import.js.map +1 -0
  78. package/dist/javascript/recipes/index.d.ts +1 -0
  79. package/dist/javascript/recipes/index.d.ts.map +1 -1
  80. package/dist/javascript/recipes/index.js +1 -0
  81. package/dist/javascript/recipes/index.js.map +1 -1
  82. package/dist/javascript/scope.d.ts +60 -16
  83. package/dist/javascript/scope.d.ts.map +1 -1
  84. package/dist/javascript/scope.js +596 -131
  85. package/dist/javascript/scope.js.map +1 -1
  86. package/dist/javascript/search/uses-method.d.ts +1 -1
  87. package/dist/javascript/search/uses-method.d.ts.map +1 -1
  88. package/dist/javascript/search/uses-method.js +2 -2
  89. package/dist/javascript/search/uses-method.js.map +1 -1
  90. package/dist/javascript/search/uses-type.d.ts +14 -1
  91. package/dist/javascript/search/uses-type.d.ts.map +1 -1
  92. package/dist/javascript/search/uses-type.js +24 -9
  93. package/dist/javascript/search/uses-type.js.map +1 -1
  94. package/dist/javascript/templating/bindings.d.ts +1 -1
  95. package/dist/javascript/templating/bindings.d.ts.map +1 -1
  96. package/dist/javascript/templating/bindings.js +10 -50
  97. package/dist/javascript/templating/bindings.js.map +1 -1
  98. package/dist/javascript/templating/comparator.d.ts +8 -11
  99. package/dist/javascript/templating/comparator.d.ts.map +1 -1
  100. package/dist/javascript/templating/comparator.js +12 -110
  101. package/dist/javascript/templating/comparator.js.map +1 -1
  102. package/dist/javascript/templating/engine.d.ts +34 -24
  103. package/dist/javascript/templating/engine.d.ts.map +1 -1
  104. package/dist/javascript/templating/engine.js +208 -115
  105. package/dist/javascript/templating/engine.js.map +1 -1
  106. package/dist/javascript/templating/pattern.d.ts +11 -5
  107. package/dist/javascript/templating/pattern.d.ts.map +1 -1
  108. package/dist/javascript/templating/pattern.js +86 -20
  109. package/dist/javascript/templating/pattern.js.map +1 -1
  110. package/dist/javascript/templating/placeholder-replacement.d.ts +34 -3
  111. package/dist/javascript/templating/placeholder-replacement.d.ts.map +1 -1
  112. package/dist/javascript/templating/placeholder-replacement.js +126 -60
  113. package/dist/javascript/templating/placeholder-replacement.js.map +1 -1
  114. package/dist/javascript/templating/precedence.d.ts +3 -1
  115. package/dist/javascript/templating/precedence.d.ts.map +1 -1
  116. package/dist/javascript/templating/precedence.js +28 -8
  117. package/dist/javascript/templating/precedence.js.map +1 -1
  118. package/dist/javascript/templating/template.d.ts +7 -6
  119. package/dist/javascript/templating/template.d.ts.map +1 -1
  120. package/dist/javascript/templating/template.js +31 -28
  121. package/dist/javascript/templating/template.js.map +1 -1
  122. package/dist/javascript/templating/types.d.ts +6 -3
  123. package/dist/javascript/templating/types.d.ts.map +1 -1
  124. package/dist/javascript/templating/utils.d.ts +10 -1
  125. package/dist/javascript/templating/utils.d.ts.map +1 -1
  126. package/dist/javascript/templating/utils.js +22 -3
  127. package/dist/javascript/templating/utils.js.map +1 -1
  128. package/dist/javascript/tree-debug.d.ts +2 -0
  129. package/dist/javascript/tree-debug.d.ts.map +1 -1
  130. package/dist/javascript/tree-debug.js +6 -0
  131. package/dist/javascript/tree-debug.js.map +1 -1
  132. package/dist/javascript/tsconfig.d.ts.map +1 -1
  133. package/dist/javascript/tsconfig.js +7 -17
  134. package/dist/javascript/tsconfig.js.map +1 -1
  135. package/dist/javascript/type-mapping.d.ts +35 -16
  136. package/dist/javascript/type-mapping.d.ts.map +1 -1
  137. package/dist/javascript/type-mapping.js +271 -252
  138. package/dist/javascript/type-mapping.js.map +1 -1
  139. package/dist/javascript/type-report-cli.d.ts +7 -0
  140. package/dist/javascript/type-report-cli.d.ts.map +1 -0
  141. package/dist/javascript/type-report-cli.js +129 -0
  142. package/dist/javascript/type-report-cli.js.map +1 -0
  143. package/dist/javascript/type-report.d.ts +61 -0
  144. package/dist/javascript/type-report.d.ts.map +1 -0
  145. package/dist/javascript/type-report.js +488 -0
  146. package/dist/javascript/type-report.js.map +1 -0
  147. package/dist/markers.d.ts +4 -0
  148. package/dist/markers.d.ts.map +1 -1
  149. package/dist/markers.js +8 -0
  150. package/dist/markers.js.map +1 -1
  151. package/dist/print.d.ts +6 -1
  152. package/dist/print.d.ts.map +1 -1
  153. package/dist/print.js +21 -7
  154. package/dist/print.js.map +1 -1
  155. package/dist/recipe.d.ts +5 -0
  156. package/dist/recipe.d.ts.map +1 -1
  157. package/dist/recipe.js +5 -0
  158. package/dist/recipe.js.map +1 -1
  159. package/dist/rewrite-javascript-version.txt +1 -1
  160. package/dist/rpc/java-rpc-client.d.ts +2 -0
  161. package/dist/rpc/java-rpc-client.d.ts.map +1 -1
  162. package/dist/rpc/java-rpc-client.js +1 -0
  163. package/dist/rpc/java-rpc-client.js.map +1 -1
  164. package/dist/rpc/queue.d.ts +24 -0
  165. package/dist/rpc/queue.d.ts.map +1 -1
  166. package/dist/rpc/queue.js +71 -11
  167. package/dist/rpc/queue.js.map +1 -1
  168. package/dist/rpc/request/get-object.d.ts +4 -0
  169. package/dist/rpc/request/get-object.d.ts.map +1 -1
  170. package/dist/rpc/request/get-object.js +23 -1
  171. package/dist/rpc/request/get-object.js.map +1 -1
  172. package/dist/rpc/request/parse.d.ts.map +1 -1
  173. package/dist/rpc/request/parse.js +3 -1
  174. package/dist/rpc/request/parse.js.map +1 -1
  175. package/dist/rpc/request/prepare-recipe.d.ts +3 -1
  176. package/dist/rpc/request/prepare-recipe.d.ts.map +1 -1
  177. package/dist/rpc/request/prepare-recipe.js +13 -6
  178. package/dist/rpc/request/prepare-recipe.js.map +1 -1
  179. package/dist/rpc/request/print.d.ts +8 -2
  180. package/dist/rpc/request/print.d.ts.map +1 -1
  181. package/dist/rpc/request/print.js +12 -9
  182. package/dist/rpc/request/print.js.map +1 -1
  183. package/dist/rpc/rewrite-rpc.d.ts +3 -2
  184. package/dist/rpc/rewrite-rpc.d.ts.map +1 -1
  185. package/dist/rpc/rewrite-rpc.js +9 -3
  186. package/dist/rpc/rewrite-rpc.js.map +1 -1
  187. package/dist/rpc/server.d.ts.map +1 -1
  188. package/dist/rpc/server.js +19 -5
  189. package/dist/rpc/server.js.map +1 -1
  190. package/dist/search/is-source-file.d.ts +1 -0
  191. package/dist/search/is-source-file.d.ts.map +1 -1
  192. package/dist/search/is-source-file.js +7 -2
  193. package/dist/search/is-source-file.js.map +1 -1
  194. package/dist/test/java-rpc.d.ts.map +1 -1
  195. package/dist/test/java-rpc.js +3 -0
  196. package/dist/test/java-rpc.js.map +1 -1
  197. package/dist/test/rewrite-test.d.ts +5 -0
  198. package/dist/test/rewrite-test.d.ts.map +1 -1
  199. package/dist/test/rewrite-test.js +14 -3
  200. package/dist/test/rewrite-test.js.map +1 -1
  201. package/package.json +4 -3
  202. package/src/index.ts +3 -1
  203. package/src/java/formatting-utils.ts +5 -4
  204. package/src/java/rpc.ts +2 -0
  205. package/src/java/tree.ts +15 -0
  206. package/src/java/type.ts +19 -2
  207. package/src/javascript/add-import.ts +338 -152
  208. package/src/javascript/amd.ts +28 -12
  209. package/src/javascript/autodetect.ts +32 -41
  210. package/src/javascript/binding.ts +64 -22
  211. package/src/javascript/comparator.ts +129 -43
  212. package/src/javascript/format/format.ts +9 -0
  213. package/src/javascript/format/tabs-and-indents-visitor.ts +30 -3
  214. package/src/javascript/index.ts +6 -3
  215. package/src/javascript/markers.ts +77 -5
  216. package/src/javascript/method-matcher.ts +34 -22
  217. package/src/javascript/node-resolution-result.ts +3 -2
  218. package/src/javascript/package-name.ts +45 -0
  219. package/src/javascript/parser.ts +200 -91
  220. package/src/javascript/preconditions.ts +7 -4
  221. package/src/javascript/print.ts +123 -76
  222. package/src/javascript/recipes/change-method-target-to-import.ts +178 -0
  223. package/src/javascript/recipes/index.ts +1 -0
  224. package/src/javascript/scope.ts +705 -169
  225. package/src/javascript/search/uses-method.ts +2 -2
  226. package/src/javascript/search/uses-type.ts +30 -9
  227. package/src/javascript/templating/bindings.ts +13 -38
  228. package/src/javascript/templating/comparator.ts +16 -127
  229. package/src/javascript/templating/engine.ts +236 -129
  230. package/src/javascript/templating/pattern.ts +107 -28
  231. package/src/javascript/templating/placeholder-replacement.ts +154 -62
  232. package/src/javascript/templating/precedence.ts +33 -11
  233. package/src/javascript/templating/template.ts +35 -30
  234. package/src/javascript/templating/types.ts +6 -3
  235. package/src/javascript/templating/utils.ts +24 -2
  236. package/src/javascript/tree-debug.ts +9 -0
  237. package/src/javascript/tsconfig.ts +7 -17
  238. package/src/javascript/type-mapping.ts +277 -255
  239. package/src/javascript/type-report-cli.ts +96 -0
  240. package/src/javascript/type-report.ts +575 -0
  241. package/src/markers.ts +8 -0
  242. package/src/print.ts +23 -8
  243. package/src/recipe.ts +6 -0
  244. package/src/rpc/java-rpc-client.ts +3 -0
  245. package/src/rpc/queue.ts +80 -7
  246. package/src/rpc/request/get-object.ts +24 -0
  247. package/src/rpc/request/parse.ts +4 -1
  248. package/src/rpc/request/prepare-recipe.ts +16 -6
  249. package/src/rpc/request/print.ts +12 -8
  250. package/src/rpc/rewrite-rpc.ts +16 -5
  251. package/src/rpc/server.ts +18 -6
  252. package/src/search/is-source-file.ts +8 -3
  253. package/src/test/java-rpc.ts +3 -0
  254. package/src/test/rewrite-test.ts +14 -3
@@ -29,14 +29,17 @@ export * from "./method-matcher";
29
29
  export * from "./format";
30
30
  export * from "./autodetect";
31
31
  export * from "./tree-debug";
32
+ // The comparator visitors stay internal so what they compare is free to change.
33
+ export {isEqual} from "./comparator";
34
+ export * from "./type-report";
32
35
  export * from "./project-parser";
33
36
 
34
- export type {Scope} from "./scope";
37
+ export type {Scope, Meaning, Reference} from "./scope";
35
38
  // `cursorOf` is deliberately absent: a visitor of one's own reaches `this.cursor` directly, and
36
39
  // the free functions that have no visitor to reach it through are already in this list.
37
40
  export {
38
- scopeOf, namesDeclaredIn, namesDeclaredWithin, namesUsedIn, namesReferencedWithin, bindingNames, deconflict,
39
- isValueReference, declarationsOf, compilationUnitOf, walk
41
+ scopeOf, namesDeclaredIn, namesDeclaredWithin, namesUsedIn, namesUsedWithin, namesReferencedWithin, bindingNames,
42
+ deconflict, isReference, isValueReference, isWrite, resolve, referencesOf, declarationsOf, compilationUnitOf, walk
40
43
  } from "./scope";
41
44
  export type {QuoteChar, AddImportOptions} from "./add-import";
42
45
  export {ImportStyle, moduleNameOf, AddImport} from "./add-import";
@@ -13,7 +13,7 @@
13
13
  * See the License for the specific language governing permissions and
14
14
  * limitations under the License.
15
15
  */
16
- import {Marker} from "../markers";
16
+ import {Marker, MarkersKind} from "../markers";
17
17
  import {J} from "../java";
18
18
  import {JS} from "./tree";
19
19
  import {RpcCodecs, RpcReceiveQueue, RpcSendQueue} from "../rpc";
@@ -24,10 +24,12 @@ import {
24
24
  StyleKind,
25
25
  TabsAndIndentsStyle,
26
26
  WrappingAndBracesStyle,
27
- WrappingAndBracesStyleDetailKind
27
+ WrappingAndBracesStyleDetailKind,
28
+ prettierStyle
28
29
  } from "./style";
29
- import {Autodetect} from "./autodetect";
30
+ import {Autodetect, autodetect} from "./autodetect";
30
31
  import {updateIfChanged} from "../util";
32
+ import {NamedStyles, Style} from "../style";
31
33
 
32
34
  declare module "./tree" {
33
35
  namespace JS {
@@ -37,6 +39,7 @@ declare module "./tree" {
37
39
  readonly NonNullAssertion: "org.openrewrite.javascript.marker.NonNullAssertion";
38
40
  readonly DelegatedYield: "org.openrewrite.javascript.marker.DelegatedYield";
39
41
  readonly FunctionDeclaration: "org.openrewrite.javascript.marker.FunctionDeclaration";
42
+ readonly Computed: "org.openrewrite.javascript.marker.Computed";
40
43
  };
41
44
  }
42
45
  }
@@ -48,6 +51,7 @@ declare module "./tree" {
48
51
  Optional: "org.openrewrite.javascript.marker.Optional",
49
52
  NonNullAssertion: "org.openrewrite.javascript.marker.NonNullAssertion",
50
53
  FunctionDeclaration: "org.openrewrite.javascript.marker.FunctionDeclaration",
54
+ Computed: "org.openrewrite.javascript.marker.Computed",
51
55
  } as const;
52
56
 
53
57
  /**
@@ -80,6 +84,18 @@ export interface FunctionDeclaration extends Marker {
80
84
  readonly prefix: J.Space;
81
85
  }
82
86
 
87
+ /**
88
+ * A name written in brackets where the model has room for an identifier only, as the
89
+ * member of `enum A { ['b'] }` is. The identifier holds the bracketed literal.
90
+ */
91
+ export interface Computed extends Marker {
92
+ readonly kind: typeof JS.Markers.Computed;
93
+ /**
94
+ * The space before the closing bracket.
95
+ */
96
+ readonly suffix: J.Space;
97
+ }
98
+
83
99
  /**
84
100
  * Registers an RPC codec for any marker that has a `prefix: J.Space` field.
85
101
  */
@@ -107,11 +123,26 @@ registerPrefixedMarkerCodec<Generator>(JS.Markers.Generator);
107
123
  registerPrefixedMarkerCodec<NonNullAssertion>(JS.Markers.NonNullAssertion);
108
124
  registerPrefixedMarkerCodec<FunctionDeclaration>(JS.Markers.FunctionDeclaration);
109
125
 
126
+ RpcCodecs.registerCodec(JS.Markers.Computed, {
127
+ async rpcReceive(before: Computed, q: RpcReceiveQueue): Promise<Computed> {
128
+ return updateIfChanged(before, {
129
+ id: await q.receive(before.id),
130
+ suffix: await q.receive(before.suffix),
131
+ });
132
+ },
133
+
134
+ async rpcSend(after: Computed, q: RpcSendQueue): Promise<void> {
135
+ await q.getAndSend(after, a => a.id);
136
+ await q.getAndSend(after, a => a.suffix);
137
+ }
138
+ });
139
+
110
140
  // Register codec for PrettierStyle (a NamedStyles that contains Prettier configuration)
111
141
  // Only serialize the variable fields; constant fields are defined in the interface
112
142
  RpcCodecs.registerCodec(StyleKind.PrettierStyle, {
113
143
  async rpcReceive(before: PrettierStyle, q: RpcReceiveQueue): Promise<PrettierStyle> {
114
- return updateIfChanged(before, {
144
+ // the fields that never vary are not sent, so a marker first met here does not have them yet
145
+ return updateIfChanged(before.name ? before : {...prettierStyle(before.id, {}), ...before}, {
115
146
  id: await q.receive(before.id),
116
147
  config: await q.receive(before.config),
117
148
  prettierVersion: await q.receive(before.prettierVersion),
@@ -131,7 +162,7 @@ RpcCodecs.registerCodec(StyleKind.PrettierStyle, {
131
162
  // Only serialize the variable fields (id, styles); constant fields are defined in the interface
132
163
  RpcCodecs.registerCodec(StyleKind.Autodetect, {
133
164
  async rpcReceive(before: Autodetect, q: RpcReceiveQueue): Promise<Autodetect> {
134
- return updateIfChanged(before, {
165
+ return updateIfChanged(before.name ? before : {...autodetect(before.id, []), ...before}, {
135
166
  id: await q.receive(before.id),
136
167
  styles: (await q.receiveList(before.styles))!,
137
168
  });
@@ -266,3 +297,44 @@ RpcCodecs.registerCodec(StyleKind.WrappingAndBracesStyle, {
266
297
  await q.getAndSend(after, a => a.keepWhenReformatting);
267
298
  }
268
299
  });
300
+
301
+ const styleDetailKinds = new Set<string>([
302
+ ...Object.values(SpacesStyleDetailKind),
303
+ ...Object.values(WrappingAndBracesStyleDetailKind)
304
+ ]);
305
+
306
+ function detailKind(styleKind: string, field: string): string | undefined {
307
+ const kind = `${styleKind}$${field.charAt(0).toUpperCase()}${field.slice(1)}`;
308
+ return styleDetailKinds.has(kind) ? kind : undefined;
309
+ }
310
+
311
+ function styleFromValue(value: any): Style {
312
+ if (value === null || typeof value !== "object") {
313
+ return value;
314
+ }
315
+ const {"@c": kind, "@ref": _ref, ...fields} = value;
316
+ for (const [field, detail] of Object.entries(fields)) {
317
+ const kindOfDetail = detailKind(kind, field);
318
+ if (kindOfDetail && detail !== null && typeof detail === "object") {
319
+ fields[field] = {kind: kindOfDetail, ...detail};
320
+ }
321
+ }
322
+ return {kind, ...fields};
323
+ }
324
+
325
+ function styleToValue({kind, ...fields}: Style & Record<string, any>, ref: number): any {
326
+ for (const [field, detail] of Object.entries(fields)) {
327
+ if (detailKind(kind, field) && detail !== null && typeof detail === "object") {
328
+ const {kind: _kind, ...detailFields} = detail;
329
+ fields[field] = detailFields;
330
+ }
331
+ }
332
+ return {"@c": kind, "@ref": ref, ...fields};
333
+ }
334
+
335
+ // A plain NamedStyles has no codec on either side, so it travels in the shape Java's serializer gives it
336
+ RpcCodecs.registerValueCodec(MarkersKind.NamedStyles, {
337
+ fromValue: (value: NamedStyles): NamedStyles => ({...value, styles: (value.styles ?? []).map(styleFromValue)}),
338
+ // Java's deserializer wants an object id on each style, and gives the first to the set itself
339
+ toValue: (after: NamedStyles) => ({...after, styles: after.styles.map((style, i) => styleToValue(style, i + 2))})
340
+ });
@@ -1,13 +1,20 @@
1
1
  import {Type} from "../java";
2
2
  import FullyQualified = Type.FullyQualified;
3
3
 
4
+ /**
5
+ * Matches a method type against a `<declaring type> <name>(<args>)` pattern.
6
+ * `REWRITE_JAVASCRIPT_DUMP_TYPES=1` in a test run prints the pattern each call would match.
7
+ *
8
+ * With `matchOverrides`, the pattern's type may also be a supertype or interface of the declaring
9
+ * type, as in Java. A pattern naming a subtype of the declaring type never matches.
10
+ */
4
11
  export class MethodMatcher {
5
12
  private readonly packagePattern: string;
6
13
  private readonly typePattern: string;
7
14
  private readonly methodPattern: string;
8
15
  private readonly argumentPatterns: string[];
9
16
 
10
- constructor(pattern: string) {
17
+ constructor(pattern: string, private readonly matchOverrides: boolean = false) {
11
18
  // Find the last space before the method spec (which contains parentheses)
12
19
  const firstParenIndex = pattern.indexOf('(');
13
20
  if (firstParenIndex === -1) {
@@ -31,7 +38,9 @@ export class MethodMatcher {
31
38
  } else {
32
39
  const lastDotIndex = typeSpec.lastIndexOf('.');
33
40
  if (lastDotIndex === -1) {
34
- this.packagePattern = '*';
41
+ // As in Java, a type without a package matches only the type in no package, such as
42
+ // the global `Array` or the module `fs-extra`. `*.Array` matches any package.
43
+ this.packagePattern = '';
35
44
  this.typePattern = typeSpec;
36
45
  } else {
37
46
  // Check if we're splitting a *.. pattern incorrectly
@@ -71,23 +80,7 @@ export class MethodMatcher {
71
80
  return false;
72
81
  }
73
82
 
74
- // Extract fully qualified name from declaringType
75
- const fullyQualifiedName = FullyQualified.getFullyQualifiedName(method.declaringType);
76
-
77
- // Split fully qualified name into package and type
78
- const lastDotIndex = fullyQualifiedName.lastIndexOf('.');
79
- const packageName = lastDotIndex === -1 ? '' : fullyQualifiedName.substring(0, lastDotIndex);
80
- const typeName = lastDotIndex === -1 ? fullyQualifiedName : fullyQualifiedName.substring(lastDotIndex + 1);
81
-
82
- // Match package
83
- if (!this.matchesPackage(packageName)) {
84
- return false;
85
- }
86
-
87
- // Match type (normalize primitives for matching)
88
- const normalizedTypePattern = this.normalizePrimitiveType(this.typePattern);
89
- const normalizedTypeName = this.normalizePrimitiveType(typeName);
90
- if (!this.matchesPattern(normalizedTypePattern, normalizedTypeName)) {
83
+ if (!this.matchesTargetType(method.declaringType)) {
91
84
  return false;
92
85
  }
93
86
 
@@ -97,11 +90,30 @@ export class MethodMatcher {
97
90
  }
98
91
 
99
92
  // Match arguments - convert Type[] to string representations
100
- const argStrings = method.parameterTypes.map(type => this.typeToString(type));
93
+ const argStrings = method.parameterTypes.map(type => MethodMatcher.typeName(type));
101
94
  return this.matchesArguments(argStrings);
102
95
  }
103
96
 
104
- private typeToString(type: Type): string {
97
+ private matchesTargetType(type: Type | undefined): boolean {
98
+ const matches = (name: string) => this.matchesTypeName(name);
99
+ // A call on an untyped receiver declares on the unknown type, which a wildcard pattern
100
+ // matches by name. Java's matcher has no such case because Java calls are always typed.
101
+ return type?.kind === Type.Kind.Unknown
102
+ ? matches(FullyQualified.getFullyQualifiedName(type))
103
+ : Type.isOfTypeWithName(type, this.matchOverrides, matches);
104
+ }
105
+
106
+ private matchesTypeName(fullyQualifiedName: string): boolean {
107
+ const lastDotIndex = fullyQualifiedName.lastIndexOf('.');
108
+ const packageName = lastDotIndex === -1 ? '' : fullyQualifiedName.substring(0, lastDotIndex);
109
+ const typeName = lastDotIndex === -1 ? fullyQualifiedName : fullyQualifiedName.substring(lastDotIndex + 1);
110
+
111
+ return this.matchesPackage(packageName) &&
112
+ this.matchesPattern(this.normalizePrimitiveType(this.typePattern), this.normalizePrimitiveType(typeName));
113
+ }
114
+
115
+ /** The name an argument pattern compares against a parameter of this type. */
116
+ static typeName(type: Type): string {
105
117
  switch (type.kind) {
106
118
  case Type.Kind.Primitive:
107
119
  return (type as Type.Primitive).keyword;
@@ -111,7 +123,7 @@ export class MethodMatcher {
111
123
  return FullyQualified.getFullyQualifiedName((type as Type.Parameterized).type);
112
124
  case Type.Kind.Array:
113
125
  const arrayType = type as Type.Array;
114
- return this.typeToString(arrayType.elemType) + '[]';
126
+ return MethodMatcher.typeName(arrayType.elemType) + '[]';
115
127
  case Type.Kind.GenericTypeVariable:
116
128
  return (type as Type.GenericTypeVariable).name;
117
129
  default:
@@ -659,7 +659,7 @@ export function createNodeResolutionResultMarker(
659
659
  resolvedDependencies,
660
660
  packageManager,
661
661
  engines: normalizeEngines(packageJsonContent.engines),
662
- npmrcConfigs,
662
+ npmrcConfigs: npmrcConfigs?.length ? npmrcConfigs : undefined,
663
663
  } as NodeResolutionResult;
664
664
  }
665
665
 
@@ -1022,7 +1022,8 @@ RpcCodecs.registerCodec(NodeResolutionResultKind, {
1022
1022
  resolvedDependencies: (await q.receiveList(before.resolvedDependencies)) || [],
1023
1023
  packageManager: await q.receive(before.packageManager),
1024
1024
  engines: await q.receive(before.engines),
1025
- npmrcConfigs: (await q.receiveList(before.npmrcConfigs)) || undefined,
1025
+ // an empty list is how having none is sent
1026
+ npmrcConfigs: await q.receiveList(before.npmrcConfigs).then(configs => configs?.length ? configs : undefined),
1026
1027
  });
1027
1028
  },
1028
1029
 
@@ -0,0 +1,45 @@
1
+ /*
2
+ * Copyright 2026 the original author or authors.
3
+ * <p>
4
+ * Licensed under the Moderne Source Available License (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ * <p>
8
+ * https://docs.moderne.io/licensing/moderne-source-available-license
9
+ * <p>
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ /**
18
+ * The package a bare module specifier, or a path below `node_modules`, names. Attribution names a
19
+ * type declared in an installed package after it, so `@scope/pkg/sub` gives `@scope/pkg`.
20
+ */
21
+ export function packageNameOf(specifier: string): string {
22
+ const segments = specifier.split('/');
23
+ return normalizePackageName(segments[0].startsWith('@') && segments.length > 1
24
+ ? `${segments[0]}/${segments[1]}`
25
+ : segments[0]);
26
+ }
27
+
28
+ /**
29
+ * The specifier consumers import a node_modules package under. A DefinitelyTyped package
30
+ * `@types/<pkg>` names `<pkg>`, and `@types/testing-library__react` names `@testing-library/react`.
31
+ * Types reached directly and through a declaration file's path then share one name.
32
+ */
33
+ function normalizePackageName(packageName: string): string {
34
+ if (packageName.startsWith('@types/')) {
35
+ packageName = packageName.substring('@types/'.length);
36
+ // Decode __ encoding for scoped packages: testing-library__react -> @testing-library/react
37
+ if (packageName.includes('__')) {
38
+ const parts = packageName.split('__');
39
+ if (parts.length === 2) {
40
+ packageName = `@${parts[0]}/${parts[1]}`;
41
+ }
42
+ }
43
+ }
44
+ return packageName;
45
+ }