@openrewrite/rewrite 8.72.2 → 8.72.4
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/execution.js +3 -16
- package/dist/execution.js.map +1 -1
- package/dist/index.js +34 -49
- package/dist/index.js.map +1 -1
- package/dist/java/markers.js +12 -29
- package/dist/java/markers.js.map +1 -1
- package/dist/java/print.js +2 -13
- package/dist/java/print.js.map +1 -1
- package/dist/java/rpc.js +1344 -1725
- package/dist/java/rpc.js.map +1 -1
- package/dist/java/type-visitor.js +143 -184
- package/dist/java/type-visitor.js.map +1 -1
- package/dist/java/visitor.js +1072 -1249
- package/dist/java/visitor.js.map +1 -1
- package/dist/javascript/add-import.js +495 -534
- package/dist/javascript/add-import.js.map +1 -1
- package/dist/javascript/autodetect.js +116 -159
- package/dist/javascript/autodetect.js.map +1 -1
- package/dist/javascript/cleanup/add-parse-int-radix.js +41 -57
- package/dist/javascript/cleanup/add-parse-int-radix.js.map +1 -1
- package/dist/javascript/cleanup/prefer-optional-chain.js +89 -105
- package/dist/javascript/cleanup/prefer-optional-chain.js.map +1 -1
- package/dist/javascript/cleanup/use-object-property-shorthand.js +74 -95
- package/dist/javascript/cleanup/use-object-property-shorthand.js.map +1 -1
- package/dist/javascript/comparator.js +815 -1167
- package/dist/javascript/comparator.js.map +1 -1
- package/dist/javascript/dependency-workspace.js +206 -219
- package/dist/javascript/dependency-workspace.js.map +1 -1
- package/dist/javascript/format/format.js +682 -908
- package/dist/javascript/format/format.js.map +1 -1
- package/dist/javascript/format/minimum-viable-spacing-visitor.js +152 -231
- package/dist/javascript/format/minimum-viable-spacing-visitor.js.map +1 -1
- package/dist/javascript/format/normalize-whitespace-visitor.js +12 -31
- package/dist/javascript/format/normalize-whitespace-visitor.js.map +1 -1
- package/dist/javascript/format/prettier-config-loader.js +134 -153
- package/dist/javascript/format/prettier-config-loader.js.map +1 -1
- package/dist/javascript/format/prettier-format.js +112 -129
- package/dist/javascript/format/prettier-format.js.map +1 -1
- package/dist/javascript/format/tabs-and-indents-visitor.js +112 -136
- package/dist/javascript/format/tabs-and-indents-visitor.js.map +1 -1
- package/dist/javascript/markers.js +59 -92
- package/dist/javascript/markers.js.map +1 -1
- package/dist/javascript/migrate/es6/modernize-octal-escape-sequences.js +39 -52
- package/dist/javascript/migrate/es6/modernize-octal-escape-sequences.js.map +1 -1
- package/dist/javascript/migrate/es6/modernize-octal-literals.js +25 -38
- package/dist/javascript/migrate/es6/modernize-octal-literals.js.map +1 -1
- package/dist/javascript/migrate/es6/remove-duplicate-object-keys.js +66 -82
- package/dist/javascript/migrate/es6/remove-duplicate-object-keys.js.map +1 -1
- package/dist/javascript/migrate/typescript/export-assignment-to-export-default.js +10 -23
- package/dist/javascript/migrate/typescript/export-assignment-to-export-default.js.map +1 -1
- package/dist/javascript/node-resolution-result.js +137 -166
- package/dist/javascript/node-resolution-result.js.map +1 -1
- package/dist/javascript/package-json-parser.js +312 -343
- package/dist/javascript/package-json-parser.js.map +1 -1
- package/dist/javascript/package-manager.js +145 -170
- package/dist/javascript/package-manager.js.map +1 -1
- package/dist/javascript/parser.d.ts.map +1 -1
- package/dist/javascript/parser.js +94 -68
- package/dist/javascript/parser.js.map +1 -1
- package/dist/javascript/print.js +1572 -1835
- package/dist/javascript/print.js.map +1 -1
- package/dist/javascript/project-parser.js +151 -172
- package/dist/javascript/project-parser.js.map +1 -1
- package/dist/javascript/recipes/add-dependency.js +140 -175
- package/dist/javascript/recipes/add-dependency.js.map +1 -1
- package/dist/javascript/recipes/async-callback-in-sync-array-method.js +20 -36
- package/dist/javascript/recipes/async-callback-in-sync-array-method.js.map +1 -1
- package/dist/javascript/recipes/auto-format.js +3 -14
- package/dist/javascript/recipes/auto-format.js.map +1 -1
- package/dist/javascript/recipes/change-import.js +447 -495
- package/dist/javascript/recipes/change-import.js.map +1 -1
- package/dist/javascript/recipes/order-imports.js +162 -175
- package/dist/javascript/recipes/order-imports.js.map +1 -1
- package/dist/javascript/recipes/upgrade-dependency-version.js +167 -197
- package/dist/javascript/recipes/upgrade-dependency-version.js.map +1 -1
- package/dist/javascript/recipes/upgrade-transitive-dependency-version.js +166 -193
- package/dist/javascript/recipes/upgrade-transitive-dependency-version.js.map +1 -1
- package/dist/javascript/remove-import.js +689 -724
- package/dist/javascript/remove-import.js.map +1 -1
- package/dist/javascript/rpc.js +1007 -1332
- package/dist/javascript/rpc.js.map +1 -1
- package/dist/javascript/search/find-dependency.js +84 -110
- package/dist/javascript/search/find-dependency.js.map +1 -1
- package/dist/javascript/search/uses-method.js +5 -19
- package/dist/javascript/search/uses-method.js.map +1 -1
- package/dist/javascript/search/uses-type.js +9 -20
- package/dist/javascript/search/uses-type.js.map +1 -1
- package/dist/javascript/templating/comparator.js +737 -822
- package/dist/javascript/templating/comparator.js.map +1 -1
- package/dist/javascript/templating/engine.js +211 -245
- package/dist/javascript/templating/engine.js.map +1 -1
- package/dist/javascript/templating/pattern.js +169 -190
- package/dist/javascript/templating/pattern.js.map +1 -1
- package/dist/javascript/templating/placeholder-replacement.js +172 -210
- package/dist/javascript/templating/placeholder-replacement.js.map +1 -1
- package/dist/javascript/templating/rewrite.js +75 -97
- package/dist/javascript/templating/rewrite.js.map +1 -1
- package/dist/javascript/templating/template.js +69 -82
- package/dist/javascript/templating/template.js.map +1 -1
- package/dist/javascript/tree-debug.js +109 -137
- package/dist/javascript/tree-debug.js.map +1 -1
- package/dist/javascript/visitor.js +1090 -1254
- package/dist/javascript/visitor.js.map +1 -1
- package/dist/json/print.js +72 -103
- package/dist/json/print.js.map +1 -1
- package/dist/json/rpc.js +120 -181
- package/dist/json/rpc.js.map +1 -1
- package/dist/json/visitor.js +69 -100
- package/dist/json/visitor.js.map +1 -1
- package/dist/marketplace.js +20 -33
- package/dist/marketplace.js.map +1 -1
- package/dist/parse-error.js +41 -62
- package/dist/parse-error.js.map +1 -1
- package/dist/parser.js +7 -18
- package/dist/parser.js.map +1 -1
- package/dist/path-utils.js +46 -59
- package/dist/path-utils.js.map +1 -1
- package/dist/preconditions.js +30 -47
- package/dist/preconditions.js.map +1 -1
- package/dist/print.js +6 -19
- package/dist/print.js.map +1 -1
- package/dist/recipe.js +42 -73
- package/dist/recipe.js.map +1 -1
- package/dist/rpc/index.js +74 -115
- package/dist/rpc/index.js.map +1 -1
- package/dist/rpc/queue.js +71 -90
- package/dist/rpc/queue.js.map +1 -1
- package/dist/rpc/recipe.js +32 -57
- package/dist/rpc/recipe.js.map +1 -1
- package/dist/rpc/request/generate.js +4 -13
- package/dist/rpc/request/generate.js.map +1 -1
- package/dist/rpc/request/get-languages.js +2 -11
- package/dist/rpc/request/get-languages.js.map +1 -1
- package/dist/rpc/request/get-marketplace.js +9 -20
- package/dist/rpc/request/get-marketplace.js.map +1 -1
- package/dist/rpc/request/get-object.js +4 -13
- package/dist/rpc/request/get-object.js.map +1 -1
- package/dist/rpc/request/install-recipes.js +25 -36
- package/dist/rpc/request/install-recipes.js.map +1 -1
- package/dist/rpc/request/metrics.js +8 -17
- package/dist/rpc/request/metrics.js.map +1 -1
- package/dist/rpc/request/parse-project.js +36 -45
- package/dist/rpc/request/parse-project.js.map +1 -1
- package/dist/rpc/request/parse.js +5 -14
- package/dist/rpc/request/parse.js.map +1 -1
- package/dist/rpc/request/prepare-recipe.js +37 -52
- package/dist/rpc/request/prepare-recipe.js.map +1 -1
- package/dist/rpc/request/print.js +5 -14
- package/dist/rpc/request/print.js.map +1 -1
- package/dist/rpc/request/visit.js +56 -71
- package/dist/rpc/request/visit.js.map +1 -1
- package/dist/rpc/rewrite-rpc.js +70 -97
- package/dist/rpc/rewrite-rpc.js.map +1 -1
- package/dist/rpc/server.js +76 -89
- package/dist/rpc/server.js.map +1 -1
- package/dist/run.js +47 -66
- package/dist/run.js.map +1 -1
- package/dist/search/is-source-file.js +8 -19
- package/dist/search/is-source-file.js.map +1 -1
- package/dist/test/rewrite-test.js +154 -188
- package/dist/test/rewrite-test.js.map +1 -1
- package/dist/text/print.js +23 -38
- package/dist/text/print.js.map +1 -1
- package/dist/text/rpc.js +29 -44
- package/dist/text/rpc.js.map +1 -1
- package/dist/text/visitor.js +16 -33
- package/dist/text/visitor.js.map +1 -1
- package/dist/util.js +13 -24
- package/dist/util.js.map +1 -1
- package/dist/version.txt +1 -1
- package/dist/visitor.js +84 -115
- package/dist/visitor.js.map +1 -1
- package/dist/yaml/index.d.ts +2 -0
- package/dist/yaml/index.d.ts.map +1 -1
- package/dist/yaml/index.js +2 -0
- package/dist/yaml/index.js.map +1 -1
- package/dist/yaml/markers.d.ts +21 -0
- package/dist/yaml/markers.d.ts.map +1 -0
- package/dist/yaml/markers.js +37 -0
- package/dist/yaml/markers.js.map +1 -0
- package/dist/yaml/parser.d.ts.map +1 -1
- package/dist/yaml/parser.js +4 -1
- package/dist/yaml/parser.js.map +1 -1
- package/dist/yaml/print.d.ts +1 -1
- package/dist/yaml/print.d.ts.map +1 -1
- package/dist/yaml/print.js +175 -208
- package/dist/yaml/print.js.map +1 -1
- package/dist/yaml/rpc.js +154 -219
- package/dist/yaml/rpc.js.map +1 -1
- package/dist/yaml/visitor.js +78 -113
- package/dist/yaml/visitor.js.map +1 -1
- package/package.json +1 -1
- package/src/javascript/parser.ts +56 -14
- package/src/yaml/index.ts +2 -0
- package/src/yaml/markers.ts +70 -0
- package/src/yaml/parser.ts +5 -1
- package/src/yaml/print.ts +5 -2
|
@@ -1,13 +1,4 @@
|
|
|
1
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
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
3
|
exports.TemplateApplier = exports.TemplateEngine = void 0;
|
|
13
4
|
exports.setTemplateSourceFileCache = setTemplateSourceFileCache;
|
|
@@ -112,35 +103,33 @@ class TemplateCache {
|
|
|
112
103
|
/**
|
|
113
104
|
* Gets a cached compilation unit or creates and caches a new one.
|
|
114
105
|
*/
|
|
115
|
-
getOrParse(templateString, captures, contextStatements, dependencies) {
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
if (cu) {
|
|
120
|
-
return cu;
|
|
121
|
-
}
|
|
122
|
-
// Create workspace if dependencies are provided
|
|
123
|
-
// DependencyWorkspace has its own cache, so multiple templates with
|
|
124
|
-
// the same dependencies will automatically share the same workspace
|
|
125
|
-
let workspaceDir;
|
|
126
|
-
if (dependencies && Object.keys(dependencies).length > 0) {
|
|
127
|
-
workspaceDir = yield dependency_workspace_1.DependencyWorkspace.getOrCreateWorkspace({ dependencies });
|
|
128
|
-
}
|
|
129
|
-
// Prepend context statements for type attribution context
|
|
130
|
-
const fullTemplateString = contextStatements.length > 0
|
|
131
|
-
? contextStatements.join('\n') + '\n' + templateString
|
|
132
|
-
: templateString;
|
|
133
|
-
// Parse and cache (workspace only needed during parsing)
|
|
134
|
-
// Use templateSourceFileCache if configured for ~3.2x speedup on dependency file parsing
|
|
135
|
-
const parser = new __2.JavaScriptParser({
|
|
136
|
-
relativeTo: workspaceDir,
|
|
137
|
-
sourceFileCache: templateSourceFileCache
|
|
138
|
-
});
|
|
139
|
-
const parseGenerator = parser.parse({ text: fullTemplateString, sourcePath: 'template.ts' });
|
|
140
|
-
cu = (yield parseGenerator.next()).value;
|
|
141
|
-
this.cache.set(key, cu);
|
|
106
|
+
async getOrParse(templateString, captures, contextStatements, dependencies) {
|
|
107
|
+
const key = this.generateKey(templateString, captures, contextStatements, dependencies);
|
|
108
|
+
let cu = this.cache.get(key);
|
|
109
|
+
if (cu) {
|
|
142
110
|
return cu;
|
|
111
|
+
}
|
|
112
|
+
// Create workspace if dependencies are provided
|
|
113
|
+
// DependencyWorkspace has its own cache, so multiple templates with
|
|
114
|
+
// the same dependencies will automatically share the same workspace
|
|
115
|
+
let workspaceDir;
|
|
116
|
+
if (dependencies && Object.keys(dependencies).length > 0) {
|
|
117
|
+
workspaceDir = await dependency_workspace_1.DependencyWorkspace.getOrCreateWorkspace({ dependencies });
|
|
118
|
+
}
|
|
119
|
+
// Prepend context statements for type attribution context
|
|
120
|
+
const fullTemplateString = contextStatements.length > 0
|
|
121
|
+
? contextStatements.join('\n') + '\n' + templateString
|
|
122
|
+
: templateString;
|
|
123
|
+
// Parse and cache (workspace only needed during parsing)
|
|
124
|
+
// Use templateSourceFileCache if configured for ~3.2x speedup on dependency file parsing
|
|
125
|
+
const parser = new __2.JavaScriptParser({
|
|
126
|
+
relativeTo: workspaceDir,
|
|
127
|
+
sourceFileCache: templateSourceFileCache
|
|
143
128
|
});
|
|
129
|
+
const parseGenerator = parser.parse({ text: fullTemplateString, sourcePath: 'template.ts' });
|
|
130
|
+
cu = (await parseGenerator.next()).value;
|
|
131
|
+
this.cache.set(key, cu);
|
|
132
|
+
return cu;
|
|
144
133
|
}
|
|
145
134
|
/**
|
|
146
135
|
* Clears the cache.
|
|
@@ -176,28 +165,26 @@ class TemplateEngine {
|
|
|
176
165
|
* @param dependencies NPM dependencies for type attribution
|
|
177
166
|
* @returns A Promise resolving to the extracted template AST
|
|
178
167
|
*/
|
|
179
|
-
static getTemplateTree(
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
return (0, mutative_1.create)(extracted, _ => { });
|
|
200
|
-
});
|
|
168
|
+
static async getTemplateTree(templateParts, parameters, contextStatements = [], dependencies = {}) {
|
|
169
|
+
// Generate type preamble for captures/parameters with types
|
|
170
|
+
const preamble = TemplateEngine.generateTypePreamble(parameters);
|
|
171
|
+
// Build the template string with parameter placeholders
|
|
172
|
+
const templateString = TemplateEngine.buildTemplateString(templateParts, parameters);
|
|
173
|
+
// Add preamble to context statements (so they're skipped during extraction)
|
|
174
|
+
const contextWithPreamble = preamble.length > 0
|
|
175
|
+
? [...contextStatements, ...preamble]
|
|
176
|
+
: contextStatements;
|
|
177
|
+
// Use cache to get or parse the compilation unit
|
|
178
|
+
const cu = await templateCache.getOrParse(templateString, [], contextWithPreamble, dependencies);
|
|
179
|
+
// Check if there are any statements
|
|
180
|
+
if (!cu.statements || cu.statements.length === 0) {
|
|
181
|
+
throw new Error(`Failed to parse template code (no statements):\n${templateString}`);
|
|
182
|
+
}
|
|
183
|
+
// The template code is always the last statement (after context + preamble)
|
|
184
|
+
const lastStatement = cu.statements[cu.statements.length - 1].element;
|
|
185
|
+
// Extract from wrapper using shared utility
|
|
186
|
+
const extracted = utils_1.PlaceholderUtils.extractFromWrapper(lastStatement, 'Template');
|
|
187
|
+
return (0, mutative_1.create)(extracted, _ => { });
|
|
201
188
|
}
|
|
202
189
|
/**
|
|
203
190
|
* Applies a template from a pre-parsed AST and returns the resulting AST.
|
|
@@ -211,20 +198,18 @@ class TemplateEngine {
|
|
|
211
198
|
* @param wrappersMap Map of capture names to J.RightPadded wrappers (for preserving markers)
|
|
212
199
|
* @returns A Promise resolving to the generated AST node
|
|
213
200
|
*/
|
|
214
|
-
static applyTemplateFromAst(
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
return new TemplateApplier(cursor, coordinates, unsubstitutedAst).apply();
|
|
227
|
-
});
|
|
201
|
+
static async applyTemplateFromAst(ast, parameters, cursor, coordinates, values = new Map(), wrappersMap = new Map()) {
|
|
202
|
+
// Create substitutions map for placeholders
|
|
203
|
+
const substitutions = new Map();
|
|
204
|
+
for (let i = 0; i < parameters.length; i++) {
|
|
205
|
+
const placeholder = `${utils_1.PlaceholderUtils.PLACEHOLDER_PREFIX}${i}__`;
|
|
206
|
+
substitutions.set(placeholder, parameters[i]);
|
|
207
|
+
}
|
|
208
|
+
// Unsubstitute placeholders with actual parameter values and match results
|
|
209
|
+
const visitor = new placeholder_replacement_1.PlaceholderReplacementVisitor(substitutions, values, wrappersMap);
|
|
210
|
+
const unsubstitutedAst = (await visitor.visit(ast, null));
|
|
211
|
+
// Apply the template to the current AST
|
|
212
|
+
return new TemplateApplier(cursor, coordinates, unsubstitutedAst).apply();
|
|
228
213
|
}
|
|
229
214
|
/**
|
|
230
215
|
* Generates type preamble declarations for captures/parameters with type annotations.
|
|
@@ -369,70 +354,68 @@ class TemplateEngine {
|
|
|
369
354
|
* @param dependencies NPM dependencies for type attribution
|
|
370
355
|
* @returns A Promise resolving to the extracted pattern AST with capture markers
|
|
371
356
|
*/
|
|
372
|
-
static getPatternTree(
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
continue;
|
|
380
|
-
}
|
|
381
|
-
const captureName = capture[capture_1.CAPTURE_NAME_SYMBOL] || capture.getName();
|
|
382
|
-
const captureType = capture[capture_1.CAPTURE_TYPE_SYMBOL];
|
|
383
|
-
if (captureType) {
|
|
384
|
-
// Convert Type to string if needed
|
|
385
|
-
const typeString = typeof captureType === 'string'
|
|
386
|
-
? captureType
|
|
387
|
-
: this.typeToString(captureType);
|
|
388
|
-
// Only add preamble if we have a concrete type (not 'any')
|
|
389
|
-
if (typeString !== 'any') {
|
|
390
|
-
const placeholder = utils_1.PlaceholderUtils.createCapture(captureName, undefined);
|
|
391
|
-
preamble.push(`let ${placeholder}: ${typeString};`);
|
|
392
|
-
}
|
|
393
|
-
}
|
|
394
|
-
// Don't add preamble declarations without types - they don't provide type attribution
|
|
357
|
+
static async getPatternTree(templateParts, captures, contextStatements = [], dependencies = {}) {
|
|
358
|
+
// Generate type preamble for captures with types (skip RawCode)
|
|
359
|
+
const preamble = [];
|
|
360
|
+
for (const capture of captures) {
|
|
361
|
+
// Skip raw code - it's not a capture
|
|
362
|
+
if (capture instanceof capture_1.RawCode || (capture && typeof capture === 'object' && capture[capture_1.RAW_CODE_SYMBOL])) {
|
|
363
|
+
continue;
|
|
395
364
|
}
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
// Use symbol to access capture name without triggering Proxy
|
|
408
|
-
const captureName = capture[capture_1.CAPTURE_NAME_SYMBOL] || capture.getName();
|
|
409
|
-
result += utils_1.PlaceholderUtils.createCapture(captureName, undefined);
|
|
410
|
-
}
|
|
365
|
+
const captureName = capture[capture_1.CAPTURE_NAME_SYMBOL] || capture.getName();
|
|
366
|
+
const captureType = capture[capture_1.CAPTURE_TYPE_SYMBOL];
|
|
367
|
+
if (captureType) {
|
|
368
|
+
// Convert Type to string if needed
|
|
369
|
+
const typeString = typeof captureType === 'string'
|
|
370
|
+
? captureType
|
|
371
|
+
: this.typeToString(captureType);
|
|
372
|
+
// Only add preamble if we have a concrete type (not 'any')
|
|
373
|
+
if (typeString !== 'any') {
|
|
374
|
+
const placeholder = utils_1.PlaceholderUtils.createCapture(captureName, undefined);
|
|
375
|
+
preamble.push(`let ${placeholder}: ${typeString};`);
|
|
411
376
|
}
|
|
412
377
|
}
|
|
413
|
-
//
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
378
|
+
// Don't add preamble declarations without types - they don't provide type attribution
|
|
379
|
+
}
|
|
380
|
+
// Build the template string with placeholders for captures and raw code
|
|
381
|
+
let result = '';
|
|
382
|
+
for (let i = 0; i < templateParts.length; i++) {
|
|
383
|
+
result += templateParts[i];
|
|
384
|
+
if (i < captures.length) {
|
|
385
|
+
const capture = captures[i];
|
|
386
|
+
// Check if this is a RawCode instance - splice directly
|
|
387
|
+
if (capture instanceof capture_1.RawCode || (capture && typeof capture === 'object' && capture[capture_1.RAW_CODE_SYMBOL])) {
|
|
388
|
+
result += capture.code;
|
|
389
|
+
}
|
|
390
|
+
else {
|
|
391
|
+
// Use symbol to access capture name without triggering Proxy
|
|
392
|
+
const captureName = capture[capture_1.CAPTURE_NAME_SYMBOL] || capture.getName();
|
|
393
|
+
result += utils_1.PlaceholderUtils.createCapture(captureName, undefined);
|
|
394
|
+
}
|
|
427
395
|
}
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
396
|
+
}
|
|
397
|
+
// Always wrap in function body - let the parser decide what it is,
|
|
398
|
+
// then we'll extract intelligently based on what was parsed
|
|
399
|
+
const templateString = `function ${utils_1.WRAPPER_FUNCTION_NAME}() { ${result} }`;
|
|
400
|
+
// Add preamble to context statements (so they're skipped during extraction)
|
|
401
|
+
const contextWithPreamble = preamble.length > 0
|
|
402
|
+
? [...contextStatements, ...preamble]
|
|
403
|
+
: contextStatements;
|
|
404
|
+
// Filter out RawCode from captures for cache and marker attachment
|
|
405
|
+
const actualCaptures = captures.filter(c => !(c instanceof capture_1.RawCode || (c && typeof c === 'object' && c[capture_1.RAW_CODE_SYMBOL])));
|
|
406
|
+
// Use cache to get or parse the compilation unit
|
|
407
|
+
const cu = await templateCache.getOrParse(templateString, actualCaptures, contextWithPreamble, dependencies);
|
|
408
|
+
// Check if there are any statements
|
|
409
|
+
if (!cu.statements || cu.statements.length === 0) {
|
|
410
|
+
throw new Error(`Failed to parse pattern code (no statements):\n${templateString}`);
|
|
411
|
+
}
|
|
412
|
+
// The pattern code is always the last statement (after context + preamble)
|
|
413
|
+
const lastStatement = cu.statements[cu.statements.length - 1].element;
|
|
414
|
+
// Extract from wrapper using shared utility
|
|
415
|
+
const extracted = utils_1.PlaceholderUtils.extractFromWrapper(lastStatement, 'Pattern');
|
|
416
|
+
// Attach CaptureMarkers to capture identifiers (only for actual captures, not raw code)
|
|
417
|
+
const visitor = new MarkerAttachmentVisitor(actualCaptures);
|
|
418
|
+
return (await visitor.visit(extracted, undefined));
|
|
436
419
|
}
|
|
437
420
|
}
|
|
438
421
|
exports.TemplateEngine = TemplateEngine;
|
|
@@ -449,82 +432,73 @@ class MarkerAttachmentVisitor extends __2.JavaScriptVisitor {
|
|
|
449
432
|
/**
|
|
450
433
|
* Attaches CaptureMarker to capture identifiers.
|
|
451
434
|
*/
|
|
452
|
-
visitIdentifier(ident, p) {
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
const
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
const marker = new utils_1.CaptureMarker(captureInfo.name, variadicOptions, constraint);
|
|
474
|
-
return (0, __1.updateIfChanged)(ident, {
|
|
475
|
-
markers: Object.assign(Object.assign({}, ident.markers), { markers: [...ident.markers.markers, marker] })
|
|
476
|
-
});
|
|
477
|
-
}
|
|
435
|
+
async visitIdentifier(ident, p) {
|
|
436
|
+
var _a, _b;
|
|
437
|
+
// First call parent to handle standard visitation
|
|
438
|
+
const visited = await super.visitIdentifier(ident, p);
|
|
439
|
+
if (!visited || visited.kind !== java_1.J.Kind.Identifier) {
|
|
440
|
+
return visited;
|
|
441
|
+
}
|
|
442
|
+
ident = visited;
|
|
443
|
+
// Check if this is a capture placeholder
|
|
444
|
+
if ((_a = ident.simpleName) === null || _a === void 0 ? void 0 : _a.startsWith(utils_1.PlaceholderUtils.CAPTURE_PREFIX)) {
|
|
445
|
+
const captureInfo = utils_1.PlaceholderUtils.parseCapture(ident.simpleName);
|
|
446
|
+
if (captureInfo) {
|
|
447
|
+
// Find the original capture object to get variadic options and constraint
|
|
448
|
+
const captureObj = this.captures.find(c => c.getName() === captureInfo.name);
|
|
449
|
+
const variadicOptions = captureObj === null || captureObj === void 0 ? void 0 : captureObj.getVariadicOptions();
|
|
450
|
+
const constraint = (_b = captureObj === null || captureObj === void 0 ? void 0 : captureObj.getConstraint) === null || _b === void 0 ? void 0 : _b.call(captureObj);
|
|
451
|
+
// Add CaptureMarker to the Identifier with constraint
|
|
452
|
+
const marker = new utils_1.CaptureMarker(captureInfo.name, variadicOptions, constraint);
|
|
453
|
+
return (0, __1.updateIfChanged)(ident, {
|
|
454
|
+
markers: Object.assign(Object.assign({}, ident.markers), { markers: [...ident.markers.markers, marker] })
|
|
455
|
+
});
|
|
478
456
|
}
|
|
479
|
-
|
|
480
|
-
|
|
457
|
+
}
|
|
458
|
+
return ident;
|
|
481
459
|
}
|
|
482
460
|
/**
|
|
483
461
|
* Propagates markers from element to RightPadded wrapper.
|
|
484
462
|
*/
|
|
485
|
-
visitRightPadded(right, p) {
|
|
486
|
-
|
|
487
|
-
if (!(0, __1.isTree)(right.element)) {
|
|
488
|
-
return right;
|
|
489
|
-
}
|
|
490
|
-
const visitedElement = yield this.visit(right.element, p);
|
|
491
|
-
if (visitedElement && visitedElement !== right.element) {
|
|
492
|
-
const result = yield (0, __1.produceAsync)(right, (draft) => __awaiter(this, void 0, void 0, function* () {
|
|
493
|
-
// Visit element first
|
|
494
|
-
if (right.element && right.element.kind) {
|
|
495
|
-
// Check if element has a CaptureMarker
|
|
496
|
-
const elementMarker = utils_1.PlaceholderUtils.getCaptureMarker(visitedElement);
|
|
497
|
-
if (elementMarker) {
|
|
498
|
-
draft.markers.markers.push(elementMarker);
|
|
499
|
-
}
|
|
500
|
-
else {
|
|
501
|
-
draft.element = visitedElement;
|
|
502
|
-
}
|
|
503
|
-
}
|
|
504
|
-
}));
|
|
505
|
-
return result;
|
|
506
|
-
}
|
|
463
|
+
async visitRightPadded(right, p) {
|
|
464
|
+
if (!(0, __1.isTree)(right.element)) {
|
|
507
465
|
return right;
|
|
508
|
-
}
|
|
466
|
+
}
|
|
467
|
+
const visitedElement = await this.visit(right.element, p);
|
|
468
|
+
if (visitedElement && visitedElement !== right.element) {
|
|
469
|
+
const result = await (0, __1.produceAsync)(right, async (draft) => {
|
|
470
|
+
// Visit element first
|
|
471
|
+
if (right.element && right.element.kind) {
|
|
472
|
+
// Check if element has a CaptureMarker
|
|
473
|
+
const elementMarker = utils_1.PlaceholderUtils.getCaptureMarker(visitedElement);
|
|
474
|
+
if (elementMarker) {
|
|
475
|
+
draft.markers.markers.push(elementMarker);
|
|
476
|
+
}
|
|
477
|
+
else {
|
|
478
|
+
draft.element = visitedElement;
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
});
|
|
482
|
+
return result;
|
|
483
|
+
}
|
|
484
|
+
return right;
|
|
509
485
|
}
|
|
510
486
|
/**
|
|
511
487
|
* Propagates markers from expression to ExpressionStatement.
|
|
512
488
|
*/
|
|
513
|
-
visitExpressionStatement(expressionStatement, p) {
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
if (expressionMarker) {
|
|
520
|
-
return (0, __1.updateIfChanged)(expressionStatement, {
|
|
521
|
-
markers: Object.assign(Object.assign({}, expressionStatement.markers), { markers: [...expressionStatement.markers.markers, expressionMarker] }),
|
|
522
|
-
});
|
|
523
|
-
}
|
|
524
|
-
// No marker to move, just update with visited expression
|
|
489
|
+
async visitExpressionStatement(expressionStatement, p) {
|
|
490
|
+
// Visit the expression
|
|
491
|
+
const visitedExpression = await this.visit(expressionStatement.expression, p);
|
|
492
|
+
// Check if expression has a CaptureMarker
|
|
493
|
+
const expressionMarker = utils_1.PlaceholderUtils.getCaptureMarker(visitedExpression);
|
|
494
|
+
if (expressionMarker) {
|
|
525
495
|
return (0, __1.updateIfChanged)(expressionStatement, {
|
|
526
|
-
|
|
496
|
+
markers: Object.assign(Object.assign({}, expressionStatement.markers), { markers: [...expressionStatement.markers.markers, expressionMarker] }),
|
|
527
497
|
});
|
|
498
|
+
}
|
|
499
|
+
// No marker to move, just update with visited expression
|
|
500
|
+
return (0, __1.updateIfChanged)(expressionStatement, {
|
|
501
|
+
expression: visitedExpression
|
|
528
502
|
});
|
|
529
503
|
}
|
|
530
504
|
/**
|
|
@@ -532,22 +506,20 @@ class MarkerAttachmentVisitor extends __2.JavaScriptVisitor {
|
|
|
532
506
|
* This handles destructuring patterns like {${props}} where the capture marker
|
|
533
507
|
* is on the identifier but needs to be on the BindingElement for container matching.
|
|
534
508
|
*/
|
|
535
|
-
visitBindingElement(bindingElement, p) {
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
if (nameMarker) {
|
|
542
|
-
return (0, __1.updateIfChanged)(bindingElement, {
|
|
543
|
-
name: visitedName,
|
|
544
|
-
markers: Object.assign(Object.assign({}, bindingElement.markers), { markers: [...bindingElement.markers.markers, nameMarker] }),
|
|
545
|
-
});
|
|
546
|
-
}
|
|
547
|
-
// No marker to move, just update with visited name
|
|
509
|
+
async visitBindingElement(bindingElement, p) {
|
|
510
|
+
// Visit the name
|
|
511
|
+
const visitedName = await this.visit(bindingElement.name, p);
|
|
512
|
+
// Check if name has a CaptureMarker
|
|
513
|
+
const nameMarker = utils_1.PlaceholderUtils.getCaptureMarker(visitedName);
|
|
514
|
+
if (nameMarker) {
|
|
548
515
|
return (0, __1.updateIfChanged)(bindingElement, {
|
|
549
|
-
name: visitedName
|
|
516
|
+
name: visitedName,
|
|
517
|
+
markers: Object.assign(Object.assign({}, bindingElement.markers), { markers: [...bindingElement.markers.markers, nameMarker] }),
|
|
550
518
|
});
|
|
519
|
+
}
|
|
520
|
+
// No marker to move, just update with visited name
|
|
521
|
+
return (0, __1.updateIfChanged)(bindingElement, {
|
|
522
|
+
name: visitedName
|
|
551
523
|
});
|
|
552
524
|
}
|
|
553
525
|
}
|
|
@@ -565,48 +537,42 @@ class TemplateApplier {
|
|
|
565
537
|
*
|
|
566
538
|
* @returns A Promise resolving to the modified AST
|
|
567
539
|
*/
|
|
568
|
-
apply() {
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
}
|
|
580
|
-
});
|
|
540
|
+
async apply() {
|
|
541
|
+
const { loc } = this.coordinates;
|
|
542
|
+
// Apply the template based on the location and mode
|
|
543
|
+
switch (loc || 'EXPRESSION_PREFIX') {
|
|
544
|
+
case 'EXPRESSION_PREFIX':
|
|
545
|
+
case 'STATEMENT_PREFIX':
|
|
546
|
+
case 'BLOCK_END':
|
|
547
|
+
return this.applyInternal();
|
|
548
|
+
default:
|
|
549
|
+
throw new Error(`Unsupported location: ${loc}`);
|
|
550
|
+
}
|
|
581
551
|
}
|
|
582
552
|
/**
|
|
583
553
|
* Applies the template to an expression.
|
|
584
554
|
*
|
|
585
555
|
* @returns A Promise resolving to the modified AST
|
|
586
556
|
*/
|
|
587
|
-
applyInternal() {
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
return this.format(resultToUse, originalTree);
|
|
596
|
-
});
|
|
557
|
+
async applyInternal() {
|
|
558
|
+
const { tree } = this.coordinates;
|
|
559
|
+
if (!tree) {
|
|
560
|
+
return this.ast;
|
|
561
|
+
}
|
|
562
|
+
const originalTree = tree;
|
|
563
|
+
const resultToUse = this.wrapTree(originalTree, this.ast);
|
|
564
|
+
return this.format(resultToUse, originalTree);
|
|
597
565
|
}
|
|
598
|
-
format(resultToUse, originalTree) {
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
return Object.assign(Object.assign({}, formatted), { id: resultToUse.id });
|
|
609
|
-
});
|
|
566
|
+
async format(resultToUse, originalTree) {
|
|
567
|
+
var _a;
|
|
568
|
+
// Create a copy of the AST with the prefix from the target
|
|
569
|
+
const result = Object.assign(Object.assign({}, resultToUse), {
|
|
570
|
+
// We temporarily set the ID so that the formatter can identify the tree
|
|
571
|
+
id: originalTree.id, prefix: originalTree.prefix });
|
|
572
|
+
// Apply auto-formatting to the result
|
|
573
|
+
const formatted = await (0, format_1.maybeAutoFormat)(originalTree, result, null, undefined, (_a = this.cursor) === null || _a === void 0 ? void 0 : _a.parent);
|
|
574
|
+
// Restore the original ID
|
|
575
|
+
return Object.assign(Object.assign({}, formatted), { id: resultToUse.id });
|
|
610
576
|
}
|
|
611
577
|
wrapTree(originalTree, resultToUse) {
|
|
612
578
|
var _a, _b;
|