@openrewrite/rewrite 8.117.0 → 8.119.0

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.
@@ -72,6 +72,7 @@ exports.YamlParser = YamlParser;
72
72
  */
73
73
  class YamlCstReader {
74
74
  constructor(source) {
75
+ this.anchors = new Map();
75
76
  const parser = new yaml_1.Parser();
76
77
  this.cstTokens = [...parser.parse(source)];
77
78
  }
@@ -150,39 +151,19 @@ class YamlCstReader {
150
151
  // The document prefix is content BEFORE the --- marker
151
152
  // Content AFTER --- goes into the block's first entry
152
153
  const startTokens = cstDoc.start || [];
153
- let prefix = pendingPrefix; // Content before this document
154
- let explicit = false;
155
- let afterDocStart = ""; // Content after ---
156
- let seenDocStart = false;
157
- for (const token of startTokens) {
158
- if (token.type === 'doc-start') {
159
- explicit = true;
160
- seenDocStart = true;
161
- }
162
- else if (seenDocStart) {
163
- // Content after --- goes into block's prefix
164
- afterDocStart += token.source;
165
- }
166
- else {
167
- // Content before --- goes into document prefix
168
- prefix += token.source;
169
- }
170
- }
171
- // Convert the document body and get trailing content
172
- let block;
173
- let trailing = "";
174
- if (cstDoc.value) {
175
- const result = this.convertTokenWithTrailing(cstDoc.value);
176
- block = result.node;
177
- trailing = result.trailing;
178
- // Prepend the whitespace after --- to the block
179
- if (afterDocStart) {
180
- block = this.prependWhitespaceToValue(block, afterDocStart);
181
- }
182
- }
183
- else {
184
- block = this.createEmptyScalar(afterDocStart);
185
- }
154
+ const docStart = startTokens.findIndex(token => token.type === 'doc-start');
155
+ const explicit = docStart >= 0;
156
+ let prefix = pendingPrefix + this.concatenateSources(startTokens.slice(0, Math.max(docStart, 0)));
157
+ let afterDocStart = startTokens.slice(docStart + 1);
158
+ const properties = this.readProperties(afterDocStart);
159
+ if (!explicit) {
160
+ // Without ---, what comes before the root and its properties is the document's
161
+ const firstProperty = afterDocStart.findIndex(token => token.type === 'anchor' || token.type === 'tag');
162
+ const before = properties ? afterDocStart.slice(0, firstProperty) : afterDocStart;
163
+ prefix += this.concatenateSources(before);
164
+ afterDocStart = afterDocStart.slice(before.length);
165
+ }
166
+ const { node: block, trailing } = this.convertValue(afterDocStart, cstDoc.value, "", "prefix");
186
167
  // Convert document end, passing the trailing content from block
187
168
  const { end, afterEnd } = this.convertDocumentEnd(cstDoc.end, cstDocEnd, trailing);
188
169
  return {
@@ -279,9 +260,42 @@ class YamlCstReader {
279
260
  };
280
261
  }
281
262
  convertMappingEntry(item, pendingPrefix = "") {
282
- // Prefix comes from start tokens plus any pending prefix from previous entry's trailing content
283
- const prefix = pendingPrefix + this.concatenateSources(item.start || []);
284
- // Convert key and get its trailing content
263
+ const { prefix, key, keyTrailing } = this.convertKey(item, pendingPrefix);
264
+ let beforeMappingValueIndicator = keyTrailing;
265
+ const afterColon = [];
266
+ let seenColon = false;
267
+ for (const token of item.sep || []) {
268
+ if (token.type === 'map-value-ind') {
269
+ seenColon = true;
270
+ }
271
+ else if (seenColon) {
272
+ afterColon.push(token);
273
+ }
274
+ else {
275
+ beforeMappingValueIndicator += token.source;
276
+ }
277
+ }
278
+ const { node: value, trailing: valueTrailing } = this.convertValue(afterColon, item.value, "", "trailing");
279
+ return {
280
+ entry: {
281
+ kind: tree_1.Yaml.Kind.MappingEntry,
282
+ id: (0, uuid_1.randomId)(),
283
+ prefix,
284
+ markers: markers_1.emptyMarkers,
285
+ key,
286
+ beforeMappingValueIndicator,
287
+ value
288
+ },
289
+ trailingContent: valueTrailing
290
+ };
291
+ }
292
+ /**
293
+ * Converts the key of a mapping entry, with its anchor and tag. The space before them is the entry's prefix.
294
+ */
295
+ convertKey(item, pendingPrefix) {
296
+ const startTokens = item.start || [];
297
+ const properties = item.key ? this.readProperties(startTokens) : undefined;
298
+ const prefix = pendingPrefix + (properties ? properties.before : this.concatenateSources(startTokens));
285
299
  let keyTrailing = "";
286
300
  let key;
287
301
  if (item.key) {
@@ -305,98 +319,14 @@ class YamlCstReader {
305
319
  };
306
320
  }
307
321
  keyTrailing = keyResult.trailing;
308
- }
309
- else {
310
- key = this.createEmptyScalar();
311
- }
312
- // Extract whitespace before ':' and any anchor/tag after it
313
- let beforeMappingValueIndicator = keyTrailing;
314
- let anchorForValue;
315
- let tagForValue;
316
- let afterColonWhitespace = "";
317
- let seenColon = false;
318
- if (item.sep) {
319
- const sepTokens = item.sep;
320
- for (let i = 0; i < sepTokens.length; i++) {
321
- const sepToken = sepTokens[i];
322
- if (sepToken.type === 'map-value-ind') {
323
- seenColon = true;
324
- }
325
- else if (!seenColon) {
326
- // Before the ':'
327
- beforeMappingValueIndicator += sepToken.source;
328
- }
329
- else if (sepToken.type === 'anchor') {
330
- // Collect all remaining tokens after anchor as postfix
331
- let postfix = "";
332
- for (let j = i + 1; j < sepTokens.length; j++) {
333
- const nextToken = sepTokens[j];
334
- if (nextToken.type === 'tag') {
335
- tagForValue = this.parseTagTokenWithSuffix(nextToken.source, postfix, sepTokens, j + 1);
336
- postfix = "";
337
- break; // Tag handler consumed remaining tokens
338
- }
339
- else {
340
- postfix += nextToken.source;
341
- }
342
- }
343
- anchorForValue = {
344
- kind: tree_1.Yaml.Kind.Anchor,
345
- id: (0, uuid_1.randomId)(),
346
- prefix: afterColonWhitespace,
347
- markers: markers_1.emptyMarkers,
348
- postfix,
349
- key: sepToken.source.substring(1) // Remove &
350
- };
351
- afterColonWhitespace = "";
352
- break; // We've consumed all remaining tokens
353
- }
354
- else if (sepToken.type === 'tag') {
355
- // Parse tag with its suffix (whitespace after the tag)
356
- tagForValue = this.parseTagTokenWithSuffix(sepToken.source, afterColonWhitespace, sepTokens, i + 1);
357
- afterColonWhitespace = ""; // Remaining whitespace is in tag suffix, not value prefix
358
- break; // Tag handler consumed remaining tokens
359
- }
360
- else {
361
- // After the ':'
362
- afterColonWhitespace += sepToken.source;
363
- }
364
- }
365
- }
366
- // Convert value
367
- let value;
368
- let valueTrailing = "";
369
- if (item.value) {
370
- const valueResult = this.convertTokenWithTrailing(item.value);
371
- value = valueResult.node;
372
- valueTrailing = valueResult.trailing;
373
- // Apply anchor and tag if found in separator
374
- if (anchorForValue && 'anchor' in value) {
375
- value = Object.assign(Object.assign({}, value), { anchor: anchorForValue });
376
- }
377
- if (tagForValue && 'tag' in value) {
378
- value = Object.assign(Object.assign({}, value), { tag: tagForValue });
379
- }
380
- // Prepend accumulated whitespace based on value type
381
- if (afterColonWhitespace) {
382
- value = this.prependWhitespaceToValue(value, afterColonWhitespace);
322
+ if (properties) {
323
+ key = this.withProperties(key, Object.assign(Object.assign({}, properties), { before: "" })).node;
383
324
  }
384
325
  }
385
326
  else {
386
- value = this.createEmptyScalar(afterColonWhitespace);
327
+ key = this.createEmptyScalar();
387
328
  }
388
- return {
389
- entry: {
390
- kind: tree_1.Yaml.Kind.MappingEntry,
391
- id: (0, uuid_1.randomId)(),
392
- prefix,
393
- markers: markers_1.emptyMarkers,
394
- key,
395
- beforeMappingValueIndicator,
396
- value
397
- },
398
- trailingContent: valueTrailing
399
- };
329
+ return { prefix, key, keyTrailing };
400
330
  }
401
331
  convertBlockSequenceWithTrailing(cst) {
402
332
  const entries = [];
@@ -427,81 +357,11 @@ class YamlCstReader {
427
357
  };
428
358
  }
429
359
  convertSequenceEntry(item, dash, pendingPrefix = "") {
430
- // Build prefix from start tokens, but exclude the dash indicator itself
431
- // Also extract anchor and tag if present
432
- let prefix = pendingPrefix;
433
- let afterDashSpace = "";
434
- let seenDash = false;
435
- let anchorForValue;
436
- let tagForValue;
437
360
  const startTokens = item.start || [];
438
- for (let i = 0; i < startTokens.length; i++) {
439
- const token = startTokens[i];
440
- if (token.type === 'seq-item-ind') {
441
- seenDash = true;
442
- }
443
- else if (seenDash) {
444
- if (token.type === 'anchor') {
445
- // Collect all remaining tokens after anchor as postfix
446
- let postfix = "";
447
- for (let j = i + 1; j < startTokens.length; j++) {
448
- const nextToken = startTokens[j];
449
- if (nextToken.type === 'tag') {
450
- tagForValue = this.parseTagTokenWithSuffix(nextToken.source, postfix, startTokens, j + 1);
451
- postfix = "";
452
- break;
453
- }
454
- else {
455
- postfix += nextToken.source;
456
- }
457
- }
458
- anchorForValue = {
459
- kind: tree_1.Yaml.Kind.Anchor,
460
- id: (0, uuid_1.randomId)(),
461
- prefix: afterDashSpace,
462
- markers: markers_1.emptyMarkers,
463
- postfix,
464
- key: token.source.substring(1) // Remove &
465
- };
466
- afterDashSpace = "";
467
- break; // We've consumed all remaining tokens
468
- }
469
- else if (token.type === 'tag') {
470
- tagForValue = this.parseTagTokenWithSuffix(token.source, afterDashSpace, startTokens, i + 1);
471
- afterDashSpace = "";
472
- break; // Tag handler consumed remaining tokens
473
- }
474
- else {
475
- afterDashSpace += token.source;
476
- }
477
- }
478
- else {
479
- prefix += token.source;
480
- }
481
- }
482
- // Convert value
483
- let block;
484
- let trailing = "";
485
- if (item.value) {
486
- const valueResult = this.convertTokenWithTrailing(item.value);
487
- block = valueResult.node;
488
- trailing = valueResult.trailing;
489
- // Apply anchor and tag if found in start tokens
490
- if (anchorForValue && 'anchor' in block) {
491
- block = Object.assign(Object.assign({}, block), { anchor: anchorForValue });
492
- }
493
- if (tagForValue && 'tag' in block) {
494
- block = Object.assign(Object.assign({}, block), { tag: tagForValue });
495
- }
496
- // Prepend the space after dash to the block
497
- // Use prependWhitespaceToValue to ensure Mapping/Sequence keep empty prefix
498
- if (afterDashSpace) {
499
- block = this.prependWhitespaceToValue(block, afterDashSpace);
500
- }
501
- }
502
- else {
503
- block = this.createEmptyScalar(afterDashSpace);
504
- }
361
+ const dashIndex = startTokens.findIndex(token => token.type === 'seq-item-ind');
362
+ const prefix = pendingPrefix + this.concatenateSources(dashIndex >= 0 ? startTokens.slice(0, dashIndex) : startTokens);
363
+ const afterDash = dashIndex >= 0 ? startTokens.slice(dashIndex + 1) : [];
364
+ const { node: block, trailing } = this.convertValue(afterDash, item.value, "", "trailing");
505
365
  return {
506
366
  entry: {
507
367
  kind: tree_1.Yaml.Kind.SequenceEntry,
@@ -611,33 +471,9 @@ class YamlCstReader {
611
471
  }
612
472
  }
613
473
  convertFlowMappingEntry(item, pendingPrefix) {
614
- const prefix = pendingPrefix + this.concatenateSources(item.start || []);
615
- let keyTrailing = "";
616
- let key;
617
- if (item.key) {
618
- const keyResult = this.convertTokenWithTrailing(item.key);
619
- if ((0, tree_1.isYamlKey)(keyResult.node)) {
620
- key = keyResult.node;
621
- }
622
- else {
623
- key = {
624
- kind: tree_1.Yaml.Kind.Scalar,
625
- id: (0, uuid_1.randomId)(),
626
- prefix: keyResult.node.prefix,
627
- markers: markers_1.emptyMarkers,
628
- style: tree_1.Yaml.ScalarStyle.PLAIN,
629
- anchor: undefined,
630
- tag: undefined,
631
- value: yaml_1.CST.stringify(item.key).trim()
632
- };
633
- }
634
- keyTrailing = keyResult.trailing;
635
- }
636
- else {
637
- key = this.createEmptyScalar();
638
- }
474
+ const { prefix, key, keyTrailing } = this.convertKey(item, pendingPrefix);
639
475
  let beforeMappingValueIndicator = keyTrailing;
640
- let afterColonSpace = "";
476
+ const afterColon = [];
641
477
  let seenColon = false;
642
478
  if (item.sep) {
643
479
  for (const token of item.sep) {
@@ -645,7 +481,7 @@ class YamlCstReader {
645
481
  seenColon = true;
646
482
  }
647
483
  else if (seenColon) {
648
- afterColonSpace += token.source;
484
+ afterColon.push(token);
649
485
  }
650
486
  else if (token.type === 'comma') {
651
487
  // This is the comma after the entry, not before
@@ -656,19 +492,7 @@ class YamlCstReader {
656
492
  }
657
493
  }
658
494
  }
659
- let value;
660
- let valueTrailing = "";
661
- if (item.value) {
662
- const valueResult = this.convertTokenWithTrailing(item.value);
663
- value = valueResult.node;
664
- valueTrailing = valueResult.trailing;
665
- if (afterColonSpace) {
666
- value = this.prependWhitespaceToValue(value, afterColonSpace);
667
- }
668
- }
669
- else {
670
- value = this.createEmptyScalar(afterColonSpace);
671
- }
495
+ const { node: value, trailing: valueTrailing } = this.convertValue(afterColon, item.value, "", "prefix");
672
496
  // In flow mappings like { "MV7", "7J04" }, entries may lack explicit colons
673
497
  const entryMarkers = seenColon ? markers_1.emptyMarkers : (0, markers_1.markers)((0, markers_2.omitColon)());
674
498
  return {
@@ -704,11 +528,9 @@ class YamlCstReader {
704
528
  };
705
529
  trailing = entryResult.trailingContent;
706
530
  }
707
- else if (item.value) {
708
- prefix += this.concatenateSources(startTokens);
709
- const valueResult = this.convertTokenWithTrailing(item.value);
710
- block = valueResult.node;
711
- trailing = valueResult.trailing;
531
+ else if (item.value || startTokens.some(token => token.type === 'anchor' || token.type === 'tag')) {
532
+ ({ node: block, trailing } = this.convertValue(startTokens, item.value, prefix, "trailing"));
533
+ prefix = "";
712
534
  }
713
535
  else {
714
536
  prefix += this.concatenateSources(startTokens);
@@ -802,9 +624,11 @@ class YamlCstReader {
802
624
  };
803
625
  }
804
626
  convertAliasWithTrailing(cst) {
627
+ var _a;
805
628
  // Alias source is like "*anchorName"
806
629
  const key = cst.source.substring(1); // Remove the *
807
- const anchor = {
630
+ // As in the Java parser, an alias holds the anchor it refers to
631
+ const anchor = (_a = this.anchors.get(key)) !== null && _a !== void 0 ? _a : {
808
632
  kind: tree_1.Yaml.Kind.Anchor,
809
633
  id: (0, uuid_1.randomId)(),
810
634
  prefix: "",
@@ -828,7 +652,7 @@ class YamlCstReader {
828
652
  parseAnchorToken(source, prefix) {
829
653
  // Anchor source is like "&anchorName"
830
654
  const key = source.substring(1); // Remove the &
831
- return {
655
+ const anchor = {
832
656
  kind: tree_1.Yaml.Kind.Anchor,
833
657
  id: (0, uuid_1.randomId)(),
834
658
  prefix,
@@ -836,11 +660,10 @@ class YamlCstReader {
836
660
  postfix: "",
837
661
  key
838
662
  };
663
+ this.anchors.set(key, anchor);
664
+ return anchor;
839
665
  }
840
666
  parseTagToken(source, prefix) {
841
- return this.parseTagTokenWithSuffix(source, prefix, [], 0);
842
- }
843
- parseTagTokenWithSuffix(source, prefix, remainingTokens, startIndex) {
844
667
  let name;
845
668
  let tagKind;
846
669
  if (source.startsWith('!<') && source.endsWith('>')) {
@@ -858,21 +681,137 @@ class YamlCstReader {
858
681
  name = source.substring(1);
859
682
  tagKind = tree_1.Yaml.TagKind.LOCAL;
860
683
  }
861
- // Collect remaining tokens as suffix (whitespace after the tag)
862
- let suffix = "";
863
- for (let i = startIndex; i < remainingTokens.length; i++) {
864
- suffix += remainingTokens[i].source;
865
- }
866
684
  return {
867
685
  kind: tree_1.Yaml.Kind.Tag,
868
686
  id: (0, uuid_1.randomId)(),
869
687
  prefix,
870
688
  markers: markers_1.emptyMarkers,
871
689
  name,
872
- suffix,
690
+ suffix: "",
873
691
  tagKind
874
692
  };
875
693
  }
694
+ /**
695
+ * Converts the value after an indicator such as `:` or `-`, given the tokens between them. Any anchor and tag
696
+ * among the tokens go on the value. Without them, the space before an absent value goes in the empty scalar's
697
+ * prefix or in the trailing content, as `emptySpace` says.
698
+ */
699
+ convertValue(tokens, token, lead, emptySpace) {
700
+ var _a;
701
+ const result = token ? this.convertTokenWithTrailing(token) : undefined;
702
+ const properties = this.readProperties(tokens);
703
+ if (properties) {
704
+ const placed = this.withProperties(result === null || result === void 0 ? void 0 : result.node, Object.assign(Object.assign({}, properties), { before: lead + properties.before }));
705
+ return { node: placed.node, trailing: placed.trailing + ((_a = result === null || result === void 0 ? void 0 : result.trailing) !== null && _a !== void 0 ? _a : "") };
706
+ }
707
+ const space = lead + this.concatenateSources(tokens);
708
+ if (result) {
709
+ const node = result.node;
710
+ return { node: space ? this.prependWhitespaceToValue(node, space) : node, trailing: result.trailing };
711
+ }
712
+ return emptySpace === "prefix" ?
713
+ { node: this.createEmptyScalar(space), trailing: "" } :
714
+ { node: this.createEmptyScalar(), trailing: space };
715
+ }
716
+ /**
717
+ * Reads the anchor and tag among the tokens before a node, or `undefined` if there are none.
718
+ */
719
+ readProperties(tokens) {
720
+ let anchor;
721
+ let tag;
722
+ let tagFirst = false;
723
+ let before = "";
724
+ let between = "";
725
+ let after = "";
726
+ for (const token of tokens) {
727
+ if (token.type === 'anchor' || token.type === 'tag') {
728
+ if (anchor === undefined && tag === undefined) {
729
+ tagFirst = token.type === 'tag';
730
+ }
731
+ else {
732
+ between = after;
733
+ after = "";
734
+ }
735
+ if (token.type === 'anchor') {
736
+ anchor = this.parseAnchorToken(token.source, "");
737
+ }
738
+ else {
739
+ tag = this.parseTagToken(token.source, "");
740
+ }
741
+ }
742
+ else if (anchor === undefined && tag === undefined) {
743
+ before += token.source;
744
+ }
745
+ else {
746
+ after += token.source;
747
+ }
748
+ }
749
+ return anchor || tag ? { before, anchor, tag, tagFirst, between, after, source: this.concatenateSources(tokens) } : undefined;
750
+ }
751
+ /**
752
+ * Gives a node the anchor and tag before it, with the space around them where the Java parser puts it. An absent
753
+ * node becomes an empty scalar, and the space after its properties is returned as trailing content.
754
+ */
755
+ withProperties(node, properties) {
756
+ const { before, between, after, tagFirst } = properties;
757
+ let anchor = properties.anchor;
758
+ let tag = properties.tag;
759
+ const nodeMarkers = anchor && tag && tagFirst ? (0, markers_1.markers)((0, markers_2.tagBeforeAnchor)()) : markers_1.emptyMarkers;
760
+ if (node === undefined || node.kind === tree_1.Yaml.Kind.Scalar) {
761
+ // On a scalar, each property holds the space after it, unless the scalar is empty
762
+ const empty = node === undefined || (node.style === tree_1.Yaml.ScalarStyle.PLAIN && node.value === "");
763
+ const last = empty ? "" : after;
764
+ if (anchor && tag) {
765
+ anchor = Object.assign(Object.assign({}, anchor), { postfix: tagFirst ? last : between });
766
+ tag = Object.assign(Object.assign({}, tag), { suffix: tagFirst ? between : last });
767
+ }
768
+ else if (anchor) {
769
+ anchor = Object.assign(Object.assign({}, anchor), { postfix: last });
770
+ }
771
+ else if (tag) {
772
+ tag = Object.assign(Object.assign({}, tag), { suffix: last });
773
+ }
774
+ this.remember(anchor);
775
+ const scalar = (node !== null && node !== void 0 ? node : this.createEmptyScalar());
776
+ return {
777
+ node: Object.assign(Object.assign({}, scalar), { prefix: before + scalar.prefix, markers: nodeMarkers, anchor, tag }),
778
+ trailing: empty ? after : ""
779
+ };
780
+ }
781
+ if (node.kind === tree_1.Yaml.Kind.Alias) {
782
+ // An alias can't have properties
783
+ return { node: this.prependWhitespaceToValue(node, properties.source), trailing: "" };
784
+ }
785
+ // A collection has no prefix, so its first property holds the space before it
786
+ let rest;
787
+ if (anchor && tag && tagFirst) {
788
+ tag = Object.assign(Object.assign({}, tag), { prefix: before, suffix: between });
789
+ anchor = Object.assign(Object.assign({}, anchor), { prefix: "", postfix: "" });
790
+ rest = after;
791
+ }
792
+ else {
793
+ if (anchor) {
794
+ anchor = Object.assign(Object.assign({}, anchor), { prefix: before, postfix: "" });
795
+ }
796
+ if (tag) {
797
+ // As the Java parser does, a tag holds the space after it up to the end of its line
798
+ const lineBreak = after.indexOf("\n");
799
+ tag = Object.assign(Object.assign({}, tag), { prefix: anchor ? between : before, suffix: lineBreak >= 0 ? after.slice(0, lineBreak + 1) : after });
800
+ rest = lineBreak >= 0 ? after.slice(lineBreak + 1) : "";
801
+ }
802
+ else {
803
+ rest = after;
804
+ }
805
+ }
806
+ this.remember(anchor);
807
+ const collection = Object.assign(Object.assign({}, node), { markers: nodeMarkers, anchor, tag });
808
+ return { node: rest ? this.prependWhitespaceToValue(collection, rest) : collection, trailing: "" };
809
+ }
810
+ remember(anchor) {
811
+ if (anchor) {
812
+ this.anchors.set(anchor.key, anchor);
813
+ }
814
+ }
876
815
  createEmptyScalar(prefix = "") {
877
816
  return {
878
817
  kind: tree_1.Yaml.Kind.Scalar,