Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add trim_trailing_whitespace option #188

Merged
merged 1 commit into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
42 changes: 42 additions & 0 deletions internal/trim/trim_whitespace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

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
}
}
Loading