@openrewrite/rewrite 8.67.0-20251104-084009 → 8.67.0-20251104-114312
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 +67 -4
- package/dist/javascript/comparator.d.ts.map +1 -1
- package/dist/javascript/comparator.js +523 -2794
- package/dist/javascript/comparator.js.map +1 -1
- package/dist/javascript/format.d.ts.map +1 -1
- package/dist/javascript/format.js +3 -2
- 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/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/version.txt +1 -1
- package/package.json +2 -1
- package/src/javascript/comparator.ts +554 -3323
- package/src/javascript/format.ts +2 -1
- package/src/javascript/index.ts +1 -1
- 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/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,204 @@
|
|
|
1
|
+
import { Cursor, Tree } from '../..';
|
|
2
|
+
import { J } from '../../java';
|
|
3
|
+
import { TemplateOptions, TemplateParameter, Capture } from './types';
|
|
4
|
+
import { Parameter } from './engine';
|
|
5
|
+
/**
|
|
6
|
+
* Coordinates for template application.
|
|
7
|
+
*/
|
|
8
|
+
type JavaCoordinates = {
|
|
9
|
+
tree?: Tree;
|
|
10
|
+
loc?: JavaCoordinates.Location;
|
|
11
|
+
mode?: JavaCoordinates.Mode;
|
|
12
|
+
};
|
|
13
|
+
declare namespace JavaCoordinates {
|
|
14
|
+
type Location = 'EXPRESSION_PREFIX' | 'STATEMENT_PREFIX' | 'BLOCK_END';
|
|
15
|
+
enum Mode {
|
|
16
|
+
Before = 0,
|
|
17
|
+
After = 1,
|
|
18
|
+
Replace = 2
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Builder for creating templates programmatically.
|
|
23
|
+
* Use when template structure is not known at compile time.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* // Conditional construction
|
|
27
|
+
* const builder = Template.builder().code('function foo(x) {');
|
|
28
|
+
* if (needsValidation) {
|
|
29
|
+
* builder.code('if (typeof x !== "number") throw new Error("Invalid");');
|
|
30
|
+
* }
|
|
31
|
+
* builder.code('return x * 2; }');
|
|
32
|
+
* const tmpl = builder.build();
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* // Composition from fragments
|
|
36
|
+
* function createWrapper(innerBody: Capture): Template {
|
|
37
|
+
* return Template.builder()
|
|
38
|
+
* .code('function wrapper() { try { ')
|
|
39
|
+
* .param(innerBody)
|
|
40
|
+
* .code(' } catch(e) { console.error(e); } }')
|
|
41
|
+
* .build();
|
|
42
|
+
* }
|
|
43
|
+
*/
|
|
44
|
+
export declare class TemplateBuilder {
|
|
45
|
+
private parts;
|
|
46
|
+
private params;
|
|
47
|
+
/**
|
|
48
|
+
* Adds a static string part to the template.
|
|
49
|
+
*
|
|
50
|
+
* @param str The string to add
|
|
51
|
+
* @returns This builder for chaining
|
|
52
|
+
*/
|
|
53
|
+
code(str: string): this;
|
|
54
|
+
/**
|
|
55
|
+
* Adds a parameter to the template.
|
|
56
|
+
*
|
|
57
|
+
* @param value The parameter value (Capture, Tree, or primitive)
|
|
58
|
+
* @returns This builder for chaining
|
|
59
|
+
*/
|
|
60
|
+
param(value: TemplateParameter): this;
|
|
61
|
+
/**
|
|
62
|
+
* Builds the template from accumulated parts and parameters.
|
|
63
|
+
*
|
|
64
|
+
* @returns A Template instance
|
|
65
|
+
*/
|
|
66
|
+
build(): Template;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Template for creating AST nodes.
|
|
70
|
+
*
|
|
71
|
+
* This class provides the public API for template generation.
|
|
72
|
+
* The actual templating logic is handled by the internal TemplateEngine.
|
|
73
|
+
*
|
|
74
|
+
* Templates can reference captures from patterns, and you can access properties
|
|
75
|
+
* of captured nodes using dot notation. This allows you to extract and insert
|
|
76
|
+
* specific subtrees from matched AST nodes.
|
|
77
|
+
*
|
|
78
|
+
* @example
|
|
79
|
+
* // Generate a literal AST node
|
|
80
|
+
* const result = template`2`.apply(cursor, coordinates);
|
|
81
|
+
*
|
|
82
|
+
* @example
|
|
83
|
+
* // Generate an AST node with a parameter
|
|
84
|
+
* const result = template`${capture()}`.apply(cursor, coordinates);
|
|
85
|
+
*
|
|
86
|
+
* @example
|
|
87
|
+
* // Access properties of captured nodes in templates
|
|
88
|
+
* const method = capture<J.MethodInvocation>('method');
|
|
89
|
+
* const pat = pattern`foo(${method})`;
|
|
90
|
+
* const tmpl = template`bar(${method.name})`; // Access the 'name' property
|
|
91
|
+
*
|
|
92
|
+
* const match = await pat.match(someNode);
|
|
93
|
+
* if (match) {
|
|
94
|
+
* // The template will insert just the 'name' subtree from the captured method
|
|
95
|
+
* const result = await tmpl.apply(cursor, someNode, match);
|
|
96
|
+
* }
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* // Deep property access chains
|
|
100
|
+
* const method = capture<J.MethodInvocation>('method');
|
|
101
|
+
* template`console.log(${method.name.simpleName})` // Navigate multiple properties
|
|
102
|
+
*
|
|
103
|
+
* @example
|
|
104
|
+
* // Array element access
|
|
105
|
+
* const invocation = capture<J.MethodInvocation>('invocation');
|
|
106
|
+
* template`bar(${invocation.arguments.elements[0].element})` // Access array elements
|
|
107
|
+
*/
|
|
108
|
+
export declare class Template {
|
|
109
|
+
private readonly templateParts;
|
|
110
|
+
private readonly parameters;
|
|
111
|
+
private options;
|
|
112
|
+
/**
|
|
113
|
+
* Creates a new builder for constructing templates programmatically.
|
|
114
|
+
*
|
|
115
|
+
* @returns A new TemplateBuilder instance
|
|
116
|
+
*
|
|
117
|
+
* @example
|
|
118
|
+
* const tmpl = Template.builder()
|
|
119
|
+
* .code('function foo() {')
|
|
120
|
+
* .code('return ')
|
|
121
|
+
* .param(capture('value'))
|
|
122
|
+
* .code('; }')
|
|
123
|
+
* .build();
|
|
124
|
+
*/
|
|
125
|
+
static builder(): TemplateBuilder;
|
|
126
|
+
/**
|
|
127
|
+
* Creates a new template.
|
|
128
|
+
*
|
|
129
|
+
* @param templateParts The string parts of the template
|
|
130
|
+
* @param parameters The parameters between the string parts
|
|
131
|
+
*/
|
|
132
|
+
constructor(templateParts: TemplateStringsArray, parameters: Parameter[]);
|
|
133
|
+
/**
|
|
134
|
+
* Configures this template with additional options.
|
|
135
|
+
*
|
|
136
|
+
* @param options Configuration options
|
|
137
|
+
* @returns This template for method chaining
|
|
138
|
+
*
|
|
139
|
+
* @example
|
|
140
|
+
* template`isDate(${capture('date')})`
|
|
141
|
+
* .configure({
|
|
142
|
+
* imports: ['import { isDate } from "util"'],
|
|
143
|
+
* dependencies: { 'util': '^1.0.0' }
|
|
144
|
+
* })
|
|
145
|
+
*/
|
|
146
|
+
configure(options: TemplateOptions): Template;
|
|
147
|
+
/**
|
|
148
|
+
* Applies this template and returns the resulting tree.
|
|
149
|
+
*
|
|
150
|
+
* @param cursor The cursor pointing to the current location in the AST
|
|
151
|
+
* @param tree Input tree
|
|
152
|
+
* @param values values for parameters in template
|
|
153
|
+
* @returns A Promise resolving to the generated AST node
|
|
154
|
+
*/
|
|
155
|
+
apply(cursor: Cursor, tree: J, values?: Map<Capture | string, J> | Pick<Map<string, J>, 'get'>): Promise<J | undefined>;
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Tagged template function for creating templates that generate AST nodes.
|
|
159
|
+
*
|
|
160
|
+
* Templates support property access on captures from patterns, allowing you to
|
|
161
|
+
* extract and insert specific subtrees from matched AST nodes. Use dot notation
|
|
162
|
+
* to navigate properties (e.g., `method.name`) or array bracket notation to
|
|
163
|
+
* access array elements (e.g., `args.elements[0].element`).
|
|
164
|
+
*
|
|
165
|
+
* @param strings The string parts of the template
|
|
166
|
+
* @param parameters The parameters between the string parts (Capture, Tree, or primitives)
|
|
167
|
+
* @returns A Template object that can be applied to generate AST nodes
|
|
168
|
+
*
|
|
169
|
+
* @example
|
|
170
|
+
* // Simple template with literal
|
|
171
|
+
* const tmpl = template`console.log("hello")`;
|
|
172
|
+
* const result = await tmpl.apply(cursor, node);
|
|
173
|
+
*
|
|
174
|
+
* @example
|
|
175
|
+
* // Template with capture - matches captured value from pattern
|
|
176
|
+
* const expr = capture('expr');
|
|
177
|
+
* const pat = pattern`foo(${expr})`;
|
|
178
|
+
* const tmpl = template`bar(${expr})`;
|
|
179
|
+
*
|
|
180
|
+
* const match = await pat.match(node);
|
|
181
|
+
* if (match) {
|
|
182
|
+
* const result = await tmpl.apply(cursor, node, match);
|
|
183
|
+
* }
|
|
184
|
+
*
|
|
185
|
+
* @example
|
|
186
|
+
* // Property access on captures - extract subtrees
|
|
187
|
+
* const method = capture<J.MethodInvocation>('method');
|
|
188
|
+
* const pat = pattern`foo(${method})`;
|
|
189
|
+
* // Access the 'name' property of the captured method invocation
|
|
190
|
+
* const tmpl = template`bar(${method.name})`;
|
|
191
|
+
*
|
|
192
|
+
* @example
|
|
193
|
+
* // Deep property chains
|
|
194
|
+
* const method = capture<J.MethodInvocation>('method');
|
|
195
|
+
* template`console.log(${method.name.simpleName})`
|
|
196
|
+
*
|
|
197
|
+
* @example
|
|
198
|
+
* // Array element access
|
|
199
|
+
* const invocation = capture<J.MethodInvocation>('invocation');
|
|
200
|
+
* template`bar(${invocation.arguments.elements[0].element})`
|
|
201
|
+
*/
|
|
202
|
+
export declare function template(strings: TemplateStringsArray, ...parameters: TemplateParameter[]): Template;
|
|
203
|
+
export type { JavaCoordinates };
|
|
204
|
+
//# sourceMappingURL=template.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"template.d.ts","sourceRoot":"","sources":["../../../src/javascript/templating/template.ts"],"names":[],"mappings":"AAeA,OAAO,EAAC,MAAM,EAAE,IAAI,EAAC,MAAM,OAAO,CAAC;AACnC,OAAO,EAAC,CAAC,EAAC,MAAM,YAAY,CAAC;AAC7B,OAAO,EAAC,eAAe,EAAE,iBAAiB,EAAE,OAAO,EAAC,MAAM,SAAS,CAAC;AAIpE,OAAO,EAAiB,SAAS,EAAC,MAAM,UAAU,CAAC;AAEnD;;GAEG;AACH,KAAK,eAAe,GAAG;IACnB,IAAI,CAAC,EAAE,IAAI,CAAC;IACZ,GAAG,CAAC,EAAE,eAAe,CAAC,QAAQ,CAAC;IAC/B,IAAI,CAAC,EAAE,eAAe,CAAC,IAAI,CAAC;CAC/B,CAAC;AAEF,kBAAU,eAAe,CAAC;IAEtB,KAAY,QAAQ,GAAG,mBAAmB,GAAG,kBAAkB,GAAG,WAAW,CAAC;IAE9E,KAAY,IAAI;QACZ,MAAM,IAAA;QACN,KAAK,IAAA;QACL,OAAO,IAAA;KACV;CACJ;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,qBAAa,eAAe;IACxB,OAAO,CAAC,KAAK,CAAgB;IAC7B,OAAO,CAAC,MAAM,CAA2B;IAEzC;;;;;OAKG;IACH,IAAI,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI;IAcvB;;;;;OAKG;IACH,KAAK,CAAC,KAAK,EAAE,iBAAiB,GAAG,IAAI;IAWrC;;;;OAIG;IACH,KAAK,IAAI,QAAQ;CAiBpB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;AACH,qBAAa,QAAQ;IA2Bb,OAAO,CAAC,QAAQ,CAAC,aAAa;IAC9B,OAAO,CAAC,QAAQ,CAAC,UAAU;IA3B/B,OAAO,CAAC,OAAO,CAAuB;IAEtC;;;;;;;;;;;;OAYG;IACH,MAAM,CAAC,OAAO,IAAI,eAAe;IAIjC;;;;;OAKG;gBAEkB,aAAa,EAAE,oBAAoB,EACnC,UAAU,EAAE,SAAS,EAAE;IAI5C;;;;;;;;;;;;OAYG;IACH,SAAS,CAAC,OAAO,EAAE,eAAe,GAAG,QAAQ;IAK7C;;;;;;;OAOG;IACG,KAAK,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,EAAE,MAAM,CAAC,EAAE,GAAG,CAAC,OAAO,GAAG,MAAM,EAAE,CAAC,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,MAAM,EAAE,CAAC,CAAC,EAAE,KAAK,CAAC,GAAG,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC;CAuChI;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4CG;AACH,wBAAgB,QAAQ,CAAC,OAAO,EAAE,oBAAoB,EAAE,GAAG,UAAU,EAAE,iBAAiB,EAAE,GAAG,QAAQ,CAQpG;AAED,YAAY,EAAC,eAAe,EAAC,CAAC"}
|
|
@@ -0,0 +1,293 @@
|
|
|
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
|
+
* imports: ['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
|
+
return this;
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Applies this template and returns the resulting tree.
|
|
200
|
+
*
|
|
201
|
+
* @param cursor The cursor pointing to the current location in the AST
|
|
202
|
+
* @param tree Input tree
|
|
203
|
+
* @param values values for parameters in template
|
|
204
|
+
* @returns A Promise resolving to the generated AST node
|
|
205
|
+
*/
|
|
206
|
+
apply(cursor, tree, values) {
|
|
207
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
208
|
+
// Normalize the values map: convert any Capture keys to string keys
|
|
209
|
+
let normalizedValues;
|
|
210
|
+
let wrappersMap = new Map();
|
|
211
|
+
if (values instanceof Map) {
|
|
212
|
+
const normalized = new Map();
|
|
213
|
+
for (const [key, value] of values.entries()) {
|
|
214
|
+
const stringKey = typeof key === 'string'
|
|
215
|
+
? key
|
|
216
|
+
: (key[capture_1.CAPTURE_NAME_SYMBOL] || key.getName());
|
|
217
|
+
normalized.set(stringKey, value);
|
|
218
|
+
}
|
|
219
|
+
normalizedValues = normalized;
|
|
220
|
+
}
|
|
221
|
+
else if (values instanceof pattern_1.MatchResult) {
|
|
222
|
+
// MatchResult - extract both bindings and wrappersMap
|
|
223
|
+
normalizedValues = values;
|
|
224
|
+
wrappersMap = values[utils_1.WRAPPERS_MAP_SYMBOL]();
|
|
225
|
+
}
|
|
226
|
+
else {
|
|
227
|
+
// Other Pick<Map> implementation
|
|
228
|
+
normalizedValues = values;
|
|
229
|
+
}
|
|
230
|
+
// Prefer 'context' over deprecated 'imports'
|
|
231
|
+
const contextStatements = this.options.context || this.options.imports || [];
|
|
232
|
+
return engine_1.TemplateEngine.applyTemplate(this.templateParts, this.parameters, cursor, {
|
|
233
|
+
tree,
|
|
234
|
+
mode: JavaCoordinates.Mode.Replace
|
|
235
|
+
}, normalizedValues, wrappersMap, contextStatements, this.options.dependencies || {});
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
exports.Template = Template;
|
|
240
|
+
/**
|
|
241
|
+
* Tagged template function for creating templates that generate AST nodes.
|
|
242
|
+
*
|
|
243
|
+
* Templates support property access on captures from patterns, allowing you to
|
|
244
|
+
* extract and insert specific subtrees from matched AST nodes. Use dot notation
|
|
245
|
+
* to navigate properties (e.g., `method.name`) or array bracket notation to
|
|
246
|
+
* access array elements (e.g., `args.elements[0].element`).
|
|
247
|
+
*
|
|
248
|
+
* @param strings The string parts of the template
|
|
249
|
+
* @param parameters The parameters between the string parts (Capture, Tree, or primitives)
|
|
250
|
+
* @returns A Template object that can be applied to generate AST nodes
|
|
251
|
+
*
|
|
252
|
+
* @example
|
|
253
|
+
* // Simple template with literal
|
|
254
|
+
* const tmpl = template`console.log("hello")`;
|
|
255
|
+
* const result = await tmpl.apply(cursor, node);
|
|
256
|
+
*
|
|
257
|
+
* @example
|
|
258
|
+
* // Template with capture - matches captured value from pattern
|
|
259
|
+
* const expr = capture('expr');
|
|
260
|
+
* const pat = pattern`foo(${expr})`;
|
|
261
|
+
* const tmpl = template`bar(${expr})`;
|
|
262
|
+
*
|
|
263
|
+
* const match = await pat.match(node);
|
|
264
|
+
* if (match) {
|
|
265
|
+
* const result = await tmpl.apply(cursor, node, match);
|
|
266
|
+
* }
|
|
267
|
+
*
|
|
268
|
+
* @example
|
|
269
|
+
* // Property access on captures - extract subtrees
|
|
270
|
+
* const method = capture<J.MethodInvocation>('method');
|
|
271
|
+
* const pat = pattern`foo(${method})`;
|
|
272
|
+
* // Access the 'name' property of the captured method invocation
|
|
273
|
+
* const tmpl = template`bar(${method.name})`;
|
|
274
|
+
*
|
|
275
|
+
* @example
|
|
276
|
+
* // Deep property chains
|
|
277
|
+
* const method = capture<J.MethodInvocation>('method');
|
|
278
|
+
* template`console.log(${method.name.simpleName})`
|
|
279
|
+
*
|
|
280
|
+
* @example
|
|
281
|
+
* // Array element access
|
|
282
|
+
* const invocation = capture<J.MethodInvocation>('invocation');
|
|
283
|
+
* template`bar(${invocation.arguments.elements[0].element})`
|
|
284
|
+
*/
|
|
285
|
+
function template(strings, ...parameters) {
|
|
286
|
+
// Convert parameters to Parameter objects (no longer need to check for mutable tree property)
|
|
287
|
+
const processedParameters = parameters.map(param => {
|
|
288
|
+
// Just wrap each parameter value in a Parameter object
|
|
289
|
+
return { value: param };
|
|
290
|
+
});
|
|
291
|
+
return new Template(strings, processedParameters);
|
|
292
|
+
}
|
|
293
|
+
//# sourceMappingURL=template.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"template.js","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