@openrewrite/rewrite 8.67.0-20251119-160338 → 8.67.0-20251120-075051

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 (38) hide show
  1. package/dist/javascript/add-import.d.ts +21 -10
  2. package/dist/javascript/add-import.d.ts.map +1 -1
  3. package/dist/javascript/add-import.js +55 -21
  4. package/dist/javascript/add-import.js.map +1 -1
  5. package/dist/javascript/remove-import.d.ts +18 -14
  6. package/dist/javascript/remove-import.d.ts.map +1 -1
  7. package/dist/javascript/remove-import.js +37 -47
  8. package/dist/javascript/remove-import.js.map +1 -1
  9. package/dist/javascript/templating/capture.d.ts +3 -4
  10. package/dist/javascript/templating/capture.d.ts.map +1 -1
  11. package/dist/javascript/templating/capture.js +3 -3
  12. package/dist/javascript/templating/capture.js.map +1 -1
  13. package/dist/javascript/templating/comparator.d.ts +7 -1
  14. package/dist/javascript/templating/comparator.d.ts.map +1 -1
  15. package/dist/javascript/templating/comparator.js +45 -10
  16. package/dist/javascript/templating/comparator.js.map +1 -1
  17. package/dist/javascript/templating/pattern.d.ts +10 -11
  18. package/dist/javascript/templating/pattern.d.ts.map +1 -1
  19. package/dist/javascript/templating/pattern.js +18 -36
  20. package/dist/javascript/templating/pattern.js.map +1 -1
  21. package/dist/javascript/templating/rewrite.js +2 -2
  22. package/dist/javascript/templating/rewrite.js.map +1 -1
  23. package/dist/javascript/templating/template.d.ts +27 -13
  24. package/dist/javascript/templating/template.d.ts.map +1 -1
  25. package/dist/javascript/templating/template.js +31 -14
  26. package/dist/javascript/templating/template.js.map +1 -1
  27. package/dist/javascript/templating/types.d.ts +111 -15
  28. package/dist/javascript/templating/types.d.ts.map +1 -1
  29. package/dist/version.txt +1 -1
  30. package/package.json +1 -1
  31. package/src/javascript/add-import.ts +70 -27
  32. package/src/javascript/remove-import.ts +37 -46
  33. package/src/javascript/templating/capture.ts +7 -7
  34. package/src/javascript/templating/comparator.ts +50 -11
  35. package/src/javascript/templating/pattern.ts +32 -24
  36. package/src/javascript/templating/rewrite.ts +2 -2
  37. package/src/javascript/templating/template.ts +36 -18
  38. package/src/javascript/templating/types.ts +127 -16
@@ -34,10 +34,49 @@ export interface VariadicOptions {
34
34
  max?: number;
35
35
  }
36
36
 
37
+ /**
38
+ * Read-only access to captures matched so far during pattern matching.
39
+ * Provides a consistent interface with MatchResult for looking up captured values.
40
+ */
41
+ export interface CaptureMap {
42
+ /**
43
+ * Gets the value of a capture by Capture object or name.
44
+ * Returns undefined if the capture hasn't been matched yet.
45
+ */
46
+ get<T>(capture: Capture<T>): T | undefined;
47
+ get(capture: string): any;
48
+
49
+ /**
50
+ * Checks if a capture has been matched.
51
+ */
52
+ has(capture: Capture | string): boolean;
53
+ }
54
+
55
+ /**
56
+ * Context passed to capture constraint functions.
57
+ * Provides access to the cursor for AST navigation and previously matched captures.
58
+ */
59
+ export interface CaptureConstraintContext {
60
+ /**
61
+ * The cursor pointing to the node being matched.
62
+ * Allows navigating the AST (parent, root, etc.).
63
+ */
64
+ cursor: Cursor;
65
+
66
+ /**
67
+ * Read-only view of values captured so far in the matching process.
68
+ * Allows constraints to depend on previous captures.
69
+ * Returns undefined for captures that haven't been processed yet.
70
+ */
71
+ captures: CaptureMap;
72
+ }
73
+
37
74
  /**
38
75
  * Constraint function for captures.
39
- * The cursor parameter is always provided with a defined value, but functions can
40
- * choose to accept it or not (TypeScript allows functions with fewer parameters).
76
+ *
77
+ * Receives the node being validated and a context providing access to:
78
+ * - cursor: For navigating the AST
79
+ * - captures: For accessing previously matched captures
41
80
  *
42
81
  * For non-variadic captures: use ConstraintFunction<T> where T is the node type
43
82
  * For variadic captures: use ConstraintFunction<T[]> where T[] is the array type
@@ -45,7 +84,7 @@ export interface VariadicOptions {
45
84
  * When used with variadic captures, the cursor points to the nearest common parent
46
85
  * of the captured elements.
47
86
  */
