Skip to content

Commit 398e72a

Browse files
authored
Remove trailing whitespace from merge aliases. (#260)
* fix(yaml): Remove trailing whitespace from merge aliases. PR #247 introduced a regression with YAML documents such as this: ```yaml config: &shared key: value instance: <<: *shared ``` Due to the "correct alias keys" setting, a extra space was emitted after `*shared`. This created trailing whitespace which many tools (rightly) complain about. This change adds an extra condition to only emit the space in the "simple key" context. I'm not enough of a YAML expert to be sure this is the right thing to do, but it fixes my problem without failing any of the existing tests. * test(formattest): Print a diff between the expected and actual result.
1 parent 1e2fd2c commit 398e72a

File tree

4 files changed

+17
-5
lines changed

4 files changed

+17
-5
lines changed

pkg/yaml/emitterc.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -888,7 +888,7 @@ func yaml_emitter_emit_node(emitter *yaml_emitter_t, event *yaml_event_t,
888888

889889
switch event.typ {
890890
case yaml_ALIAS_EVENT:
891-
return yaml_emitter_emit_alias(emitter, event)
891+
return yaml_emitter_emit_alias(emitter)
892892
case yaml_SCALAR_EVENT:
893893
return yaml_emitter_emit_scalar(emitter, event)
894894
case yaml_SEQUENCE_START_EVENT:
@@ -902,11 +902,12 @@ func yaml_emitter_emit_node(emitter *yaml_emitter_t, event *yaml_event_t,
902902
}
903903

904904
// Expect ALIAS.
905-
func yaml_emitter_emit_alias(emitter *yaml_emitter_t, event *yaml_event_t) bool {
905+
func yaml_emitter_emit_alias(emitter *yaml_emitter_t) bool {
906906
if !yaml_emitter_process_anchor(emitter) {
907907
return false
908908
}
909-
if emitter.correct_alias_keys && len(emitter.states) > 1 && emitter.states[len(emitter.states)-2] == yaml_EMIT_BLOCK_MAPPING_KEY_STATE {
909+
if emitter.correct_alias_keys && emitter.simple_key_context &&
910+
len(emitter.states) > 1 && emitter.states[len(emitter.states)-2] == yaml_EMIT_BLOCK_MAPPING_KEY_STATE {
910911
if !put(emitter, ' ') {
911912
return false
912913
}

pkg/yaml/formattest/testcase.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"path/filepath"
2424
"testing"
2525

26+
"github.com/google/go-cmp/cmp"
2627
"github.com/google/yamlfmt/pkg/yaml"
2728
)
2829

@@ -77,8 +78,9 @@ func (tc formatTestCase) Run(t *testing.T) {
7778
if err != nil {
7879
t.Fatal(err)
7980
}
80-
if buf.String() != string(expected) {
81-
t.Fatalf("expected:\n%s\nactual:\n%s", string(expected), buf.String())
81+
82+
if diff := cmp.Diff(string(expected), buf.String()); diff != "" {
83+
t.Fatalf("Encode() result differs (-want/+got):\n%s", diff)
8284
}
8385
})
8486
}

pkg/yaml/formattest/testdata/alias_key/expected.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,7 @@ map_key: &anchor hello
22
map:
33
*anchor :
44
key: value
5+
to_be_merged: &tbm
6+
key1: value1
7+
merged_map:
8+
!!merge <<: *tbm

pkg/yaml/formattest/testdata/alias_key/input.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,8 @@ map_key: &anchor hello
22
map:
33
*anchor :
44
key: value
5+
6+
to_be_merged: &tbm
7+
key1: value1
8+
merged_map:
9+
<<: *tbm

0 commit comments

Comments
 (0)