-
Notifications
You must be signed in to change notification settings - Fork 54
Add fixer for prefer-equals-comparison rule #1790
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
Merged
charlieegan3
merged 3 commits into
open-policy-agent:main
from
SeanLedford:s_ledford/prefer-equals-comparison-fixer
Dec 8, 2025
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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, | ||
| ) | ||
| } | ||
| }) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have you run
?
There seems to be a slight difference in how the locations are matched which is causing the fix not to fire.
There was a problem hiding this comment.
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.