Skip to content
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/fixing.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Currently, the following rules are automatically fixable:
- [use-assignment-operator](https://www.openpolicyagent.org/projects/regal/rules/style/use-assignment-operator)
- [no-whitespace-comment](https://www.openpolicyagent.org/projects/regal/rules/style/no-whitespace-comment)
- [directory-package-mismatch](https://www.openpolicyagent.org/projects/regal/rules/idiomatic/directory-package-mismatch)
- [prefer-equals-comparison](https://www.openpolicyagent.org/projects/regal/rules/idiomatic/prefer-equals-comparison)
- [use-rego-v1](https://www.openpolicyagent.org/projects/regal/rules/imports/use-rego-v1) (v0 Rego only)

So, how do you go on about automatically fixing reported violations?
Expand Down
2 changes: 2 additions & 0 deletions docs/rules/idiomatic/prefer-equals-comparison.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

**Category**: Idiomatic

**Automatically fixable**: [Yes](/regal/fixing)

**Avoid**
```rego
package policy
Expand Down
2 changes: 2 additions & 0 deletions pkg/fixer/fixes/fixes.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func NewDefaultFixes() []Fix {
&NoWhitespaceComment{},
&DirectoryPackageMismatch{},
&NonRawRegexPattern{},
&PreferEqualsComparison{},
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you run

go run main.go lint foo/wow.rego
go run main.go fix --force foo/wow.rego

?

There seems to be a slight difference in how the locations are matched which is causing the fix not to fire.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha, addressed! Wondering how we feel about updating the rule itself to provide the exact column location of the violation 🤔, the use-assignment-operator rule does something like that it looks like.

}
}

Expand All @@ -33,6 +34,7 @@ func NewDefaultFormatterFixes() []Fix {
&UseAssignmentOperator{},
&NoWhitespaceComment{},
&NonRawRegexPattern{},
&PreferEqualsComparison{},
}
}

Expand Down
44 changes: 44 additions & 0 deletions pkg/fixer/fixes/preferequalscomparison.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package fixes

import (
"errors"
"strings"
)

type PreferEqualsComparison struct{}

func (*PreferEqualsComparison) Name() string {
return "prefer-equals-comparison"
}

func (p *PreferEqualsComparison) Fix(fc *FixCandidate, opts *RuntimeOptions) ([]FixResult, error) {
if opts == nil {
return nil, errors.New("missing runtime options")
}

lines := strings.Split(fc.Contents, "\n")
fixed := false

for _, loc := range opts.Locations {
line := lines[loc.Row-1]
if loc.Row > len(lines) || loc.Column-1 < 0 || loc.Column-1 >= len(line) {
continue
}

eqIndex := strings.Index(line, "=")

// unification operator not found, skipping
if eqIndex == -1 {
continue
}

lines[loc.Row-1] = line[0:eqIndex] + "=" + line[eqIndex:]
fixed = true
}

if !fixed {
return nil, nil
}

return []FixResult{{Title: p.Name(), Root: opts.BaseDir, Contents: strings.Join(lines, "\n")}}, nil
}
156 changes: 156 additions & 0 deletions pkg/fixer/fixes/preferequalscomparison_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
package fixes

import (
"testing"

"github.com/google/go-cmp/cmp"

"github.com/open-policy-agent/regal/pkg/report"
)

func TestPreferEqualsComparison(t *testing.T) {
t.Parallel()

testCases := map[string]struct {
contentAfterFix string
fc *FixCandidate
fixExpected bool
runtimeOptions *RuntimeOptions
}{
"no change": {
fc: &FixCandidate{Filename: "test.rego", Contents: "package test\n\nallow = true\n"},
contentAfterFix: "package test\n\nallow = true\n",
fixExpected: false,
runtimeOptions: &RuntimeOptions{},
},
"no change because no location": {
fc: &FixCandidate{
Filename: "test.rego",
Contents: `package test
allow := true

test_rule {
allow = true
}`,
},
contentAfterFix: `package test
allow := true

test_rule {
allow = true
}`,
fixExpected: false,
runtimeOptions: &RuntimeOptions{},
},
"single change": {
fc: &FixCandidate{
Filename: "test.rego",
Contents: `package test
allow := true

test_rule {
allow = true
}`,
},
contentAfterFix: `package test
allow := true

test_rule {
allow == true
}`,
fixExpected: true,
runtimeOptions: &RuntimeOptions{Locations: []report.Location{{Row: 5, Column: 2}}},
},
"bad change": {
fc: &FixCandidate{
Filename: "test.rego",
Contents: `package test
allow := true
allow = true

test_rule {
allow = false
}`,
},
contentAfterFix: `package test
allow := true
allow = true

test_rule {
allow = false
}`,
fixExpected: false,
runtimeOptions: &RuntimeOptions{Locations: []report.Location{{Row: 1, Column: 1}}},
},
"many changes": {
fc: &FixCandidate{
Filename: "test.rego",
Contents: `package test
allow := true

test_rule {
allow = true
allow = false
}`,
},
contentAfterFix: `package test
allow := true

test_rule {
allow == true
allow == false
}`,
fixExpected: true,
runtimeOptions: &RuntimeOptions{Locations: []report.Location{{Row: 5, Column: 2}, {Row: 6, Column: 2}}},
},
"different columns": {
fc: &FixCandidate{
Filename: "test.rego",
Contents: `package test
allow := true

test_rule {
allow = true
allow = false
}`,
},
contentAfterFix: `package test
allow := true

test_rule {
allow == true
allow == false
}`,
fixExpected: true,
runtimeOptions: &RuntimeOptions{Locations: []report.Location{{Row: 5, Column: 2}, {Row: 6, Column: 2}}},
},
}
for testName, tc := range testCases {
t.Run(testName, func(t *testing.T) {
t.Parallel()

pec := PreferEqualsComparison{}

fixResults, err := pec.Fix(tc.fc, tc.runtimeOptions)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}

if !tc.fixExpected && len(fixResults) != 0 {
t.Fatalf("unexpected fix applied")
}

if !tc.fixExpected {
return
}

if diff := cmp.Diff(fixResults[0].Contents, tc.contentAfterFix); tc.fixExpected && diff != "" {
t.Fatalf(
"unexpected content, got:\n%s---\nexpected:\n%s---",
fixResults[0].Contents,
tc.contentAfterFix,
)
}
})
}
}
Loading