Skip to content

Commit

Permalink
Compare load statements case-insensitively (#1192)
Browse files Browse the repository at this point in the history
  • Loading branch information
vladmos authored Aug 31, 2023
1 parent ae65eee commit 386244e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
16 changes: 11 additions & 5 deletions warn/warn_cosmetic.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ func packageOnTopWarning(f *build.File) []*LinterFinding {
}
return []*LinterFinding{makeLinterFinding(rule.Call,
"Package declaration should be at the top of the file, after the load() statements, "+
"but before any call to a rule or a macro. "+
"package_group() and licenses() may be called before package().")}
"but before any call to a rule or a macro. "+
"package_group() and licenses() may be called before package().")}
}
seenRule = true
}
Expand Down Expand Up @@ -206,11 +206,17 @@ func comparePaths(path1, path2 string) bool {
return false
}
chunk2 := chunks2[i]
if chunk1 != chunk2 {
return chunk1 < chunk2
// Compare case-insensitively
chunk1Lower := strings.ToLower(chunk1)
chunk2Lower := strings.ToLower(chunk2)
if chunk1Lower != chunk2Lower {
return chunk1Lower < chunk2Lower
}
}
return true

// No case-insensitive difference detected. Likely the difference is just in
// the case of some symbols, compare case-sensitively for the determinism.
return path1 <= path2
}

// compareLoadLabels compares two module names
Expand Down
19 changes: 19 additions & 0 deletions warn/warn_cosmetic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,25 @@ load("//foo:b.bzl", "b")`,
[]string{
":2: Load statement is out of its lexicographical order.",
}, scopeEverywhere)

checkFindingsAndFix(t, "out-of-order-load", `
load("//Foo:aaa.bzl", "bar1")
load("//Foo:Bbb.bzl", "bar2")
load("//Foo:BBB.bzl", "bar3")
load("//bar/Baz2:bar.bzl", "bar4")
load("//bar/baz1:bar.bzl", "bar5")
`, `
load("//bar/baz1:bar.bzl", "bar5")
load("//bar/Baz2:bar.bzl", "bar4")
load("//Foo:aaa.bzl", "bar1")
load("//Foo:BBB.bzl", "bar3")
load("//Foo:Bbb.bzl", "bar2")`,
[]string{
":3: Load statement is out of its lexicographical order.",
":4: Load statement is out of its lexicographical order.",
":5: Load statement is out of its lexicographical order.",
}, scopeEverywhere)
}

func TestUnsortedDictItems(t *testing.T) {
Expand Down

0 comments on commit 386244e

Please sign in to comment.