Skip to content

Commit

Permalink
Add trim_trailing_whitespace option
Browse files Browse the repository at this point in the history
This PR adds a feature to trim all trailing whitespace from every line
of yaml content.
  • Loading branch information
braydonk committed Jun 24, 2024
1 parent e2d7d7a commit a30467d
Show file tree
Hide file tree
Showing 16 changed files with 84 additions and 6 deletions.
1 change: 1 addition & 0 deletions docs/config-file.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ The basic formatter is a barebones formatter that simply takes the data provided
| `indentless_arrays` | bool | false | Render `-` array items (block sequence items) without an increased indent. |
| `drop_merge_tag` | bool | false | Assume that any well formed merge using just a `<<` token will be a merge, and drop the `!!merge` tag from the formatted result. |
| `pad_line_comments` | int | 1 | The number of padding spaces to insert before line comments. |
| `trim_trailing_whitespace` | bool | false | Whether to trim trailing whitespace from lines. |

### Note on `max_line_length`

Expand Down
1 change: 1 addition & 0 deletions formatters/basic/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type Config struct {
IndentlessArrays bool `mapstructure:"indentless_arrays"`
DropMergeTag bool `mapstructure:"drop_merge_tag"`
PadLineComments int `mapstructure:"pad_line_comments"`
TrimTrailingWhitespace bool `mapstructure:"trim_trailing_whitespace"`
}

func DefaultConfig() *Config {
Expand Down
21 changes: 15 additions & 6 deletions formatters/basic/features.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,26 @@ import (
"github.com/google/yamlfmt"
"github.com/google/yamlfmt/formatters/basic/anchors"
"github.com/google/yamlfmt/internal/hotfix"
"github.com/google/yamlfmt/internal/trim"
)

func ConfigureFeaturesFromConfig(config *Config) yamlfmt.FeatureList {
lineSep, err := config.LineEnding.Separator()
if err != nil {
lineSep = "\n"
}
features := []yamlfmt.Feature{}
if config.RetainLineBreaks || config.RetainLineBreaksSingle {
lineSep, err := config.LineEnding.Separator()
if err != nil {
lineSep = "\n"
}
featLineBreak := hotfix.MakeFeatureRetainLineBreak(lineSep, config.RetainLineBreaksSingle)
features = append(features, featLineBreak)
features = append(
features,
hotfix.MakeFeatureRetainLineBreak(lineSep, config.RetainLineBreaksSingle),
)
}
if config.TrimTrailingWhitespace {
features = append(
features,
trim.MakeFeatureTrimTrailingWhitespace(lineSep),
)
}
return features
}
Expand Down
20 changes: 20 additions & 0 deletions formatters/basic/formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,3 +309,23 @@ func TestPadLineComments(t *testing.T) {
t.Fatalf("expected: '%s', got: '%s'", expectedStr, resultStr)
}
}

func TestTrimTrailingWhitespace(t *testing.T) {
config := basic.DefaultConfig()
config.TrimTrailingWhitespace = true
f := newFormatter(config)

yml := `a: 1
b: 2 `
expectedYml := `a: 1
b: 2`

result, err := f.Format([]byte(yml))
if err != nil {
t.Fatalf("expected formatting to pass, returned error: %v", err)
}
resultStr := strings.TrimSuffix(string(result), "\n")
if resultStr != expectedYml {
t.Fatalf("expected: '%s', got: '%s'", expectedYml, resultStr)
}
}
8 changes: 8 additions & 0 deletions integrationtest/command/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,11 @@ func TestPrintConfFlagsAndFile(t *testing.T) {
Update: *updateFlag,
}.Run(t)
}

func TestMultilineStringBug(t *testing.T) {
TestCase{
Dir: "multiline_string_bug",
Command: yamlfmtWithArgs("-formatter trim_trailing_whitespace=true ."),
Update: *updateFlag,
}.Run(t)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# There is a trailing whitespace within the string
a: |-
hello
hi
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# There is a trailing whitespace within the string
a: |-
hello
hi
Empty file.
Empty file.
Empty file modified integrationtest/command/testdata/print_conf_file/after/.yamlfmt
100644 → 100755
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ formatter:
retain_line_breaks: false
retain_line_breaks_single: true
scan_folded_as_literal: false
trim_trailing_whitespace: false
type: basic
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ formatter:
retain_line_breaks: true
retain_line_breaks_single: false
scan_folded_as_literal: false
trim_trailing_whitespace: false
type: basic
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ formatter:
retain_line_breaks: true
retain_line_breaks_single: true
scan_folded_as_literal: false
trim_trailing_whitespace: false
type: basic
28 changes: 28 additions & 0 deletions internal/trim/trim_whitespace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package trim

import (
"bufio"
"bytes"
"strings"

"github.com/google/yamlfmt"
)

func MakeFeatureTrimTrailingWhitespace(linebreakStr string) yamlfmt.Feature {
return yamlfmt.Feature{
Name: "Trim Trailing Whitespace",
BeforeAction: trimTrailingWhitespaceFeature(linebreakStr),
}
}

func trimTrailingWhitespaceFeature(linebreakStr string) yamlfmt.FeatureFunc {
return func(content []byte) ([]byte, error) {
buf := bytes.NewBuffer(content)
s := bufio.NewScanner(buf)
newLines := []string{}
for s.Scan() {
newLines = append(newLines, strings.TrimRight(s.Text(), " "))
}
return []byte(strings.Join(newLines, linebreakStr)), nil
}
}

0 comments on commit a30467d

Please sign in to comment.