@openrewrite/rewrite 8.72.2 → 8.72.3

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.
Files changed (197) hide show
  1. package/dist/execution.js +3 -16
  2. package/dist/execution.js.map +1 -1
  3. package/dist/index.js +34 -49
  4. package/dist/index.js.map +1 -1
  5. package/dist/java/markers.js +12 -29
  6. package/dist/java/markers.js.map +1 -1
  7. package/dist/java/print.js +2 -13
  8. package/dist/java/print.js.map +1 -1
  9. package/dist/java/rpc.js +1344 -1725
  10. package/dist/java/rpc.js.map +1 -1
  11. package/dist/java/type-visitor.js +143 -184
  12. package/dist/java/type-visitor.js.map +1 -1
  13. package/dist/java/visitor.js +1072 -1249
  14. package/dist/java/visitor.js.map +1 -1
  15. package/dist/javascript/add-import.js +495 -534
  16. package/dist/javascript/add-import.js.map +1 -1
  17. package/dist/javascript/autodetect.js +116 -159
  18. package/dist/javascript/autodetect.js.map +1 -1
  19. package/dist/javascript/cleanup/add-parse-int-radix.js +41 -57
  20. package/dist/javascript/cleanup/add-parse-int-radix.js.map +1 -1
  21. package/dist/javascript/cleanup/prefer-optional-chain.js +89 -105
  22. package/dist/javascript/cleanup/prefer-optional-chain.js.map +1 -1
  23. package/dist/javascript/cleanup/use-object-property-shorthand.js +74 -95
  24. package/dist/javascript/cleanup/use-object-property-shorthand.js.map +1 -1
  25. package/dist/javascript/comparator.js +815 -1167
  26. package/dist/javascript/comparator.js.map +1 -1
  27. package/dist/javascript/dependency-workspace.js +206 -219
  28. package/dist/javascript/dependency-workspace.js.map +1 -1
  29. package/dist/javascript/format/format.js +682 -908
  30. package/dist/javascript/format/format.js.map +1 -1
  31. package/dist/javascript/format/minimum-viable-spacing-visitor.js +152 -231
  32. package/dist/javascript/format/minimum-viable-spacing-visitor.js.map +1 -1
  33. package/dist/javascript/format/normalize-whitespace-visitor.js +12 -31
  34. package/dist/javascript/format/normalize-whitespace-visitor.js.map +1 -1
  35. package/dist/javascript/format/prettier-config-loader.js +134 -153
  36. package/dist/javascript/format/prettier-config-loader.js.map +1 -1
  37. package/dist/javascript/format/prettier-format.js +112 -129
  38. package/dist/javascript/format/prettier-format.js.map +1 -1
  39. package/dist/javascript/format/tabs-and-indents-visitor.js +112 -136
  40. package/dist/javascript/format/tabs-and-indents-visitor.js.map +1 -1
  41. package/dist/javascript/markers.js +59 -92
  42. package/dist/javascript/markers.js.map +1 -1
  43. package/dist/javascript/migrate/es6/modernize-octal-escape-sequences.js +39 -52
  44. package/dist/javascript/migrate/es6/modernize-octal-escape-sequences.js.map +1 -1
  45. package/dist/javascript/migrate/es6/modernize-octal-literals.js +25 -38
  46. package/dist/javascript/migrate/es6/modernize-octal-literals.js.map +1 -1
  47. package/dist/javascript/migrate/es6/remove-duplicate-object-keys.js +66 -82
  48. package/dist/javascript/migrate/es6/remove-duplicate-object-keys.js.map +1 -1
  49. package/dist/javascript/migrate/typescript/export-assignment-to-export-default.js +10 -23
  50. package/dist/javascript/migrate/typescript/export-assignment-to-export-default.js.map +1 -1
  51. package/dist/javascript/node-resolution-result.js +137 -166
  52. package/dist/javascript/node-resolution-result.js.map +1 -1
  53. package/dist/javascript/package-json-parser.js +312 -343
  54. package/dist/javascript/package-json-parser.js.map +1 -1
  55. package/dist/javascript/package-manager.js +145 -170
  56. package/dist/javascript/package-manager.js.map +1 -1
  57. package/dist/javascript/parser.d.ts.map +1 -1
  58. package/dist/javascript/parser.js +94 -68
  59. package/dist/javascript/parser.js.map +1 -1
  60. package/dist/javascript/print.js +1572 -1835
  61. package/dist/javascript/print.js.map +1 -1
  62. package/dist/javascript/project-parser.js +151 -172
  63. package/dist/javascript/project-parser.js.map +1 -1
  64. package/dist/javascript/recipes/add-dependency.js +140 -175
  65. package/dist/javascript/recipes/add-dependency.js.map +1 -1
  66. package/dist/javascript/recipes/async-callback-in-sync-array-method.js +20 -36
  67. package/dist/javascript/recipes/async-callback-in-sync-array-method.js.map +1 -1
  68. package/dist/javascript/recipes/auto-format.js +3 -14
  69. package/dist/javascript/recipes/auto-format.js.map +1 -1
  70. package/dist/javascript/recipes/change-import.js +447 -495
  71. package/dist/javascript/recipes/change-import.js.map +1 -1
  72. package/dist/javascript/recipes/order-imports.js +162 -175
  73. package/dist/javascript/recipes/order-imports.js.map +1 -1
  74. package/dist/javascript/recipes/upgrade-dependency-version.js +167 -197
  75. package/dist/javascript/recipes/upgrade-dependency-version.js.map +1 -1
  76. package/dist/javascript/recipes/upgrade-transitive-dependency-version.js +166 -193
  77. package/dist/javascript/recipes/upgrade-transitive-dependency-version.js.map +1 -1
  78. package/dist/javascript/remove-import.js +689 -724
  79. package/dist/javascript/remove-import.js.map +1 -1
  80. package/dist/javascript/rpc.js +1007 -1332
  81. package/dist/javascript/rpc.js.map +1 -1
  82. package/dist/javascript/search/find-dependency.js +84 -110
  83. package/dist/javascript/search/find-dependency.js.map +1 -1
  84. package/dist/javascript/search/uses-method.js +5 -19
  85. package/dist/javascript/search/uses-method.js.map +1 -1
  86. package/dist/javascript/search/uses-type.js +9 -20
  87. package/dist/javascript/search/uses-type.js.map +1 -1
  88. package/dist/javascript/templating/comparator.js +737 -822
  89. package/dist/javascript/templating/comparator.js.map +1 -1
  90. package/dist/javascript/templating/engine.js +211 -245
  91. package/dist/javascript/templating/engine.js.map +1 -1
  92. package/dist/javascript/templating/pattern.js +169 -190
  93. package/dist/javascript/templating/pattern.js.map +1 -1
  94. package/dist/javascript/templating/placeholder-replacement.js +172 -210
  95. package/dist/javascript/templating/placeholder-replacement.js.map +1 -1
  96. package/dist/javascript/templating/rewrite.js +75 -97
  97. package/dist/javascript/templating/rewrite.js.map +1 -1
  98. package/dist/javascript/templating/template.js +69 -82
  99. package/dist/javascript/templating/template.js.map +1 -1
  100. package/dist/javascript/tree-debug.js +109 -137
  101. package/dist/javascript/tree-debug.js.map +1 -1
  102. package/dist/javascript/visitor.js +1090 -1254
  103. package/dist/javascript/visitor.js.map +1 -1
  104. package/dist/json/print.js +72 -103
  105. package/dist/json/print.js.map +1 -1
  106. package/dist/json/rpc.js +120 -181
  107. package/dist/json/rpc.js.map +1 -1
  108. package/dist/json/visitor.js +69 -100
  109. package/dist/json/visitor.js.map +1 -1
  110. package/dist/marketplace.js +20 -33
  111. package/dist/marketplace.js.map +1 -1
  112. package/dist/parse-error.js +41 -62
  113. package/dist/parse-error.js.map +1 -1
  114. package/dist/parser.js +7 -18
  115. package/dist/parser.js.map +1 -1
  116. package/dist/path-utils.js +46 -59
  117. package/dist/path-utils.js.map +1 -1
  118. package/dist/preconditions.js +30 -47
  119. package/dist/preconditions.js.map +1 -1
  120. package/dist/print.js +6 -19
  121. package/dist/print.js.map +1 -1
  122. package/dist/recipe.js +42 -73
  123. package/dist/recipe.js.map +1 -1
  124. package/dist/rpc/index.js +74 -115
  125. package/dist/rpc/index.js.map +1 -1
  126. package/dist/rpc/queue.js +71 -90
  127. package/dist/rpc/queue.js.map +1 -1
  128. package/dist/rpc/recipe.js +32 -57
  129. package/dist/rpc/recipe.js.map +1 -1
  130. package/dist/rpc/request/generate.js +4 -13
  131. package/dist/rpc/request/generate.js.map +1 -1
  132. package/dist/rpc/request/get-languages.js +2 -11
  133. package/dist/rpc/request/get-languages.js.map +1 -1
  134. package/dist/rpc/request/get-marketplace.js +9 -20
  135. package/dist/rpc/request/get-marketplace.js.map +1 -1
  136. package/dist/rpc/request/get-object.js +4 -13
  137. package/dist/rpc/request/get-object.js.map +1 -1
  138. package/dist/rpc/request/install-recipes.js +25 -36
  139. package/dist/rpc/request/install-recipes.js.map +1 -1
  140. package/dist/rpc/request/metrics.js +8 -17
  141. package/dist/rpc/request/metrics.js.map +1 -1
  142. package/dist/rpc/request/parse-project.js +36 -45
  143. package/dist/rpc/request/parse-project.js.map +1 -1
  144. package/dist/rpc/request/parse.js +5 -14
  145. package/dist/rpc/request/parse.js.map +1 -1
  146. package/dist/rpc/request/prepare-recipe.js +37 -52
  147. package/dist/rpc/request/prepare-recipe.js.map +1 -1
  148. package/dist/rpc/request/print.js +5 -14
  149. package/dist/rpc/request/print.js.map +1 -1
  150. package/dist/rpc/request/visit.js +56 -71
  151. package/dist/rpc/request/visit.js.map +1 -1
  152. package/dist/rpc/rewrite-rpc.js +70 -97
  153. package/dist/rpc/rewrite-rpc.js.map +1 -1
  154. package/dist/rpc/server.js +76 -89
  155. package/dist/rpc/server.js.map +1 -1
  156. package/dist/run.js +47 -66
  157. package/dist/run.js.map +1 -1
  158. package/dist/search/is-source-file.js +8 -19
  159. package/dist/search/is-source-file.js.map +1 -1
  160. package/dist/test/rewrite-test.js +154 -188
  161. package/dist/test/rewrite-test.js.map +1 -1
  162. package/dist/text/print.js +23 -38
  163. package/dist/text/print.js.map +1 -1
  164. package/dist/text/rpc.js +29 -44
  165. package/dist/text/rpc.js.map +1 -1
  166. package/dist/text/visitor.js +16 -33
  167. package/dist/text/visitor.js.map +1 -1
  168. package/dist/util.js +13 -24
  169. package/dist/util.js.map +1 -1
  170. package/dist/version.txt +1 -1
  171. package/dist/visitor.js +84 -115
  172. package/dist/visitor.js.map +1 -1
  173. package/dist/yaml/index.d.ts +2 -0
  174. package/dist/yaml/index.d.ts.map +1 -1
  175. package/dist/yaml/index.js +2 -0
  176. package/dist/yaml/index.js.map +1 -1
  177. package/dist/yaml/markers.d.ts +21 -0
  178. package/dist/yaml/markers.d.ts.map +1 -0
  179. package/dist/yaml/markers.js +37 -0
  180. package/dist/yaml/markers.js.map +1 -0
  181. package/dist/yaml/parser.d.ts.map +1 -1
  182. package/dist/yaml/parser.js +4 -1
  183. package/dist/yaml/parser.js.map +1 -1
  184. package/dist/yaml/print.d.ts +1 -1
  185. package/dist/yaml/print.d.ts.map +1 -1
  186. package/dist/yaml/print.js +175 -208
  187. package/dist/yaml/print.js.map +1 -1
  188. package/dist/yaml/rpc.js +154 -219
  189. package/dist/yaml/rpc.js.map +1 -1
  190. package/dist/yaml/visitor.js +78 -113
  191. package/dist/yaml/visitor.js.map +1 -1
  192. package/package.json +1 -1
  193. package/src/javascript/parser.ts +56 -14
  194. package/src/yaml/index.ts +2 -0
  195. package/src/yaml/markers.ts +70 -0
  196. package/src/yaml/parser.ts +5 -1
  197. 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
