@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.
@@ -107,7 +107,8 @@ export class JavaScriptVisitor<P> extends JavaVisitor<P> {
107
107
  modifiers: await mapAsync(arrowFunction.modifiers, item => this.visitDefined<J.Modifier>(item, p)),
108
108
  typeParameters: arrowFunction.typeParameters && await this.visitDefined<J.TypeParameters>(arrowFunction.typeParameters, p),
109
109
  lambda: await this.visitDefined<J.Lambda>(arrowFunction.lambda, p),
110
- returnTypeExpression: arrowFunction.returnTypeExpression && await this.visitDefined<TypedTree>(arrowFunction.returnTypeExpression, p)
110
+ returnTypeExpression: arrowFunction.returnTypeExpression && await this.visitDefined<TypedTree>(arrowFunction.returnTypeExpression, p),
111
+ methodType: await this.visitType(arrowFunction.methodType, p) as Type.Method | undefined
111
112
  }));
112
113
  return updateIfChanged(arrowFunction, updates);
113
114
  }
@@ -162,7 +163,8 @@ export class JavaScriptVisitor<P> extends JavaVisitor<P> {
162
163
  selfClosing: element.selfClosing && await this.visitSpace(element.selfClosing, p),
163
164
  children: element.children && await mapAsync(element.children, child => this.visit(child, p)),
164
165
  closingName: element.closingName && await this.visitLeftPadded(element.closingName, p),
165
- afterClosingName: element.afterClosingName && await this.visitSpace(element.afterClosingName, p)
166
+ afterClosingName: element.afterClosingName && await this.visitSpace(element.afterClosingName, p),
167
+ methodType: await this.visitType(element.methodType, p) as Type.Method | undefined
166
168
  }));
167
169
  return updateIfChanged(element, updates);
168
170
  }
@@ -14,7 +14,7 @@
14
14
  * limitations under the License.
15
15
  */
16
16
  import {emptyMarkers, markers, MarkersKind, ParseExceptionResult} from "../markers";
17
- import {omitColon} from "./markers";
17
+ import {omitColon, tagBeforeAnchor} from "./markers";
18
18
  import {Parser, ParserInput, parserInputRead, Parsers} from "../parser";
19
19
  import {randomId} from "../uuid";
20
20
  import {SourceFile} from "../tree";
@@ -34,6 +34,19 @@ type CstFlowScalar = CST.FlowScalar;
34
34
  type CstBlockScalar = CST.BlockScalar;
35
35
  type CstCollectionItem = CST.CollectionItem;
36
36
 
