@openrewrite/rewrite 8.67.0-20251119-160338 → 8.67.0-20251120-075051
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/add-import.d.ts +21 -10
- package/dist/javascript/add-import.d.ts.map +1 -1
- package/dist/javascript/add-import.js +55 -21
- package/dist/javascript/add-import.js.map +1 -1
- package/dist/javascript/remove-import.d.ts +18 -14
- package/dist/javascript/remove-import.d.ts.map +1 -1
- package/dist/javascript/remove-import.js +37 -47
- package/dist/javascript/remove-import.js.map +1 -1
- package/dist/javascript/templating/capture.d.ts +3 -4
- package/dist/javascript/templating/capture.d.ts.map +1 -1
- package/dist/javascript/templating/capture.js +3 -3
- package/dist/javascript/templating/capture.js.map +1 -1
- package/dist/javascript/templating/comparator.d.ts +7 -1
- package/dist/javascript/templating/comparator.d.ts.map +1 -1
- package/dist/javascript/templating/comparator.js +45 -10
- package/dist/javascript/templating/comparator.js.map +1 -1
- package/dist/javascript/templating/pattern.d.ts +10 -11
- package/dist/javascript/templating/pattern.d.ts.map +1 -1
- package/dist/javascript/templating/pattern.js +18 -36
- package/dist/javascript/templating/pattern.js.map +1 -1
- package/dist/javascript/templating/rewrite.js +2 -2
- package/dist/javascript/templating/rewrite.js.map +1 -1
- package/dist/javascript/templating/template.d.ts +27 -13
- package/dist/javascript/templating/template.d.ts.map +1 -1
- package/dist/javascript/templating/template.js +31 -14
- package/dist/javascript/templating/template.js.map +1 -1
- package/dist/javascript/templating/types.d.ts +111 -15
- package/dist/javascript/templating/types.d.ts.map +1 -1
- package/dist/version.txt +1 -1
- package/package.json +1 -1
- package/src/javascript/add-import.ts +70 -27
- package/src/javascript/remove-import.ts +37 -46
- package/src/javascript/templating/capture.ts +7 -7
- package/src/javascript/templating/comparator.ts +50 -11
- package/src/javascript/templating/pattern.ts +32 -24
- package/src/javascript/templating/rewrite.ts +2 -2
- package/src/javascript/templating/template.ts +36 -18
- package/src/javascript/templating/types.ts +127 -16
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"template.d.ts","sourceRoot":"","sources":["../../../src/javascript/templating/template.ts"],"names":[],"mappings":"AAeA,OAAO,EAAC,MAAM,EAAE,IAAI,EAAC,MAAM,OAAO,CAAC;AACnC,OAAO,EAAC,CAAC,EAAC,MAAM,YAAY,CAAC;AAC7B,OAAO,EAAC,
|
|
1
|
+
{"version":3,"file":"template.d.ts","sourceRoot":"","sources":["../../../src/javascript/templating/template.ts"],"names":[],"mappings":"AAeA,OAAO,EAAC,MAAM,EAAE,IAAI,EAAC,MAAM,OAAO,CAAC;AACnC,OAAO,EAAC,CAAC,EAAC,MAAM,YAAY,CAAC;AAC7B,OAAO,EAAC,YAAY,EAAE,SAAS,EAAE,eAAe,EAAE,iBAAiB,EAAC,MAAM,SAAS,CAAC;AAOpF;;GAEG;AACH,KAAK,eAAe,GAAG;IACnB,IAAI,CAAC,EAAE,IAAI,CAAC;IACZ,GAAG,CAAC,EAAE,eAAe,CAAC,QAAQ,CAAC;IAC/B,IAAI,CAAC,EAAE,eAAe,CAAC,IAAI,CAAC;CAC/B,CAAC;AAEF,kBAAU,eAAe,CAAC;IAEtB,KAAY,QAAQ,GAAG,mBAAmB,GAAG,kBAAkB,GAAG,WAAW,CAAC;IAE9E,KAAY,IAAI;QACZ,MAAM,IAAA;QACN,KAAK,IAAA;QACL,OAAO,IAAA;KACV;CACJ;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,qBAAa,eAAe;IACxB,OAAO,CAAC,KAAK,CAAgB;IAC7B,OAAO,CAAC,MAAM,CAA2B;IAEzC;;;;;OAKG;IACH,IAAI,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI;IAcvB;;;;;OAKG;IACH,KAAK,CAAC,KAAK,EAAE,iBAAiB,GAAG,IAAI;IAWrC;;;;OAIG;IACH,KAAK,IAAI,QAAQ;CAiBpB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;AACH,qBAAa,QAAQ;IAWb,OAAO,CAAC,QAAQ,CAAC,aAAa;IAC9B,OAAO,CAAC,QAAQ,CAAC,UAAU;IAX/B,OAAO,CAAC,OAAO,CAAuB;IACtC,OAAO,CAAC,eAAe,CAAC,CAAI;IAE5B;;;;;OAKG;gBAEkB,aAAa,EAAE,oBAAoB,EACnC,UAAU,EAAE,SAAS,EAAE;IAI5C;;;;;;;;;;;;OAYG;IACH,MAAM,CAAC,OAAO,IAAI,eAAe;IAIjC;;;;;;;;;;;;OAYG;IACH,SAAS,CAAC,OAAO,EAAE,eAAe,GAAG,QAAQ;IAO7C;;;;;;;;;;;;OAYG;YACW,eAAe;IA+C7B;;;;;;;;;;;;;;;;;;;;;;OAsBG;IACG,KAAK,CAAC,IAAI,EAAE,CAAC,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,YAAY,GAAG,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC;CAuDvF;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuDG;AACH,wBAAgB,QAAQ,CAAC,OAAO,EAAE,oBAAoB,EAAE,GAAG,UAAU,EAAE,iBAAiB,EAAE,GAAG,QAAQ,CAQpG;AAED,YAAY,EAAC,eAAe,EAAC,CAAC"}
|
|
@@ -151,6 +151,17 @@ exports.TemplateBuilder = TemplateBuilder;
|
|
|
151
151
|
* template`bar(${invocation.arguments.elements[0].element})` // Access array elements
|
|
152
152
|
*/
|
|
153
153
|
class Template {
|
|
154
|
+
/**
|
|
155
|
+
* Creates a new template.
|
|
156
|
+
*
|
|
157
|
+
* @param templateParts The string parts of the template
|
|
158
|
+
* @param parameters The parameters between the string parts
|
|
159
|
+
*/
|
|
160
|
+
constructor(templateParts, parameters) {
|
|
161
|
+
this.templateParts = templateParts;
|
|
162
|
+
this.parameters = parameters;
|
|
163
|
+
this.options = {};
|
|
164
|
+
}
|
|
154
165
|
/**
|
|
155
166
|
* Creates a new builder for constructing templates programmatically.
|
|
156
167
|
*
|
|
@@ -167,17 +178,6 @@ class Template {
|
|
|
167
178
|
static builder() {
|
|
168
179
|
return new TemplateBuilder();
|
|
169
180
|
}
|
|
170
|
-
/**
|
|
171
|
-
* Creates a new template.
|
|
172
|
-
*
|
|
173
|
-
* @param templateParts The string parts of the template
|
|
174
|
-
* @param parameters The parameters between the string parts
|
|
175
|
-
*/
|
|
176
|
-
constructor(templateParts, parameters) {
|
|
177
|
-
this.templateParts = templateParts;
|
|
178
|
-
this.parameters = parameters;
|
|
179
|
-
this.options = {};
|
|
180
|
-
}
|
|
181
181
|
/**
|
|
182
182
|
* Configures this template with additional options.
|
|
183
183
|
*
|
|
@@ -246,13 +246,30 @@ class Template {
|
|
|
246
246
|
/**
|
|
247
247
|
* Applies this template and returns the resulting tree.
|
|
248
248
|
*
|
|
249
|
+
* @param tree Input tree to transform
|
|
249
250
|
* @param cursor The cursor pointing to the current location in the AST
|
|
250
|
-
* @param
|
|
251
|
-
* @param values values for parameters in template
|
|
251
|
+
* @param options Optional configuration including values for parameters
|
|
252
252
|
* @returns A Promise resolving to the generated AST node
|
|
253
|
+
*
|
|
254
|
+
* @example
|
|
255
|
+
* ```typescript
|
|
256
|
+
* // Simple application without values
|
|
257
|
+
* const result = await tmpl.apply(node, cursor);
|
|
258
|
+
*
|
|
259
|
+
* // With values from pattern match
|
|
260
|
+
* const match = await pat.match(node, cursor);
|
|
261
|
+
* const result = await tmpl.apply(node, cursor, { values: match });
|
|
262
|
+
*
|
|
263
|
+
* // With explicit values
|
|
264
|
+
* const result = await tmpl.apply(node, cursor, {
|
|
265
|
+
* values: { x: someNode, y: anotherNode }
|
|
266
|
+
* });
|
|
267
|
+
* ```
|
|
253
268
|
*/
|
|
254
|
-
apply(
|
|
269
|
+
apply(tree, cursor, options) {
|
|
255
270
|
return __awaiter(this, void 0, void 0, function* () {
|
|
271
|
+
// Extract values from options
|
|
272
|
+
const values = options === null || options === void 0 ? void 0 : options.values;
|
|
256
273
|
// Normalize the values map: convert any Capture keys to string keys
|
|
257
274
|
let normalizedValues;
|
|
258
275
|
let wrappersMap = new Map();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"template.js","sourceRoot":"","sources":["../../../src/javascript/templating/template.ts"],"names":[],"mappings":";;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"template.js","sourceRoot":"","sources":["../../../src/javascript/templating/template.ts"],"names":[],"mappings":";;;;;;;;;;;;AAqaA,4BAQC;AA3ZD,uCAAsC;AACtC,mCAA8E;AAC9E,uCAA+D;AAC/D,qCAAwC;AAYxC,IAAU,eAAe,CASxB;AATD,WAAU,eAAe;IAIrB,IAAY,IAIX;IAJD,WAAY,IAAI;QACZ,mCAAM,CAAA;QACN,iCAAK,CAAA;QACL,qCAAO,CAAA;IACX,CAAC,EAJW,IAAI,GAAJ,oBAAI,KAAJ,oBAAI,QAIf;AACL,CAAC,EATS,eAAe,KAAf,eAAe,QASxB;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAa,eAAe;IAA5B;QACY,UAAK,GAAa,EAAE,CAAC;QACrB,WAAM,GAAwB,EAAE,CAAC;IA6D7C,CAAC;IA3DG;;;;;OAKG;IACH,IAAI,CAAC,GAAW;QACZ,0EAA0E;QAC1E,IAAI,IAAI,CAAC,MAAM,CAAC,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,EAAE,CAAC;YACzC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;QACxB,CAAC;QACD,6CAA6C;QAC7C,IAAI,IAAI,CAAC,KAAK,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YAC1B,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QACzB,CAAC;aAAM,CAAC;YACJ,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,IAAI,GAAG,CAAC;QAC7C,CAA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A+B;IACtF,8FAA8F;IAC9F,MAAM,mBAAmB,GAAG,UAAU,CAAC,GAAG,CAAC,KAAK,CAAC,EAAE;QAC/C,uDAAuD;QACvD,OAAO,EAAC,KAAK,EAAE,KAAK,EAAC,CAAC;IAC1B,CAAC,CAAC,CAAC;IAEH,OAAO,IAAI,QAAQ,CAAC,OAAO,EAAE,mBAAmB,CAAC,CAAC;AACtD,CAAC"}
|
|
@@ -16,10 +16,45 @@ export interface VariadicOptions {
|
|
|
16
16
|
*/
|
|
17
17
|
max?: number;
|
|
18
18
|
}
|
|
19
|
+
/**
|
|
20
|
+
* Read-only access to captures matched so far during pattern matching.
|
|
21
|
+
* Provides a consistent interface with MatchResult for looking up captured values.
|
|
22
|
+
*/
|
|
23
|
+
export interface CaptureMap {
|
|
24
|
+
/**
|
|
25
|
+
* Gets the value of a capture by Capture object or name.
|
|
26
|
+
* Returns undefined if the capture hasn't been matched yet.
|
|
27
|
+
*/
|
|
28
|
+
get<T>(capture: Capture<T>): T | undefined;
|
|
29
|
+
get(capture: string): any;
|
|
30
|
+
/**
|
|
31
|
+
* Checks if a capture has been matched.
|
|
32
|
+
*/
|
|
33
|
+
has(capture: Capture | string): boolean;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Context passed to capture constraint functions.
|
|
37
|
+
* Provides access to the cursor for AST navigation and previously matched captures.
|
|
38
|
+
*/
|
|
39
|
+
export interface CaptureConstraintContext {
|
|
40
|
+
/**
|
|
41
|
+
* The cursor pointing to the node being matched.
|
|
42
|
+
* Allows navigating the AST (parent, root, etc.).
|
|
43
|
+
*/
|
|
44
|
+
cursor: Cursor;
|
|
45
|
+
/**
|
|
46
|
+
* Read-only view of values captured so far in the matching process.
|
|
47
|
+
* Allows constraints to depend on previous captures.
|
|
48
|
+
* Returns undefined for captures that haven't been processed yet.
|
|
49
|
+
*/
|
|
50
|
+
captures: CaptureMap;
|
|
51
|
+
}
|
|
19
52
|
/**
|
|
20
53
|
* Constraint function for captures.
|
|
21
|
-
*
|
|
22
|
-
*
|
|
54
|
+
*
|
|
55
|
+
* Receives the node being validated and a context providing access to:
|
|
56
|
+
* - cursor: For navigating the AST
|
|
57
|
+
* - captures: For accessing previously matched captures
|
|
23
58
|
*
|
|
24
59
|
* For non-variadic captures: use ConstraintFunction<T> where T is the node type
|
|
25
60
|
* For variadic captures: use ConstraintFunction<T[]> where T[] is the array type
|
|
@@ -27,7 +62,7 @@ export interface VariadicOptions {
|
|
|
27
62
|
* When used with variadic captures, the cursor points to the nearest common parent
|
|
28
63
|
* of the captured elements.
|
|
29
64
|
*/
|
|
30
|
-
export type ConstraintFunction<T> = (node: T,
|
|
65
|
+
export type ConstraintFunction<T> = (node: T, context: CaptureConstraintContext) => boolean;
|
|
31
66
|
/**
|
|
32
67
|
* Options for the capture function.
|
|
33
68
|
*
|
|
@@ -36,39 +71,45 @@ export type ConstraintFunction<T> = (node: T, cursor: Cursor) => boolean;
|
|
|
36
71
|
* - For regular captures: constraint receives a single node of type T
|
|
37
72
|
* - For variadic captures: constraint receives an array of nodes of type T[]
|
|
38
73
|
*
|
|
39
|
-
* The
|
|
40
|
-
* context-aware validation during pattern matching.
|
|
74
|
+
* The context parameter provides access to the cursor and previously matched captures.
|
|
41
75
|
*/
|
|
42
76
|
export interface CaptureOptions<T = any> {
|
|
43
77
|
name?: string;
|
|
44
78
|
variadic?: boolean | VariadicOptions;
|
|
45
79
|
/**
|
|
46
80
|
* Optional constraint function that validates whether a captured node should be accepted.
|
|
47
|
-
* The function
|
|
81
|
+
* The function receives:
|
|
48
82
|
* - node: The captured node (or array of nodes for variadic captures)
|
|
49
|
-
* -
|
|
50
|
-
*
|
|
51
|
-
* Functions can choose to accept just the node parameter if they don't need the cursor.
|
|
83
|
+
* - context: Provides access to cursor and previously matched captures
|
|
52
84
|
*
|
|
53
85
|
* @param node The captured node to validate
|
|
54
|
-
* @param cursor
|
|
86
|
+
* @param context Provides cursor for AST navigation and previously matched captures
|
|
55
87
|
* @returns true if the capture should be accepted, false otherwise
|
|
56
88
|
*
|
|
57
89
|
* @example
|
|
58
90
|
* ```typescript
|
|
59
|
-
* // Simple node validation
|
|
91
|
+
* // Simple node validation
|
|
60
92
|
* capture<J.Literal>('size', {
|
|
61
93
|
* constraint: (node) => typeof node.value === 'number' && node.value > 100
|
|
62
94
|
* })
|
|
63
95
|
*
|
|
64
|
-
* // Context-aware validation
|
|
96
|
+
* // Context-aware validation using cursor
|
|
65
97
|
* capture<J.MethodInvocation>('method', {
|
|
66
|
-
* constraint: (node,
|
|
98
|
+
* constraint: (node, context) => {
|
|
67
99
|
* if (!node.name.simpleName.startsWith('get')) return false;
|
|
68
|
-
* const cls = cursor.firstEnclosing(isClassDeclaration);
|
|
100
|
+
* const cls = context.cursor.firstEnclosing(isClassDeclaration);
|
|
69
101
|
* return cls?.name.simpleName === 'ApiController';
|
|
70
102
|
* }
|
|
71
103
|
* })
|
|
104
|
+
*
|
|
105
|
+
* // Validation depending on previous captures
|
|
106
|
+
* const min = capture('min');
|
|
107
|
+
* const max = capture('max', {
|
|
108
|
+
* constraint: (node, context) => {
|
|
109
|
+
* const minVal = context.captures.get(min);
|
|
110
|
+
* return minVal && node.value > minVal.value;
|
|
111
|
+
* }
|
|
112
|
+
* })
|
|
72
113
|
* ```
|
|
73
114
|
*/
|
|
74
115
|
constraint?: ConstraintFunction<T>;
|
|
@@ -77,9 +118,37 @@ export interface CaptureOptions<T = any> {
|
|
|
77
118
|
* a preamble declaring the capture identifier with this type annotation, allowing
|
|
78
119
|
* the TypeScript parser/compiler to produce a properly type-attributed AST.
|
|
79
120
|
*
|
|
121
|
+
* **Why Use Type Attribution:**
|
|
122
|
+
* When matching against TypeScript code with type information, providing a type ensures
|
|
123
|
+
* the pattern's AST has matching type attribution, which can be important for:
|
|
124
|
+
* - Semantic matching based on types
|
|
125
|
+
* - Matching code that depends on type inference
|
|
126
|
+
* - Ensuring pattern parses with correct type context
|
|
127
|
+
*
|
|
80
128
|
* Can be specified as:
|
|
81
|
-
* - A string type annotation (e.g., "boolean", "string", "number")
|
|
129
|
+
* - A string type annotation (e.g., "boolean", "string", "number", "Promise<any>", "User[]")
|
|
82
130
|
* - A Type instance from the AST (the type will be inferred from the Type)
|
|
131
|
+
*
|
|
132
|
+
* @example
|
|
133
|
+
* ```typescript
|
|
134
|
+
* // Match promise chains with proper type attribution
|
|
135
|
+
* const chain = capture({
|
|
136
|
+
* name: 'chain',
|
|
137
|
+
* type: 'Promise<any>', // TypeScript will attribute this as Promise type
|
|
138
|
+
* constraint: (call: J.MethodInvocation) => {
|
|
139
|
+
* // Validate promise chain structure
|
|
140
|
+
* return call.name.simpleName === 'then';
|
|
141
|
+
* }
|
|
142
|
+
* });
|
|
143
|
+
* pattern`${chain}.catch(err => console.log(err))`
|
|
144
|
+
*
|
|
145
|
+
* // Match arrays with type annotation
|
|
146
|
+
* const items = capture({
|
|
147
|
+
* name: 'items',
|
|
148
|
+
* type: 'number[]', // Array of numbers
|
|
149
|
+
* });
|
|
150
|
+
* pattern`${items}.map(x => x * 2)`
|
|
151
|
+
* ```
|
|
83
152
|
*/
|
|
84
153
|
type?: string | Type;
|
|
85
154
|
}
|
|
@@ -360,6 +429,33 @@ export interface TemplateOptions {
|
|
|
360
429
|
*/
|
|
361
430
|
dependencies?: Record<string, string>;
|
|
362
431
|
}
|
|
432
|
+
/**
|
|
433
|
+
* Options for template application.
|
|
434
|
+
*/
|
|
435
|
+
export interface ApplyOptions {
|
|
436
|
+
/**
|
|
437
|
+
* Values for parameters in the template.
|
|
438
|
+
* Can be a Map, MatchResult, or plain object with capture names as keys.
|
|
439
|
+
*
|
|
440
|
+
* @example
|
|
441
|
+
* ```typescript
|
|
442
|
+
* // Using MatchResult from pattern matching
|
|
443
|
+
* const match = await pattern.match(node, cursor);
|
|
444
|
+
* await template.apply(node, cursor, { values: match });
|
|
445
|
+
*
|
|
446
|
+
* // Using a Map
|
|
447
|
+
* await template.apply(node, cursor, {
|
|
448
|
+
* values: new Map([['x', someNode]])
|
|
449
|
+
* });
|
|
450
|
+
*
|
|
451
|
+
* // Using a plain object
|
|
452
|
+
* await template.apply(node, cursor, {
|
|
453
|
+
* values: { x: someNode }
|
|
454
|
+
* });
|
|
455
|
+
* ```
|
|
456
|
+
*/
|
|
457
|
+
values?: Map<Capture | string, J> | MatchResult | Record<string, J>;
|
|
458
|
+
}
|
|
363
459
|
/**
|
|
364
460
|
* Represents a replacement rule that can match a pattern and apply a template.
|
|
365
461
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/javascript/templating/types.ts"],"names":[],"mappings":"AAeA,OAAO,EAAC,MAAM,EAAE,IAAI,EAAC,MAAM,OAAO,CAAC;AACnC,OAAO,EAAC,CAAC,EAAE,IAAI,EAAC,MAAM,YAAY,CAAC;AACnC,OAAO,KAAK,EAAC,OAAO,EAAC,MAAM,WAAW,CAAC;AACvC,OAAO,KAAK,EAAC,QAAQ,EAAC,MAAM,YAAY,CAAC;AACzC,OAAO,KAAK,EAAC,YAAY,EAAE,OAAO,EAAC,MAAM,WAAW,CAAC;AAErD;;GAEG;AACH,MAAM,WAAW,eAAe;IAC5B;;OAEG;IACH,GAAG,CAAC,EAAE,MAAM,CAAC;IAEb;;OAEG;IACH,GAAG,CAAC,EAAE,MAAM,CAAC;CAChB;AAED
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/javascript/templating/types.ts"],"names":[],"mappings":"AAeA,OAAO,EAAC,MAAM,EAAE,IAAI,EAAC,MAAM,OAAO,CAAC;AACnC,OAAO,EAAC,CAAC,EAAE,IAAI,EAAC,MAAM,YAAY,CAAC;AACnC,OAAO,KAAK,EAAC,OAAO,EAAC,MAAM,WAAW,CAAC;AACvC,OAAO,KAAK,EAAC,QAAQ,EAAC,MAAM,YAAY,CAAC;AACzC,OAAO,KAAK,EAAC,YAAY,EAAE,OAAO,EAAC,MAAM,WAAW,CAAC;AAErD;;GAEG;AACH,M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pB,WAAW,EAAE,GAAG,CAAC;QACjB,KAAK,CAAC,EAAE,MAAM,CAAC;KAClB,CAAC,CAAC;IAEH;;OAEG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;CACpB;AAED;;;GAGG;AACH,MAAM,WAAW,WAAW;IACxB;;;;;OAKG;IACH,GAAG,CAAC,OAAO,EAAE,MAAM,GAAG,GAAG,CAAC;IAE1B,GAAG,CAAC,CAAC,EAAE,OAAO,EAAE,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,GAAG,SAAS,CAAC;CAC9C;AAED;;;GAGG;AACH,MAAM,WAAW,kBAAkB;IAC/B;;OAEG;IACH,OAAO,EAAE,OAAO,CAAC;IAEjB;;;OAGG;IACH,MAAM,CAAC,EAAE,WAAW,CAAC;IAErB;;OAEG;IACH,WAAW,CAAC,EAAE,gBAAgB,CAAC;IAE/B;;OAEG;IACH,QAAQ,CAAC,EAAE,aAAa,EAAE,CAAC;CAC9B"}
|
package/dist/version.txt
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
8.67.0-
|
|
1
|
+
8.67.0-20251120-075051
|
package/package.json
CHANGED
|
@@ -13,23 +13,30 @@ export enum ImportStyle {
|
|
|
13
13
|
}
|
|
14
14
|
|
|
15
15
|
export interface AddImportOptions {
|
|
16
|
-
/** The module name (e.g., 'fs') to import from */
|
|
17
|
-
|
|
16
|
+
/** The module name (e.g., 'fs', 'react') to import from */
|
|
17
|
+
module: string;
|
|
18
18
|
|
|
19
19
|
/** Optionally, the specific member to import from the module.
|
|
20
20
|
* If not specified, adds a default import or namespace import.
|
|
21
21
|
* Special values:
|
|
22
|
-
* - 'default': Adds a default import from the
|
|
23
|
-
* When using 'default', the `alias` parameter is required.
|
|
22
|
+
* - 'default': Adds a default import from the module.
|
|
23
|
+
* When using 'default', the `alias` parameter is required.
|
|
24
|
+
* Cannot be combined with `sideEffectOnly`. */
|
|
24
25
|
member?: string;
|
|
25
26
|
|
|
26
27
|
/** Optional alias for the imported member.
|
|
27
|
-
* Required when member is 'default'.
|
|
28
|
+
* Required when member is 'default'.
|
|
29
|
+
* Cannot be combined with `sideEffectOnly`. */
|
|
28
30
|
alias?: string;
|
|
29
31
|
|
|
30
|
-
/** If true, only add the import if the member is actually used in the file. Default: true
|
|
32
|
+
/** If true, only add the import if the member is actually used in the file. Default: true
|
|
33
|
+
* Cannot be combined with `sideEffectOnly`. */
|
|
31
34
|
onlyIfReferenced?: boolean;
|
|
32
35
|
|
|
36
|
+
/** If true, adds a side-effect import without bindings (e.g., `import 'module'` or `require('module')`).
|
|
37
|
+
* Cannot be combined with `member`, `alias`, or `onlyIfReferenced`. */
|
|
38
|
+
sideEffectOnly?: boolean;
|
|
39
|
+
|
|
33
40
|
/** Optional import style to use. If not specified, auto-detects from file and existing imports */
|
|
34
41
|
style?: ImportStyle;
|
|
35
42
|
}
|
|
@@ -41,15 +48,19 @@ export interface AddImportOptions {
|
|
|
41
48
|
*
|
|
42
49
|
* @example
|
|
43
50
|
* // Add a named import
|
|
44
|
-
* maybeAddImport(visitor, {
|
|
51
|
+
* maybeAddImport(visitor, { module: 'fs', member: 'readFile' });
|
|
45
52
|
*
|
|
46
53
|
* @example
|
|
47
54
|
* // Add a default import using the 'default' member specifier
|
|
48
|
-
* maybeAddImport(visitor, {
|
|
55
|
+
* maybeAddImport(visitor, { module: 'react', member: 'default', alias: 'React' });
|
|
49
56
|
*
|
|
50
57
|
* @example
|
|
51
58
|
* // Add a default import (legacy way, without specifying member)
|
|
52
|
-
* maybeAddImport(visitor, {
|
|
59
|
+
* maybeAddImport(visitor, { module: 'react', alias: 'React' });
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* // Add a side-effect import
|
|
63
|
+
* maybeAddImport(visitor, { module: 'core-js/stable', sideEffectOnly: true });
|
|
53
64
|
*/
|
|
54
65
|
export function maybeAddImport(
|
|
55
66
|
visitor: JavaScriptVisitor<any>,
|
|
@@ -57,9 +68,10 @@ export function maybeAddImport(
|
|
|
57
68
|
) {
|
|
58
69
|
for (const v of visitor.afterVisit || []) {
|
|
59
70
|
if (v instanceof AddImport &&
|
|
60
|
-
v.
|
|
71
|
+
v.module === options.module &&
|
|
61
72
|
v.member === options.member &&
|
|
62
|
-
v.alias === options.alias
|
|
73
|
+
v.alias === options.alias &&
|
|
74
|
+
v.sideEffectOnly === (options.sideEffectOnly ?? false)) {
|
|
63
75
|
return;
|
|
64
76
|
}
|
|
65
77
|
}
|
|
@@ -67,10 +79,11 @@ export function maybeAddImport(
|
|
|
67
79
|
}
|
|
68
80
|
|
|
69
81
|
export class AddImport<P> extends JavaScriptVisitor<P> {
|
|
70
|
-
readonly
|
|
82
|
+
readonly module: string;
|
|
71
83
|
readonly member?: string;
|
|
72
84
|
readonly alias?: string;
|
|
73
85
|
readonly onlyIfReferenced: boolean;
|
|
86
|
+
readonly sideEffectOnly: boolean;
|
|
74
87
|
readonly style?: ImportStyle;
|
|
75
88
|
|
|
76
89
|
constructor(options: AddImportOptions) {
|
|
@@ -81,10 +94,24 @@ export class AddImport<P> extends JavaScriptVisitor<P> {
|
|
|
81
94
|
throw new Error("When member is 'default', the alias parameter is required");
|
|
82
95
|
}
|
|
83
96
|
|
|
84
|
-
|
|
97
|
+
// Validate that sideEffectOnly is not combined with incompatible options
|
|
98
|
+
if (options.sideEffectOnly) {
|
|
99
|
+
if (options.member !== undefined) {
|
|
100
|
+
throw new Error("Cannot combine sideEffectOnly with member");
|
|
101
|
+
}
|
|
102
|
+
if (options.alias !== undefined) {
|
|
103
|
+
throw new Error("Cannot combine sideEffectOnly with alias");
|
|
104
|
+
}
|
|
105
|
+
if (options.onlyIfReferenced !== undefined) {
|
|
106
|
+
throw new Error("Cannot combine sideEffectOnly with onlyIfReferenced");
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
this.module = options.module;
|
|
85
111
|
this.member = options.member;
|
|
86
112
|
this.alias = options.alias;
|
|
87
113
|
this.onlyIfReferenced = options.onlyIfReferenced ?? true;
|
|
114
|
+
this.sideEffectOnly = options.sideEffectOnly ?? false;
|
|
88
115
|
this.style = options.style;
|
|
89
116
|
}
|
|
90
117
|
|
|
@@ -238,7 +265,7 @@ export class AddImport<P> extends JavaScriptVisitor<P> {
|
|
|
238
265
|
if (moduleSpecifier) {
|
|
239
266
|
const moduleName = this.getModuleName(moduleSpecifier);
|
|
240
267
|
|
|
241
|
-
if (moduleName === this.
|
|
268
|
+
if (moduleName === this.module) {
|
|
242
269
|
const importClause = jsImport.importClause;
|
|
243
270
|
if (importClause?.namedBindings) {
|
|
244
271
|
if (importClause.namedBindings.kind === JS.Kind.NamedImports) {
|
|
@@ -264,7 +291,7 @@ export class AddImport<P> extends JavaScriptVisitor<P> {
|
|
|
264
291
|
if (initializer?.kind === J.Kind.MethodInvocation &&
|
|
265
292
|
this.isRequireCall(initializer as J.MethodInvocation)) {
|
|
266
293
|
const moduleName = this.getModuleNameFromRequire(initializer as J.MethodInvocation);
|
|
267
|
-
if (moduleName === this.
|
|
294
|
+
if (moduleName === this.module) {
|
|
268
295
|
return ImportStyle.CommonJS;
|
|
269
296
|
}
|
|
270
297
|
}
|
|
@@ -283,7 +310,8 @@ export class AddImport<P> extends JavaScriptVisitor<P> {
|
|
|
283
310
|
}
|
|
284
311
|
|
|
285
312
|
// If onlyIfReferenced is true, check if the identifier is actually used
|
|
286
|
-
|
|
313
|
+
// Skip this check for side-effect imports
|
|
314
|
+
if (!this.sideEffectOnly && this.onlyIfReferenced) {
|
|
287
315
|
const isReferenced = await this.checkIdentifierReferenced(compilationUnit);
|
|
288
316
|
if (!isReferenced) {
|
|
289
317
|
return compilationUnit;
|
|
@@ -294,8 +322,8 @@ export class AddImport<P> extends JavaScriptVisitor<P> {
|
|
|
294
322
|
const importStyle = this.determineImportStyle(compilationUnit);
|
|
295
323
|
|
|
296
324
|
// For ES6 named imports, check if we can merge into an existing import from the same module
|
|
297
|
-
// Don't try to merge default imports (member === 'default')
|
|
298
|
-
if (importStyle === ImportStyle.ES6Named && this.member !== undefined && this.member !== 'default') {
|
|
325
|
+
// Don't try to merge default imports (member === 'default') or side-effect imports
|
|
326
|
+
if (!this.sideEffectOnly && importStyle === ImportStyle.ES6Named && this.member !== undefined && this.member !== 'default') {
|
|
299
327
|
const mergedCu = await this.tryMergeIntoExistingImport(compilationUnit, p);
|
|
300
328
|
if (mergedCu !== compilationUnit) {
|
|
301
329
|
return mergedCu;
|
|
@@ -393,7 +421,7 @@ export class AddImport<P> extends JavaScriptVisitor<P> {
|
|
|
393
421
|
const moduleName = this.getModuleName(moduleSpecifier);
|
|
394
422
|
|
|
395
423
|
// Check if this is an import from our target module
|
|
396
|
-
if (moduleName !== this.
|
|
424
|
+
if (moduleName !== this.module) {
|
|
397
425
|
continue;
|
|
398
426
|
}
|
|
399
427
|
|
|
@@ -487,12 +515,21 @@ export class AddImport<P> extends JavaScriptVisitor<P> {
|
|
|
487
515
|
}
|
|
488
516
|
|
|
489
517
|
const moduleName = this.getModuleName(moduleSpecifier);
|
|
490
|
-
if (moduleName !== this.
|
|
518
|
+
if (moduleName !== this.module) {
|
|
491
519
|
return false;
|
|
492
520
|
}
|
|
493
521
|
|
|
494
522
|
const importClause = jsImport.importClause;
|
|
523
|
+
|
|
524
|
+
// Handle side-effect imports (no import clause)
|
|
495
525
|
if (!importClause) {
|
|
526
|
+
// If we're trying to add a side-effect import and one already exists, it's a match
|
|
527
|
+
return this.sideEffectOnly;
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
// If we're adding a side-effect import but there's an existing import with bindings,
|
|
531
|
+
// it's not a match (side-effect import should be separate)
|
|
532
|
+
if (this.sideEffectOnly) {
|
|
496
533
|
return false;
|
|
497
534
|
}
|
|
498
535
|
|
|
@@ -559,7 +596,7 @@ export class AddImport<P> extends JavaScriptVisitor<P> {
|
|
|
559
596
|
}
|
|
560
597
|
|
|
561
598
|
const moduleName = this.getModuleNameFromRequire(methodInv);
|
|
562
|
-
if (moduleName !== this.
|
|
599
|
+
if (moduleName !== this.module) {
|
|
563
600
|
return false;
|
|
564
601
|
}
|
|
565
602
|
|
|
@@ -645,7 +682,7 @@ export class AddImport<P> extends JavaScriptVisitor<P> {
|
|
|
645
682
|
await collector.visit(compilationUnit, new ExecutionContext());
|
|
646
683
|
|
|
647
684
|
// Check if our target import is used based on type attribution
|
|
648
|
-
const moduleMembers = usedImports.get(this.
|
|
685
|
+
const moduleMembers = usedImports.get(this.module);
|
|
649
686
|
if (!moduleMembers) {
|
|
650
687
|
return false;
|
|
651
688
|
}
|
|
@@ -662,20 +699,26 @@ export class AddImport<P> extends JavaScriptVisitor<P> {
|
|
|
662
699
|
const prefix = this.determineImportPrefix(compilationUnit, insertionIndex);
|
|
663
700
|
|
|
664
701
|
// Create the module specifier
|
|
702
|
+
// For side-effect imports, use emptySpace since space comes from LeftPadded.before
|
|
703
|
+
// For regular imports with import clause, use emptySpace since space comes from LeftPadded.before
|
|
704
|
+
// However, the printer expects the space after 'from' in the literal's prefix
|
|
665
705
|
const moduleSpecifier: J.Literal = {
|
|
666
706
|
id: randomId(),
|
|
667
707
|
kind: J.Kind.Literal,
|
|
668
|
-
prefix: singleSpace,
|
|
708
|
+
prefix: this.sideEffectOnly ? emptySpace : singleSpace,
|
|
669
709
|
markers: emptyMarkers,
|
|
670
|
-
value: `'${this.
|
|
671
|
-
valueSource: `'${this.
|
|
710
|
+
value: `'${this.module}'`,
|
|
711
|
+
valueSource: `'${this.module}'`,
|
|
672
712
|
unicodeEscapes: [],
|
|
673
713
|
type: undefined
|
|
674
714
|
};
|
|
675
715
|
|
|
676
716
|
let importClause: JS.ImportClause | undefined;
|
|
677
717
|
|
|
678
|
-
if (this.
|
|
718
|
+
if (this.sideEffectOnly) {
|
|
719
|
+
// Side-effect import: import 'module'
|
|
720
|
+
importClause = undefined;
|
|
721
|
+
} else if (this.member === undefined || this.member === 'default') {
|
|
679
722
|
// Default import: import target from 'module'
|
|
680
723
|
// or: import alias from 'module' (when member === 'default')
|
|
681
724
|
const defaultName: J.Identifier = {
|
|
@@ -684,7 +727,7 @@ export class AddImport<P> extends JavaScriptVisitor<P> {
|
|
|
684
727
|
prefix: singleSpace,
|
|
685
728
|
markers: emptyMarkers,
|
|
686
729
|
annotations: [],
|
|
687
|
-
simpleName: this.alias || this.
|
|
730
|
+
simpleName: this.alias || this.module,
|
|
688
731
|
type: undefined,
|
|
689
732
|
fieldType: undefined
|
|
690
733
|
};
|