@openrewrite/rewrite 8.97.0 → 8.97.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +42 -0
- package/dist/java/visitor.d.ts +2 -0
- package/dist/java/visitor.d.ts.map +1 -1
- package/dist/java/visitor.js +7 -3
- package/dist/java/visitor.js.map +1 -1
- package/dist/javascript/add-import.d.ts +1 -6
- package/dist/javascript/add-import.d.ts.map +1 -1
- package/dist/javascript/add-import.js +90 -58
- package/dist/javascript/add-import.js.map +1 -1
- package/dist/javascript/comparator.d.ts +16 -10
- package/dist/javascript/comparator.d.ts.map +1 -1
- package/dist/javascript/comparator.js +10 -8
- package/dist/javascript/comparator.js.map +1 -1
- package/dist/javascript/package-json-parser.d.ts +2 -0
- package/dist/javascript/package-json-parser.d.ts.map +1 -1
- package/dist/javascript/package-json-parser.js +27 -15
- package/dist/javascript/package-json-parser.js.map +1 -1
- package/dist/javascript/package-manager.d.ts +13 -4
- package/dist/javascript/package-manager.d.ts.map +1 -1
- package/dist/javascript/package-manager.js +134 -17
- package/dist/javascript/package-manager.js.map +1 -1
- package/dist/javascript/parser.d.ts +2 -0
- package/dist/javascript/parser.d.ts.map +1 -1
- package/dist/javascript/parser.js +23 -58
- package/dist/javascript/parser.js.map +1 -1
- package/dist/javascript/print.d.ts.map +1 -1
- package/dist/javascript/print.js +1 -1
- package/dist/javascript/print.js.map +1 -1
- package/dist/javascript/recipes/change-method-target-to-import.d.ts +4 -0
- package/dist/javascript/recipes/change-method-target-to-import.d.ts.map +1 -1
- package/dist/javascript/recipes/change-method-target-to-import.js +20 -2
- package/dist/javascript/recipes/change-method-target-to-import.js.map +1 -1
- package/dist/javascript/recipes/dependencies.d.ts +19 -0
- package/dist/javascript/recipes/dependencies.d.ts.map +1 -1
- package/dist/javascript/recipes/dependencies.js +8 -0
- package/dist/javascript/recipes/dependencies.js.map +1 -1
- package/dist/javascript/recipes/order-imports.js +3 -3
- package/dist/javascript/recipes/order-imports.js.map +1 -1
- package/dist/javascript/remove-import.d.ts.map +1 -1
- package/dist/javascript/remove-import.js +115 -33
- package/dist/javascript/remove-import.js.map +1 -1
- package/dist/javascript/scope.d.ts +7 -0
- package/dist/javascript/scope.d.ts.map +1 -1
- package/dist/javascript/scope.js +23 -0
- package/dist/javascript/scope.js.map +1 -1
- package/dist/javascript/templating/bindings.d.ts.map +1 -1
- package/dist/javascript/templating/bindings.js +0 -4
- package/dist/javascript/templating/bindings.js.map +1 -1
- package/dist/javascript/templating/comparator.d.ts +32 -79
- package/dist/javascript/templating/comparator.d.ts.map +1 -1
- package/dist/javascript/templating/comparator.js +35 -628
- package/dist/javascript/templating/comparator.js.map +1 -1
- package/dist/javascript/templating/debug.d.ts +143 -0
- package/dist/javascript/templating/debug.d.ts.map +1 -0
- package/dist/javascript/templating/debug.js +769 -0
- package/dist/javascript/templating/debug.js.map +1 -0
- package/dist/javascript/templating/engine.d.ts +12 -3
- package/dist/javascript/templating/engine.d.ts.map +1 -1
- package/dist/javascript/templating/engine.js +135 -76
- package/dist/javascript/templating/engine.js.map +1 -1
- package/dist/javascript/templating/index.d.ts +1 -1
- package/dist/javascript/templating/index.d.ts.map +1 -1
- package/dist/javascript/templating/index.js.map +1 -1
- package/dist/javascript/templating/pattern.d.ts +44 -35
- package/dist/javascript/templating/pattern.d.ts.map +1 -1
- package/dist/javascript/templating/pattern.js +224 -238
- package/dist/javascript/templating/pattern.js.map +1 -1
- package/dist/javascript/templating/rewrite.d.ts +4 -0
- package/dist/javascript/templating/rewrite.d.ts.map +1 -1
- package/dist/javascript/templating/rewrite.js +5 -1
- package/dist/javascript/templating/rewrite.js.map +1 -1
- package/dist/javascript/templating/template.js +1 -1
- package/dist/javascript/templating/template.js.map +1 -1
- package/dist/javascript/templating/types.d.ts +38 -35
- package/dist/javascript/templating/types.d.ts.map +1 -1
- package/dist/javascript/templating/utils.d.ts.map +1 -1
- package/dist/javascript/templating/utils.js +18 -9
- package/dist/javascript/templating/utils.js.map +1 -1
- package/dist/javascript/tree.d.ts +3 -1
- package/dist/javascript/tree.d.ts.map +1 -1
- package/dist/javascript/tree.js +5 -0
- package/dist/javascript/tree.js.map +1 -1
- package/dist/json/tree.d.ts +1 -2
- package/dist/json/tree.d.ts.map +1 -1
- package/dist/json/tree.js +10 -7
- package/dist/json/tree.js.map +1 -1
- package/dist/rewrite-javascript-version.txt +1 -1
- package/dist/rpc/request/get-object.d.ts.map +1 -1
- package/dist/rpc/request/get-object.js +5 -3
- package/dist/rpc/request/get-object.js.map +1 -1
- package/dist/rpc/request/install-recipes.d.ts +10 -0
- package/dist/rpc/request/install-recipes.d.ts.map +1 -1
- package/dist/rpc/request/install-recipes.js +125 -51
- package/dist/rpc/request/install-recipes.js.map +1 -1
- package/dist/rpc/request/prepare-recipe.d.ts +1 -0
- package/dist/rpc/request/prepare-recipe.d.ts.map +1 -1
- package/dist/rpc/request/prepare-recipe.js +13 -6
- package/dist/rpc/request/prepare-recipe.js.map +1 -1
- package/dist/rpc/rewrite-rpc.d.ts +1 -1
- package/dist/rpc/rewrite-rpc.d.ts.map +1 -1
- package/dist/rpc/rewrite-rpc.js +2 -3
- package/dist/rpc/rewrite-rpc.js.map +1 -1
- package/dist/rpc/worker-transport.d.ts +7 -3
- package/dist/rpc/worker-transport.d.ts.map +1 -1
- package/dist/rpc/worker-transport.js +54 -41
- package/dist/rpc/worker-transport.js.map +1 -1
- package/dist/test/rewrite-test.d.ts +1 -16
- package/dist/test/rewrite-test.d.ts.map +1 -1
- package/dist/test/rewrite-test.js +7 -58
- package/dist/test/rewrite-test.js.map +1 -1
- package/dist/util.d.ts +16 -0
- package/dist/util.d.ts.map +1 -1
- package/dist/util.js +53 -0
- package/dist/util.js.map +1 -1
- package/dist/yaml/parser.js +8 -5
- package/dist/yaml/parser.js.map +1 -1
- package/dist/yaml/print.js +9 -3
- package/dist/yaml/print.js.map +1 -1
- package/package.json +1 -1
- package/src/java/visitor.ts +8 -3
- package/src/javascript/add-import.ts +98 -64
- package/src/javascript/comparator.ts +66 -53
- package/src/javascript/package-json-parser.ts +25 -16
- package/src/javascript/package-manager.ts +121 -15
- package/src/javascript/parser.ts +29 -81
- package/src/javascript/print.ts +1 -2
- package/src/javascript/recipes/change-method-target-to-import.ts +23 -3
- package/src/javascript/recipes/dependencies.ts +23 -0
- package/src/javascript/recipes/order-imports.ts +5 -5
- package/src/javascript/remove-import.ts +113 -41
- package/src/javascript/scope.ts +23 -0
- package/src/javascript/templating/bindings.ts +1 -6
- package/src/javascript/templating/comparator.ts +76 -766
- package/src/javascript/templating/debug.ts +806 -0
- package/src/javascript/templating/engine.ts +138 -77
- package/src/javascript/templating/index.ts +1 -0
- package/src/javascript/templating/pattern.ts +267 -270
- package/src/javascript/templating/rewrite.ts +5 -1
- package/src/javascript/templating/template.ts +1 -1
- package/src/javascript/templating/types.ts +40 -37
- package/src/javascript/templating/utils.ts +4 -12
- package/src/javascript/tree.ts +6 -1
- package/src/json/tree.ts +9 -7
- package/src/rpc/request/get-object.ts +34 -28
- package/src/rpc/request/install-recipes.ts +128 -52
- package/src/rpc/request/prepare-recipe.ts +18 -15
- package/src/rpc/rewrite-rpc.ts +2 -4
- package/src/rpc/worker-transport.ts +63 -43
- package/src/test/rewrite-test.ts +2 -59
- package/src/util.ts +58 -0
- package/src/yaml/parser.ts +9 -5
- package/src/yaml/print.ts +9 -3
package/src/rpc/rewrite-rpc.ts
CHANGED
|
@@ -49,7 +49,7 @@ import {InstallRecipes, InstallRecipesResponse} from "./request/install-recipes"
|
|
|
49
49
|
import {ParserInput} from "../parser";
|
|
50
50
|
import {ReferenceMap} from "../reference";
|
|
51
51
|
import {GetLanguages} from "./request/get-languages";
|
|
52
|
-
import type
|
|
52
|
+
import {endsTransfer, type WorkerPageChannel} from "./worker-transport";
|
|
53
53
|
|
|
54
54
|
// Each page is a round trip through both JSON-RPC stacks, so larger pages amortize it
|
|
55
55
|
export const DEFAULT_BATCH_SIZE = 5000;
|
|
@@ -256,8 +256,6 @@ export class RewriteRpc {
|
|
|
256
256
|
try {
|
|
257
257
|
// The codecs receive synchronously. Over the worker transport each page is pulled when the
|
|
258
258
|
// previous one runs out. Otherwise the whole transfer is fetched before any of it is read.
|
|
259
|
-
// A page ending in END_OF_OBJECT has no successor. The remote drops its transfer state
|
|
260
|
-
// when it sends that marker, so asking again would restart the transfer.
|
|
261
259
|
let pages: RpcObjectData[][] | (() => readonly RpcObjectData[]);
|
|
262
260
|
if (this.pageChannel) {
|
|
263
261
|
pages = this.pageChannel.pages(id, sourceFileType);
|
|
@@ -270,7 +268,7 @@ export class RewriteRpc {
|
|
|
270
268
|
new GetObject(id, sourceFileType),
|
|
271
269
|
);
|
|
272
270
|
pages.push(page);
|
|
273
|
-
} while (page
|
|
271
|
+
} while (!endsTransfer(page));
|
|
274
272
|
}
|
|
275
273
|
|
|
276
274
|
q = new RpcReceiveQueue(this.remoteRefs, sourceFileType, pages, this.logger,
|
|
@@ -62,11 +62,13 @@ function ensureCapacity(buffer: GrowableSharedArrayBuffer, length: number): void
|
|
|
62
62
|
export class PageAbortedError extends Error {
|
|
63
63
|
}
|
|
64
64
|
|
|
65
|
+
// The outbound buffer holds the JSON of a GetObject response's result or error
|
|
66
|
+
type Answer = { t: "answer", member: "result" | "error", last: boolean };
|
|
67
|
+
|
|
65
68
|
type ToMain =
|
|
66
69
|
{ t: "frame", bytes: Uint8Array } |
|
|
67
70
|
{ t: "pull", id: string, sourceFileType?: string } |
|
|
68
|
-
|
|
69
|
-
{ t: "serveError", message: string } |
|
|
71
|
+
Answer |
|
|
70
72
|
{ t: "served" };
|
|
71
73
|
|
|
72
74
|
type ToWorker = { t: "serve", params: { id: string, sourceFileType?: string } };
|
|
@@ -76,6 +78,12 @@ function frame(body: Uint8Array): Buffer {
|
|
|
76
78
|
return Buffer.concat([header, body]);
|
|
77
79
|
}
|
|
78
80
|
|
|
81
|
+
// No page follows one ending in END_OF_OBJECT. The remote drops its transfer state when it sends
|
|
82
|
+
// that page, so asking again would start the transfer over.
|
|
83
|
+
export function endsTransfer(page: readonly RpcObjectData[]): boolean {
|
|
84
|
+
return page.length === 0 || page[page.length - 1].state === RpcObjectState.END_OF_OBJECT;
|
|
85
|
+
}
|
|
86
|
+
|
|
79
87
|
/**
|
|
80
88
|
* Hands each JSON-RPC message from the worker to the main thread as one whole frame, so a frame the
|
|
81
89
|
* main thread writes for a page never lands between another frame's header and body.
|
|
@@ -128,7 +136,7 @@ export class WorkerPageChannel {
|
|
|
128
136
|
this.requestPage(id, sourceFileType);
|
|
129
137
|
}
|
|
130
138
|
const page = this.awaitPage();
|
|
131
|
-
if (page
|
|
139
|
+
if (!endsTransfer(page)) {
|
|
132
140
|
this.requestPage(id, sourceFileType);
|
|
133
141
|
}
|
|
134
142
|
return page;
|
|
@@ -168,7 +176,8 @@ export class WorkerPageChannel {
|
|
|
168
176
|
|
|
169
177
|
/**
|
|
170
178
|
* Runs `serve` for each object the remote asks for. Its sink blocks until the main thread has
|
|
171
|
-
* taken the previous page, and throws {@link PageAbortedError} once the remote aborts.
|
|
179
|
+
* taken the previous page, and throws {@link PageAbortedError} once the remote aborts. An error
|
|
180
|
+
* `serve` throws before its last page answers the remote's next request for the object.
|
|
172
181
|
*/
|
|
173
182
|
onServe(serve: (params: { id: string, sourceFileType?: string },
|
|
174
183
|
sink: (page: RpcObjectData[]) => void) => Promise<void>): void {
|
|
@@ -176,33 +185,38 @@ export class WorkerPageChannel {
|
|
|
176
185
|
if (m.t !== "serve") {
|
|
177
186
|
return;
|
|
178
187
|
}
|
|
179
|
-
|
|
188
|
+
let ended = false;
|
|
189
|
+
serve(m.params, page => {
|
|
190
|
+
ended = endsTransfer(page);
|
|
191
|
+
this.handOver("result", page, ended);
|
|
192
|
+
})
|
|
180
193
|
.catch(e => {
|
|
181
|
-
|
|
182
|
-
|
|
194
|
+
// No request follows the last page, so an error after it has nothing to answer
|
|
195
|
+
if (!(e instanceof PageAbortedError) && !ended) {
|
|
196
|
+
this.handOver("error", {code: -32603, message: String(e?.message ?? e)}, true);
|
|
183
197
|
}
|
|
184
198
|
})
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
199
|
+
// The main thread forgets the serve on "served", so this waits until the remote has the last answer
|
|
200
|
+
.then(() => this.awaitTaken())
|
|
201
|
+
.catch(e => {
|
|
202
|
+
if (!(e instanceof PageAbortedError)) {
|
|
203
|
+
throw e;
|
|
190
204
|
}
|
|
191
|
-
|
|
192
|
-
});
|
|
205
|
+
})
|
|
206
|
+
.finally(() => this.port.postMessage({t: "served"} satisfies ToMain));
|
|
193
207
|
});
|
|
194
208
|
}
|
|
195
209
|
|
|
196
|
-
private
|
|
210
|
+
private handOver(member: Answer["member"], value: unknown, last: boolean): void {
|
|
197
211
|
this.awaitTaken();
|
|
198
212
|
let text: string | Buffer;
|
|
199
213
|
try {
|
|
200
|
-
text = JSON.stringify(
|
|
214
|
+
text = JSON.stringify(value);
|
|
201
215
|
} catch (err) {
|
|
202
216
|
if (!(err instanceof RangeError)) {
|
|
203
217
|
throw err;
|
|
204
218
|
}
|
|
205
|
-
text = encodeFragmented(
|
|
219
|
+
text = encodeFragmented(value, "utf8");
|
|
206
220
|
}
|
|
207
221
|
const length = typeof text === "string" ? Buffer.byteLength(text, "utf8") : text.byteLength;
|
|
208
222
|
ensureCapacity(this.buffers.outbound, length);
|
|
@@ -213,13 +227,18 @@ export class WorkerPageChannel {
|
|
|
213
227
|
text.copy(view);
|
|
214
228
|
}
|
|
215
229
|
this.control[OUT_LEN] = length;
|
|
216
|
-
|
|
217
|
-
this.
|
|
230
|
+
// the remote may have aborted while the answer was written
|
|
231
|
+
this.throwIfAborted(Atomics.compareExchange(this.control, OUT_STATE, EMPTY, FULL));
|
|
232
|
+
this.port.postMessage({t: "answer", member, last} satisfies ToMain);
|
|
218
233
|
}
|
|
219
234
|
|
|
220
235
|
private awaitTaken(): void {
|
|
221
236
|
Atomics.wait(this.control, OUT_STATE, FULL);
|
|
222
|
-
|
|
237
|
+
this.throwIfAborted(Atomics.load(this.control, OUT_STATE));
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
private throwIfAborted(state: number): void {
|
|
241
|
+
if (state === ABORTED) {
|
|
223
242
|
Atomics.store(this.control, OUT_STATE, EMPTY);
|
|
224
243
|
throw new PageAbortedError("The remote aborted the transfer");
|
|
225
244
|
}
|
|
@@ -236,7 +255,10 @@ interface Serving {
|
|
|
236
255
|
objectId: string;
|
|
237
256
|
// JSON of the ids of GetObject requests not yet answered
|
|
238
257
|
waiting: string[];
|
|
239
|
-
|
|
258
|
+
// the answer in the outbound buffer, until a request takes it
|
|
259
|
+
ready?: Answer;
|
|
260
|
+
// The remote has the transfer's last answer, so its next GetObject of the object starts another transfer
|
|
261
|
+
complete: boolean;
|
|
240
262
|
}
|
|
241
263
|
|
|
242
264
|
/**
|
|
@@ -311,7 +333,7 @@ export class StdioRelay {
|
|
|
311
333
|
this.onGetObject(JSON.stringify(message.id), message.params);
|
|
312
334
|
return;
|
|
313
335
|
}
|
|
314
|
-
if (message.method === "AbortGetObject" && this.
|
|
336
|
+
if (message.method === "AbortGetObject" && this.transferring(message.params?.id)) {
|
|
315
337
|
// the worker's traversal stops at its next page, then the abort reaches its handler
|
|
316
338
|
Atomics.store(this.control, OUT_STATE, ABORTED);
|
|
317
339
|
Atomics.notify(this.control, OUT_STATE);
|
|
@@ -350,10 +372,14 @@ export class StdioRelay {
|
|
|
350
372
|
Atomics.notify(this.control, IN_STATE);
|
|
351
373
|
}
|
|
352
374
|
|
|
375
|
+
private transferring(objectId: string): boolean {
|
|
376
|
+
return this.serving?.objectId === objectId && !this.serving.complete;
|
|
377
|
+
}
|
|
378
|
+
|
|
353
379
|
private onGetObject(id: string, params: { id: string, sourceFileType?: string }): void {
|
|
354
|
-
if (this.
|
|
355
|
-
this.serving
|
|
356
|
-
this.
|
|
380
|
+
if (this.transferring(params.id)) {
|
|
381
|
+
this.serving!.waiting.push(id);
|
|
382
|
+
this.respond();
|
|
357
383
|
} else if (this.serving) {
|
|
358
384
|
this.queuedServes.push({id, params});
|
|
359
385
|
} else {
|
|
@@ -362,20 +388,22 @@ export class StdioRelay {
|
|
|
362
388
|
}
|
|
363
389
|
|
|
364
390
|
private startServe(id: string, params: { id: string, sourceFileType?: string }): void {
|
|
365
|
-
this.serving = {objectId: params.id, waiting: [id],
|
|
391
|
+
this.serving = {objectId: params.id, waiting: [id], complete: false};
|
|
366
392
|
this.port.postMessage({t: "serve", params} satisfies ToWorker);
|
|
367
393
|
}
|
|
368
394
|
|
|
369
|
-
private
|
|
395
|
+
private respond(): void {
|
|
370
396
|
const serving = this.serving!;
|
|
371
|
-
if (!serving.
|
|
397
|
+
if (!serving.ready || serving.waiting.length === 0) {
|
|
372
398
|
return;
|
|
373
399
|
}
|
|
374
|
-
const
|
|
375
|
-
const
|
|
400
|
+
const id = serving.waiting.shift();
|
|
401
|
+
const prefix = Buffer.from(`{"jsonrpc":"2.0","id":${id},"${serving.ready.member}":`, "utf8");
|
|
402
|
+
const answer = Buffer.from(this.buffers.outbound, 0, this.control[OUT_LEN]);
|
|
376
403
|
// copied out before the worker may overwrite the buffer with its next page
|
|
377
|
-
const body = Buffer.concat([prefix,
|
|
378
|
-
serving.
|
|
404
|
+
const body = Buffer.concat([prefix, answer, Buffer.from("}")]);
|
|
405
|
+
serving.complete = serving.ready.last;
|
|
406
|
+
serving.ready = undefined;
|
|
379
407
|
Atomics.store(this.control, OUT_STATE, EMPTY);
|
|
380
408
|
Atomics.notify(this.control, OUT_STATE);
|
|
381
409
|
this.output.write(frame(body));
|
|
@@ -397,18 +425,10 @@ export class StdioRelay {
|
|
|
397
425
|
}), "utf8")));
|
|
398
426
|
break;
|
|
399
427
|
}
|
|
400
|
-
case "
|
|
401
|
-
this.serving!.
|
|
402
|
-
this.
|
|
428
|
+
case "answer":
|
|
429
|
+
this.serving!.ready = m;
|
|
430
|
+
this.respond();
|
|
403
431
|
break;
|
|
404
|
-
case "serveError": {
|
|
405
|
-
const id = this.serving!.waiting.shift();
|
|
406
|
-
if (id !== undefined) {
|
|
407
|
-
this.output.write(frame(Buffer.from(
|
|
408
|
-
`{"jsonrpc":"2.0","id":${id},"error":${JSON.stringify({code: -32603, message: m.message})}}`, "utf8")));
|
|
409
|
-
}
|
|
410
|
-
break;
|
|
411
|
-
}
|
|
412
432
|
case "served": {
|
|
413
433
|
this.serving = undefined;
|
|
414
434
|
const next = this.queuedServes.shift();
|
package/src/test/rewrite-test.ts
CHANGED
|
@@ -20,7 +20,8 @@ import {Parser} from "../parser";
|
|
|
20
20
|
import {MarkerPrinter, PrintOutputCapture, TreePrinters} from "../print";
|
|
21
21
|
import {Cursor, isTree, rootCursor, SourceFile} from "../tree";
|
|
22
22
|
import {ACCUMULATOR_LISTENER, Result, scheduleRun} from "../run";
|
|
23
|
-
import {mapAsync, trimIndent} from "../util";
|
|
23
|
+
import {dedent, mapAsync, trimIndent} from "../util";
|
|
24
|
+
export {dedent} from "../util";
|
|
24
25
|
import {isParseError, ParseErrorKind} from "../parse-error";
|
|
25
26
|
import {Marker, Markers, MarkersKind, ParseExceptionResult} from "../markers";
|
|
26
27
|
import {isNamedStyles} from "../style";
|
|
@@ -854,64 +855,6 @@ class NoopRecipe extends Recipe {
|
|
|
854
855
|
|
|
855
856
|
export type AfterRecipeText = string | ((actual: string) => string | undefined) | undefined | null;
|
|
856
857
|
|
|
857
|
-
/**
|
|
858
|
-
* Simple dedent implementation that removes common leading whitespace from each line.
|
|
859
|
-
*
|
|
860
|
-
* Behavior:
|
|
861
|
-
* - Removes ONE leading newline if present (for template string ergonomics)
|
|
862
|
-
* - Preserves trailing newlines (important for testing formatters like Prettier)
|
|
863
|
-
* - For lines with content: removes common indentation
|
|
864
|
-
* - For lines with only whitespace: removes common indentation, preserving remaining spaces
|
|
865
|
-
*
|
|
866
|
-
* Examples:
|
|
867
|
-
* - `\n code` → `code` (single leading newline removed)
|
|
868
|
-
* - `\n\n code` → `\ncode` (first newline removed, second preserved)
|
|
869
|
-
* - ` code\n` → `code\n` (trailing newline preserved)
|
|
870
|
-
* - ` code\n\n` → `code\n\n` (trailing newlines preserved)
|
|
871
|
-
*/
|
|
872
|
-
export function dedent(s: string): string {
|
|
873
|
-
if (!s) return s;
|
|
874
|
-
|
|
875
|
-
// Remove single leading newline for ergonomics
|
|
876
|
-
const start = s.charCodeAt(0) === 10 ? 1 : 0; // 10 = '\n'
|
|
877
|
-
|
|
878
|
-
if (start >= s.length) return '';
|
|
879
|
-
|
|
880
|
-
const str = start > 0 ? s.slice(start) : s;
|
|
881
|
-
const lines = str.split('\n');
|
|
882
|
-
|
|
883
|
-
// Always consider all lines for minIndent calculation
|
|
884
|
-
// If first line has content at column 0, minIndent will be 0 and no dedenting happens
|
|
885
|
-
const startLine = 0;
|
|
886
|
-
|
|
887
|
-
// Find minimum indentation
|
|
888
|
-
let minIndent = Infinity;
|
|
889
|
-
for (let i = startLine; i < lines.length; i++) {
|
|
890
|
-
const line = lines[i];
|
|
891
|
-
let indent = 0;
|
|
892
|
-
for (let j = 0; j < line.length; j++) {
|
|
893
|
-
const ch = line.charCodeAt(j);
|
|
894
|
-
if (ch === 32 || ch === 9) { // ' ' or '\t'
|
|
895
|
-
indent++;
|
|
896
|
-
} else {
|
|
897
|
-
// Found non-whitespace, update minIndent
|
|
898
|
-
if (indent < minIndent) minIndent = indent;
|
|
899
|
-
break;
|
|
900
|
-
}
|
|
901
|
-
}
|
|
902
|
-
}
|
|
903
|
-
|
|
904
|
-
// If all lines are empty or no indentation
|
|
905
|
-
if (minIndent === Infinity || minIndent === 0) {
|
|
906
|
-
return lines.join('\n');
|
|
907
|
-
}
|
|
908
|
-
|
|
909
|
-
// Remove common indentation from all lines
|
|
910
|
-
return lines.map(line =>
|
|
911
|
-
line.length >= minIndent ? line.slice(minIndent) : ''
|
|
912
|
-
).join('\n');
|
|
913
|
-
}
|
|
914
|
-
|
|
915
858
|
export function dedentAfter(s?: AfterRecipeText): AfterRecipeText {
|
|
916
859
|
if (s !== null) {
|
|
917
860
|
if (s === undefined) {
|
package/src/util.ts
CHANGED
|
@@ -50,6 +50,64 @@ export function trimIndent(str: string | null | undefined): string {
|
|
|
50
50
|
.trim();
|
|
51
51
|
}
|
|
52
52
|
|
|
53
|
+
/**
|
|
54
|
+
* Removes common leading whitespace from each line, optimized for template strings.
|
|
55
|
+
*
|
|
56
|
+
* Behavior:
|
|
57
|
+
* - Removes ONE leading newline if present (for template string ergonomics)
|
|
58
|
+
* - Preserves trailing newlines (important for testing formatters like Prettier)
|
|
59
|
+
* - For lines with content: removes common indentation
|
|
60
|
+
* - For lines with only whitespace: removes common indentation, preserving remaining spaces
|
|
61
|
+
*
|
|
62
|
+
* Examples:
|
|
63
|
+
* - `\n code` → `code` (single leading newline removed)
|
|
64
|
+
* - `\n\n code` → `\ncode` (first newline removed, second preserved)
|
|
65
|
+
* - ` code\n` → `code\n` (trailing newline preserved)
|
|
66
|
+
* - ` code\n\n` → `code\n\n` (trailing newlines preserved)
|
|
67
|
+
*/
|
|
68
|
+
export function dedent(s: string): string {
|
|
69
|
+
if (!s) return s;
|
|
70
|
+
|
|
71
|
+
// Remove single leading newline for ergonomics
|
|
72
|
+
const start = s.charCodeAt(0) === 10 ? 1 : 0; // 10 = '\n'
|
|
73
|
+
|
|
74
|
+
if (start >= s.length) return '';
|
|
75
|
+
|
|
76
|
+
const str = start > 0 ? s.slice(start) : s;
|
|
77
|
+
const lines = str.split('\n');
|
|
78
|
+
|
|
79
|
+
// Always consider all lines for minIndent calculation
|
|
80
|
+
// If first line has content at column 0, minIndent will be 0 and no dedenting happens
|
|
81
|
+
const startLine = 0;
|
|
82
|
+
|
|
83
|
+
// Find minimum indentation
|
|
84
|
+
let minIndent = Infinity;
|
|
85
|
+
for (let i = startLine; i < lines.length; i++) {
|
|
86
|
+
const line = lines[i];
|
|
87
|
+
let indent = 0;
|
|
88
|
+
for (let j = 0; j < line.length; j++) {
|
|
89
|
+
const ch = line.charCodeAt(j);
|
|
90
|
+
if (ch === 32 || ch === 9) { // ' ' or '\t'
|
|
91
|
+
indent++;
|
|
92
|
+
} else {
|
|
93
|
+
// Found non-whitespace, update minIndent
|
|
94
|
+
if (indent < minIndent) minIndent = indent;
|
|
95
|
+
break;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// If all lines are empty or no indentation
|
|
101
|
+
if (minIndent === Infinity || minIndent === 0) {
|
|
102
|
+
return lines.join('\n');
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// Remove common indentation from all lines
|
|
106
|
+
return lines.map(line =>
|
|
107
|
+
line.length >= minIndent ? line.slice(minIndent) : ''
|
|
108
|
+
).join('\n');
|
|
109
|
+
}
|
|
110
|
+
|
|
53
111
|
/**
|
|
54
112
|
* A compiled `(original, updates) => merged` builder that names every resulting property in an
|
|
55
113
|
* object literal. V8 sizes a literal's in-object storage to the properties it names, so the merged
|
package/src/yaml/parser.ts
CHANGED
|
@@ -129,7 +129,7 @@ class YamlCstReader {
|
|
|
129
129
|
prefix: "",
|
|
130
130
|
markers: emptyMarkers,
|
|
131
131
|
documents,
|
|
132
|
-
suffix: undefined
|
|
132
|
+
suffix: pendingPrefix || undefined
|
|
133
133
|
};
|
|
134
134
|
}
|
|
135
135
|
|
|
@@ -749,20 +749,20 @@ class YamlCstReader {
|
|
|
749
749
|
}
|
|
750
750
|
|
|
751
751
|
private convertFlowScalarWithTrailing(cst: CstFlowScalar): {node: Yaml.Scalar, trailing: string} {
|
|
752
|
-
const resolved = CST.resolveAsScalar(cst);
|
|
753
|
-
const value = resolved?.value ?? "";
|
|
754
|
-
|
|
755
|
-
// Determine scalar style
|
|
756
752
|
let style: Yaml.ScalarStyle;
|
|
753
|
+
let value: string;
|
|
757
754
|
switch (cst.type) {
|
|
758
755
|
case 'single-quoted-scalar':
|
|
759
756
|
style = Yaml.ScalarStyle.SINGLE_QUOTED;
|
|
757
|
+
value = this.unwrapQuotes(cst.source);
|
|
760
758
|
break;
|
|
761
759
|
case 'double-quoted-scalar':
|
|
762
760
|
style = Yaml.ScalarStyle.DOUBLE_QUOTED;
|
|
761
|
+
value = this.unwrapQuotes(cst.source);
|
|
763
762
|
break;
|
|
764
763
|
default:
|
|
765
764
|
style = Yaml.ScalarStyle.PLAIN;
|
|
765
|
+
value = cst.source;
|
|
766
766
|
}
|
|
767
767
|
|
|
768
768
|
// End tokens contain trailing whitespace/newlines
|
|
@@ -962,6 +962,10 @@ class YamlCstReader {
|
|
|
962
962
|
private concatenateSources(tokens: CstSourceToken[]): string {
|
|
963
963
|
return tokens.map(t => t.source).join('');
|
|
964
964
|
}
|
|
965
|
+
|
|
966
|
+
private unwrapQuotes(source: string): string {
|
|
967
|
+
return source.substring(1, source.length - 1);
|
|
968
|
+
}
|
|
965
969
|
}
|
|
966
970
|
|
|
967
971
|
Parsers.registerParser("yaml", YamlParser);
|
package/src/yaml/print.ts
CHANGED
|
@@ -70,6 +70,9 @@ class YamlPrinter extends YamlVisitor<PrintOutputCapture> {
|
|
|
70
70
|
if (mapping.anchor) {
|
|
71
71
|
await this.visit(mapping.anchor, p);
|
|
72
72
|
}
|
|
73
|
+
if (mapping.tag) {
|
|
74
|
+
await this.visit(mapping.tag, p);
|
|
75
|
+
}
|
|
73
76
|
if (mapping.openingBracePrefix !== undefined) {
|
|
74
77
|
p.append(mapping.openingBracePrefix);
|
|
75
78
|
p.append('{');
|
|
@@ -99,12 +102,12 @@ class YamlPrinter extends YamlVisitor<PrintOutputCapture> {
|
|
|
99
102
|
|
|
100
103
|
protected async visitScalar(scalar: Yaml.Scalar, p: PrintOutputCapture): Promise<Yaml | undefined> {
|
|
101
104
|
await this.beforeSyntax(scalar, p);
|
|
102
|
-
if (scalar.tag) {
|
|
103
|
-
await this.visit(scalar.tag, p);
|
|
104
|
-
}
|
|
105
105
|
if (scalar.anchor) {
|
|
106
106
|
await this.visit(scalar.anchor, p);
|
|
107
107
|
}
|
|
108
|
+
if (scalar.tag) {
|
|
109
|
+
await this.visit(scalar.tag, p);
|
|
110
|
+
}
|
|
108
111
|
switch (scalar.style) {
|
|
109
112
|
case Yaml.ScalarStyle.DOUBLE_QUOTED:
|
|
110
113
|
p.append('"');
|
|
@@ -138,6 +141,9 @@ class YamlPrinter extends YamlVisitor<PrintOutputCapture> {
|
|
|
138
141
|
if (sequence.anchor) {
|
|
139
142
|
await this.visit(sequence.anchor, p);
|
|
140
143
|
}
|
|
144
|
+
if (sequence.tag) {
|
|
145
|
+
await this.visit(sequence.tag, p);
|
|
146
|
+
}
|
|
141
147
|
if (sequence.openingBracketPrefix !== undefined) {
|
|
142
148
|
p.append(sequence.openingBracketPrefix);
|
|
143
149
|
p.append('[');
|