Skip to content

Commit

Permalink
Fix package-on-top warning (bazelbuild#1211)
Browse files Browse the repository at this point in the history
  • Loading branch information
vladmos authored Oct 17, 2023
1 parent e8decc8 commit 23aa65d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
6 changes: 4 additions & 2 deletions warn/warn_cosmetic.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func sameOriginLoadWarning(f *build.File) []*LinterFinding {
return findings
}

// packageOnTopWarning hoists package statements to the top after any comments / docstrings / load statments.
// packageOnTopWarning hoists package statements to the top after any comments / docstrings / load statements.
// If applied together with loadOnTopWarning and/or outOfOrderLoadWarning, it should be applied after them.
// This is currently guaranteed by sorting the warning categories names before applying them:
// "load-on-top" < "out-of-order-load" < "package-on-top"
Expand All @@ -120,7 +120,9 @@ func packageOnTopWarning(f *build.File) []*LinterFinding {
_, isString := stmt.(*build.StringExpr) // typically a docstring
_, isComment := stmt.(*build.CommentBlock)
_, isLoad := stmt.(*build.LoadStmt)
if isString || isComment || isLoad || stmt == nil {
_, isLicenses := edit.ExprToRule(stmt, "licenses")
_, isPackageGroup := edit.ExprToRule(stmt, "package_group")
if isString || isComment || isLoad || isLicenses || isPackageGroup || stmt == nil {
continue
}
rule, ok := edit.ExprToRule(stmt, "package")
Expand Down
30 changes: 30 additions & 0 deletions warn/warn_cosmetic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,36 @@ package()
foo(baz)`,
[]string{},
scopeDefault|scopeBzl|scopeBuild)

checkFindingsAndFix(t,
"package-on-top",
`
# Some comments
"""This is a docstring"""
load(":foo.bzl", "foo")
load(":bar.bzl", baz = "bar")
package_group(name = "my_group")
licenses(["my_license"])
foo(baz)
package()`,
`
# Some comments
"""This is a docstring"""
load(":foo.bzl", "foo")
load(":bar.bzl", baz = "bar")
package_group(name = "my_group")
licenses(["my_license"])
package()
foo(baz)`,
[]string{":11: 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()."},
scopeDefault|scopeBzl|scopeBuild)
}

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

0 comments on commit 23aa65d

Please sign in to comment.