48
- export type ConstraintFunction<T> = (node: T, cursor: Cursor) => boolean;
87
+ export type ConstraintFunction<T> = (node: T, context: CaptureConstraintContext) => boolean;
49
88
 
50
89
  /**
51
90
  * Options for the capture function.
@@ -55,39 +94,45 @@ export type ConstraintFunction<T> = (node: T, cursor: Cursor) => boolean;
55
94
  * - For regular captures: constraint receives a single node of type T
56
95
  * - For variadic captures: constraint receives an array of nodes of type T[]
57
96
  *
58
- * The constraint function can optionally receive a cursor parameter to perform
59
- * context-aware validation during pattern matching.
97
+ * The context parameter provides access to the cursor and previously matched captures.
60
98
  */
61
99
  export interface CaptureOptions<T = any> {
62
100
  name?: string;
63
101
  variadic?: boolean | VariadicOptions;
64
102
  /**
65
103
  * Optional constraint function that validates whether a captured node should be accepted.
66
- * The function always receives:
104
+ * The function receives:
67
105
  * - node: The captured node (or array of nodes for variadic captures)
68
- * - cursor: A cursor at the captured node's position (always defined)
69
- *
70
- * Functions can choose to accept just the node parameter if they don't need the cursor.
106
+ * - context: Provides access to cursor and previously matched captures
71
107
  *
72
108
  * @param node The captured node to validate
73
- * @param cursor Cursor at the captured node's position
109
+ * @param context Provides cursor for AST navigation and previously matched captures
74
110
  * @returns true if the capture should be accepted, false otherwise
75
111
  *
76
112
  * @example
77
113
  * ```typescript
78
- * // Simple node validation (cursor parameter ignored)
114
+ * // Simple node validation
79
115
  * capture<J.Literal>('size', {
80
116
  * constraint: (node) => typeof node.value === 'number' && node.value > 100
81
117
  * })
82
118
  *
83
- * // Context-aware validation (using cursor)
119
+ * // Context-aware validation using cursor
84
120
  * capture<J.MethodInvocation>('method', {
85
- * constraint: (node, cursor) => {
121
+ * constraint: (node, context) => {
86
122
  * if (!node.name.simpleName.startsWith('get')) return false;
87
- * const cls = cursor.firstEnclosing(isClassDeclaration);
123
+ * const cls = context.cursor.firstEnclosing(isClassDeclaration);
88
124
  * return cls?.name.simpleName === 'ApiController';
89
125
  * }
90
126
  * })
127
+ *
128
+ * // Validation depending on previous captures
129
+ * const min = capture('min');
130
+ * const max = capture('max', {
131
+ * constraint: (node, context) => {
132
+ * const minVal = context.captures.get(min);
133
+ * return minVal && node.value > minVal.value;
134
+ * }
135
+ * })
91
136
  * ```
92
137
  */
93
138
  constraint?: ConstraintFunction<T>;
@@ -96,9 +141,37 @@ export interface CaptureOptions<T = any> {
96
141
  * a preamble declaring the capture identifier with this type annotation, allowing
97
142
  * the TypeScript parser/compiler to produce a properly type-attributed AST.
98
143
  *
144
+ * **Why Use Type Attribution:**
145
+ * When matching against TypeScript code with type information, providing a type ensures
146
+ * the pattern's AST has matching type attribution, which can be important for:
147
+ * - Semantic matching based on types
148
+ * - Matching code that depends on type inference
149
+ * - Ensuring pattern parses with correct type context
150
+ *
99
151
  * Can be specified as:
100
- * - A string type annotation (e.g., "boolean", "string", "number")
152
+ * - A string type annotation (e.g., "boolean", "string", "number", "Promise<any>", "User[]")
101
153
  * - A Type instance from the AST (the type will be inferred from the Type)
154
+ *
155
+ * @example
156
+ * ```typescript
157
+ * // Match promise chains with proper type attribution
158
+ * const chain = capture({
159
+ * name: 'chain',
160
+ * type: 'Promise<any>', // TypeScript will attribute this as Promise type
161
+ * constraint: (call: J.MethodInvocation) => {
162
+ * // Validate promise chain structure
163
+ * return call.name.simpleName === 'then';
164
+ * }
165
+ * });
166
+ * pattern`${chain}.catch(err => console.log(err))`
167
+ *
168
+ * // Match arrays with type annotation
169
+ * const items = capture({
170
+ * name: 'items',
171
+ * type: 'number[]', // Array of numbers
172
+ * });
173
+ * pattern`${items}.map(x => x * 2)`
174
+ * ```
102
175
  */
103
176
  type?: string | Type;
104
177
  }
@@ -344,7 +417,16 @@ export interface MatchOptions {
344
417
  * Note: Primitive values (string, number, boolean) are NOT supported in template literals.
345
418
  * Use raw() for inserting code strings, or Template.builder() API for programmatic construction.
346
419
  */
347
- export type TemplateParameter = Capture | CaptureValue | TemplateParam | RawCode | Tree | Tree[] | J.RightPadded<any> | J.RightPadded<any>[] | J.Container<any>;
420
+ export type TemplateParameter =
421
+ Capture
422
+ | CaptureValue
423
+ | TemplateParam
424
+ | RawCode
425
+ | Tree
426
+ | Tree[]
427
+ | J.RightPadded<any>
428
+ | J.RightPadded<any>[]
429
+ | J.Container<any>;
348
430
 
349
431
  /**
350
432
  * Parameter specification for template generation (internal).
@@ -401,6 +483,34 @@ export interface TemplateOptions {
401
483
  dependencies?: Record<string, string>;
402
484
  }
403
485
 
486
+ /**
487
+ * Options for template application.
488
+ */
489
+ export interface ApplyOptions {
490
+ /**
491
+ * Values for parameters in the template.
492
+ * Can be a Map, MatchResult, or plain object with capture names as keys.
493
+ *
494
+ * @example
495
+ * ```typescript
496
+ * // Using MatchResult from pattern matching
497
+ * const match = await pattern.match(node, cursor);
498
+ * await template.apply(node, cursor, { values: match });
499
+ *
500
+ * // Using a Map
501
+ * await template.apply(node, cursor, {
502
+ * values: new Map([['x', someNode]])
503
+ * });
504
+ *
505
+ * // Using a plain object
506
+ * await template.apply(node, cursor, {
507
+ * values: { x: someNode }
508
+ * });
509
+ * ```
510
+ */
511
+ values?: Map<Capture | string, J> | MatchResult | Record<string, J>;
512
+ }
513
+
404
514
  /**
405
515
  * Represents a replacement rule that can match a pattern and apply a template.
406
516
  */
@@ -643,6 +753,7 @@ export interface MatchResult {
643
753
  * @returns The captured node(s), or undefined if not found
644
754
  */
645
755
  get(capture: string): any;
756
+
646
757
  get<T>(capture: Capture<T>): T | undefined;
647
758
  }
648
759