@openrewrite/rewrite 8.66.0 → 8.66.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/java/tree.d.ts +10 -1
- package/dist/java/tree.d.ts.map +1 -1
- package/dist/java/tree.js +21 -5
- package/dist/java/tree.js.map +1 -1
- package/dist/java/type-visitor.d.ts +1 -1
- package/dist/java/type-visitor.d.ts.map +1 -1
- package/dist/java/visitor.d.ts +2 -2
- package/dist/java/visitor.d.ts.map +1 -1
- package/dist/java/visitor.js +8 -2
- package/dist/java/visitor.js.map +1 -1
- package/dist/javascript/assertions.d.ts +6 -0
- package/dist/javascript/assertions.d.ts.map +1 -1
- package/dist/javascript/assertions.js +14 -6
- package/dist/javascript/assertions.js.map +1 -1
- package/dist/javascript/comparator.d.ts +217 -7
- package/dist/javascript/comparator.d.ts.map +1 -1
- package/dist/javascript/comparator.js +1020 -2848
- package/dist/javascript/comparator.js.map +1 -1
- package/dist/javascript/format.d.ts +5 -3
- package/dist/javascript/format.d.ts.map +1 -1
- package/dist/javascript/format.js +87 -44
- package/dist/javascript/format.js.map +1 -1
- package/dist/javascript/index.d.ts +2 -1
- package/dist/javascript/index.d.ts.map +1 -1
- package/dist/javascript/index.js +2 -1
- package/dist/javascript/index.js.map +1 -1
- package/dist/javascript/parser.d.ts +2 -1
- package/dist/javascript/parser.d.ts.map +1 -1
- package/dist/javascript/parser.js +54 -43
- package/dist/javascript/parser.js.map +1 -1
- package/dist/javascript/templating/capture.d.ts +293 -0
- package/dist/javascript/templating/capture.d.ts.map +1 -0
- package/dist/javascript/templating/capture.js +461 -0
- package/dist/javascript/templating/capture.js.map +1 -0
- package/dist/javascript/templating/comparator.d.ts +171 -0
- package/dist/javascript/templating/comparator.d.ts.map +1 -0
- package/dist/javascript/templating/comparator.js +1221 -0
- package/dist/javascript/templating/comparator.js.map +1 -0
- package/dist/javascript/templating/engine.d.ts +108 -0
- package/dist/javascript/templating/engine.d.ts.map +1 -0
- package/dist/javascript/templating/engine.js +661 -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 +44 -0
- package/dist/javascript/templating/index.js.map +1 -0
- package/dist/javascript/templating/pattern.d.ts +276 -0
- package/dist/javascript/templating/pattern.d.ts.map +1 -0
- package/dist/javascript/templating/pattern.js +952 -0
- package/dist/javascript/templating/pattern.js.map +1 -0
- package/dist/javascript/templating/placeholder-replacement.d.ts +83 -0
- package/dist/javascript/templating/placeholder-replacement.d.ts.map +1 -0
- package/dist/javascript/templating/placeholder-replacement.js +467 -0
- package/dist/javascript/templating/placeholder-replacement.js.map +1 -0
- package/dist/javascript/templating/rewrite.d.ts +84 -0
- package/dist/javascript/templating/rewrite.d.ts.map +1 -0
- package/dist/javascript/templating/rewrite.js +208 -0
- package/dist/javascript/templating/rewrite.js.map +1 -0
- package/dist/javascript/templating/template.d.ts +230 -0
- package/dist/javascript/templating/template.d.ts.map +1 -0
- package/dist/javascript/templating/template.js +367 -0
- package/dist/javascript/templating/template.js.map +1 -0
- package/dist/javascript/templating/types.d.ts +610 -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 +135 -0
- package/dist/javascript/templating/utils.d.ts.map +1 -0
- package/dist/javascript/templating/utils.js +251 -0
- package/dist/javascript/templating/utils.js.map +1 -0
- package/dist/javascript/type-mapping.d.ts.map +1 -1
- package/dist/javascript/type-mapping.js +21 -11
- package/dist/javascript/type-mapping.js.map +1 -1
- package/dist/json/rpc.js +2 -2
- package/dist/json/rpc.js.map +1 -1
- package/dist/recipe/order-imports.js.map +1 -1
- package/dist/test/rewrite-test.d.ts.map +1 -1
- package/dist/test/rewrite-test.js +10 -6
- package/dist/test/rewrite-test.js.map +1 -1
- package/dist/version.txt +1 -1
- package/dist/visitor.d.ts +4 -4
- package/dist/visitor.d.ts.map +1 -1
- package/dist/visitor.js +8 -3
- package/dist/visitor.js.map +1 -1
- package/package.json +5 -2
- package/src/java/tree.ts +10 -3
- package/src/java/type-visitor.ts +1 -1
- package/src/java/visitor.ts +11 -5
- package/src/javascript/assertions.ts +9 -3
- package/src/javascript/comparator.ts +1095 -3373
- package/src/javascript/format.ts +72 -33
- package/src/javascript/index.ts +2 -1
- package/src/javascript/parser.ts +67 -45
- package/src/javascript/templating/capture.ts +595 -0
- package/src/javascript/templating/comparator.ts +1383 -0
- package/src/javascript/templating/engine.ts +750 -0
- package/src/javascript/templating/index.ts +67 -0
- package/src/javascript/templating/pattern.ts +1101 -0
- package/src/javascript/templating/placeholder-replacement.ts +475 -0
- package/src/javascript/templating/rewrite.ts +229 -0
- package/src/javascript/templating/template.ts +414 -0
- package/src/javascript/templating/types.ts +674 -0
- package/src/javascript/templating/utils.ts +298 -0
- package/src/javascript/type-mapping.ts +20 -11
- package/src/json/rpc.ts +2 -2
- package/src/recipe/order-imports.ts +1 -1
- package/src/test/rewrite-test.ts +12 -7
- package/src/visitor.ts +14 -6
- package/dist/javascript/templating.d.ts +0 -265
- package/dist/javascript/templating.d.ts.map +0 -1
- package/dist/javascript/templating.js +0 -1027
- package/dist/javascript/templating.js.map +0 -1
- package/src/javascript/templating.ts +0 -1226
|
@@ -0,0 +1,367 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.Template = exports.TemplateBuilder = void 0;
|
|
13
|
+
exports.template = template;
|
|
14
|
+
const pattern_1 = require("./pattern");
|
|
15
|
+
const utils_1 = require("./utils");
|
|
16
|
+
const capture_1 = require("./capture");
|
|
17
|
+
const engine_1 = require("./engine");
|
|
18
|
+
var JavaCoordinates;
|
|
19
|
+
(function (JavaCoordinates) {
|
|
20
|
+
let Mode;
|
|
21
|
+
(function (Mode) {
|
|
22
|
+
Mode[Mode["Before"] = 0] = "Before";
|
|
23
|
+
Mode[Mode["After"] = 1] = "After";
|
|
24
|
+
Mode[Mode["Replace"] = 2] = "Replace";
|
|
25
|
+
})(Mode = JavaCoordinates.Mode || (JavaCoordinates.Mode = {}));
|
|
26
|
+
})(JavaCoordinates || (JavaCoordinates = {}));
|
|
27
|
+
/**
|
|
28
|
+
* Builder for creating templates programmatically.
|
|
29
|
+
* Use when template structure is not known at compile time.
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* // Conditional construction
|
|
33
|
+
* const builder = Template.builder().code('function foo(x) {');
|
|
34
|
+
* if (needsValidation) {
|
|
35
|
+
* builder.code('if (typeof x !== "number") throw new Error("Invalid");');
|
|
36
|
+
* }
|
|
37
|
+
* builder.code('return x * 2; }');
|
|
38
|
+
* const tmpl = builder.build();
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* // Composition from fragments
|
|
42
|
+
* function createWrapper(innerBody: Capture): Template {
|
|
43
|
+
* return Template.builder()
|
|
44
|
+
* .code('function wrapper() { try { ')
|
|
45
|
+
* .param(innerBody)
|
|
46
|
+
* .code(' } catch(e) { console.error(e); } }')
|
|
47
|
+
* .build();
|
|
48
|
+
* }
|
|
49
|
+
*/
|
|
50
|
+
class TemplateBuilder {
|
|
51
|
+
constructor() {
|
|
52
|
+
this.parts = [];
|
|
53
|
+
this.params = [];
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Adds a static string part to the template.
|
|
57
|
+
*
|
|
58
|
+
* @param str The string to add
|
|
59
|
+
* @returns This builder for chaining
|
|
60
|
+
*/
|
|
61
|
+
code(str) {
|
|
62
|
+
// If there are already params, we need to add an empty string before this
|
|
63
|
+
if (this.params.length > this.parts.length) {
|
|
64
|
+
this.parts.push('');
|
|
65
|
+
}
|
|
66
|
+
// Append to the last part or start a new one
|
|
67
|
+
if (this.parts.length === 0) {
|
|
68
|
+
this.parts.push(str);
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
this.parts[this.parts.length - 1] += str;
|
|
72
|
+
}
|
|
73
|
+
return this;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Adds a parameter to the template.
|
|
77
|
+
*
|
|
78
|
+
* @param value The parameter value (Capture, Tree, or primitive)
|
|
79
|
+
* @returns This builder for chaining
|
|
80
|
+
*/
|
|
81
|
+
param(value) {
|
|
82
|
+
// Ensure we have a part for after this parameter
|
|
83
|
+
if (this.parts.length === 0) {
|
|
84
|
+
this.parts.push('');
|
|
85
|
+
}
|
|
86
|
+
this.params.push(value);
|
|
87
|
+
// Add an empty string for the next part
|
|
88
|
+
this.parts.push('');
|
|
89
|
+
return this;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Builds the template from accumulated parts and parameters.
|
|
93
|
+
*
|
|
94
|
+
* @returns A Template instance
|
|
95
|
+
*/
|
|
96
|
+
build() {
|
|
97
|
+
// Ensure parts array is one longer than params array
|
|
98
|
+
while (this.parts.length <= this.params.length) {
|
|
99
|
+
this.parts.push('');
|
|
100
|
+
}
|
|
101
|
+
// Create a synthetic TemplateStringsArray
|
|
102
|
+
const templateStrings = this.parts.slice();
|
|
103
|
+
templateStrings.raw = this.parts.slice();
|
|
104
|
+
Object.defineProperty(templateStrings, 'raw', {
|
|
105
|
+
value: this.parts.slice(),
|
|
106
|
+
writable: false
|
|
107
|
+
});
|
|
108
|
+
// Delegate to the template() function
|
|
109
|
+
return template(templateStrings, ...this.params);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
exports.TemplateBuilder = TemplateBuilder;
|
|
113
|
+
/**
|
|
114
|
+
* Template for creating AST nodes.
|
|
115
|
+
*
|
|
116
|
+
* This class provides the public API for template generation.
|
|
117
|
+
* The actual templating logic is handled by the internal TemplateEngine.
|
|
118
|
+
*
|
|
119
|
+
* Templates can reference captures from patterns, and you can access properties
|
|
120
|
+
* of captured nodes using dot notation. This allows you to extract and insert
|
|
121
|
+
* specific subtrees from matched AST nodes.
|
|
122
|
+
*
|
|
123
|
+
* @example
|
|
124
|
+
* // Generate a literal AST node
|
|
125
|
+
* const result = template`2`.apply(cursor, coordinates);
|
|
126
|
+
*
|
|
127
|
+
* @example
|
|
128
|
+
* // Generate an AST node with a parameter
|
|
129
|
+
* const result = template`${capture()}`.apply(cursor, coordinates);
|
|
130
|
+
*
|
|
131
|
+
* @example
|
|
132
|
+
* // Access properties of captured nodes in templates
|
|
133
|
+
* const method = capture<J.MethodInvocation>('method');
|
|
134
|
+
* const pat = pattern`foo(${method})`;
|
|
135
|
+
* const tmpl = template`bar(${method.name})`; // Access the 'name' property
|
|
136
|
+
*
|
|
137
|
+
* const match = await pat.match(someNode);
|
|
138
|
+
* if (match) {
|
|
139
|
+
* // The template will insert just the 'name' subtree from the captured method
|
|
140
|
+
* const result = await tmpl.apply(cursor, someNode, match);
|
|
141
|
+
* }
|
|
142
|
+
*
|
|
143
|
+
* @example
|
|
144
|
+
* // Deep property access chains
|
|
145
|
+
* const method = capture<J.MethodInvocation>('method');
|
|
146
|
+
* template`console.log(${method.name.simpleName})` // Navigate multiple properties
|
|
147
|
+
*
|
|
148
|
+
* @example
|
|
149
|
+
* // Array element access
|
|
150
|
+
* const invocation = capture<J.MethodInvocation>('invocation');
|
|
151
|
+
* template`bar(${invocation.arguments.elements[0].element})` // Access array elements
|
|
152
|
+
*/
|
|
153
|
+
class Template {
|
|
154
|
+
/**
|
|
155
|
+
* Creates a new builder for constructing templates programmatically.
|
|
156
|
+
*
|
|
157
|
+
* @returns A new TemplateBuilder instance
|
|
158
|
+
*
|
|
159
|
+
* @example
|
|
160
|
+
* const tmpl = Template.builder()
|
|
161
|
+
* .code('function foo() {')
|
|
162
|
+
* .code('return ')
|
|
163
|
+
* .param(capture('value'))
|
|
164
|
+
* .code('; }')
|
|
165
|
+
* .build();
|
|
166
|
+
*/
|
|
167
|
+
static builder() {
|
|
168
|
+
return new TemplateBuilder();
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Creates a new template.
|
|
172
|
+
*
|
|
173
|
+
* @param templateParts The string parts of the template
|
|
174
|
+
* @param parameters The parameters between the string parts
|
|
175
|
+
*/
|
|
176
|
+
constructor(templateParts, parameters) {
|
|
177
|
+
this.templateParts = templateParts;
|
|
178
|
+
this.parameters = parameters;
|
|
179
|
+
this.options = {};
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Configures this template with additional options.
|
|
183
|
+
*
|
|
184
|
+
* @param options Configuration options
|
|
185
|
+
* @returns This template for method chaining
|
|
186
|
+
*
|
|
187
|
+
* @example
|
|
188
|
+
* template`isDate(${capture('date')})`
|
|
189
|
+
* .configure({
|
|
190
|
+
* context: ['import { isDate } from "util"'],
|
|
191
|
+
* dependencies: { 'util': '^1.0.0' }
|
|
192
|
+
* })
|
|
193
|
+
*/
|
|
194
|
+
configure(options) {
|
|
195
|
+
this.options = Object.assign(Object.assign({}, this.options), options);
|
|
196
|
+
// Invalidate cache when configuration changes
|
|
197
|
+
this._cachedTemplate = undefined;
|
|
198
|
+
return this;
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Gets the template tree for this template, using two-level caching:
|
|
202
|
+
* - Level 1: Instance cache (this._cachedTemplate) - fastest, no lookup needed
|
|
203
|
+
* - Level 2: Global cache (globalAstCache) - fast, shared across all templates
|
|
204
|
+
* - Level 3: TemplateEngine - slow, parses and processes the template
|
|
205
|
+
*
|
|
206
|
+
* Most parameters use placeholders that are replaced during application, so templates
|
|
207
|
+
* with the same structure share cached ASTs. However, raw() parameters are spliced at
|
|
208
|
+
* construction time, so their values must be included in the cache key.
|
|
209
|
+
*
|
|
210
|
+
* @returns The cached or newly computed template tree
|
|
211
|
+
* @internal
|
|
212
|
+
*/
|
|
213
|
+
getTemplateTree() {
|
|
214
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
215
|
+
// Level 1: Instance cache (fastest path)
|
|
216
|
+
if (this._cachedTemplate) {
|
|
217
|
+
return this._cachedTemplate;
|
|
218
|
+
}
|
|
219
|
+
// Generate cache key for global lookup
|
|
220
|
+
// For raw() parameters, we need to include their code values in the key
|
|
221
|
+
// since they're spliced at construction time, not application time
|
|
222
|
+
const contextStatements = this.options.context || this.options.imports || [];
|
|
223
|
+
const parametersKey = this.parameters.map((p, i) => {
|
|
224
|
+
const value = p.value;
|
|
225
|
+
// Include raw code values in the cache key using the symbol
|
|
226
|
+
if (value && typeof value === 'object' && value[capture_1.RAW_CODE_SYMBOL]) {
|
|
227
|
+
return `raw:${value.code}`;
|
|
228
|
+
}
|
|
229
|
+
return i.toString();
|
|
230
|
+
}).join(',');
|
|
231
|
+
const cacheKey = (0, utils_1.generateCacheKey)(this.templateParts, parametersKey, contextStatements, this.options.dependencies || {});
|
|
232
|
+
// Level 2: Global cache (fast path - shared with Pattern)
|
|
233
|
+
const cached = utils_1.globalAstCache.get(cacheKey);
|
|
234
|
+
if (cached) {
|
|
235
|
+
this._cachedTemplate = cached;
|
|
236
|
+
return cached;
|
|
237
|
+
}
|
|
238
|
+
// Level 3: Compute via TemplateEngine (slow path)
|
|
239
|
+
const result = yield engine_1.TemplateEngine.getTemplateTree(this.templateParts, this.parameters, contextStatements, this.options.dependencies || {});
|
|
240
|
+
// Cache in both levels
|
|
241
|
+
utils_1.globalAstCache.set(cacheKey, result);
|
|
242
|
+
this._cachedTemplate = result;
|
|
243
|
+
return result;
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* Applies this template and returns the resulting tree.
|
|
248
|
+
*
|
|
249
|
+
* @param cursor The cursor pointing to the current location in the AST
|
|
250
|
+
* @param tree Input tree
|
|
251
|
+
* @param values values for parameters in template
|
|
252
|
+
* @returns A Promise resolving to the generated AST node
|
|
253
|
+
*/
|
|
254
|
+
apply(cursor, tree, values) {
|
|
255
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
256
|
+
// Normalize the values map: convert any Capture keys to string keys
|
|
257
|
+
let normalizedValues;
|
|
258
|
+
let wrappersMap = new Map();
|
|
259
|
+
if (values instanceof pattern_1.MatchResult) {
|
|
260
|
+
// MatchResult - extract both bindings and wrappersMap
|
|
261
|
+
normalizedValues = values;
|
|
262
|
+
wrappersMap = values[utils_1.WRAPPERS_MAP_SYMBOL]();
|
|
263
|
+
}
|
|
264
|
+
else if (values instanceof Map) {
|
|
265
|
+
const normalized = new Map();
|
|
266
|
+
for (const [key, value] of values.entries()) {
|
|
267
|
+
const stringKey = typeof key === 'string'
|
|
268
|
+
? key
|
|
269
|
+
: (key[capture_1.CAPTURE_NAME_SYMBOL] || key.getName());
|
|
270
|
+
normalized.set(stringKey, value);
|
|
271
|
+
}
|
|
272
|
+
normalizedValues = normalized;
|
|
273
|
+
}
|
|
274
|
+
else if (values && typeof values === 'object') {
|
|
275
|
+
// Check if it's a Map-like object with 'get' method, or a plain object literal
|
|
276
|
+
if ('get' in values && typeof values.get === 'function') {
|
|
277
|
+
// Map-like object with get method
|
|
278
|
+
normalizedValues = values;
|
|
279
|
+
}
|
|
280
|
+
else {
|
|
281
|
+
// Plain object literal - convert to Map
|
|
282
|
+
// Keys may be strings or Capture objects (via computed properties {[x]: value})
|
|
283
|
+
const normalized = new Map();
|
|
284
|
+
for (const [key, value] of Object.entries(values)) {
|
|
285
|
+
// If the key happens to be a stringified Capture (from computed properties),
|
|
286
|
+
// it's already been converted to a string by JavaScript
|
|
287
|
+
normalized.set(key, value);
|
|
288
|
+
}
|
|
289
|
+
normalizedValues = normalized;
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
// Use instance-level cache to get the template tree
|
|
293
|
+
const ast = yield this.getTemplateTree();
|
|
294
|
+
// Delegate to TemplateEngine for placeholder substitution and application
|
|
295
|
+
return engine_1.TemplateEngine.applyTemplateFromAst(ast, this.parameters, cursor, {
|
|
296
|
+
tree,
|
|
297
|
+
mode: JavaCoordinates.Mode.Replace
|
|
298
|
+
}, normalizedValues, wrappersMap);
|
|
299
|
+
});
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
exports.Template = Template;
|
|
303
|
+
/**
|
|
304
|
+
* Tagged template function for creating templates that generate AST nodes.
|
|
305
|
+
*
|
|
306
|
+
* Templates support property access on captures from patterns, allowing you to
|
|
307
|
+
* extract and insert specific subtrees from matched AST nodes. Use dot notation
|
|
308
|
+
* to navigate properties (e.g., `method.name`) or array bracket notation to
|
|
309
|
+
* access array elements (e.g., `args.elements[0].element`).
|
|
310
|
+
*
|
|
311
|
+
* Templates can also accept AST wrapper types directly:
|
|
312
|
+
* - J.RightPadded<T>: The element will be extracted and inserted
|
|
313
|
+
* - J.RightPadded<T>[]: Elements will be expanded in place
|
|
314
|
+
* - J.Container<T>: Elements will be expanded in place
|
|
315
|
+
*
|
|
316
|
+
* @param strings The string parts of the template
|
|
317
|
+
* @param parameters The parameters between the string parts (Capture, CaptureValue, TemplateParam, Tree, Tree[], J.RightPadded, J.RightPadded[], or J.Container)
|
|
318
|
+
* @returns A Template object that can be applied to generate AST nodes
|
|
319
|
+
*
|
|
320
|
+
* @example
|
|
321
|
+
* // Simple template with literal
|
|
322
|
+
* const tmpl = template`console.log("hello")`;
|
|
323
|
+
* const result = await tmpl.apply(cursor, node);
|
|
324
|
+
*
|
|
325
|
+
* @example
|
|
326
|
+
* // Template with capture - matches captured value from pattern
|
|
327
|
+
* const expr = capture('expr');
|
|
328
|
+
* const pat = pattern`foo(${expr})`;
|
|
329
|
+
* const tmpl = template`bar(${expr})`;
|
|
330
|
+
*
|
|
331
|
+
* const match = await pat.match(node);
|
|
332
|
+
* if (match) {
|
|
333
|
+
* const result = await tmpl.apply(cursor, node, match);
|
|
334
|
+
* }
|
|
335
|
+
*
|
|
336
|
+
* @example
|
|
337
|
+
* // Property access on captures - extract subtrees
|
|
338
|
+
* const method = capture<J.MethodInvocation>('method');
|
|
339
|
+
* const pat = pattern`foo(${method})`;
|
|
340
|
+
* // Access the 'name' property of the captured method invocation
|
|
341
|
+
* const tmpl = template`bar(${method.name})`;
|
|
342
|
+
*
|
|
343
|
+
* @example
|
|
344
|
+
* // Deep property chains
|
|
345
|
+
* const method = capture<J.MethodInvocation>('method');
|
|
346
|
+
* template`console.log(${method.name.simpleName})`
|
|
347
|
+
*
|
|
348
|
+
* @example
|
|
349
|
+
* // Array element access
|
|
350
|
+
* const invocation = capture<J.MethodInvocation>('invocation');
|
|
351
|
+
* template`bar(${invocation.arguments.elements[0].element})`
|
|
352
|
+
*
|
|
353
|
+
* @example
|
|
354
|
+
* // Using J.RightPadded and J.Container directly
|
|
355
|
+
* const selectExpr = method.select; // J.RightPadded<Expression>
|
|
356
|
+
* const args = method.arguments; // J.Container<Expression>
|
|
357
|
+
* template`${selectExpr}.newMethod(${args})`
|
|
358
|
+
*/
|
|
359
|
+
function template(strings, ...parameters) {
|
|
360
|
+
// Convert parameters to Parameter objects (no longer need to check for mutable tree property)
|
|
361
|
+
const processedParameters = parameters.map(param => {
|
|
362
|
+
// Just wrap each parameter value in a Parameter object
|
|
363
|
+
return { value: param };
|
|
364
|
+
});
|
|
365
|
+
return new Template(strings, processedParameters);
|
|
366
|
+
}
|
|
367
|
+
//# sourceMappingURL=template.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"template.js","sourceRoot":"","sources":["../../../src/javascript/templating/template.ts"],"names":[],"mappings":";;;;;;;;;;;;AAmZA,4BAQC;AAzYD,uCAA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