- return __awaiter(this, void 0, void 0, function* () {
117
- const key = this.generateKey(templateString, captures, contextStatements, dependencies);
118
- let cu = this.cache.get(key);
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(templateParts_1, parameters_1) {
180
- return __awaiter(this, arguments, void 0, function* (templateParts, parameters, contextStatements = [], dependencies = {}) {
181
- // Generate type preamble for captures/parameters with types
182
- const preamble = TemplateEngine.generateTypePreamble(parameters);
183
- // Build the template string with parameter placeholders
184
- const templateString = TemplateEngine.buildTemplateString(templateParts, parameters);
185
- // Add preamble to context statements (so they're skipped during extraction)
186
- const contextWithPreamble = preamble.length > 0
187
- ? [...contextStatements, ...preamble]
188
- : contextStatements;
189
- // Use cache to get or parse the compilation unit
190
- const cu = yield templateCache.getOrParse(templateString, [], contextWithPreamble, dependencies);
191
- // Check if there are any statements
192
- if (!cu.statements || cu.statements.length === 0) {
193
- throw new Error(`Failed to parse template code (no statements):\n${templateString}`);
194
- }
195
- // The template code is always the last statement (after context + preamble)
196
- const lastStatement = cu.statements[cu.statements.length - 1].element;
197
- // Extract from wrapper using shared utility
198
- const extracted = utils_1.PlaceholderUtils.extractFromWrapper(lastStatement, 'Template');
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(ast_1, parameters_1, cursor_1, coordinates_1) {
215
- return __awaiter(this, arguments, void 0, function* (ast, parameters, cursor, coordinates, values = new Map(), wrappersMap = new Map()) {
216
- // Create substitutions map for placeholders
217
- const substitutions = new Map();
218
- for (let i = 0; i < parameters.length; i++) {
219
- const placeholder = `${utils_1.PlaceholderUtils.PLACEHOLDER_PREFIX}${i}__`;
220
- substitutions.set(placeholder, parameters[i]);
221
- }
222
- // Unsubstitute placeholders with actual parameter values and match results
223
- const visitor = new placeholder_replacement_1.PlaceholderReplacementVisitor(substitutions, values, wrappersMap);
224
- const unsubstitutedAst = (yield visitor.visit(ast, null));
225
- // Apply the template to the current AST
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(templateParts_1, captures_1) {
373
- return __awaiter(this, arguments, void 0, function* (templateParts, captures, contextStatements = [], dependencies = {}) {
374
- // Generate type preamble for captures with types (skip RawCode)
375
- const preamble = [];
376
- for (const capture of captures) {
377
- // Skip raw code - it's not a capture
378
- if (capture instanceof capture_1.RawCode || (capture && typeof capture === 'object' && capture[capture_1.RAW_CODE_SYMBOL])) {
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
- // Build the template string with placeholders for captures and raw code
397
- let result = '';
398
- for (let i = 0; i < templateParts.length; i++) {
399
- result += templateParts[i];
400
- if (i < captures.length) {
401
- const capture = captures[i];
402
- // Check if this is a RawCode instance - splice directly
403
- if (capture instanceof capture_1.RawCode || (capture && typeof capture === 'object' && capture[capture_1.RAW_CODE_SYMBOL])) {
404
- result += capture.code;
405
- }
406
- else {
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
- // Always wrap in function body - let the parser decide what it is,
414
- // then we'll extract intelligently based on what was parsed
415
- const templateString = `function ${utils_1.WRAPPER_FUNCTION_NAME}() { ${result} }`;
416
- // Add preamble to context statements (so they're skipped during extraction)
417
- const contextWithPreamble = preamble.length > 0
418
- ? [...contextStatements, ...preamble]
419
- : contextStatements;
420
- // Filter out RawCode from captures for cache and marker attachment
421
- const actualCaptures = captures.filter(c => !(c instanceof capture_1.RawCode || (c && typeof c === 'object' && c[capture_1.RAW_CODE_SYMBOL])));
422
- // Use cache to get or parse the compilation unit
423
- const cu = yield templateCache.getOrParse(templateString, actualCaptures, contextWithPreamble, dependencies);
424
- // Check if there are any statements
425
- if (!cu.statements || cu.statements.length === 0) {
426
- throw new Error(`Failed to parse pattern code (no statements):\n${templateString}`);
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
- // The pattern code is always the last statement (after context + preamble)
429
- const lastStatement = cu.statements[cu.statements.length - 1].element;
430
- // Extract from wrapper using shared utility
431
- const extracted = utils_1.PlaceholderUtils.extractFromWrapper(lastStatement, 'Pattern');
432
- // Attach CaptureMarkers to capture identifiers (only for actual captures, not raw code)
433
- const visitor = new MarkerAttachmentVisitor(actualCaptures);
434
- return (yield visitor.visit(extracted, undefined));
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
- const _super = Object.create(null, {
454
- visitIdentifier: { get: () => super.visitIdentifier }
455
- });
456
- return __awaiter(this, void 0, void 0, function* () {
457
- var _a, _b;
458
- // First call parent to handle standard visitation
459
- const visited = yield _super.visitIdentifier.call(this, ident, p);
460
- if (!visited || visited.kind !== java_1.J.Kind.Identifier) {
461
- return visited;
462
- }
463
- ident = visited;
464
- // Check if this is a capture placeholder
465
- if ((_a = ident.simpleName) === null || _a === void 0 ? void 0 : _a.startsWith(utils_1.PlaceholderUtils.CAPTURE_PREFIX)) {
466
- const captureInfo = utils_1.PlaceholderUtils.parseCapture(ident.simpleName);
467
- if (captureInfo) {
468
- // Find the original capture object to get variadic options and constraint
469
- const captureObj = this.captures.find(c => c.getName() === captureInfo.name);
470
- const variadicOptions = captureObj === null || captureObj === void 0 ? void 0 : captureObj.getVariadicOptions();
471
- const constraint = (_b = captureObj === null || captureObj === void 0 ? void 0 : captureObj.getConstraint) === null || _b === void 0 ? void 0 : _b.call(captureObj);
472
- // Add CaptureMarker to the Identifier with constraint
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
- return ident;
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
- return __awaiter(this, void 0, void 0, function* () {
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
- return __awaiter(this, void 0, void 0, function* () {
515
- // Visit the expression
516
- const visitedExpression = yield this.visit(expressionStatement.expression, p);
517
- // Check if expression has a CaptureMarker
518
- const expressionMarker = utils_1.PlaceholderUtils.getCaptureMarker(visitedExpression);
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
- expression: visitedExpression
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
- return __awaiter(this, void 0, void 0, function* () {
537
- // Visit the name
538
- const visitedName = yield this.visit(bindingElement.name, p);
539
- // Check if name has a CaptureMarker
540
- const nameMarker = utils_1.PlaceholderUtils.getCaptureMarker(visitedName);
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
- return __awaiter(this, void 0, void 0, function* () {
570
- const { loc } = this.coordinates;
571
- // Apply the template based on the location and mode
572
- switch (loc || 'EXPRESSION_PREFIX') {
573
- case 'EXPRESSION_PREFIX':
574
- case 'STATEMENT_PREFIX':
575
- case 'BLOCK_END':
576
- return this.applyInternal();
577
- default:
578
- throw new Error(`Unsupported location: ${loc}`);
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
- return __awaiter(this, void 0, void 0, function* () {
589
- const { tree } = this.coordinates;
590
- if (!tree) {
591
- return this.ast;
592
- }
593
- const originalTree = tree;
594
- const resultToUse = this.wrapTree(originalTree, this.ast);
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
- return __awaiter(this, void 0, void 0, function* () {
600
- var _a;
601
- // Create a copy of the AST with the prefix from the target
602
- const result = Object.assign(Object.assign({}, resultToUse), {
603
- // We temporarily set the ID so that the formatter can identify the tree
604
- id: originalTree.id, prefix: originalTree.prefix });
605
- // Apply auto-formatting to the result
606
- const formatted = yield (0, format_1.maybeAutoFormat)(originalTree, result, null, undefined, (_a = this.cursor) === null || _a === void 0 ? void 0 : _a.parent);
607
- // Restore the original ID
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;