@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,750 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2025 the original author or authors.
|
|
3
|
+
* <p>
|
|
4
|
+
* Licensed under the Moderne Source Available License (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
* <p>
|
|
8
|
+
* https://docs.moderne.io/licensing/moderne-source-available-license
|
|
9
|
+
* <p>
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
import {Cursor, isTree, produceAsync, Tree, updateIfChanged} from '../..';
|
|
17
|
+
import {emptySpace, J, Statement, Type} from '../../java';
|
|
18
|
+
import {Any, Capture, JavaScriptParser, JavaScriptVisitor, JS} from '..';
|
|
19
|
+
import {produce} from 'immer';
|
|
20
|
+
import {CaptureMarker, PlaceholderUtils, WRAPPER_FUNCTION_NAME} from './utils';
|
|
21
|
+
import {CAPTURE_NAME_SYMBOL, CAPTURE_TYPE_SYMBOL, CaptureImpl, CaptureValue, RAW_CODE_SYMBOL, RawCode} from './capture';
|
|
22
|
+
import {PlaceholderReplacementVisitor} from './placeholder-replacement';
|
|
23
|
+
import {JavaCoordinates} from './template';
|
|
24
|
+
import {maybeAutoFormat} from '../format';
|
|
25
|
+
import {isExpression, isStatement} from '../parser-utils';
|
|
26
|
+
import {randomId} from '../../uuid';
|
|
27
|
+
import ts from "typescript";
|
|
28
|
+
import {DependencyWorkspace} from "../dependency-workspace";
|
|
29
|
+
import {Parameter} from "./types";
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Simple LRU (Least Recently Used) cache implementation.
|
|
33
|
+
* Used for template/pattern compilation caching with bounded memory usage.
|
|
34
|
+
*/
|
|
35
|
+
class LRUCache<K, V> {
|
|
36
|
+
private cache = new Map<K, V>();
|
|
37
|
+
|
|
38
|
+
constructor(private maxSize: number) {}
|
|
39
|
+
|
|
40
|
+
get(key: K): V | undefined {
|
|
41
|
+
const value = this.cache.get(key);
|
|
42
|
+
if (value !== undefined) {
|
|
43
|
+
// Move to end (most recently used)
|
|
44
|
+
this.cache.delete(key);
|
|
45
|
+
this.cache.set(key, value);
|
|
46
|
+
}
|
|
47
|
+
return value;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
set(key: K, value: V): void {
|
|
51
|
+
// Remove if exists (to update position)
|
|
52
|
+
this.cache.delete(key);
|
|
53
|
+
|
|
54
|
+
// Add to end
|
|
55
|
+
this.cache.set(key, value);
|
|
56
|
+
|
|
57
|
+
// Evict oldest if over capacity
|
|
58
|
+
if (this.cache.size > this.maxSize) {
|
|
59
|
+
const iterator = this.cache.keys();
|
|
60
|
+
const firstEntry = iterator.next();
|
|
61
|
+
if (!firstEntry.done) {
|
|
62
|
+
this.cache.delete(firstEntry.value);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
clear(): void {
|
|
68
|
+
this.cache.clear();
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Module-level TypeScript sourceFileCache for template parsing.
|
|
74
|
+
*/
|
|
75
|
+
let templateSourceFileCache: Map<string, ts.SourceFile> | undefined;
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Configure the sourceFileCache used for template parsing.
|
|
79
|
+
*
|
|
80
|
+
* @param cache The sourceFileCache to use, or undefined to disable caching
|
|
81
|
+
*/
|
|
82
|
+
export function setTemplateSourceFileCache(cache?: Map<string, ts.SourceFile>): void {
|
|
83
|
+
templateSourceFileCache = cache;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Cache for compiled templates and patterns.
|
|
88
|
+
* Stores parsed ASTs to avoid expensive re-parsing and dependency resolution.
|
|
89
|
+
* Bounded to 100 entries using LRU eviction to prevent unbounded memory growth.
|
|
90
|
+
*/
|
|
91
|
+
class TemplateCache {
|
|
92
|
+
private cache = new LRUCache<string, JS.CompilationUnit>(100);
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Generates a cache key from template string, captures, and options.
|
|
96
|
+
*/
|
|
97
|
+
private generateKey(
|
|
98
|
+
templateString: string,
|
|
99
|
+
captures: (Capture | Any)[],
|
|
100
|
+
contextStatements: string[],
|
|
101
|
+
dependencies: Record<string, string>
|
|
102
|
+
): string {
|
|
103
|
+
// Use the actual template string (with placeholders) as the primary key
|
|
104
|
+
const templateKey = templateString;
|
|
105
|
+
|
|
106
|
+
// Capture names
|
|
107
|
+
const capturesKey = captures.map(c => c.getName()).join(',');
|
|
108
|
+
|
|
109
|
+
// Context statements
|
|
110
|
+
const contextKey = contextStatements.join(';');
|
|
111
|
+
|
|
112
|
+
// Dependencies
|
|
113
|
+
const depsKey = JSON.stringify(dependencies || {});
|
|
114
|
+
|
|
115
|
+
return `${templateKey}::${capturesKey}::${contextKey}::${depsKey}`;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Gets a cached compilation unit or creates and caches a new one.
|
|
120
|
+
*/
|
|
121
|
+
async getOrParse(
|
|
122
|
+
templateString: string,
|
|
123
|
+
captures: (Capture | Any)[],
|
|
124
|
+
contextStatements: string[],
|
|
125
|
+
dependencies: Record<string, string>
|
|
126
|
+
): Promise<JS.CompilationUnit> {
|
|
127
|
+
const key = this.generateKey(templateString, captures, contextStatements, dependencies);
|
|
128
|
+
|
|
129
|
+
let cu = this.cache.get(key);
|
|
130
|
+
if (cu) {
|
|
131
|
+
return cu;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// Create workspace if dependencies are provided
|
|
135
|
+
// DependencyWorkspace has its own cache, so multiple templates with
|
|
136
|
+
// the same dependencies will automatically share the same workspace
|
|
137
|
+
let workspaceDir: string | undefined;
|
|
138
|
+
if (dependencies && Object.keys(dependencies).length > 0) {
|
|
139
|
+
workspaceDir = await DependencyWorkspace.getOrCreateWorkspace(dependencies);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// Prepend context statements for type attribution context
|
|
143
|
+
const fullTemplateString = contextStatements.length > 0
|
|
144
|
+
? contextStatements.join('\n') + '\n' + templateString
|
|
145
|
+
: templateString;
|
|
146
|
+
|
|
147
|
+
// Parse and cache (workspace only needed during parsing)
|
|
148
|
+
// Use templateSourceFileCache if configured for ~3.2x speedup on dependency file parsing
|
|
149
|
+
const parser = new JavaScriptParser({
|
|
150
|
+
relativeTo: workspaceDir,
|
|
151
|
+
sourceFileCache: templateSourceFileCache
|
|
152
|
+
});
|
|
153
|
+
const parseGenerator = parser.parse({text: fullTemplateString, sourcePath: 'template.ts'});
|
|
154
|
+
cu = (await parseGenerator.next()).value as JS.CompilationUnit;
|
|
155
|
+
|
|
156
|
+
this.cache.set(key, cu);
|
|
157
|
+
return cu;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Clears the cache.
|
|
162
|
+
*/
|
|
163
|
+
clear(): void {
|
|
164
|
+
this.cache.clear();
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Cache for compiled templates and patterns.
|
|
170
|
+
* Private to the engine module - encapsulates caching implementation.
|
|
171
|
+
*/
|
|
172
|
+
const templateCache = new TemplateCache();
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Clears the template cache. Only exported for testing and benchmarking purposes.
|
|
176
|
+
* Normal application code should not need to call this.
|
|
177
|
+
*/
|
|
178
|
+
export function clearTemplateCache(): void {
|
|
179
|
+
templateCache.clear();
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Internal template engine - handles the core templating logic.
|
|
184
|
+
* Not exported from index, so only visible within the templating module.
|
|
185
|
+
*/
|
|
186
|
+
export class TemplateEngine {
|
|
187
|
+
/**
|
|
188
|
+
* Gets the parsed and extracted template tree (before value substitution).
|
|
189
|
+
* This is the cacheable part of template processing.
|
|
190
|
+
*
|
|
191
|
+
* @param templateParts The string parts of the template
|
|
192
|
+
* @param parameters The parameters between the string parts
|
|
193
|
+
* @param contextStatements Context declarations (imports, types, etc.) to prepend for type attribution
|
|
194
|
+
* @param dependencies NPM dependencies for type attribution
|
|
195
|
+
* @returns A Promise resolving to the extracted template AST
|
|
196
|
+
*/
|
|
197
|
+
static async getTemplateTree(
|
|
198
|
+
templateParts: TemplateStringsArray,
|
|
199
|
+
parameters: Parameter[],
|
|
200
|
+
contextStatements: string[] = [],
|
|
201
|
+
dependencies: Record<string, string> = {}
|
|
202
|
+
): Promise<J> {
|
|
203
|
+
// Generate type preamble for captures/parameters with types
|
|
204
|
+
const preamble = TemplateEngine.generateTypePreamble(parameters);
|
|
205
|
+
|
|
206
|
+
// Build the template string with parameter placeholders
|
|
207
|
+
const templateString = TemplateEngine.buildTemplateString(templateParts, parameters);
|
|
208
|
+
|
|
209
|
+
// Add preamble to context statements (so they're skipped during extraction)
|
|
210
|
+
const contextWithPreamble = preamble.length > 0
|
|
211
|
+
? [...contextStatements, ...preamble]
|
|
212
|
+
: contextStatements;
|
|
213
|
+
|
|
214
|
+
// Use cache to get or parse the compilation unit
|
|
215
|
+
const cu = await templateCache.getOrParse(
|
|
216
|
+
templateString,
|
|
217
|
+
[],
|
|
218
|
+
contextWithPreamble,
|
|
219
|
+
dependencies
|
|
220
|
+
);
|
|
221
|
+
|
|
222
|
+
// Check if there are any statements
|
|
223
|
+
if (!cu.statements || cu.statements.length === 0) {
|
|
224
|
+
throw new Error(`Failed to parse template code (no statements):\n${templateString}`);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
// The template code is always the last statement (after context + preamble)
|
|
228
|
+
const lastStatement = cu.statements[cu.statements.length - 1].element;
|
|
229
|
+
|
|
230
|
+
// Extract from wrapper using shared utility
|
|
231
|
+
const extracted = PlaceholderUtils.extractFromWrapper(lastStatement, 'Template');
|
|
232
|
+
|
|
233
|
+
return produce(extracted, _ => {});
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Applies a template from a pre-parsed AST and returns the resulting AST.
|
|
238
|
+
* This method is used by Template.apply() after getting the cached template tree.
|
|
239
|
+
*
|
|
240
|
+
* @param ast The pre-parsed template AST
|
|
241
|
+
* @param parameters The parameters between the string parts
|
|
242
|
+
* @param cursor The cursor pointing to the current location in the AST
|
|
243
|
+
* @param coordinates The coordinates specifying where and how to insert the generated AST
|
|
244
|
+
* @param values Map of capture names to values to replace the parameters with
|
|
245
|
+
* @param wrappersMap Map of capture names to J.RightPadded wrappers (for preserving markers)
|
|
246
|
+
* @returns A Promise resolving to the generated AST node
|
|
247
|
+
*/
|
|
248
|
+
static async applyTemplateFromAst(
|
|
249
|
+
ast: JS.CompilationUnit,
|
|
250
|
+
parameters: Parameter[],
|
|
251
|
+
cursor: Cursor,
|
|
252
|
+
coordinates: JavaCoordinates,
|
|
253
|
+
values: Pick<Map<string, J>, 'get'> = new Map(),
|
|
254
|
+
wrappersMap: Pick<Map<string, J.RightPadded<J> | J.RightPadded<J>[]>, 'get'> = new Map()
|
|
255
|
+
): Promise<J | undefined> {
|
|
256
|
+
// Create substitutions map for placeholders
|
|
257
|
+
const substitutions = new Map<string, Parameter>();
|
|
258
|
+
for (let i = 0; i < parameters.length; i++) {
|
|
259
|
+
const placeholder = `${PlaceholderUtils.PLACEHOLDER_PREFIX}${i}__`;
|
|
260
|
+
substitutions.set(placeholder, parameters[i]);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
// Unsubstitute placeholders with actual parameter values and match results
|
|
264
|
+
const visitor = new PlaceholderReplacementVisitor(substitutions, values, wrappersMap);
|
|
265
|
+
const unsubstitutedAst = (await visitor.visit(ast, null))!;
|
|
266
|
+
|
|
267
|
+
// Apply the template to the current AST
|
|
268
|
+
return new TemplateApplier(cursor, coordinates, unsubstitutedAst).apply();
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* Generates type preamble declarations for captures/parameters with type annotations.
|
|
273
|
+
*
|
|
274
|
+
* @param parameters The parameters
|
|
275
|
+
* @returns Array of preamble statements
|
|
276
|
+
*/
|
|
277
|
+
private static generateTypePreamble(parameters: Parameter[]): string[] {
|
|
278
|
+
const preamble: string[] = [];
|
|
279
|
+
|
|
280
|
+
for (let i = 0; i < parameters.length; i++) {
|
|
281
|
+
const param = parameters[i].value;
|
|
282
|
+
const placeholder = `${PlaceholderUtils.PLACEHOLDER_PREFIX}${i}__`;
|
|
283
|
+
|
|
284
|
+
// Check for Capture (could be a Proxy, so check for symbol property)
|
|
285
|
+
const isCapture = param instanceof CaptureImpl ||
|
|
286
|
+
(param && typeof param === 'object' && param[CAPTURE_NAME_SYMBOL]);
|
|
287
|
+
const isCaptureValue = param instanceof CaptureValue;
|
|
288
|
+
const isTreeArray = Array.isArray(param) && param.length > 0 && isTree(param[0]);
|
|
289
|
+
|
|
290
|
+
if (isCapture) {
|
|
291
|
+
const captureType = param[CAPTURE_TYPE_SYMBOL];
|
|
292
|
+
if (captureType) {
|
|
293
|
+
const typeString = typeof captureType === 'string'
|
|
294
|
+
? captureType
|
|
295
|
+
: this.typeToString(captureType);
|
|
296
|
+
// Only add preamble if we have a concrete type (not 'any')
|
|
297
|
+
if (typeString !== 'any') {
|
|
298
|
+
preamble.push(`let ${placeholder}: ${typeString};`);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
} else if (isCaptureValue) {
|
|
302
|
+
// For CaptureValue, check if the root capture has a type
|
|
303
|
+
const rootCapture = param.rootCapture;
|
|
304
|
+
if (rootCapture) {
|
|
305
|
+
const captureType = (rootCapture as any)[CAPTURE_TYPE_SYMBOL];
|
|
306
|
+
if (captureType) {
|
|
307
|
+
const typeString = typeof captureType === 'string'
|
|
308
|
+
? captureType
|
|
309
|
+
: this.typeToString(captureType);
|
|
310
|
+
// Only add preamble if we have a concrete type (not 'any')
|
|
311
|
+
if (typeString !== 'any') {
|
|
312
|
+
preamble.push(`let ${placeholder}: ${typeString};`);
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
} else if (isTree(param) && !isTreeArray) {
|
|
317
|
+
// For J elements, derive type from the element's type property if it exists
|
|
318
|
+
const jElement = param as J;
|
|
319
|
+
if ((jElement as any).type) {
|
|
320
|
+
const typeString = this.typeToString((jElement as any).type);
|
|
321
|
+
// Only add preamble if we have a concrete type (not 'any')
|
|
322
|
+
if (typeString !== 'any') {
|
|
323
|
+
preamble.push(`let ${placeholder}: ${typeString};`);
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
return preamble;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
/**
|
|
333
|
+
* Builds a template string with parameter placeholders.
|
|
334
|
+
* RawCode parameters are spliced directly into the template at construction time.
|
|
335
|
+
* Other parameters use placeholders that are replaced during application.
|
|
336
|
+
*
|
|
337
|
+
* @param templateParts The string parts of the template
|
|
338
|
+
* @param parameters The parameters between the string parts
|
|
339
|
+
* @returns The template string
|
|
340
|
+
*/
|
|
341
|
+
private static buildTemplateString(
|
|
342
|
+
templateParts: TemplateStringsArray,
|
|
343
|
+
parameters: Parameter[]
|
|
344
|
+
): string {
|
|
345
|
+
let result = '';
|
|
346
|
+
for (let i = 0; i < templateParts.length; i++) {
|
|
347
|
+
result += templateParts[i];
|
|
348
|
+
if (i < parameters.length) {
|
|
349
|
+
const param = parameters[i].value;
|
|
350
|
+
|
|
351
|
+
// Check if this is a RawCode instance - splice directly
|
|
352
|
+
if (param instanceof RawCode || (param && typeof param === 'object' && param[RAW_CODE_SYMBOL])) {
|
|
353
|
+
result += (param as RawCode).code;
|
|
354
|
+
} else {
|
|
355
|
+
// All other parameters use placeholders
|
|
356
|
+
// This ensures templates with the same structure always produce the same AST
|
|
357
|
+
const placeholder = `${PlaceholderUtils.PLACEHOLDER_PREFIX}${i}__`;
|
|
358
|
+
result += placeholder;
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
// Always wrap in function body - let the parser decide what it is,
|
|
364
|
+
// then we'll extract intelligently based on what was parsed
|
|
365
|
+
return `function ${WRAPPER_FUNCTION_NAME}() { ${result} }`;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
/**
|
|
369
|
+
* Converts a Type instance to a TypeScript type string.
|
|
370
|
+
*
|
|
371
|
+
* @param type The Type instance
|
|
372
|
+
* @returns A TypeScript type string
|
|
373
|
+
*/
|
|
374
|
+
private static typeToString(type: Type): string {
|
|
375
|
+
// Handle Type.Class and Type.ShallowClass - return their fully qualified names
|
|
376
|
+
if (type.kind === Type.Kind.Class || type.kind === Type.Kind.ShallowClass) {
|
|
377
|
+
const classType = type as Type.Class;
|
|
378
|
+
return classType.fullyQualifiedName;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
// Handle Type.Primitive - map to TypeScript primitive types
|
|
382
|
+
if (type.kind === Type.Kind.Primitive) {
|
|
383
|
+
const primitiveType = type as Type.Primitive;
|
|
384
|
+
switch (primitiveType.keyword) {
|
|
385
|
+
case 'String':
|
|
386
|
+
return 'string';
|
|
387
|
+
case 'boolean':
|
|
388
|
+
return 'boolean';
|
|
389
|
+
case 'double':
|
|
390
|
+
case 'float':
|
|
391
|
+
case 'int':
|
|
392
|
+
case 'long':
|
|
393
|
+
case 'short':
|
|
394
|
+
case 'byte':
|
|
395
|
+
return 'number';
|
|
396
|
+
case 'void':
|
|
397
|
+
return 'void';
|
|
398
|
+
default:
|
|
399
|
+
return 'any';
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
// Handle Type.Array - render component type plus []
|
|
404
|
+
if (type.kind === Type.Kind.Array) {
|
|
405
|
+
const arrayType = type as Type.Array;
|
|
406
|
+
const componentTypeString = this.typeToString(arrayType.elemType);
|
|
407
|
+
return `${componentTypeString}[]`;
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
// For other types, return 'any' as a fallback
|
|
411
|
+
// TODO: Implement proper Type to string conversion for other Type.Kind values
|
|
412
|
+
return 'any';
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
/**
|
|
416
|
+
* Gets the parsed and extracted pattern tree with capture markers attached.
|
|
417
|
+
* This is the entry point for pattern processing, providing pattern-specific
|
|
418
|
+
* functionality on top of the shared template tree generation.
|
|
419
|
+
*
|
|
420
|
+
* @param templateParts The string parts of the template
|
|
421
|
+
* @param captures The captures between the string parts (can include RawCode)
|
|
422
|
+
* @param contextStatements Context declarations (imports, types, etc.) to prepend for type attribution
|
|
423
|
+
* @param dependencies NPM dependencies for type attribution
|
|
424
|
+
* @returns A Promise resolving to the extracted pattern AST with capture markers
|
|
425
|
+
*/
|
|
426
|
+
static async getPatternTree(
|
|
427
|
+
templateParts: TemplateStringsArray,
|
|
428
|
+
captures: (Capture | Any | RawCode)[],
|
|
429
|
+
contextStatements: string[] = [],
|
|
430
|
+
dependencies: Record<string, string> = {}
|
|
431
|
+
): Promise<J> {
|
|
432
|
+
// Generate type preamble for captures with types (skip RawCode)
|
|
433
|
+
const preamble: string[] = [];
|
|
434
|
+
for (const capture of captures) {
|
|
435
|
+
// Skip raw code - it's not a capture
|
|
436
|
+
if (capture instanceof RawCode || (capture && typeof capture === 'object' && (capture as any)[RAW_CODE_SYMBOL])) {
|
|
437
|
+
continue;
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
const captureName = (capture as any)[CAPTURE_NAME_SYMBOL] || capture.getName();
|
|
441
|
+
const captureType = (capture as any)[CAPTURE_TYPE_SYMBOL];
|
|
442
|
+
if (captureType) {
|
|
443
|
+
// Convert Type to string if needed
|
|
444
|
+
const typeString = typeof captureType === 'string'
|
|
445
|
+
? captureType
|
|
446
|
+
: this.typeToString(captureType);
|
|
447
|
+
// Only add preamble if we have a concrete type (not 'any')
|
|
448
|
+
if (typeString !== 'any') {
|
|
449
|
+
const placeholder = PlaceholderUtils.createCapture(captureName, undefined);
|
|
450
|
+
preamble.push(`let ${placeholder}: ${typeString};`);
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
// Don't add preamble declarations without types - they don't provide type attribution
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
// Build the template string with placeholders for captures and raw code
|
|
457
|
+
let result = '';
|
|
458
|
+
for (let i = 0; i < templateParts.length; i++) {
|
|
459
|
+
result += templateParts[i];
|
|
460
|
+
if (i < captures.length) {
|
|
461
|
+
const capture = captures[i];
|
|
462
|
+
|
|
463
|
+
// Check if this is a RawCode instance - splice directly
|
|
464
|
+
if (capture instanceof RawCode || (capture && typeof capture === 'object' && (capture as any)[RAW_CODE_SYMBOL])) {
|
|
465
|
+
result += (capture as RawCode).code;
|
|
466
|
+
} else {
|
|
467
|
+
// Use symbol to access capture name without triggering Proxy
|
|
468
|
+
const captureName = (capture as any)[CAPTURE_NAME_SYMBOL] || capture.getName();
|
|
469
|
+
result += PlaceholderUtils.createCapture(captureName, undefined);
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
// Always wrap in function body - let the parser decide what it is,
|
|
475
|
+
// then we'll extract intelligently based on what was parsed
|
|
476
|
+
const templateString = `function ${WRAPPER_FUNCTION_NAME}() { ${result} }`;
|
|
477
|
+
|
|
478
|
+
// Add preamble to context statements (so they're skipped during extraction)
|
|
479
|
+
const contextWithPreamble = preamble.length > 0
|
|
480
|
+
? [...contextStatements, ...preamble]
|
|
481
|
+
: contextStatements;
|
|
482
|
+
|
|
483
|
+
// Filter out RawCode from captures for cache and marker attachment
|
|
484
|
+
const actualCaptures = captures.filter(c =>
|
|
485
|
+
!(c instanceof RawCode || (c && typeof c === 'object' && (c as any)[RAW_CODE_SYMBOL]))
|
|
486
|
+
) as (Capture | Any)[];
|
|
487
|
+
|
|
488
|
+
// Use cache to get or parse the compilation unit
|
|
489
|
+
const cu = await templateCache.getOrParse(
|
|
490
|
+
templateString,
|
|
491
|
+
actualCaptures,
|
|
492
|
+
contextWithPreamble,
|
|
493
|
+
dependencies
|
|
494
|
+
);
|
|
495
|
+
|
|
496
|
+
// Check if there are any statements
|
|
497
|
+
if (!cu.statements || cu.statements.length === 0) {
|
|
498
|
+
throw new Error(`Failed to parse pattern code (no statements):\n${templateString}`);
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
// The pattern code is always the last statement (after context + preamble)
|
|
502
|
+
const lastStatement = cu.statements[cu.statements.length - 1].element;
|
|
503
|
+
|
|
504
|
+
// Extract from wrapper using shared utility
|
|
505
|
+
const extracted = PlaceholderUtils.extractFromWrapper(lastStatement, 'Pattern');
|
|
506
|
+
|
|
507
|
+
// Attach CaptureMarkers to capture identifiers (only for actual captures, not raw code)
|
|
508
|
+
const visitor = new MarkerAttachmentVisitor(actualCaptures);
|
|
509
|
+
return (await visitor.visit(extracted, undefined))!;
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
/**
|
|
514
|
+
* Visitor that attaches CaptureMarkers to capture identifiers in pattern ASTs.
|
|
515
|
+
* This allows efficient capture detection without string parsing during matching.
|
|
516
|
+
* Used by TemplateEngine.getPatternTree() for pattern-specific processing.
|
|
517
|
+
*/
|
|
518
|
+
class MarkerAttachmentVisitor extends JavaScriptVisitor<undefined> {
|
|
519
|
+
constructor(private readonly captures: (Capture | Any)[]) {
|
|
520
|
+
super();
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
/**
|
|
524
|
+
* Attaches CaptureMarker to capture identifiers.
|
|
525
|
+
*/
|
|
526
|
+
protected override async visitIdentifier(ident: J.Identifier, p: undefined): Promise<J | undefined> {
|
|
527
|
+
// First call parent to handle standard visitation
|
|
528
|
+
const visited = await super.visitIdentifier(ident, p);
|
|
529
|
+
if (!visited || visited.kind !== J.Kind.Identifier) {
|
|
530
|
+
return visited;
|
|
531
|
+
}
|
|
532
|
+
ident = visited as J.Identifier;
|
|
533
|
+
|
|
534
|
+
// Check if this is a capture placeholder
|
|
535
|
+
if (ident.simpleName?.startsWith(PlaceholderUtils.CAPTURE_PREFIX)) {
|
|
536
|
+
const captureInfo = PlaceholderUtils.parseCapture(ident.simpleName);
|
|
537
|
+
if (captureInfo) {
|
|
538
|
+
// Find the original capture object to get variadic options and constraint
|
|
539
|
+
const captureObj = this.captures.find(c => c.getName() === captureInfo.name);
|
|
540
|
+
const variadicOptions = captureObj?.getVariadicOptions();
|
|
541
|
+
const constraint = captureObj?.getConstraint?.();
|
|
542
|
+
|
|
543
|
+
// Add CaptureMarker to the Identifier with constraint
|
|
544
|
+
const marker = new CaptureMarker(captureInfo.name, variadicOptions, constraint);
|
|
545
|
+
return updateIfChanged(ident, {
|
|
546
|
+
markers: {
|
|
547
|
+
...ident.markers,
|
|
548
|
+
markers: [...ident.markers.markers, marker]
|
|
549
|
+
}
|
|
550
|
+
});
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
return ident;
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
/**
|
|
558
|
+
* Propagates markers from element to RightPadded wrapper.
|
|
559
|
+
*/
|
|
560
|
+
public override async visitRightPadded<T extends J | boolean>(right: J.RightPadded<T>, p: undefined): Promise<J.RightPadded<T> | undefined> {
|
|
561
|
+
if (!isTree(right.element)) {
|
|
562
|
+
return right;
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
const visitedElement = await this.visit(right.element as J, p);
|
|
566
|
+
if (visitedElement && visitedElement !== right.element as Tree) {
|
|
567
|
+
const result = await produceAsync<J.RightPadded<T>>(right, async (draft: any) => {
|
|
568
|
+
// Visit element first
|
|
569
|
+
if (right.element && (right.element as any).kind) {
|
|
570
|
+
// Check if element has a CaptureMarker
|
|
571
|
+
const elementMarker = PlaceholderUtils.getCaptureMarker(visitedElement);
|
|
572
|
+
if (elementMarker) {
|
|
573
|
+
draft.markers.markers.push(elementMarker);
|
|
574
|
+
} else {
|
|
575
|
+
draft.element = visitedElement;
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
});
|
|
579
|
+
return result!;
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
return right;
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
/**
|
|
586
|
+
* Propagates markers from expression to ExpressionStatement.
|
|
587
|
+
*/
|
|
588
|
+
protected override async visitExpressionStatement(expressionStatement: JS.ExpressionStatement, p: undefined): Promise<J | undefined> {
|
|
589
|
+
// Visit the expression
|
|
590
|
+
const visitedExpression = await this.visit(expressionStatement.expression, p);
|
|
591
|
+
|
|
592
|
+
// Check if expression has a CaptureMarker
|
|
593
|
+
const expressionMarker = PlaceholderUtils.getCaptureMarker(visitedExpression as any);
|
|
594
|
+
if (expressionMarker) {
|
|
595
|
+
return updateIfChanged(expressionStatement, {
|
|
596
|
+
markers: {
|
|
597
|
+
...expressionStatement.markers,
|
|
598
|
+
markers: [...expressionStatement.markers.markers, expressionMarker]
|
|
599
|
+
},
|
|
600
|
+
});
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
// No marker to move, just update with visited expression
|
|
604
|
+
return updateIfChanged(expressionStatement, {
|
|
605
|
+
expression: visitedExpression
|
|
606
|
+
});
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
/**
|
|
610
|
+
* Propagates markers from name identifier to BindingElement.
|
|
611
|
+
* This handles destructuring patterns like {${props}} where the capture marker
|
|
612
|
+
* is on the identifier but needs to be on the BindingElement for container matching.
|
|
613
|
+
*/
|
|
614
|
+
protected override async visitBindingElement(bindingElement: JS.BindingElement, p: undefined): Promise<J | undefined> {
|
|
615
|
+
// Visit the name
|
|
616
|
+
const visitedName = await this.visit(bindingElement.name, p);
|
|
617
|
+
|
|
618
|
+
// Check if name has a CaptureMarker
|
|
619
|
+
const nameMarker = PlaceholderUtils.getCaptureMarker(visitedName as any);
|
|
620
|
+
if (nameMarker) {
|
|
621
|
+
return updateIfChanged(bindingElement, {
|
|
622
|
+
name: visitedName,
|
|
623
|
+
markers: {
|
|
624
|
+
...bindingElement.markers,
|
|
625
|
+
markers: [...bindingElement.markers.markers, nameMarker]
|
|
626
|
+
},
|
|
627
|
+
});
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
// No marker to move, just update with visited name
|
|
631
|
+
return updateIfChanged(bindingElement, {
|
|
632
|
+
name: visitedName
|
|
633
|
+
});
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
/**
|
|
638
|
+
* Helper class for applying a template to an AST.
|
|
639
|
+
*/
|
|
640
|
+
export class TemplateApplier {
|
|
641
|
+
constructor(
|
|
642
|
+
private readonly cursor: Cursor,
|
|
643
|
+
private readonly coordinates: JavaCoordinates,
|
|
644
|
+
private readonly ast: J
|
|
645
|
+
) {
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
/**
|
|
649
|
+
* Applies the template to the current AST.
|
|
650
|
+
*
|
|
651
|
+
* @returns A Promise resolving to the modified AST
|
|
652
|
+
*/
|
|
653
|
+
async apply(): Promise<J | undefined> {
|
|
654
|
+
const {loc} = this.coordinates;
|
|
655
|
+
|
|
656
|
+
// Apply the template based on the location and mode
|
|
657
|
+
switch (loc || 'EXPRESSION_PREFIX') {
|
|
658
|
+
case 'EXPRESSION_PREFIX':
|
|
659
|
+
case 'STATEMENT_PREFIX':
|
|
660
|
+
case 'BLOCK_END':
|
|
661
|
+
return this.applyInternal();
|
|
662
|
+
default:
|
|
663
|
+
throw new Error(`Unsupported location: ${loc}`);
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
/**
|
|
668
|
+
* Applies the template to an expression.
|
|
669
|
+
*
|
|
670
|
+
* @returns A Promise resolving to the modified AST
|
|
671
|
+
*/
|
|
672
|
+
private async applyInternal(): Promise<J | undefined> {
|
|
673
|
+
const {tree} = this.coordinates;
|
|
674
|
+
|
|
675
|
+
if (!tree) {
|
|
676
|
+
return this.ast;
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
const originalTree = tree as J;
|
|
680
|
+
const resultToUse = this.wrapTree(originalTree, this.ast);
|
|
681
|
+
return this.format(resultToUse, originalTree);
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
private async format(resultToUse: J, originalTree: J) {
|
|
685
|
+
// Create a copy of the AST with the prefix from the target
|
|
686
|
+
const result = {
|
|
687
|
+
...resultToUse,
|
|
688
|
+
// We temporarily set the ID so that the formatter can identify the tree
|
|
689
|
+
id: originalTree.id,
|
|
690
|
+
prefix: originalTree.prefix
|
|
691
|
+
};
|
|
692
|
+
|
|
693
|
+
// Apply auto-formatting to the result
|
|
694
|
+
const formatted =
|
|
695
|
+
await maybeAutoFormat(originalTree, result, null, undefined, this.cursor?.parent);
|
|
696
|
+
|
|
697
|
+
// Restore the original ID
|
|
698
|
+
return {...formatted, id: resultToUse.id};
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
private wrapTree(originalTree: J, resultToUse: J) {
|
|
702
|
+
const parentTree = this.cursor?.parentTree()?.value;
|
|
703
|
+
|
|
704
|
+
// Only apply wrapping logic if we have parent context
|
|
705
|
+
if (parentTree) {
|
|
706
|
+
// FIXME: This is a heuristic to determine if the parent expects a statement child
|
|
707
|
+
const parentExpectsStatement = parentTree.kind === J.Kind.Block ||
|
|
708
|
+
parentTree.kind === J.Kind.Case ||
|
|
709
|
+
parentTree.kind === J.Kind.DoWhileLoop ||
|
|
710
|
+
parentTree.kind === J.Kind.ForEachLoop ||
|
|
711
|
+
parentTree.kind === J.Kind.ForLoop ||
|
|
712
|
+
parentTree.kind === J.Kind.If ||
|
|
713
|
+
parentTree.kind === J.Kind.IfElse ||
|
|
714
|
+
parentTree.kind === J.Kind.WhileLoop ||
|
|
715
|
+
parentTree.kind === JS.Kind.CompilationUnit ||
|
|
716
|
+
parentTree.kind === JS.Kind.ForInLoop;
|
|
717
|
+
const originalIsStatement = isStatement(originalTree);
|
|
718
|
+
|
|
719
|
+
const resultIsStatement = isStatement(resultToUse);
|
|
720
|
+
const resultIsExpression = isExpression(resultToUse);
|
|
721
|
+
|
|
722
|
+
// Determine context and wrap if needed
|
|
723
|
+
if (parentExpectsStatement && originalIsStatement) {
|
|
724
|
+
// Statement context: wrap in ExpressionStatement if result is not a statement
|
|
725
|
+
if (!resultIsStatement && resultIsExpression) {
|
|
726
|
+
resultToUse = {
|
|
727
|
+
kind: JS.Kind.ExpressionStatement,
|
|
728
|
+
id: randomId(),
|
|
729
|
+
prefix: resultToUse.prefix,
|
|
730
|
+
markers: resultToUse.markers,
|
|
731
|
+
expression: { ...resultToUse, prefix: emptySpace }
|
|
732
|
+
} as JS.ExpressionStatement;
|
|
733
|
+
}
|
|
734
|
+
} else if (!parentExpectsStatement) {
|
|
735
|
+
// Expression context: wrap in StatementExpression if result is statement-only
|
|
736
|
+
if (resultIsStatement && !resultIsExpression) {
|
|
737
|
+
const stmt = resultToUse as Statement;
|
|
738
|
+
resultToUse = {
|
|
739
|
+
kind: JS.Kind.StatementExpression,
|
|
740
|
+
id: randomId(),
|
|
741
|
+
prefix: stmt.prefix,
|
|
742
|
+
markers: stmt.markers,
|
|
743
|
+
statement: { ...stmt, prefix: emptySpace }
|
|
744
|
+
} as JS.StatementExpression;
|
|
745
|
+
}
|
|
746
|
+
}
|
|
747
|
+
}
|
|
748
|
+
return resultToUse;
|
|
749
|
+
}
|
|
750
|
+
}
|