37
+ /**
38
+ * The anchor and tag before a node, with the space before, between and after them.
39
+ */
40
+ interface Properties {
41
+ before: string;
42
+ anchor?: Yaml.Anchor;
43
+ tag?: Yaml.Tag;
44
+ tagFirst: boolean;
45
+ between: string;
46
+ after: string;
47
+ source: string;
48
+ }
49
+
37
50
  export class YamlParser extends Parser {
38
51
 
39
52
  async *parse(...sourcePaths: ParserInput[]): AsyncGenerator<SourceFile> {
@@ -71,6 +84,7 @@ export class YamlParser extends Parser {
71
84
  */
72
85
  class YamlCstReader {
73
86
  private readonly cstTokens: CstToken[];
87
+ private readonly anchors = new Map<string, Yaml.Anchor>();
74
88
 
75
89
  constructor(source: string) {
76
90
  const parser = new YamlCstParser();
@@ -154,39 +168,20 @@ class YamlCstReader {
154
168
  // The document prefix is content BEFORE the --- marker
155
169
  // Content AFTER --- goes into the block's first entry
156
170
  const startTokens = cstDoc.start || [];
157
- let prefix = pendingPrefix; // Content before this document
158
- let explicit = false;
159
- let afterDocStart = ""; // Content after ---
160
- let seenDocStart = false;
161
-
162
- for (const token of startTokens) {
163
- if (token.type === 'doc-start') {
164
- explicit = true;
165
- seenDocStart = true;
166
- } else if (seenDocStart) {
167
- // Content after --- goes into block's prefix
168
- afterDocStart += token.source;
169
- } else {
170
- // Content before --- goes into document prefix
171
- prefix += token.source;
172
- }
173
- }
174
-
175
- // Convert the document body and get trailing content
176
- let block: Yaml.Block;
177
- let trailing = "";
178
- if (cstDoc.value) {
179
- const result = this.convertTokenWithTrailing(cstDoc.value);
180
- block = result.node as Yaml.Block;
181
- trailing = result.trailing;
182
-
183
- // Prepend the whitespace after --- to the block
184
- if (afterDocStart) {
185
- block = this.prependWhitespaceToValue(block, afterDocStart);
186
- }
187
- } else {
188
- block = this.createEmptyScalar(afterDocStart);
171
+ const docStart = startTokens.findIndex(token => token.type === 'doc-start');
172
+ const explicit = docStart >= 0;
173
+ let prefix = pendingPrefix + this.concatenateSources(startTokens.slice(0, Math.max(docStart, 0)));
174
+ let afterDocStart = startTokens.slice(docStart + 1);
175
+
176
+ const properties = this.readProperties(afterDocStart);
177
+ if (!explicit) {
178
+ // Without ---, what comes before the root and its properties is the document's
179
+ const firstProperty = afterDocStart.findIndex(token => token.type === 'anchor' || token.type === 'tag');
180
+ const before = properties ? afterDocStart.slice(0, firstProperty) : afterDocStart;
181
+ prefix += this.concatenateSources(before);
182
+ afterDocStart = afterDocStart.slice(before.length);
189
183
  }
184
+ const {node: block, trailing} = this.convertValue(afterDocStart, cstDoc.value, "", "prefix");
190
185
 
191
186
  // Convert document end, passing the trailing content from block
192
187
  const {end, afterEnd} = this.convertDocumentEnd(cstDoc.end, cstDocEnd, trailing);
@@ -293,10 +288,44 @@ class YamlCstReader {
293
288
  }
294
289
 
295
290
  private convertMappingEntry(item: CstCollectionItem, pendingPrefix: string = ""): {entry: Yaml.MappingEntry, trailingContent: string} {
296
- // Prefix comes from start tokens plus any pending prefix from previous entry's trailing content
297
- const prefix = pendingPrefix + this.concatenateSources(item.start || []);
291
+ const {prefix, key, keyTrailing} = this.convertKey(item, pendingPrefix);
292
+
293
+ let beforeMappingValueIndicator = keyTrailing;
294
+ const afterColon: CstSourceToken[] = [];
295
+ let seenColon = false;
296
+ for (const token of item.sep || []) {
297
+ if (token.type === 'map-value-ind') {
298
+ seenColon = true;
299
+ } else if (seenColon) {
300
+ afterColon.push(token);
301
+ } else {
302
+ beforeMappingValueIndicator += token.source;
303
+ }
304
+ }
305
+ const {node: value, trailing: valueTrailing} = this.convertValue(afterColon, item.value, "", "trailing");
306
+
307
+ return {
308
+ entry: {
309
+ kind: Yaml.Kind.MappingEntry,
310
+ id: randomId(),
311
+ prefix,
312
+ markers: emptyMarkers,
313
+ key,
314
+ beforeMappingValueIndicator,
315
+ value
316
+ },
317
+ trailingContent: valueTrailing
318
+ };
319
+ }
320
+
321
+ /**
322
+ * Converts the key of a mapping entry, with its anchor and tag. The space before them is the entry's prefix.
323
+ */
324
+ private convertKey(item: CstCollectionItem, pendingPrefix: string): {prefix: string, key: Yaml.YamlKey, keyTrailing: string} {
325
+ const startTokens = item.start || [];
326
+ const properties = item.key ? this.readProperties(startTokens) : undefined;
327
+ const prefix = pendingPrefix + (properties ? properties.before : this.concatenateSources(startTokens));
298
328
 
299
- // Convert key and get its trailing content
300
329
  let keyTrailing = "";
301
330
  let key: Yaml.YamlKey;
302
331
  if (item.key) {
@@ -319,97 +348,13 @@ class YamlCstReader {
319
348
  };
320
349
  }
321
350
  keyTrailing = keyResult.trailing;
322
- } else {
323
- key = this.createEmptyScalar();
324
- }
325
-
326
- // Extract whitespace before ':' and any anchor/tag after it
327
- let beforeMappingValueIndicator = keyTrailing;
328
- let anchorForValue: Yaml.Anchor | undefined;
329
- let tagForValue: Yaml.Tag | undefined;
330
- let afterColonWhitespace = "";
331
- let seenColon = false;
332
-
333
- if (item.sep) {
334
- const sepTokens = item.sep;
335
- for (let i = 0; i < sepTokens.length; i++) {
336
- const sepToken = sepTokens[i];
337
- if (sepToken.type === 'map-value-ind') {
338
- seenColon = true;
339
- } else if (!seenColon) {
340
- // Before the ':'
341
- beforeMappingValueIndicator += sepToken.source;
342
- } else if (sepToken.type === 'anchor') {
343
- // Collect all remaining tokens after anchor as postfix
344
- let postfix = "";
345
- for (let j = i + 1; j < sepTokens.length; j++) {
346
- const nextToken = sepTokens[j];
347
- if (nextToken.type === 'tag') {
348
- tagForValue = this.parseTagTokenWithSuffix(nextToken.source, postfix, sepTokens, j + 1);
349
- postfix = "";
350
- break; // Tag handler consumed remaining tokens
351
- } else {
352
- postfix += nextToken.source;
353
- }
354
- }
355
- anchorForValue = {
356
- kind: Yaml.Kind.Anchor,
357
- id: randomId(),
358
- prefix: afterColonWhitespace,
359
- markers: emptyMarkers,
360
- postfix,
361
- key: sepToken.source.substring(1) // Remove &
362
- };
363
- afterColonWhitespace = "";
364
- break; // We've consumed all remaining tokens
365
- } else if (sepToken.type === 'tag') {
366
- // Parse tag with its suffix (whitespace after the tag)
367
- tagForValue = this.parseTagTokenWithSuffix(sepToken.source, afterColonWhitespace, sepTokens, i + 1);
368
- afterColonWhitespace = ""; // Remaining whitespace is in tag suffix, not value prefix
369
- break; // Tag handler consumed remaining tokens
370
- } else {
371
- // After the ':'
372
- afterColonWhitespace += sepToken.source;
373
- }
374
- }
375
- }
376
-
377
- // Convert value
378
- let value: Yaml.Block;
379
- let valueTrailing = "";
380
- if (item.value) {
381
- const valueResult = this.convertTokenWithTrailing(item.value);
382
- value = valueResult.node as Yaml.Block;
383
- valueTrailing = valueResult.trailing;
384
-
385
- // Apply anchor and tag if found in separator
386
- if (anchorForValue && 'anchor' in value) {
387
- value = {...value, anchor: anchorForValue} as Yaml.Block;
388
- }
389
- if (tagForValue && 'tag' in value) {
390
- value = {...value, tag: tagForValue} as Yaml.Block;
391
- }
392
-
393
- // Prepend accumulated whitespace based on value type
394
- if (afterColonWhitespace) {
395
- value = this.prependWhitespaceToValue(value, afterColonWhitespace);
351
+ if (properties) {
352
+ key = this.withProperties(key, {...properties, before: ""}).node as Yaml.YamlKey;
396
353
  }
397
354
  } else {
398
- value = this.createEmptyScalar(afterColonWhitespace);
355
+ key = this.createEmptyScalar();
399
356
  }
400
-
401
- return {
402
- entry: {
403
- kind: Yaml.Kind.MappingEntry,
404
- id: randomId(),
405
- prefix,
406
- markers: emptyMarkers,
407
- key,
408
- beforeMappingValueIndicator,
409
- value
410
- },
411
- trailingContent: valueTrailing
412
- };
357
+ return {prefix, key, keyTrailing};
413
358
  }
414
359
 
415
360
  private convertBlockSequenceWithTrailing(cst: CstBlockSequence): {node: Yaml.Sequence, trailing: string} {
@@ -443,79 +388,11 @@ class YamlCstReader {
443
388
  }
444
389
 
445
390
  private convertSequenceEntry(item: CstCollectionItem, dash: boolean, pendingPrefix: string = ""): {entry: Yaml.SequenceEntry, trailingContent: string} {
446
- // Build prefix from start tokens, but exclude the dash indicator itself
447
- // Also extract anchor and tag if present
448
- let prefix = pendingPrefix;
449
- let afterDashSpace = "";
450
- let seenDash = false;
451
- let anchorForValue: Yaml.Anchor | undefined;
452
- let tagForValue: Yaml.Tag | undefined;
453
-
454
391
  const startTokens = item.start || [];
455
- for (let i = 0; i < startTokens.length; i++) {
456
- const token = startTokens[i];
457
- if (token.type === 'seq-item-ind') {
458
- seenDash = true;
459
- } else if (seenDash) {
460
- if (token.type === 'anchor') {
461
- // Collect all remaining tokens after anchor as postfix
462
- let postfix = "";
463
- for (let j = i + 1; j < startTokens.length; j++) {
464
- const nextToken = startTokens[j];
465
- if (nextToken.type === 'tag') {
466
- tagForValue = this.parseTagTokenWithSuffix(nextToken.source, postfix, startTokens, j + 1);
467
- postfix = "";
468
- break;
469
- } else {
470
- postfix += nextToken.source;
471
- }
472
- }
473
- anchorForValue = {
474
- kind: Yaml.Kind.Anchor,
475
- id: randomId(),
476
- prefix: afterDashSpace,
477
- markers: emptyMarkers,
478
- postfix,
479
- key: token.source.substring(1) // Remove &
480
- };
481
- afterDashSpace = "";
482
- break; // We've consumed all remaining tokens
483
- } else if (token.type === 'tag') {
484
- tagForValue = this.parseTagTokenWithSuffix(token.source, afterDashSpace, startTokens, i + 1);
485
- afterDashSpace = "";
486
- break; // Tag handler consumed remaining tokens
487
- } else {
488
- afterDashSpace += token.source;
489
- }
490
- } else {
491
- prefix += token.source;
492
- }
493
- }
494
-
495
- // Convert value
496
- let block: Yaml.Block;
497
- let trailing = "";
498
- if (item.value) {
499
- const valueResult = this.convertTokenWithTrailing(item.value);
500
- block = valueResult.node as Yaml.Block;
501
- trailing = valueResult.trailing;
502
-
503
- // Apply anchor and tag if found in start tokens
504
- if (anchorForValue && 'anchor' in block) {
505
- block = {...block, anchor: anchorForValue} as Yaml.Block;
506
- }
507
- if (tagForValue && 'tag' in block) {
508
- block = {...block, tag: tagForValue} as Yaml.Block;
509
- }
510
-
511
- // Prepend the space after dash to the block
512
- // Use prependWhitespaceToValue to ensure Mapping/Sequence keep empty prefix
513
- if (afterDashSpace) {
514
- block = this.prependWhitespaceToValue(block, afterDashSpace);
515
- }
516
- } else {
517
- block = this.createEmptyScalar(afterDashSpace);
518
- }
392
+ const dashIndex = startTokens.findIndex(token => token.type === 'seq-item-ind');
393
+ const prefix = pendingPrefix + this.concatenateSources(dashIndex >= 0 ? startTokens.slice(0, dashIndex) : startTokens);
394
+ const afterDash = dashIndex >= 0 ? startTokens.slice(dashIndex + 1) : [];
395
+ const {node: block, trailing} = this.convertValue(afterDash, item.value, "", "trailing");
519
396
 
520
397
  return {
521
398
  entry: {
@@ -635,33 +512,10 @@ class YamlCstReader {
635
512
  }
636
513
 
637
514
  private convertFlowMappingEntry(item: CstCollectionItem, pendingPrefix: string): {entry: Yaml.MappingEntry, trailingContent: string} {
638
- const prefix = pendingPrefix + this.concatenateSources(item.start || []);
639
-
640
- let keyTrailing = "";
641
- let key: Yaml.YamlKey;
642
- if (item.key) {
643
- const keyResult = this.convertTokenWithTrailing(item.key);
644
- if (isYamlKey(keyResult.node)) {
645
- key = keyResult.node;
646
- } else {
647
- key = {
648
- kind: Yaml.Kind.Scalar,
649
- id: randomId(),
650
- prefix: keyResult.node.prefix,
651
- markers: emptyMarkers,
652
- style: Yaml.ScalarStyle.PLAIN,
653
- anchor: undefined,
654
- tag: undefined,
655
- value: CST.stringify(item.key).trim()
656
- };
657
- }
658
- keyTrailing = keyResult.trailing;
659
- } else {
660
- key = this.createEmptyScalar();
661
- }
515
+ const {prefix, key, keyTrailing} = this.convertKey(item, pendingPrefix);
662
516
 
663
517
  let beforeMappingValueIndicator = keyTrailing;
664
- let afterColonSpace = "";
518
+ const afterColon: CstSourceToken[] = [];
665
519
  let seenColon = false;
666
520
 
667
521
  if (item.sep) {
@@ -669,7 +523,7 @@ class YamlCstReader {
669
523
  if (token.type === 'map-value-ind') {
670
524
  seenColon = true;
671
525
  } else if (seenColon) {
672
- afterColonSpace += token.source;
526
+ afterColon.push(token);
673
527
  } else if (token.type === 'comma') {
674
528
  // This is the comma after the entry, not before
675
529
  // Skip it as we handle comma separately
@@ -679,18 +533,7 @@ class YamlCstReader {
679
533
  }
680
534
  }
681
535
 
682
- let value: Yaml.Block;
683
- let valueTrailing = "";
684
- if (item.value) {
685
- const valueResult = this.convertTokenWithTrailing(item.value);
686
- value = valueResult.node as Yaml.Block;
687
- valueTrailing = valueResult.trailing;
688
- if (afterColonSpace) {
689
- value = this.prependWhitespaceToValue(value, afterColonSpace);
690
- }
691
- } else {
692
- value = this.createEmptyScalar(afterColonSpace);
693
- }
536
+ const {node: value, trailing: valueTrailing} = this.convertValue(afterColon, item.value, "", "prefix");
694
537
 
695
538
  // In flow mappings like { "MV7", "7J04" }, entries may lack explicit colons
696
539
  const entryMarkers = seenColon ? emptyMarkers : markers(omitColon());
@@ -729,11 +572,9 @@ class YamlCstReader {
729
572
  tag: undefined
730
573
  };
731
574
  trailing = entryResult.trailingContent;
732
- } else if (item.value) {
733
- prefix += this.concatenateSources(startTokens);
734
- const valueResult = this.convertTokenWithTrailing(item.value);
735
- block = valueResult.node as Yaml.Block;
736
- trailing = valueResult.trailing;
575
+ } else if (item.value || startTokens.some(token => token.type === 'anchor' || token.type === 'tag')) {
576
+ ({node: block, trailing} = this.convertValue(startTokens, item.value, prefix, "trailing"));
577
+ prefix = "";
737
578
  } else {
738
579
  prefix += this.concatenateSources(startTokens);
739
580
  block = this.createEmptyScalar();
@@ -839,7 +680,8 @@ class YamlCstReader {
839
680
  // Alias source is like "*anchorName"
840
681
  const key = cst.source.substring(1); // Remove the *
841
682
 
842
- const anchor: Yaml.Anchor = {
683
+ // As in the Java parser, an alias holds the anchor it refers to
684
+ const anchor: Yaml.Anchor = this.anchors.get(key) ?? {
843
685
  kind: Yaml.Kind.Anchor,
844
686
  id: randomId(),
845
687
  prefix: "",
@@ -867,7 +709,7 @@ class YamlCstReader {
867
709
  // Anchor source is like "&anchorName"
868
710
  const key = source.substring(1); // Remove the &
869
711
 
870
- return {
712
+ const anchor: Yaml.Anchor = {
871
713
  kind: Yaml.Kind.Anchor,
872
714
  id: randomId(),
873
715
  prefix,
@@ -875,13 +717,11 @@ class YamlCstReader {
875
717
  postfix: "",
876
718
  key
877
719
  };
720
+ this.anchors.set(key, anchor);
721
+ return anchor;
878
722
  }
879
723
 
880
724
  private parseTagToken(source: string, prefix: string): Yaml.Tag {
881
- return this.parseTagTokenWithSuffix(source, prefix, [], 0);
882
- }
883
-
884
- private parseTagTokenWithSuffix(source: string, prefix: string, remainingTokens: CstSourceToken[], startIndex: number): Yaml.Tag {
885
725
  let name: string;
886
726
  let tagKind: Yaml.TagKind;
887
727
 
@@ -899,23 +739,137 @@ class YamlCstReader {
899
739
  tagKind = Yaml.TagKind.LOCAL;
900
740
  }
901
741
 
902
- // Collect remaining tokens as suffix (whitespace after the tag)
903
- let suffix = "";
904
- for (let i = startIndex; i < remainingTokens.length; i++) {
905
- suffix += remainingTokens[i].source;
906
- }
907
-
908
742
  return {
909
743
  kind: Yaml.Kind.Tag,
910
744
  id: randomId(),
911
745
  prefix,
912
746
  markers: emptyMarkers,
913
747
  name,
914
- suffix,
748
+ suffix: "",
915
749
  tagKind
916
750
  };
917
751
  }
918
752
 
753
+ /**
754
+ * Converts the value after an indicator such as `:` or `-`, given the tokens between them. Any anchor and tag
755
+ * among the tokens go on the value. Without them, the space before an absent value goes in the empty scalar's
756
+ * prefix or in the trailing content, as `emptySpace` says.
757
+ */
758
+ private convertValue(tokens: CstSourceToken[], token: CstToken | undefined, lead: string,
759
+ emptySpace: "prefix" | "trailing"): {node: Yaml.Block, trailing: string} {
760
+ const result = token ? this.convertTokenWithTrailing(token) : undefined;
761
+ const properties = this.readProperties(tokens);
762
+ if (properties) {
763
+ const placed = this.withProperties(result?.node as Yaml.Block | undefined, {...properties, before: lead + properties.before});
764
+ return {node: placed.node, trailing: placed.trailing + (result?.trailing ?? "")};
765
+ }
766
+ const space = lead + this.concatenateSources(tokens);
767
+ if (result) {
768
+ const node = result.node as Yaml.Block;
769
+ return {node: space ? this.prependWhitespaceToValue(node, space) : node, trailing: result.trailing};
770
+ }
771
+ return emptySpace === "prefix" ?
772
+ {node: this.createEmptyScalar(space), trailing: ""} :
773
+ {node: this.createEmptyScalar(), trailing: space};
774
+ }
775
+
776
+ /**
777
+ * Reads the anchor and tag among the tokens before a node, or `undefined` if there are none.
778
+ */
779
+ private readProperties(tokens: CstSourceToken[]): Properties | undefined {
780
+ let anchor: Yaml.Anchor | undefined;
781
+ let tag: Yaml.Tag | undefined;
782
+ let tagFirst = false;
783
+ let before = "";
784
+ let between = "";
785
+ let after = "";
786
+ for (const token of tokens) {
787
+ if (token.type === 'anchor' || token.type === 'tag') {
788
+ if (anchor === undefined && tag === undefined) {
789
+ tagFirst = token.type === 'tag';
790
+ } else {
791
+ between = after;
792
+ after = "";
793
+ }
794
+ if (token.type === 'anchor') {
795
+ anchor = this.parseAnchorToken(token.source, "");
796
+ } else {
797
+ tag = this.parseTagToken(token.source, "");
798
+ }
799
+ } else if (anchor === undefined && tag === undefined) {
800
+ before += token.source;
801
+ } else {
802
+ after += token.source;
803
+ }
804
+ }
805
+ return anchor || tag ? {before, anchor, tag, tagFirst, between, after, source: this.concatenateSources(tokens)} : undefined;
806
+ }
807
+
808
+ /**
809
+ * Gives a node the anchor and tag before it, with the space around them where the Java parser puts it. An absent
810
+ * node becomes an empty scalar, and the space after its properties is returned as trailing content.
811
+ */
812
+ private withProperties(node: Yaml.Block | undefined, properties: Properties): {node: Yaml.Block, trailing: string} {
813
+ const {before, between, after, tagFirst} = properties;
814
+ let anchor = properties.anchor;
815
+ let tag = properties.tag;
816
+ const nodeMarkers = anchor && tag && tagFirst ? markers(tagBeforeAnchor()) : emptyMarkers;
817
+
818
+ if (node === undefined || node.kind === Yaml.Kind.Scalar) {
819
+ // On a scalar, each property holds the space after it, unless the scalar is empty
820
+ const empty = node === undefined || ((node as Yaml.Scalar).style === Yaml.ScalarStyle.PLAIN && (node as Yaml.Scalar).value === "");
821
+ const last = empty ? "" : after;
822
+ if (anchor && tag) {
823
+ anchor = {...anchor, postfix: tagFirst ? last : between};
824
+ tag = {...tag, suffix: tagFirst ? between : last};
825
+ } else if (anchor) {
826
+ anchor = {...anchor, postfix: last};
827
+ } else if (tag) {
828
+ tag = {...tag, suffix: last};
829
+ }
830
+ this.remember(anchor);
831
+ const scalar = (node ?? this.createEmptyScalar()) as Yaml.Scalar;
832
+ return {
833
+ node: {...scalar, prefix: before + scalar.prefix, markers: nodeMarkers, anchor, tag},
834
+ trailing: empty ? after : ""
835
+ };
836
+ }
837
+
838
+ if (node.kind === Yaml.Kind.Alias) {
839
+ // An alias can't have properties
840
+ return {node: this.prependWhitespaceToValue(node, properties.source), trailing: ""};
841
+ }
842
+
843
+ // A collection has no prefix, so its first property holds the space before it
844
+ let rest: string;
845
+ if (anchor && tag && tagFirst) {
846
+ tag = {...tag, prefix: before, suffix: between};
847
+ anchor = {...anchor, prefix: "", postfix: ""};
848
+ rest = after;
849
+ } else {
850
+ if (anchor) {
851
+ anchor = {...anchor, prefix: before, postfix: ""};
852
+ }
853
+ if (tag) {
854
+ // As the Java parser does, a tag holds the space after it up to the end of its line
855
+ const lineBreak = after.indexOf("\n");
856
+ tag = {...tag, prefix: anchor ? between : before, suffix: lineBreak >= 0 ? after.slice(0, lineBreak + 1) : after};
857
+ rest = lineBreak >= 0 ? after.slice(lineBreak + 1) : "";
858
+ } else {
859
+ rest = after;
860
+ }
861
+ }
862
+ this.remember(anchor);
863
+ const collection = {...node, markers: nodeMarkers, anchor, tag} as Yaml.Mapping | Yaml.Sequence;
864
+ return {node: rest ? this.prependWhitespaceToValue(collection, rest) : collection, trailing: ""};
865
+ }
866
+
867
+ private remember(anchor: Yaml.Anchor | undefined): void {
868
+ if (anchor) {
869
+ this.anchors.set(anchor.key, anchor);
870
+ }
871
+ }
872
+
919
873
  private createEmptyScalar(prefix: string = ""): Yaml.Scalar {
920
874
  return {
921
875
  kind: Yaml.Kind.Scalar,