Skip to content

Commit

Permalink
refactor: Remove the undocumented Lint option. (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffFaer authored Sep 27, 2024
1 parent ac4af49 commit 74cee98
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 34 deletions.
9 changes: 3 additions & 6 deletions keepsorted/keep_sorted.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (f *Fixer) errorMissingEnd() string {
// Fix all of the findings on contents to make keep-sorted happy.
func (f *Fixer) Fix(filename, contents string, modifiedLines []LineRange) (fixed string, alreadyCorrect bool, warnings []*Finding) {
lines := strings.Split(contents, "\n")
fs := f.findings(filename, lines, modifiedLines, false)
fs := f.findings(filename, lines, modifiedLines)
if len(fs) == 0 {
return contents, true, nil
}
Expand Down Expand Up @@ -91,7 +91,7 @@ func (f *Fixer) Fix(filename, contents string, modifiedLines []LineRange) (fixed
// If modifiedLines is non-nil, we only report findings for issues within the
// modified lines. Otherwise, we report all findings.
func (f *Fixer) Findings(filename, contents string, modifiedLines []LineRange) []*Finding {
return f.findings(filename, strings.Split(contents, "\n"), modifiedLines, true)
return f.findings(filename, strings.Split(contents, "\n"), modifiedLines)
}

// Finding is something that keep-sorted thinks is wrong with a particular file.
Expand Down Expand Up @@ -130,15 +130,12 @@ type Replacement struct {
NewContent string `json:"new_content"`
}

func (f *Fixer) findings(filename string, contents []string, modifiedLines []LineRange, considerLintOption bool) []*Finding {
func (f *Fixer) findings(filename string, contents []string, modifiedLines []LineRange) []*Finding {
blocks, incompleteBlocks, warns := f.newBlocks(filename, contents, 1, includeModifiedLines(modifiedLines))

var fs []*Finding
fs = append(fs, warns...)
for _, b := range blocks {
if considerLintOption && !b.metadata.opts.Lint {
continue
}
if s, alreadySorted := b.sorted(); !alreadySorted {
fs = append(fs, finding(filename, b.start+1, b.end-1, errorUnordered, replacement(b.start+1, b.end-1, linesToString(s))))
}
Expand Down
20 changes: 3 additions & 17 deletions keepsorted/keep_sorted_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,8 @@ func TestFindings(t *testing.T) {
for _, tc := range []struct {
name string

in string
modifiedLines []int
considerLintOption bool
in string
modifiedLines []int

want []*Finding
}{
Expand Down Expand Up @@ -294,19 +293,6 @@ baz

want: []*Finding{finding(filename, 3, 5, errorUnordered, replacement(3, 5, "1\n2\n3\n"))},
},
{
name: "lint=no",

in: `
// keep-sorted-test start lint=no
2
1
3
// keep-sorted-test end`,
considerLintOption: true,

want: nil,
},
} {
t.Run(tc.name, func(t *testing.T) {
initZerolog(t)
Expand All @@ -316,7 +302,7 @@ baz
mod = append(mod, LineRange{l, l})
}
}
got := New("keep-sorted-test", BlockOptions{}).findings(filename, strings.Split(tc.in, "\n"), mod, tc.considerLintOption)
got := New("keep-sorted-test", BlockOptions{}).findings(filename, strings.Split(tc.in, "\n"), mod)
if diff := cmp.Diff(tc.want, got); diff != "" {
t.Errorf("Findings diff (-want +got):\n%s", diff)
}
Expand Down
3 changes: 0 additions & 3 deletions keepsorted/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ func (opts BlockOptions) String() string {
// 3. map[string]bool: key=a,b,c,d
// 4. int: key=123
type blockOptions struct {
// Lint determines whether we emit lint warnings for this block.
Lint bool
// AllowYAMLLists determines whether list.set valued options are allowed to be specified by YAML.
AllowYAMLLists bool `key:"allow_yaml_lists"`

Expand Down Expand Up @@ -106,7 +104,6 @@ type blockOptions struct {

var (
defaultOptions = blockOptions{
Lint: true,
AllowYAMLLists: true,
Group: true,
StickyComments: true,
Expand Down
16 changes: 8 additions & 8 deletions keepsorted/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ func TestBlockOptions(t *testing.T) {
},
{
name: "SimpleSwitch",
in: "lint=yes",
in: "group=yes",

want: blockOptions{Lint: true},
want: blockOptions{Group: true},
},
{
name: "SkipLines",
Expand Down Expand Up @@ -145,27 +145,27 @@ func TestBlockOptions(t *testing.T) {
{
name: "OptionInTrailingComment",
commentMarker: "#",
in: "block=yes # lint=yes",
in: "block=yes # group=yes",

want: blockOptions{
Block: true,
Lint: true,
Group: true,
commentMarker: "#",
},
},
{
name: "ErrorDoesNotStopParsing",
in: "lint=nah case=no",
in: "group=nah case=no",
defaultOptions: blockOptions{
Lint: true,
Group: true,
CaseSensitive: true,
},

want: blockOptions{
Lint: true, // The default value should not change.
Group: true, // The default value should not change.
CaseSensitive: false,
},
wantErr: `while parsing option "lint": unrecognized bool value "nah"`,
wantErr: `while parsing option "group": unrecognized bool value "nah"`,
},
} {
t.Run(tc.name, func(t *testing.T) {
Expand Down

0 comments on commit 74cee98

Please sign in to comment.