@openrewrite/rewrite 8.66.0-SNAPSHOT → 8.66.1
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.
- package/dist/javascript/comparator.d.ts +91 -5
- package/dist/javascript/comparator.d.ts.map +1 -1
- package/dist/javascript/comparator.js +679 -3091
- package/dist/javascript/comparator.js.map +1 -1
- package/dist/javascript/format.d.ts.map +1 -1
- package/dist/javascript/format.js +4 -3
- package/dist/javascript/format.js.map +1 -1
- package/dist/javascript/index.d.ts +1 -1
- package/dist/javascript/index.d.ts.map +1 -1
- package/dist/javascript/index.js +1 -1
- package/dist/javascript/index.js.map +1 -1
- package/dist/javascript/parser.d.ts.map +1 -1
- package/dist/javascript/parser.js +22 -21
- package/dist/javascript/parser.js.map +1 -1
- package/dist/javascript/print.d.ts +2 -2
- package/dist/javascript/print.d.ts.map +1 -1
- package/dist/javascript/print.js +4 -4
- package/dist/javascript/print.js.map +1 -1
- package/dist/javascript/templating/capture.d.ts +226 -0
- package/dist/javascript/templating/capture.d.ts.map +1 -0
- package/dist/javascript/templating/capture.js +371 -0
- package/dist/javascript/templating/capture.js.map +1 -0
- package/dist/javascript/templating/comparator.d.ts +61 -0
- package/dist/javascript/templating/comparator.d.ts.map +1 -0
- package/dist/javascript/templating/comparator.js +393 -0
- package/dist/javascript/templating/comparator.js.map +1 -0
- package/dist/javascript/templating/engine.d.ts +75 -0
- package/dist/javascript/templating/engine.d.ts.map +1 -0
- package/dist/javascript/templating/engine.js +228 -0
- package/dist/javascript/templating/engine.js.map +1 -0
- package/dist/javascript/templating/index.d.ts +6 -0
- package/dist/javascript/templating/index.d.ts.map +1 -0
- package/dist/javascript/templating/index.js +42 -0
- package/dist/javascript/templating/index.js.map +1 -0
- package/dist/javascript/templating/pattern.d.ts +171 -0
- package/dist/javascript/templating/pattern.d.ts.map +1 -0
- package/dist/javascript/templating/pattern.js +681 -0
- package/dist/javascript/templating/pattern.js.map +1 -0
- package/dist/javascript/templating/placeholder-replacement.d.ts +58 -0
- package/dist/javascript/templating/placeholder-replacement.d.ts.map +1 -0
- package/dist/javascript/templating/placeholder-replacement.js +365 -0
- package/dist/javascript/templating/placeholder-replacement.js.map +1 -0
- package/dist/javascript/templating/rewrite.d.ts +39 -0
- package/dist/javascript/templating/rewrite.d.ts.map +1 -0
- package/dist/javascript/templating/rewrite.js +81 -0
- package/dist/javascript/templating/rewrite.js.map +1 -0
- package/dist/javascript/templating/template.d.ts +204 -0
- package/dist/javascript/templating/template.d.ts.map +1 -0
- package/dist/javascript/templating/template.js +293 -0
- package/dist/javascript/templating/template.js.map +1 -0
- package/dist/javascript/templating/types.d.ts +263 -0
- package/dist/javascript/templating/types.d.ts.map +1 -0
- package/dist/javascript/templating/types.js +3 -0
- package/dist/javascript/templating/types.js.map +1 -0
- package/dist/javascript/templating/utils.d.ts +118 -0
- package/dist/javascript/templating/utils.d.ts.map +1 -0
- package/dist/javascript/templating/utils.js +253 -0
- package/dist/javascript/templating/utils.js.map +1 -0
- package/dist/test/rewrite-test.d.ts.map +1 -1
- package/dist/test/rewrite-test.js +65 -9
- package/dist/test/rewrite-test.js.map +1 -1
- package/dist/version.txt +1 -1
- package/package.json +2 -2
- package/src/javascript/comparator.ts +721 -3607
- package/src/javascript/format.ts +3 -2
- package/src/javascript/index.ts +1 -1
- package/src/javascript/parser.ts +23 -22
- package/src/javascript/print.ts +6 -6
- package/src/javascript/templating/capture.ts +503 -0
- package/src/javascript/templating/comparator.ts +430 -0
- package/src/javascript/templating/engine.ts +252 -0
- package/src/javascript/templating/index.ts +60 -0
- package/src/javascript/templating/pattern.ts +727 -0
- package/src/javascript/templating/placeholder-replacement.ts +372 -0
- package/src/javascript/templating/rewrite.ts +95 -0
- package/src/javascript/templating/template.ts +326 -0
- package/src/javascript/templating/types.ts +300 -0
- package/src/javascript/templating/utils.ts +284 -0
- package/src/test/rewrite-test.ts +65 -1
- package/dist/javascript/templating.d.ts +0 -265
- package/dist/javascript/templating.d.ts.map +0 -1
- package/dist/javascript/templating.js +0 -1069
- package/dist/javascript/templating.js.map +0 -1
- package/src/javascript/templating.ts +0 -1277
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
import { J } from '../../java';
|
|
2
|
+
import { Capture, Any, TemplateParam, CaptureOptions, VariadicOptions } from './types';
|
|
3
|
+
/**
|
|
4
|
+
* Combines multiple constraints with AND logic.
|
|
5
|
+
* All constraints must return true for the combined constraint to pass.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* const largeEvenNumber = capture('n', {
|
|
9
|
+
* constraint: and(
|
|
10
|
+
* (node) => typeof node.value === 'number',
|
|
11
|
+
* (node) => node.value > 100,
|
|
12
|
+
* (node) => node.value % 2 === 0
|
|
13
|
+
* )
|
|
14
|
+
* });
|
|
15
|
+
*/
|
|
16
|
+
export declare function and<T>(...constraints: ((node: T) => boolean)[]): (node: T) => boolean;
|
|
17
|
+
/**
|
|
18
|
+
* Combines multiple constraints with OR logic.
|
|
19
|
+
* At least one constraint must return true for the combined constraint to pass.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* const stringOrNumber = capture('value', {
|
|
23
|
+
* constraint: or(
|
|
24
|
+
* (node) => node.kind === J.Kind.Literal && typeof node.value === 'string',
|
|
25
|
+
* (node) => node.kind === J.Kind.Literal && typeof node.value === 'number'
|
|
26
|
+
* )
|
|
27
|
+
* });
|
|
28
|
+
*/
|
|
29
|
+
export declare function or<T>(...constraints: ((node: T) => boolean)[]): (node: T) => boolean;
|
|
30
|
+
/**
|
|
31
|
+
* Negates a constraint.
|
|
32
|
+
* Returns true when the constraint returns false, and vice versa.
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* const notString = capture('value', {
|
|
36
|
+
* constraint: not((node) => typeof node.value === 'string')
|
|
37
|
+
* });
|
|
38
|
+
*/
|
|
39
|
+
export declare function not<T>(constraint: (node: T) => boolean): (node: T) => boolean;
|
|
40
|
+
export declare const CAPTURE_NAME_SYMBOL: unique symbol;
|
|
41
|
+
export declare const CAPTURE_VARIADIC_SYMBOL: unique symbol;
|
|
42
|
+
export declare const CAPTURE_CONSTRAINT_SYMBOL: unique symbol;
|
|
43
|
+
export declare const CAPTURE_CAPTURING_SYMBOL: unique symbol;
|
|
44
|
+
export declare class CaptureImpl<T = any> implements Capture<T> {
|
|
45
|
+
readonly name: string;
|
|
46
|
+
[CAPTURE_NAME_SYMBOL]: string;
|
|
47
|
+
[CAPTURE_VARIADIC_SYMBOL]: VariadicOptions | undefined;
|
|
48
|
+
[CAPTURE_CONSTRAINT_SYMBOL]: ((node: T) => boolean) | undefined;
|
|
49
|
+
[CAPTURE_CAPTURING_SYMBOL]: boolean;
|
|
50
|
+
constructor(name: string, options?: CaptureOptions<T>, capturing?: boolean);
|
|
51
|
+
getName(): string;
|
|
52
|
+
isVariadic(): boolean;
|
|
53
|
+
getVariadicOptions(): VariadicOptions | undefined;
|
|
54
|
+
getConstraint(): ((node: T) => boolean) | undefined;
|
|
55
|
+
isCapturing(): boolean;
|
|
56
|
+
}
|
|
57
|
+
export declare class TemplateParamImpl<T = any> implements TemplateParam<T> {
|
|
58
|
+
readonly name: string;
|
|
59
|
+
constructor(name: string);
|
|
60
|
+
getName(): string;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Represents a property access on a captured value.
|
|
64
|
+
* When you access a property on a Capture (e.g., method.name), you get a CaptureValue
|
|
65
|
+
* that knows how to resolve that property from the matched values.
|
|
66
|
+
*/
|
|
67
|
+
export declare class CaptureValue {
|
|
68
|
+
readonly rootCapture: Capture;
|
|
69
|
+
readonly propertyPath: string[];
|
|
70
|
+
readonly arrayOperation?: {
|
|
71
|
+
type: "index" | "slice" | "length";
|
|
72
|
+
args?: number[];
|
|
73
|
+
} | undefined;
|
|
74
|
+
constructor(rootCapture: Capture, propertyPath: string[], arrayOperation?: {
|
|
75
|
+
type: "index" | "slice" | "length";
|
|
76
|
+
args?: number[];
|
|
77
|
+
} | undefined);
|
|
78
|
+
/**
|
|
79
|
+
* Resolves this capture value by looking up the root capture in the values map
|
|
80
|
+
* and navigating through the property path.
|
|
81
|
+
*/
|
|
82
|
+
resolve(values: Pick<Map<string, J | J[]>, 'get'>): any;
|
|
83
|
+
/**
|
|
84
|
+
* Checks if this CaptureValue will resolve to an array that should be expanded.
|
|
85
|
+
*/
|
|
86
|
+
isArrayExpansion(): boolean;
|
|
87
|
+
}
|
|
88
|
+
export declare function capture<T = any>(options: {
|
|
89
|
+
name?: string;
|
|
90
|
+
constraint: (node: T) => boolean;
|
|
91
|
+
} & {
|
|
92
|
+
variadic?: never;
|
|
93
|
+
}): Capture<T> & T;
|
|
94
|
+
export declare function capture<T = any>(options: {
|
|
95
|
+
name?: string;
|
|
96
|
+
variadic: true | VariadicOptions;
|
|
97
|
+
constraint?: (nodes: T[]) => boolean;
|
|
98
|
+
min?: number;
|
|
99
|
+
max?: number;
|
|
100
|
+
}): Capture<T[]> & T[];
|
|
101
|
+
export declare function capture<T = any>(name?: string): Capture<T> & T;
|
|
102
|
+
export declare namespace capture {
|
|
103
|
+
var nextUnnamedId: number;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Creates a non-capturing pattern match for use in patterns.
|
|
107
|
+
*
|
|
108
|
+
* Use `any()` when you need to match AST structure without binding the matched value to a name.
|
|
109
|
+
* This is useful for validation patterns where you care about structure but not the specific values.
|
|
110
|
+
*
|
|
111
|
+
* **Key Differences from `capture()`:**
|
|
112
|
+
* - `any()` returns `Any<T>` type (not `Capture<T>`)
|
|
113
|
+
* - Cannot be used in templates (TypeScript compiler prevents this)
|
|
114
|
+
* - Does not bind matched values (more memory efficient for patterns)
|
|
115
|
+
* - Supports same features: constraints, variadic matching
|
|
116
|
+
*
|
|
117
|
+
* @template T The expected type of the matched AST node (for TypeScript autocomplete and constraints)
|
|
118
|
+
* @param options Optional configuration (variadic, constraint)
|
|
119
|
+
* @returns An Any object that matches patterns without capturing
|
|
120
|
+
*
|
|
121
|
+
* @example
|
|
122
|
+
* // Match any single argument without capturing
|
|
123
|
+
* const pat = pattern`foo(${any()})`;
|
|
124
|
+
*
|
|
125
|
+
* @example
|
|
126
|
+
* // Match with constraint validation
|
|
127
|
+
* const numericArg = any<J.Literal>({
|
|
128
|
+
* constraint: (node) => typeof node.value === 'number'
|
|
129
|
+
* });
|
|
130
|
+
* const pat = pattern`process(${numericArg})`;
|
|
131
|
+
*
|
|
132
|
+
* @example
|
|
133
|
+
* // Variadic any - match zero or more without capturing
|
|
134
|
+
* const rest = any({ variadic: true });
|
|
135
|
+
* const first = capture('first');
|
|
136
|
+
* const pat = pattern`foo(${first}, ${rest})`;
|
|
137
|
+
*
|
|
138
|
+
* @example
|
|
139
|
+
* // Mixed with captures - capture some, ignore others
|
|
140
|
+
* const important = capture('important');
|
|
141
|
+
* const pat = pattern`
|
|
142
|
+
* if (${any()}) {
|
|
143
|
+
* ${important}
|
|
144
|
+
* }
|
|
145
|
+
* `;
|
|
146
|
+
*
|
|
147
|
+
* @example
|
|
148
|
+
* // Variadic with constraints
|
|
149
|
+
* const numericArgs = any<J.Literal>({
|
|
150
|
+
* variadic: true,
|
|
151
|
+
* constraint: (nodes) => nodes.every(n => typeof n.value === 'number')
|
|
152
|
+
* });
|
|
153
|
+
* const pat = pattern`sum(${numericArgs})`;
|
|
154
|
+
*/
|
|
155
|
+
export declare function any<T = any>(options: {
|
|
156
|
+
constraint: (node: T) => boolean;
|
|
157
|
+
} & {
|
|
158
|
+
variadic?: never;
|
|
159
|
+
}): Any<T> & T;
|
|
160
|
+
export declare function any<T = any>(options: {
|
|
161
|
+
variadic: true | VariadicOptions;
|
|
162
|
+
constraint?: (nodes: T[]) => boolean;
|
|
163
|
+
min?: number;
|
|
164
|
+
max?: number;
|
|
165
|
+
}): Any<T[]> & T[];
|
|
166
|
+
export declare function any<T = any>(options?: CaptureOptions<T>): Any<T> & T;
|
|
167
|
+
export declare namespace any {
|
|
168
|
+
var nextAnonId: number;
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Creates a parameter specification for use in standalone templates (not used with patterns).
|
|
172
|
+
*
|
|
173
|
+
* Use `param()` when creating templates that are not used with pattern matching.
|
|
174
|
+
* Use `capture()` when the template works with a pattern.
|
|
175
|
+
*
|
|
176
|
+
* @template T The expected type of the parameter value (for TypeScript autocomplete only)
|
|
177
|
+
* @param name Optional name for the parameter. If not provided, an auto-generated name is used.
|
|
178
|
+
* @returns A TemplateParam object (simpler than Capture, no property access support)
|
|
179
|
+
*
|
|
180
|
+
* @remarks
|
|
181
|
+
* **When to use `param()` vs `capture()`:**
|
|
182
|
+
*
|
|
183
|
+
* - Use `param()` in **standalone templates** (no pattern matching involved)
|
|
184
|
+
* - Use `capture()` in **patterns** and templates used with patterns
|
|
185
|
+
*
|
|
186
|
+
* **Key Differences:**
|
|
187
|
+
* - `TemplateParam` is simpler - no property access proxy overhead
|
|
188
|
+
* - `Capture` supports property access (e.g., `capture('x').name.simpleName`)
|
|
189
|
+
* - Both work in templates, but `param()` makes intent clearer for standalone use
|
|
190
|
+
*
|
|
191
|
+
* @example
|
|
192
|
+
* // ✅ GOOD: Use param() for standalone templates
|
|
193
|
+
* const value = param<J.Literal>('value');
|
|
194
|
+
* const tmpl = template`return ${value} * 2;`;
|
|
195
|
+
* await tmpl.apply(cursor, node, new Map([['value', someLiteral]]));
|
|
196
|
+
*
|
|
197
|
+
* @example
|
|
198
|
+
* // ✅ GOOD: Use capture() with patterns
|
|
199
|
+
* const value = capture('value');
|
|
200
|
+
* const pat = pattern`foo(${value})`;
|
|
201
|
+
* const tmpl = template`bar(${value})`; // capture() makes sense here
|
|
202
|
+
*
|
|
203
|
+
* @example
|
|
204
|
+
* // ⚠️ CONFUSING: Using capture() in standalone template
|
|
205
|
+
* const value = capture('value');
|
|
206
|
+
* template`return ${value} * 2;`; // "Capturing" what? There's no pattern!
|
|
207
|
+
*
|
|
208
|
+
* @example
|
|
209
|
+
* // ❌ WRONG: param() doesn't support property access
|
|
210
|
+
* const node = param<J.MethodInvocation>('invocation');
|
|
211
|
+
* template`console.log(${node.name})` // Error! Use capture() for property access
|
|
212
|
+
*/
|
|
213
|
+
export declare function param<T = any>(name?: string): TemplateParam<T>;
|
|
214
|
+
/**
|
|
215
|
+
* Concise alias for `capture`. Works well for inline captures in patterns and templates.
|
|
216
|
+
*
|
|
217
|
+
* @param name Optional name for the capture. If not provided, an auto-generated name is used.
|
|
218
|
+
* @returns A Capture object
|
|
219
|
+
*
|
|
220
|
+
* @example
|
|
221
|
+
* // Inline captures with _ alias
|
|
222
|
+
* pattern`isDate(${_('dateArg')})`
|
|
223
|
+
* template`${_('dateArg')} instanceof Date`
|
|
224
|
+
*/
|
|
225
|
+
export declare const _: typeof capture;
|
|
226
|
+
//# sourceMappingURL=capture.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"capture.d.ts","sourceRoot":"","sources":["../../../src/javascript/templating/capture.ts"],"names":[],"mappings":"AAeA,OAAO,EAAC,CAAC,EAAC,MAAM,YAAY,CAAC;AAC7B,OAAO,EAAC,OAAO,EAAE,GAAG,EAAE,aAAa,EAAE,cAAc,EAAE,eAAe,EAAC,MAAM,SAAS,CAAC;AAErF;;;;;;;;;;;;GAYG;AACH,wBAAgB,GAAG,CAAC,CAAC,EAAE,GAAG,WAAW,EAAE,CAAC,CAAC,IAAI,EAAE,CAAC,KAAK,OAAO,CAAC,EAAE,GAAG,CAAC,IAAI,EAAE,CAAC,KAAK,OAAO,CAErF;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,EAAE,CAAC,CAAC,EAAE,GAAG,WAAW,EAAE,CAAC,CAAC,IAAI,EAAE,CAAC,KAAK,OAAO,CAAC,EAAE,GAAG,CAAC,IAAI,EAAE,CAAC,KAAK,OAAO,CAEpF;AAED;;;;;;;;GAQG;AACH,wBAAgB,GAAG,CAAC,CAAC,EAAE,UAAU,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,OAAO,GAAG,CAAC,IAAI,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EH,wBAAgB,GAAG,CAAC,CAAC,GAAG,GAAG,EACvB,OAAO,EAAE;IAAE,UAAU,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,OAAO,CAAA;CAAE,GAAG;IAAE,QAAQ,CAAC,EAAE,KAAK,CAAA;CAAE,GACrE,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;AAGd,wBAAgB,GAAG,CAAC,CAAC,GAAG,GAAG,EACvB,OAAO,EAAE;IAAE,QAAQ,EAAE,IAAI,GAAG,eAAe,CAAC;IAAC,UAAU,CAAC,EAAE,CAAC,KAAK,EAAE,CAAC,EAAE,KAAK,OAAO,CAAC;IAAC,GAAG,CAAC,EAAE,MAAM,CAAC;IAAC,GAAG,CAAC,EAAE,MAAM,CAAA;CAAE,GAChH,GAAG,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC;AAGlB,wBAAgB,GAAG,CAAC,CAAC,GAAG,GAAG,EACvB,OAAO,CAAC,EAAE,cAAc,CAAC,CAAC,CAAC,GAC5B,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;yBAFE,GAAG;;;AAenB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0CG;AACH,wBAAgB,KAAK,CAAC,CAAC,GAAG,GAAG,EAAE,IAAI,CAAC,EAAE,MAAM,GAAG,aAAa,CAAC,CAAC,CAAC,CAG9D;AAED;;;;;;;;;;GAUG;AACH,eAAO,MAAM,CAAC,gBAAU,CAAC"}
|
|
@@ -0,0 +1,371 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports._ = exports.CaptureValue = exports.TemplateParamImpl = exports.CaptureImpl = exports.CAPTURE_CAPTURING_SYMBOL = exports.CAPTURE_CONSTRAINT_SYMBOL = exports.CAPTURE_VARIADIC_SYMBOL = exports.CAPTURE_NAME_SYMBOL = void 0;
|
|
4
|
+
exports.and = and;
|
|
5
|
+
exports.or = or;
|
|
6
|
+
exports.not = not;
|
|
7
|
+
exports.capture = capture;
|
|
8
|
+
exports.any = any;
|
|
9
|
+
exports.param = param;
|
|
10
|
+
/**
|
|
11
|
+
* Combines multiple constraints with AND logic.
|
|
12
|
+
* All constraints must return true for the combined constraint to pass.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* const largeEvenNumber = capture('n', {
|
|
16
|
+
* constraint: and(
|
|
17
|
+
* (node) => typeof node.value === 'number',
|
|
18
|
+
* (node) => node.value > 100,
|
|
19
|
+
* (node) => node.value % 2 === 0
|
|
20
|
+
* )
|
|
21
|
+
* });
|
|
22
|
+
*/
|
|
23
|
+
function and(...constraints) {
|
|
24
|
+
return (node) => constraints.every(c => c(node));
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Combines multiple constraints with OR logic.
|
|
28
|
+
* At least one constraint must return true for the combined constraint to pass.
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* const stringOrNumber = capture('value', {
|
|
32
|
+
* constraint: or(
|
|
33
|
+
* (node) => node.kind === J.Kind.Literal && typeof node.value === 'string',
|
|
34
|
+
* (node) => node.kind === J.Kind.Literal && typeof node.value === 'number'
|
|
35
|
+
* )
|
|
36
|
+
* });
|
|
37
|
+
*/
|
|
38
|
+
function or(...constraints) {
|
|
39
|
+
return (node) => constraints.some(c => c(node));
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Negates a constraint.
|
|
43
|
+
* Returns true when the constraint returns false, and vice versa.
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* const notString = capture('value', {
|
|
47
|
+
* constraint: not((node) => typeof node.value === 'string')
|
|
48
|
+
* });
|
|
49
|
+
*/
|
|
50
|
+
function not(constraint) {
|
|
51
|
+
return (node) => !constraint(node);
|
|
52
|
+
}
|
|
53
|
+
// Symbol to access the internal capture name without triggering Proxy
|
|
54
|
+
exports.CAPTURE_NAME_SYMBOL = Symbol('captureName');
|
|
55
|
+
// Symbol to access variadic options without triggering Proxy
|
|
56
|
+
exports.CAPTURE_VARIADIC_SYMBOL = Symbol('captureVariadic');
|
|
57
|
+
// Symbol to access constraint function without triggering Proxy
|
|
58
|
+
exports.CAPTURE_CONSTRAINT_SYMBOL = Symbol('captureConstraint');
|
|
59
|
+
// Symbol to access capturing flag without triggering Proxy
|
|
60
|
+
exports.CAPTURE_CAPTURING_SYMBOL = Symbol('captureCapturing');
|
|
61
|
+
class CaptureImpl {
|
|
62
|
+
constructor(name, options, capturing = true) {
|
|
63
|
+
this.name = name;
|
|
64
|
+
this[exports.CAPTURE_NAME_SYMBOL] = name;
|
|
65
|
+
this[exports.CAPTURE_CAPTURING_SYMBOL] = capturing;
|
|
66
|
+
// Normalize variadic options
|
|
67
|
+
if (options === null || options === void 0 ? void 0 : options.variadic) {
|
|
68
|
+
if (typeof options.variadic === 'boolean') {
|
|
69
|
+
this[exports.CAPTURE_VARIADIC_SYMBOL] = {};
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
this[exports.CAPTURE_VARIADIC_SYMBOL] = {
|
|
73
|
+
min: options.variadic.min,
|
|
74
|
+
max: options.variadic.max
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
// Store constraint if provided
|
|
79
|
+
if (options === null || options === void 0 ? void 0 : options.constraint) {
|
|
80
|
+
this[exports.CAPTURE_CONSTRAINT_SYMBOL] = options.constraint;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
getName() {
|
|
84
|
+
return this[exports.CAPTURE_NAME_SYMBOL];
|
|
85
|
+
}
|
|
86
|
+
isVariadic() {
|
|
87
|
+
return this[exports.CAPTURE_VARIADIC_SYMBOL] !== undefined;
|
|
88
|
+
}
|
|
89
|
+
getVariadicOptions() {
|
|
90
|
+
return this[exports.CAPTURE_VARIADIC_SYMBOL];
|
|
91
|
+
}
|
|
92
|
+
getConstraint() {
|
|
93
|
+
return this[exports.CAPTURE_CONSTRAINT_SYMBOL];
|
|
94
|
+
}
|
|
95
|
+
isCapturing() {
|
|
96
|
+
return this[exports.CAPTURE_CAPTURING_SYMBOL];
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
exports.CaptureImpl = CaptureImpl;
|
|
100
|
+
class TemplateParamImpl {
|
|
101
|
+
constructor(name) {
|
|
102
|
+
this.name = name;
|
|
103
|
+
}
|
|
104
|
+
getName() {
|
|
105
|
+
return this.name;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
exports.TemplateParamImpl = TemplateParamImpl;
|
|
109
|
+
/**
|
|
110
|
+
* Represents a property access on a captured value.
|
|
111
|
+
* When you access a property on a Capture (e.g., method.name), you get a CaptureValue
|
|
112
|
+
* that knows how to resolve that property from the matched values.
|
|
113
|
+
*/
|
|
114
|
+
class CaptureValue {
|
|
115
|
+
constructor(rootCapture, propertyPath, arrayOperation) {
|
|
116
|
+
this.rootCapture = rootCapture;
|
|
117
|
+
this.propertyPath = propertyPath;
|
|
118
|
+
this.arrayOperation = arrayOperation;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Resolves this capture value by looking up the root capture in the values map
|
|
122
|
+
* and navigating through the property path.
|
|
123
|
+
*/
|
|
124
|
+
resolve(values) {
|
|
125
|
+
const rootName = this.rootCapture.getName();
|
|
126
|
+
let current = values.get(rootName);
|
|
127
|
+
// Handle array operations on variadic captures
|
|
128
|
+
if (this.arrayOperation && Array.isArray(current)) {
|
|
129
|
+
switch (this.arrayOperation.type) {
|
|
130
|
+
case 'index':
|
|
131
|
+
current = current[this.arrayOperation.args[0]];
|
|
132
|
+
break;
|
|
133
|
+
case 'slice':
|
|
134
|
+
current = current.slice(...this.arrayOperation.args);
|
|
135
|
+
break;
|
|
136
|
+
case 'length':
|
|
137
|
+
current = current.length;
|
|
138
|
+
break;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
// Navigate through property path
|
|
142
|
+
for (const prop of this.propertyPath) {
|
|
143
|
+
if (current === null || current === undefined || typeof current === 'number') {
|
|
144
|
+
return undefined;
|
|
145
|
+
}
|
|
146
|
+
current = current[prop];
|
|
147
|
+
}
|
|
148
|
+
return current;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Checks if this CaptureValue will resolve to an array that should be expanded.
|
|
152
|
+
*/
|
|
153
|
+
isArrayExpansion() {
|
|
154
|
+
var _a, _b, _c;
|
|
155
|
+
// Only slice operations and the root variadic capture return arrays
|
|
156
|
+
return ((_a = this.arrayOperation) === null || _a === void 0 ? void 0 : _a.type) === 'slice' ||
|
|
157
|
+
(this.arrayOperation === undefined && this.propertyPath.length === 0 &&
|
|
158
|
+
((_c = (_b = this.rootCapture).isVariadic) === null || _c === void 0 ? void 0 : _c.call(_b)));
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
exports.CaptureValue = CaptureValue;
|
|
162
|
+
/**
|
|
163
|
+
* Creates a Proxy around a CaptureValue that allows further property access.
|
|
164
|
+
* This enables chaining like method.name.simpleName
|
|
165
|
+
*/
|
|
166
|
+
function createCaptureValueProxy(rootCapture, propertyPath, arrayOperation) {
|
|
167
|
+
const captureValue = new CaptureValue(rootCapture, propertyPath, arrayOperation);
|
|
168
|
+
return new Proxy(captureValue, {
|
|
169
|
+
get(target, prop) {
|
|
170
|
+
// Allow access to the CaptureValue instance itself and its methods
|
|
171
|
+
if (prop === 'resolve' || prop === 'rootCapture' || prop === 'propertyPath' ||
|
|
172
|
+
prop === 'arrayOperation' || prop === 'isArrayExpansion') {
|
|
173
|
+
const value = target[prop];
|
|
174
|
+
return typeof value === 'function' ? value.bind(target) : value;
|
|
175
|
+
}
|
|
176
|
+
// For string property access, extend the property path
|
|
177
|
+
if (typeof prop === 'string') {
|
|
178
|
+
return createCaptureValueProxy(target.rootCapture, [...target.propertyPath, prop], target.arrayOperation);
|
|
179
|
+
}
|
|
180
|
+
return undefined;
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Creates a capture specification for use in template patterns.
|
|
186
|
+
*
|
|
187
|
+
* @template T The expected type of the captured AST node (for TypeScript autocomplete only)
|
|
188
|
+
* @returns A Capture object that supports property access for use in templates
|
|
189
|
+
*
|
|
190
|
+
* @remarks
|
|
191
|
+
* **Pattern Matching is Structural:**
|
|
192
|
+
*
|
|
193
|
+
* What a capture matches is determined by the pattern structure, not the type parameter:
|
|
194
|
+
* - `pattern`${capture('x')}`` matches ANY single expression (identifier, literal, call, binary, etc.)
|
|
195
|
+
* - `pattern`foo(${capture('x')})`` matches only expressions inside `foo()` calls
|
|
196
|
+
* - `pattern`${capture('x')} + ${capture('y')}`` matches only binary `+` expressions
|
|
197
|
+
*
|
|
198
|
+
* The TypeScript type parameter `<T>` provides IDE autocomplete but doesn't enforce runtime types.
|
|
199
|
+
*
|
|
200
|
+
* @example
|
|
201
|
+
* // Named inline captures
|
|
202
|
+
* const pat = pattern`${capture('left')} + ${capture('right')}`;
|
|
203
|
+
* // Matches: <any expression> + <any expression>
|
|
204
|
+
*
|
|
205
|
+
* // Unnamed captures
|
|
206
|
+
* const {left, right} = {left: capture(), right: capture()};
|
|
207
|
+
* const pattern = pattern`${left} + ${right}`;
|
|
208
|
+
*
|
|
209
|
+
* @example
|
|
210
|
+
* // Type parameter is for IDE autocomplete only
|
|
211
|
+
* const method = capture<J.MethodInvocation>('method');
|
|
212
|
+
* const pat = pattern`foo(${method})`;
|
|
213
|
+
* // Matches: foo(<any expression>) - not restricted to method invocations!
|
|
214
|
+
* // Type <J.MethodInvocation> only helps with autocomplete in your code
|
|
215
|
+
*
|
|
216
|
+
* @example
|
|
217
|
+
* // Structural pattern determines what matches
|
|
218
|
+
* const arg = capture('arg');
|
|
219
|
+
* const pat = pattern`process(${arg})`;
|
|
220
|
+
* // Matches: process(42), process("text"), process(x + y), etc.
|
|
221
|
+
* // The 'arg' capture will bind to whatever expression is passed to process()
|
|
222
|
+
*
|
|
223
|
+
* @example
|
|
224
|
+
* // Repeated patterns using the same capture
|
|
225
|
+
* const expr = capture('expr');
|
|
226
|
+
* const redundantOr = pattern`${expr} || ${expr}`;
|
|
227
|
+
* // Matches expressions where both sides are identical: x || x, foo() || foo()
|
|
228
|
+
*
|
|
229
|
+
* @example
|
|
230
|
+
* // Property access in templates
|
|
231
|
+
* const method = capture<J.MethodInvocation>('method');
|
|
232
|
+
* template`console.log(${method.name.simpleName})` // Accesses properties of captured node
|
|
233
|
+
*/
|
|
234
|
+
/**
|
|
235
|
+
* Creates a Proxy wrapper for CaptureImpl that intercepts property accesses.
|
|
236
|
+
* Shared logic between capture() and any() to avoid duplication.
|
|
237
|
+
*/
|
|
238
|
+
function createCaptureProxy(impl) {
|
|
239
|
+
return new Proxy(impl, {
|
|
240
|
+
get(target, prop) {
|
|
241
|
+
// Allow access to internal symbols
|
|
242
|
+
if (prop === exports.CAPTURE_NAME_SYMBOL) {
|
|
243
|
+
return target[exports.CAPTURE_NAME_SYMBOL];
|
|
244
|
+
}
|
|
245
|
+
if (prop === exports.CAPTURE_VARIADIC_SYMBOL) {
|
|
246
|
+
return target[exports.CAPTURE_VARIADIC_SYMBOL];
|
|
247
|
+
}
|
|
248
|
+
if (prop === exports.CAPTURE_CONSTRAINT_SYMBOL) {
|
|
249
|
+
return target[exports.CAPTURE_CONSTRAINT_SYMBOL];
|
|
250
|
+
}
|
|
251
|
+
if (prop === exports.CAPTURE_CAPTURING_SYMBOL) {
|
|
252
|
+
return target[exports.CAPTURE_CAPTURING_SYMBOL];
|
|
253
|
+
}
|
|
254
|
+
// Allow methods to be called directly on the target
|
|
255
|
+
if (prop === 'getName' || prop === 'isVariadic' || prop === 'getVariadicOptions' || prop === 'getConstraint' || prop === 'isCapturing') {
|
|
256
|
+
return target[prop].bind(target);
|
|
257
|
+
}
|
|
258
|
+
// For variadic captures, support array-like operations
|
|
259
|
+
if (target.isVariadic() && typeof prop === 'string') {
|
|
260
|
+
// Numeric index access: args[0], args[1], etc.
|
|
261
|
+
const indexNum = Number(prop);
|
|
262
|
+
if (!isNaN(indexNum) && indexNum >= 0 && Number.isInteger(indexNum)) {
|
|
263
|
+
return createCaptureValueProxy(target, [], { type: 'index', args: [indexNum] });
|
|
264
|
+
}
|
|
265
|
+
// Array method: slice
|
|
266
|
+
if (prop === 'slice') {
|
|
267
|
+
return (...args) => {
|
|
268
|
+
return createCaptureValueProxy(target, [], { type: 'slice', args });
|
|
269
|
+
};
|
|
270
|
+
}
|
|
271
|
+
// Array property: length
|
|
272
|
+
if (prop === 'length') {
|
|
273
|
+
return createCaptureValueProxy(target, [], { type: 'length' });
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
// For string property access, create a CaptureValue with a property path
|
|
277
|
+
if (typeof prop === 'string') {
|
|
278
|
+
return createCaptureValueProxy(target, [prop]);
|
|
279
|
+
}
|
|
280
|
+
return undefined;
|
|
281
|
+
}
|
|
282
|
+
});
|
|
283
|
+
}
|
|
284
|
+
// Implementation
|
|
285
|
+
function capture(nameOrOptions) {
|
|
286
|
+
let name;
|
|
287
|
+
let options;
|
|
288
|
+
if (typeof nameOrOptions === 'string') {
|
|
289
|
+
// Simple named capture: capture('name')
|
|
290
|
+
name = nameOrOptions;
|
|
291
|
+
options = undefined;
|
|
292
|
+
}
|
|
293
|
+
else {
|
|
294
|
+
// Options-based API: capture({ name: 'name', ...options }) or capture()
|
|
295
|
+
options = nameOrOptions;
|
|
296
|
+
name = options === null || options === void 0 ? void 0 : options.name;
|
|
297
|
+
}
|
|
298
|
+
const captureName = name || `unnamed_${capture.nextUnnamedId++}`;
|
|
299
|
+
const impl = new CaptureImpl(captureName, options);
|
|
300
|
+
return createCaptureProxy(impl);
|
|
301
|
+
}
|
|
302
|
+
// Static counter for generating unique IDs for unnamed captures
|
|
303
|
+
capture.nextUnnamedId = 1;
|
|
304
|
+
// Implementation
|
|
305
|
+
function any(options) {
|
|
306
|
+
const anonName = `anon_${any.nextAnonId++}`;
|
|
307
|
+
const impl = new CaptureImpl(anonName, options, false); // capturing = false
|
|
308
|
+
return createCaptureProxy(impl);
|
|
309
|
+
}
|
|
310
|
+
// Static counter for generating unique IDs for anonymous (non-capturing) patterns
|
|
311
|
+
any.nextAnonId = 1;
|
|
312
|
+
/**
|
|
313
|
+
* Creates a parameter specification for use in standalone templates (not used with patterns).
|
|
314
|
+
*
|
|
315
|
+
* Use `param()` when creating templates that are not used with pattern matching.
|
|
316
|
+
* Use `capture()` when the template works with a pattern.
|
|
317
|
+
*
|
|
318
|
+
* @template T The expected type of the parameter value (for TypeScript autocomplete only)
|
|
319
|
+
* @param name Optional name for the parameter. If not provided, an auto-generated name is used.
|
|
320
|
+
* @returns A TemplateParam object (simpler than Capture, no property access support)
|
|
321
|
+
*
|
|
322
|
+
* @remarks
|
|
323
|
+
* **When to use `param()` vs `capture()`:**
|
|
324
|
+
*
|
|
325
|
+
* - Use `param()` in **standalone templates** (no pattern matching involved)
|
|
326
|
+
* - Use `capture()` in **patterns** and templates used with patterns
|
|
327
|
+
*
|
|
328
|
+
* **Key Differences:**
|
|
329
|
+
* - `TemplateParam` is simpler - no property access proxy overhead
|
|
330
|
+
* - `Capture` supports property access (e.g., `capture('x').name.simpleName`)
|
|
331
|
+
* - Both work in templates, but `param()` makes intent clearer for standalone use
|
|
332
|
+
*
|
|
333
|
+
* @example
|
|
334
|
+
* // ✅ GOOD: Use param() for standalone templates
|
|
335
|
+
* const value = param<J.Literal>('value');
|
|
336
|
+
* const tmpl = template`return ${value} * 2;`;
|
|
337
|
+
* await tmpl.apply(cursor, node, new Map([['value', someLiteral]]));
|
|
338
|
+
*
|
|
339
|
+
* @example
|
|
340
|
+
* // ✅ GOOD: Use capture() with patterns
|
|
341
|
+
* const value = capture('value');
|
|
342
|
+
* const pat = pattern`foo(${value})`;
|
|
343
|
+
* const tmpl = template`bar(${value})`; // capture() makes sense here
|
|
344
|
+
*
|
|
345
|
+
* @example
|
|
346
|
+
* // ⚠️ CONFUSING: Using capture() in standalone template
|
|
347
|
+
* const value = capture('value');
|
|
348
|
+
* template`return ${value} * 2;`; // "Capturing" what? There's no pattern!
|
|
349
|
+
*
|
|
350
|
+
* @example
|
|
351
|
+
* // ❌ WRONG: param() doesn't support property access
|
|
352
|
+
* const node = param<J.MethodInvocation>('invocation');
|
|
353
|
+
* template`console.log(${node.name})` // Error! Use capture() for property access
|
|
354
|
+
*/
|
|
355
|
+
function param(name) {
|
|
356
|
+
const paramName = name || `unnamed_${capture.nextUnnamedId++}`;
|
|
357
|
+
return new TemplateParamImpl(paramName);
|
|
358
|
+
}
|
|
359
|
+
/**
|
|
360
|
+
* Concise alias for `capture`. Works well for inline captures in patterns and templates.
|
|
361
|
+
*
|
|
362
|
+
* @param name Optional name for the capture. If not provided, an auto-generated name is used.
|
|
363
|
+
* @returns A Capture object
|
|
364
|
+
*
|
|
365
|
+
* @example
|
|
366
|
+
* // Inline captures with _ alias
|
|
367
|
+
* pattern`isDate(${_('dateArg')})`
|
|
368
|
+
* template`${_('dateArg')} instanceof Date`
|
|
369
|
+
*/
|
|
370
|
+
exports._ = capture;
|
|
371
|
+
//# sourceMappingURL=capture.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"capture.js","sourceRoot":"","sources":["../../../src/javascript/templating/capture.ts"],"names":[],"mappings":";;;AA+BA,kBAEC;AAcD,gBAEC;AAWD,kBAEC;AA0RD,0BAkBC;AAuED,kBAKC;AAgDD,sBAGC;AAvdD;;;;;;;;;;;;GAYG;AACH,SAAgB,GAAG,CAAI,GAAG,WAAqC;IAC3D,OAAO,CAAC,IAAO,EAAE,EAAE,CAAC,WAAW,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC;AACxD,CAAC;AAED;;;;;;;;;;;GAWG;AACH,SAAgB,EAAE,CAAI,GAAG,WAAqC;IAC1D,OAAO,CAAC,IAAO,EAAE,EAAE,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,C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|
+
import { J } from '../../java';
|
|
3
|
+
import { JS } from '../index';
|
|
4
|
+
import { JavaScriptSemanticComparatorVisitor } from '../comparator';
|
|
5
|
+
import { CaptureStorageValue } from './utils';
|
|
6
|
+
/**
|
|
7
|
+
* A comparator for pattern matching that is lenient about optional properties.
|
|
8
|
+
* Allows patterns without type annotations to match actual code with type annotations.
|
|
9
|
+
* Uses semantic comparison to match semantically equivalent code (e.g., isDate() and util.isDate()).
|
|
10
|
+
*/
|
|
11
|
+
export declare class PatternMatchingComparator extends JavaScriptSemanticComparatorVisitor {
|
|
12
|
+
private readonly matcher;
|
|
13
|
+
constructor(matcher: {
|
|
14
|
+
handleCapture: (pattern: J, target: J, wrapper?: J.RightPadded<J>) => boolean;
|
|
15
|
+
handleVariadicCapture: (pattern: J, targets: J[], wrappers?: J.RightPadded<J>[]) => boolean;
|
|
16
|
+
saveState: () => Map<string, CaptureStorageValue>;
|
|
17
|
+
restoreState: (state: Map<string, CaptureStorageValue>) => void;
|
|
18
|
+
}, lenientTypeMatching?: boolean);
|
|
19
|
+
/**
|
|
20
|
+
* Extracts the wrapper from the cursor if the parent is a RightPadded.
|
|
21
|
+
*/
|
|
22
|
+
private getWrapperFromCursor;
|
|
23
|
+
visit<R extends J>(j: Tree, p: J, parent?: Cursor): Promise<R | undefined>;
|
|
24
|
+
protected hasSameKind(j: J, other: J): boolean;
|
|
25
|
+
visitIdentifier(identifier: J.Identifier, other: J): Promise<J | undefined>;
|
|
26
|
+
visitMethodInvocation(methodInvocation: J.MethodInvocation, other: J): Promise<J | undefined>;
|
|
27
|
+
visitBlock(block: J.Block, other: J): Promise<J | undefined>;
|
|
28
|
+
visitJsCompilationUnit(compilationUnit: JS.CompilationUnit, other: J): Promise<J | undefined>;
|
|
29
|
+
/**
|
|
30
|
+
* Matches argument lists, with special handling for variadic captures.
|
|
31
|
+
* A variadic capture can match zero or more consecutive arguments.
|
|
32
|
+
*/
|
|
33
|
+
private matchArguments;
|
|
34
|
+
/**
|
|
35
|
+
* Generic sequence matching with variadic capture support.
|
|
36
|
+
* Works for any sequence of JRightPadded elements (arguments, statements, etc.).
|
|
37
|
+
* A variadic capture can match zero or more consecutive elements.
|
|
38
|
+
*
|
|
39
|
+
* Uses pivot detection to optimize matching, with backtracking as fallback.
|
|
40
|
+
*
|
|
41
|
+
* @param patternElements The pattern elements (JRightPadded)
|
|
42
|
+
* @param targetElements The target elements to match against (JRightPadded)
|
|
43
|
+
* @param filterEmpty Whether to filter out J.Empty elements when capturing (true for arguments, false for statements)
|
|
44
|
+
* @returns true if the sequence matches, false otherwise
|
|
45
|
+
*/
|
|
46
|
+
private matchSequence;
|
|
47
|
+
/**
|
|
48
|
+
* Optimized sequence matcher with pivot detection and backtracking.
|
|
49
|
+
* For variadic patterns, tries to detect pivots (where next pattern matches) to avoid
|
|
50
|
+
* unnecessary backtracking. Falls back to full backtracking when pivots are ambiguous.
|
|
51
|
+
*
|
|
52
|
+
* @param patternElements The pattern elements (JRightPadded)
|
|
53
|
+
* @param targetElements The target elements to match against (JRightPadded)
|
|
54
|
+
* @param patternIdx Current position in pattern
|
|
55
|
+
* @param targetIdx Current position in target
|
|
56
|
+
* @param filterEmpty Whether to filter out J.Empty elements when capturing
|
|
57
|
+
* @returns true if the remaining sequence matches, false otherwise
|
|
58
|
+
*/
|
|
59
|
+
private matchSequenceOptimized;
|
|
60
|
+
}
|
|
61
|
+
//# sourceMappingURL=comparator.d.ts.map
|