@openrewrite/rewrite 8.66.0 → 8.66.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/javascript/comparator.d.ts +67 -4
- package/dist/javascript/comparator.d.ts.map +1 -1
- package/dist/javascript/comparator.js +523 -2794
- package/dist/javascript/comparator.js.map +1 -1
- package/dist/javascript/format.d.ts.map +1 -1
- package/dist/javascript/format.js +4 -3
- package/dist/javascript/format.js.map +1 -1
- package/dist/javascript/index.d.ts +1 -1
- package/dist/javascript/index.d.ts.map +1 -1
- package/dist/javascript/index.js +1 -1
- package/dist/javascript/index.js.map +1 -1
- package/dist/javascript/parser.d.ts.map +1 -1
- package/dist/javascript/parser.js +18 -16
- package/dist/javascript/parser.js.map +1 -1
- package/dist/javascript/templating/capture.d.ts +226 -0
- package/dist/javascript/templating/capture.d.ts.map +1 -0
- package/dist/javascript/templating/capture.js +371 -0
- package/dist/javascript/templating/capture.js.map +1 -0
- package/dist/javascript/templating/comparator.d.ts +61 -0
- package/dist/javascript/templating/comparator.d.ts.map +1 -0
- package/dist/javascript/templating/comparator.js +393 -0
- package/dist/javascript/templating/comparator.js.map +1 -0
- package/dist/javascript/templating/engine.d.ts +75 -0
- package/dist/javascript/templating/engine.d.ts.map +1 -0
- package/dist/javascript/templating/engine.js +228 -0
- package/dist/javascript/templating/engine.js.map +1 -0
- package/dist/javascript/templating/index.d.ts +6 -0
- package/dist/javascript/templating/index.d.ts.map +1 -0
- package/dist/javascript/templating/index.js +42 -0
- package/dist/javascript/templating/index.js.map +1 -0
- package/dist/javascript/templating/pattern.d.ts +171 -0
- package/dist/javascript/templating/pattern.d.ts.map +1 -0
- package/dist/javascript/templating/pattern.js +681 -0
- package/dist/javascript/templating/pattern.js.map +1 -0
- package/dist/javascript/templating/placeholder-replacement.d.ts +58 -0
- package/dist/javascript/templating/placeholder-replacement.d.ts.map +1 -0
- package/dist/javascript/templating/placeholder-replacement.js +365 -0
- package/dist/javascript/templating/placeholder-replacement.js.map +1 -0
- package/dist/javascript/templating/rewrite.d.ts +39 -0
- package/dist/javascript/templating/rewrite.d.ts.map +1 -0
- package/dist/javascript/templating/rewrite.js +81 -0
- package/dist/javascript/templating/rewrite.js.map +1 -0
- package/dist/javascript/templating/template.d.ts +204 -0
- package/dist/javascript/templating/template.d.ts.map +1 -0
- package/dist/javascript/templating/template.js +293 -0
- package/dist/javascript/templating/template.js.map +1 -0
- package/dist/javascript/templating/types.d.ts +263 -0
- package/dist/javascript/templating/types.d.ts.map +1 -0
- package/dist/javascript/templating/types.js +3 -0
- package/dist/javascript/templating/types.js.map +1 -0
- package/dist/javascript/templating/utils.d.ts +118 -0
- package/dist/javascript/templating/utils.d.ts.map +1 -0
- package/dist/javascript/templating/utils.js +253 -0
- package/dist/javascript/templating/utils.js.map +1 -0
- package/dist/version.txt +1 -1
- package/package.json +2 -1
- package/src/javascript/comparator.ts +554 -3323
- package/src/javascript/format.ts +3 -2
- package/src/javascript/index.ts +1 -1
- package/src/javascript/parser.ts +19 -17
- package/src/javascript/templating/capture.ts +503 -0
- package/src/javascript/templating/comparator.ts +430 -0
- package/src/javascript/templating/engine.ts +252 -0
- package/src/javascript/templating/index.ts +60 -0
- package/src/javascript/templating/pattern.ts +727 -0
- package/src/javascript/templating/placeholder-replacement.ts +372 -0
- package/src/javascript/templating/rewrite.ts +95 -0
- package/src/javascript/templating/template.ts +326 -0
- package/src/javascript/templating/types.ts +300 -0
- package/src/javascript/templating/utils.ts +284 -0
- package/dist/javascript/templating.d.ts +0 -265
- package/dist/javascript/templating.d.ts.map +0 -1
- package/dist/javascript/templating.js +0 -1027
- package/dist/javascript/templating.js.map +0 -1
- package/src/javascript/templating.ts +0 -1226
|
@@ -0,0 +1,284 @@
|
|
|
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 {J} from '../../java';
|
|
17
|
+
import {JS} from '../index';
|
|
18
|
+
import {JavaScriptParser} from '../parser';
|
|
19
|
+
import {DependencyWorkspace} from '../dependency-workspace';
|
|
20
|
+
import {Marker} from '../../markers';
|
|
21
|
+
import {randomId} from '../../uuid';
|
|
22
|
+
import {VariadicOptions, Capture, Any} from './types';
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Internal storage value type for pattern match captures.
|
|
26
|
+
* - J: Scalar captures without wrapper (fallback)
|
|
27
|
+
* - J.RightPadded<J>: Scalar captures with wrapper (preserves trailing markers like semicolons)
|
|
28
|
+
* - J[]: Variadic captures without wrapper metadata
|
|
29
|
+
* - J.RightPadded<J>[]: Variadic captures with wrapper metadata (preserves markers like commas)
|
|
30
|
+
*/
|
|
31
|
+
export type CaptureStorageValue = J | J.RightPadded<J> | J[] | J.RightPadded<J>[];
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Symbol to access wrappersMap without exposing it as public API
|
|
35
|
+
*/
|
|
36
|
+
export const WRAPPERS_MAP_SYMBOL = Symbol('wrappersMap');
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Cache for compiled templates and patterns.
|
|
40
|
+
* Stores parsed ASTs to avoid expensive re-parsing and dependency resolution.
|
|
41
|
+
*/
|
|
42
|
+
export class TemplateCache {
|
|
43
|
+
private cache = new Map<string, JS.CompilationUnit>();
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Generates a cache key from template string, captures, and options.
|
|
47
|
+
*/
|
|
48
|
+
private generateKey(
|
|
49
|
+
templateString: string,
|
|
50
|
+
captures: (Capture | Any<any>)[],
|
|
51
|
+
contextStatements: string[],
|
|
52
|
+
dependencies: Record<string, string>
|
|
53
|
+
): string {
|
|
54
|
+
// Use the actual template string (with placeholders) as the primary key
|
|
55
|
+
const templateKey = templateString;
|
|
56
|
+
|
|
57
|
+
// Capture names
|
|
58
|
+
const capturesKey = captures.map(c => c.getName()).join(',');
|
|
59
|
+
|
|
60
|
+
// Context statements
|
|
61
|
+
const contextKey = contextStatements.join(';');
|
|
62
|
+
|
|
63
|
+
// Dependencies
|
|
64
|
+
const depsKey = JSON.stringify(dependencies || {});
|
|
65
|
+
|
|
66
|
+
return `${templateKey}::${capturesKey}::${contextKey}::${depsKey}`;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Gets a cached compilation unit or creates and caches a new one.
|
|
71
|
+
*/
|
|
72
|
+
async getOrParse(
|
|
73
|
+
templateString: string,
|
|
74
|
+
captures: (Capture | Any<any>)[],
|
|
75
|
+
contextStatements: string[],
|
|
76
|
+
dependencies: Record<string, string>
|
|
77
|
+
): Promise<JS.CompilationUnit> {
|
|
78
|
+
const key = this.generateKey(templateString, captures, contextStatements, dependencies);
|
|
79
|
+
|
|
80
|
+
let cu = this.cache.get(key);
|
|
81
|
+
if (cu) {
|
|
82
|
+
return cu;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// Create workspace if dependencies are provided
|
|
86
|
+
// DependencyWorkspace has its own cache, so multiple templates with
|
|
87
|
+
// the same dependencies will automatically share the same workspace
|
|
88
|
+
let workspaceDir: string | undefined;
|
|
89
|
+
if (dependencies && Object.keys(dependencies).length > 0) {
|
|
90
|
+
workspaceDir = await DependencyWorkspace.getOrCreateWorkspace(dependencies);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// Prepend context statements for type attribution context
|
|
94
|
+
const fullTemplateString = contextStatements.length > 0
|
|
95
|
+
? contextStatements.join('\n') + '\n' + templateString
|
|
96
|
+
: templateString;
|
|
97
|
+
|
|
98
|
+
// Parse and cache (workspace only needed during parsing)
|
|
99
|
+
const parser = new JavaScriptParser({relativeTo: workspaceDir});
|
|
100
|
+
const parseGenerator = parser.parse({text: fullTemplateString, sourcePath: 'template.ts'});
|
|
101
|
+
cu = (await parseGenerator.next()).value as JS.CompilationUnit;
|
|
102
|
+
|
|
103
|
+
this.cache.set(key, cu);
|
|
104
|
+
return cu;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Clears the cache.
|
|
109
|
+
*/
|
|
110
|
+
clear(): void {
|
|
111
|
+
this.cache.clear();
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// Global cache instance
|
|
116
|
+
export const templateCache = new TemplateCache();
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Marker that stores capture metadata on pattern AST nodes.
|
|
120
|
+
* This avoids the need to parse capture names from identifiers during matching.
|
|
121
|
+
*/
|
|
122
|
+
export class CaptureMarker implements Marker {
|
|
123
|
+
readonly kind = 'org.openrewrite.javascript.CaptureMarker';
|
|
124
|
+
readonly id = randomId();
|
|
125
|
+
|
|
126
|
+
constructor(
|
|
127
|
+
public readonly captureName: string,
|
|
128
|
+
public readonly variadicOptions?: VariadicOptions
|
|
129
|
+
) {
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Utility class for managing placeholder naming and parsing.
|
|
135
|
+
* Centralizes all logic related to capture placeholders.
|
|
136
|
+
*/
|
|
137
|
+
export class PlaceholderUtils {
|
|
138
|
+
static readonly CAPTURE_PREFIX = '__capt_';
|
|
139
|
+
static readonly PLACEHOLDER_PREFIX = '__PLACEHOLDER_';
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Checks if a node is a capture placeholder.
|
|
143
|
+
*
|
|
144
|
+
* @param node The node to check
|
|
145
|
+
* @returns true if the node is a capture placeholder, false otherwise
|
|
146
|
+
*/
|
|
147
|
+
static isCapture(node: J): boolean {
|
|
148
|
+
// Check for CaptureMarker first (efficient)
|
|
149
|
+
for (const marker of node.markers.markers) {
|
|
150
|
+
if (marker instanceof CaptureMarker) {
|
|
151
|
+
return true;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
return false;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Gets the capture name from a node with a CaptureMarker.
|
|
159
|
+
*
|
|
160
|
+
* @param node The node to extract capture name from
|
|
161
|
+
* @returns The capture name, or null if not a capture
|
|
162
|
+
*/
|
|
163
|
+
static getCaptureName(node: J): string | undefined {
|
|
164
|
+
// Check for CaptureMarker
|
|
165
|
+
for (const marker of node.markers.markers) {
|
|
166
|
+
if (marker instanceof CaptureMarker) {
|
|
167
|
+
return marker.captureName;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
return undefined;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Gets the CaptureMarker from a node, if present.
|
|
176
|
+
*
|
|
177
|
+
* @param node The node to check
|
|
178
|
+
* @returns The CaptureMarker or undefined
|
|
179
|
+
*/
|
|
180
|
+
static getCaptureMarker(node: J): CaptureMarker | undefined {
|
|
181
|
+
for (const marker of node.markers.markers) {
|
|
182
|
+
if (marker instanceof CaptureMarker) {
|
|
183
|
+
return marker;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
return undefined;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Parses a capture placeholder to extract name and type constraint.
|
|
191
|
+
*
|
|
192
|
+
* @param identifier The identifier string to parse
|
|
193
|
+
* @returns Object with name and optional type constraint, or null if not a valid capture
|
|
194
|
+
*/
|
|
195
|
+
static parseCapture(identifier: string): { name: string; typeConstraint?: string } | null {
|
|
196
|
+
if (!identifier.startsWith(this.CAPTURE_PREFIX)) {
|
|
197
|
+
return null;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// Handle unnamed captures: "__capt_unnamed_N__"
|
|
201
|
+
if (identifier.startsWith(`${this.CAPTURE_PREFIX}unnamed_`)) {
|
|
202
|
+
const match = identifier.match(/__capt_(unnamed_\d+)__/);
|
|
203
|
+
return match ? {name: match[1]} : null;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// Handle all other captures (including any()): "__capt_name__" or "__capt_name_type__"
|
|
207
|
+
const match = identifier.match(/__capt_([^_]+(?:_\d+)?)(?:_([^_]+))?__/);
|
|
208
|
+
if (!match) {
|
|
209
|
+
return null;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
return {
|
|
213
|
+
name: match[1],
|
|
214
|
+
typeConstraint: match[2]
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Creates a capture placeholder string.
|
|
220
|
+
*
|
|
221
|
+
* @param name The capture name
|
|
222
|
+
* @param typeConstraint Optional type constraint
|
|
223
|
+
* @returns The formatted placeholder string
|
|
224
|
+
*/
|
|
225
|
+
static createCapture(name: string, typeConstraint?: string): string {
|
|
226
|
+
// Always use CAPTURE_PREFIX - the capturing flag is used internally for binding behavior
|
|
227
|
+
return typeConstraint
|
|
228
|
+
? `${this.CAPTURE_PREFIX}${name}_${typeConstraint}__`
|
|
229
|
+
: `${this.CAPTURE_PREFIX}${name}__`;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Checks if a capture marker indicates a variadic capture.
|
|
234
|
+
*
|
|
235
|
+
* @param node The node to check
|
|
236
|
+
* @returns true if the node has a variadic CaptureMarker, false otherwise
|
|
237
|
+
*/
|
|
238
|
+
static isVariadicCapture(node: J): boolean {
|
|
239
|
+
for (const marker of node.markers.markers) {
|
|
240
|
+
if (marker instanceof CaptureMarker && marker.variadicOptions) {
|
|
241
|
+
return true;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
return false;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* Gets the variadic options from a capture marker.
|
|
249
|
+
*
|
|
250
|
+
* @param node The node to extract variadic options from
|
|
251
|
+
* @returns The VariadicOptions, or undefined if not a variadic capture
|
|
252
|
+
*/
|
|
253
|
+
static getVariadicOptions(node: J): VariadicOptions | undefined {
|
|
254
|
+
for (const marker of node.markers.markers) {
|
|
255
|
+
if (marker instanceof CaptureMarker) {
|
|
256
|
+
return marker.variadicOptions;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
return undefined;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* Checks if a statement is an ExpressionStatement wrapping a capture identifier.
|
|
264
|
+
* When a capture placeholder appears in statement position, the parser wraps it as
|
|
265
|
+
* an ExpressionStatement. This method unwraps it to get the identifier.
|
|
266
|
+
*
|
|
267
|
+
* @param stmt The statement to check
|
|
268
|
+
* @returns The unwrapped capture identifier, or the original statement if not wrapped
|
|
269
|
+
*/
|
|
270
|
+
static unwrapStatementCapture(stmt: J): J {
|
|
271
|
+
// Check if it's an ExpressionStatement containing a capture identifier
|
|
272
|
+
if (stmt.kind === JS.Kind.ExpressionStatement) {
|
|
273
|
+
const exprStmt = stmt as JS.ExpressionStatement;
|
|
274
|
+
if (exprStmt.expression?.kind === J.Kind.Identifier) {
|
|
275
|
+
const identifier = exprStmt.expression as J.Identifier;
|
|
276
|
+
// Check if this is a capture placeholder
|
|
277
|
+
if (identifier.simpleName?.startsWith(this.CAPTURE_PREFIX)) {
|
|
278
|
+
return identifier;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
return stmt;
|
|
283
|
+
}
|
|
284
|
+
}
|
|
@@ -1,265 +0,0 @@
|
|
|
1
|
-
import { Cursor, Tree } from '..';
|
|
2
|
-
import { J } from '../java';
|
|
3
|
-
/**
|
|
4
|
-
* Capture specification for pattern matching.
|
|
5
|
-
* Represents a placeholder in a template pattern that can capture a part of the AST.
|
|
6
|
-
*/
|
|
7
|
-
export interface Capture {
|
|
8
|
-
/**
|
|
9
|
-
* The name of the capture, used to retrieve the captured node later.
|
|
10
|
-
*/
|
|
11
|
-
name: string;
|
|
12
|
-
}
|
|
13
|
-
/**
|
|
14
|
-
* Creates a capture specification for use in template patterns.
|
|
15
|
-
*
|
|
16
|
-
* @param name Optional name for the capture. If not provided, an auto-generated name is used.
|
|
17
|
-
* @returns A Capture object
|
|
18
|
-
*
|
|
19
|
-
* @example
|
|
20
|
-
* // Named inline captures
|
|
21
|
-
* const pattern = pattern`${capture('left')} + ${capture('right')}`;
|
|
22
|
-
*
|
|
23
|
-
* // Unnamed captures
|
|
24
|
-
* const {left, right} = {left: capture(), right: capture()};
|
|
25
|
-
* const pattern = pattern`${left} + ${right}`;
|
|
26
|
-
*
|
|
27
|
-
* // Repeated patterns using the same capture
|
|
28
|
-
* const expr = capture('expr');
|
|
29
|
-
* const redundantOr = pattern`${expr} || ${expr}`;
|
|
30
|
-
*/
|
|
31
|
-
export declare function capture(name?: string): Capture;
|
|
32
|
-
export declare namespace capture {
|
|
33
|
-
var nextUnnamedId: number;
|
|
34
|
-
}
|
|
35
|
-
/**
|
|
36
|
-
* Concise alias for `capture`. Works well for inline captures in patterns and templates.
|
|
37
|
-
*
|
|
38
|
-
* @param name Optional name for the capture. If not provided, an auto-generated name is used.
|
|
39
|
-
* @returns A Capture object
|
|
40
|
-
*
|
|
41
|
-
* @example
|
|
42
|
-
* // Inline captures with _ alias
|
|
43
|
-
* pattern`isDate(${_('dateArg')})`
|
|
44
|
-
* template`${_('dateArg')} instanceof Date`
|
|
45
|
-
*/
|
|
46
|
-
export declare const _: typeof capture;
|
|
47
|
-
/**
|
|
48
|
-
* Configuration options for patterns.
|
|
49
|
-
*/
|
|
50
|
-
export interface PatternOptions {
|
|
51
|
-
/**
|
|
52
|
-
* Import statements to provide type attribution context.
|
|
53
|
-
* These are prepended to the pattern when parsing to ensure proper type information.
|
|
54
|
-
*/
|
|
55
|
-
imports?: string[];
|
|
56
|
-
/**
|
|
57
|
-
* NPM dependencies required for import resolution and type attribution.
|
|
58
|
-
* Maps package names to version specifiers (e.g., { 'util': '^1.0.0' }).
|
|
59
|
-
* The template engine will create a package.json with these dependencies.
|
|
60
|
-
*/
|
|
61
|
-
dependencies?: Record<string, string>;
|
|
62
|
-
}
|
|
63
|
-
/**
|
|
64
|
-
* Represents a pattern that can be matched against AST nodes.
|
|
65
|
-
*/
|
|
66
|
-
export declare class Pattern {
|
|
67
|
-
readonly templateParts: TemplateStringsArray;
|
|
68
|
-
readonly captures: Capture[];
|
|
69
|
-
private _options;
|
|
70
|
-
/**
|
|
71
|
-
* Gets the configuration options for this pattern.
|
|
72
|
-
* @readonly
|
|
73
|
-
*/
|
|
74
|
-
get options(): Readonly<PatternOptions>;
|
|
75
|
-
/**
|
|
76
|
-
* Creates a new pattern from template parts and captures.
|
|
77
|
-
*
|
|
78
|
-
* @param templateParts The string parts of the template
|
|
79
|
-
* @param captures The captures between the string parts
|
|
80
|
-
*/
|
|
81
|
-
constructor(templateParts: TemplateStringsArray, captures: Capture[]);
|
|
82
|
-
/**
|
|
83
|
-
* Configures this pattern with additional options.
|
|
84
|
-
*
|
|
85
|
-
* @param options Configuration options
|
|
86
|
-
* @returns This pattern for method chaining
|
|
87
|
-
*
|
|
88
|
-
* @example
|
|
89
|
-
* pattern`isDate(${capture('date')})`
|
|
90
|
-
* .configure({
|
|
91
|
-
* imports: ['import { isDate } from "util"'],
|
|
92
|
-
* dependencies: { 'util': '^1.0.0' }
|
|
93
|
-
* })
|
|
94
|
-
*/
|
|
95
|
-
configure(options: PatternOptions): Pattern;
|
|
96
|
-
/**
|
|
97
|
-
* Creates a matcher for this pattern against a specific AST node.
|
|
98
|
-
*
|
|
99
|
-
* @param ast The AST node to match against
|
|
100
|
-
* @returns A Matcher object
|
|
101
|
-
*/
|
|
102
|
-
match(ast: J): Promise<MatchResult | undefined>;
|
|
103
|
-
}
|
|
104
|
-
export declare class MatchResult implements Pick<Map<string, J>, "get"> {
|
|
105
|
-
private readonly bindings;
|
|
106
|
-
constructor(bindings?: Map<string, J>);
|
|
107
|
-
get(capture: Capture | string): J | undefined;
|
|
108
|
-
}
|
|
109
|
-
/**
|
|
110
|
-
* Tagged template function for creating patterns.
|
|
111
|
-
*
|
|
112
|
-
* @param strings The string parts of the template
|
|
113
|
-
* @param captures The captures between the string parts
|
|
114
|
-
* @returns A Pattern object
|
|
115
|
-
*
|
|
116
|
-
* @example
|
|
117
|
-
* // Using the same capture multiple times for repeated patterns
|
|
118
|
-
* const expr = capture('expr');
|
|
119
|
-
* const redundantOr = pattern`${expr} || ${expr}`;
|
|
120
|
-
*/
|
|
121
|
-
export declare function pattern(strings: TemplateStringsArray, ...captures: (Capture | string)[]): Pattern;
|
|
122
|
-
/**
|
|
123
|
-
* Valid parameter types for template literals.
|
|
124
|
-
* - Capture: For pattern matching and reuse
|
|
125
|
-
* - Tree: AST nodes to be inserted directly
|
|
126
|
-
* - Primitives: Values to be converted to literals
|
|
127
|
-
*/
|
|
128
|
-
export type TemplateParameter = Capture | Tree | string | number | boolean;
|
|
129
|
-
/**
|
|
130
|
-
* Configuration options for templates.
|
|
131
|
-
*/
|
|
132
|
-
export interface TemplateOptions {
|
|
133
|
-
/**
|
|
134
|
-
* Import statements to provide type attribution context.
|
|
135
|
-
* These are prepended to the template when parsing to ensure proper type information.
|
|
136
|
-
*/
|
|
137
|
-
imports?: string[];
|
|
138
|
-
/**
|
|
139
|
-
* NPM dependencies required for import resolution and type attribution.
|
|
140
|
-
* Maps package names to version specifiers (e.g., { 'util': '^1.0.0' }).
|
|
141
|
-
* The template engine will create a package.json with these dependencies.
|
|
142
|
-
*/
|
|
143
|
-
dependencies?: Record<string, string>;
|
|
144
|
-
}
|
|
145
|
-
/**
|
|
146
|
-
* Template for creating AST nodes.
|
|
147
|
-
*
|
|
148
|
-
* This class provides the public API for template generation.
|
|
149
|
-
* The actual templating logic is handled by the internal TemplateEngine.
|
|
150
|
-
*
|
|
151
|
-
* @example
|
|
152
|
-
* // Generate a literal AST node
|
|
153
|
-
* const result = template`2`.apply(cursor, coordinates);
|
|
154
|
-
*
|
|
155
|
-
* @example
|
|
156
|
-
* // Generate an AST node with a parameter
|
|
157
|
-
* const result = template`${capture()}`.apply(cursor, coordinates);
|
|
158
|
-
*/
|
|
159
|
-
export declare class Template {
|
|
160
|
-
private readonly templateParts;
|
|
161
|
-
private readonly parameters;
|
|
162
|
-
private options;
|
|
163
|
-
/**
|
|
164
|
-
* Creates a new template.
|
|
165
|
-
*
|
|
166
|
-
* @param templateParts The string parts of the template
|
|
167
|
-
* @param parameters The parameters between the string parts
|
|
168
|
-
*/
|
|
169
|
-
constructor(templateParts: TemplateStringsArray, parameters: Parameter[]);
|
|
170
|
-
/**
|
|
171
|
-
* Configures this template with additional options.
|
|
172
|
-
*
|
|
173
|
-
* @param options Configuration options
|
|
174
|
-
* @returns This template for method chaining
|
|
175
|
-
*
|
|
176
|
-
* @example
|
|
177
|
-
* template`isDate(${capture('date')})`
|
|
178
|
-
* .configure({
|
|
179
|
-
* imports: ['import { isDate } from "util"'],
|
|
180
|
-
* dependencies: { 'util': '^1.0.0' }
|
|
181
|
-
* })
|
|
182
|
-
*/
|
|
183
|
-
configure(options: TemplateOptions): Template;
|
|
184
|
-
/**
|
|
185
|
-
* Applies this template and returns the resulting tree.
|
|
186
|
-
*
|
|
187
|
-
* @param cursor The cursor pointing to the current location in the AST
|
|
188
|
-
* @param tree Input tree
|
|
189
|
-
* @param values values for parameters in template
|
|
190
|
-
* @returns A Promise resolving to the generated AST node
|
|
191
|
-
*/
|
|
192
|
-
apply(cursor: Cursor, tree: J, values?: Pick<Map<string, J>, 'get'>): Promise<J | undefined>;
|
|
193
|
-
}
|
|
194
|
-
export declare function template(strings: TemplateStringsArray, ...parameters: TemplateParameter[]): Template;
|
|
195
|
-
/**
|
|
196
|
-
* Parameter specification for template generation.
|
|
197
|
-
* Represents a placeholder in a template that will be replaced with a parameter value.
|
|
198
|
-
*/
|
|
199
|
-
interface Parameter {
|
|
200
|
-
/**
|
|
201
|
-
* The value to substitute into the template.
|
|
202
|
-
*/
|
|
203
|
-
value: any;
|
|
204
|
-
}
|
|
205
|
-
/**
|
|
206
|
-
* Represents a replacement rule that can match a pattern and apply a template.
|
|
207
|
-
*/
|
|
208
|
-
export interface RewriteRule {
|
|
209
|
-
/**
|
|
210
|
-
* Attempts to apply this rewrite rule to the given AST node.
|
|
211
|
-
*
|
|
212
|
-
* @param cursor The cursor context at the current position in the AST
|
|
213
|
-
* @param node The AST node to try matching and transforming
|
|
214
|
-
* @returns The transformed node if a pattern matched, or `undefined` if no pattern matched.
|
|
215
|
-
* When using in a visitor, always use the `|| node` pattern to return the original
|
|
216
|
-
* node when there's no match: `return await rule.tryOn(this.cursor, node) || node;`
|
|
217
|
-
*/
|
|
218
|
-
tryOn(cursor: Cursor, node: J): Promise<J | undefined>;
|
|
219
|
-
}
|
|
220
|
-
/**
|
|
221
|
-
* Configuration for a replacement rule.
|
|
222
|
-
*/
|
|
223
|
-
export interface RewriteConfig {
|
|
224
|
-
before: Pattern | Pattern[];
|
|
225
|
-
after: Template;
|
|
226
|
-
}
|
|
227
|
-
/**
|
|
228
|
-
* Creates a replacement rule using a capture context and configuration.
|
|
229
|
-
*
|
|
230
|
-
* @param builderFn Function that takes a capture context and returns before/after configuration
|
|
231
|
-
* @returns A replacement rule that can be applied to AST nodes
|
|
232
|
-
*
|
|
233
|
-
* @example
|
|
234
|
-
* // Single pattern
|
|
235
|
-
* const swapOperands = rewrite(() => ({
|
|
236
|
-
* before: pattern`${"left"} + ${"right"}`,
|
|
237
|
-
* after: template`${"right"} + ${"left"}`
|
|
238
|
-
* }));
|
|
239
|
-
*
|
|
240
|
-
* @example
|
|
241
|
-
* // Multiple patterns
|
|
242
|
-
* const normalizeComparisons = rewrite(() => ({
|
|
243
|
-
* before: [
|
|
244
|
-
* pattern`${"left"} == ${"right"}`,
|
|
245
|
-
* pattern`${"left"} === ${"right"}`
|
|
246
|
-
* ],
|
|
247
|
-
* after: template`${"left"} === ${"right"}`
|
|
248
|
-
* }));
|
|
249
|
-
*
|
|
250
|
-
* @example
|
|
251
|
-
* // Using in a visitor - IMPORTANT: use `|| node` to handle undefined when no match
|
|
252
|
-
* class MyVisitor extends JavaScriptVisitor<any> {
|
|
253
|
-
* override async visitBinary(binary: J.Binary, p: any): Promise<J | undefined> {
|
|
254
|
-
* const rule = rewrite(() => ({
|
|
255
|
-
* before: pattern`${capture('a')} + ${capture('b')}`,
|
|
256
|
-
* after: template`${capture('b')} + ${capture('a')}`
|
|
257
|
-
* }));
|
|
258
|
-
* // tryOn() returns undefined if no pattern matches, so always use || node
|
|
259
|
-
* return await rule.tryOn(this.cursor, binary) || binary;
|
|
260
|
-
* }
|
|
261
|
-
* }
|
|
262
|
-
*/
|
|
263
|
-
export declare function rewrite(builderFn: () => RewriteConfig): RewriteRule;
|
|
264
|
-
export {};
|
|
265
|
-
//# sourceMappingURL=templating.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"templating.d.ts","sourceRoot":"","sources":["../../src/javascript/templating.ts"],"names":[],"mappings":"AAkBA,OAAO,EAAC,MAAM,EAAU,IAAI,EAAC,MAAM,IAAI,CAAC;AACxC,OAAO,EAAa,CAAC,EAAC,MAAM,SAAS,CAAC;AA2MtC;;;GAGG;AACH,MAAM,WAAW,OAAO;IACpB;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;CAChB;AASD;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,OAAO,CAAC,IAAI,CAAC,EAAE,MAAM,GAAG,OAAO,CAK9C;yBALe,OAAO;;;AAUvB;;;;;;;;;;GAUG;AACH,eAAO,MAAM,CAAC,gBAAU,CAAC;AAEzB;;GAEG;AACH,MAAM,WAAW,cAAc;IAC3B;;;OAGG;IACH,OA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