Skip to content

Commit c6d01e9

Browse files
authored
Merge branch 'main' into st/buildifier-rule-load-location
2 parents 3a2c86a + 635c122 commit c6d01e9

File tree

2 files changed

+72
-4
lines changed

2 files changed

+72
-4
lines changed

warn/warn_bazel_api.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,10 @@ func insertLoad(f *build.File, module string, symbols []string) *LinterReplaceme
158158
i := 0
159159
for i = range f.Stmt {
160160
stmt := f.Stmt[i]
161-
_, isComment := stmt.(*build.CommentBlock)
162-
_, isString := stmt.(*build.StringExpr)
163-
isDocString := isString && i == 0
164-
if !isComment && !isDocString {
161+
if _, isComment := stmt.(*build.CommentBlock); isComment {
162+
continue
163+
}
164+
if _, isDocString := stmt.(*build.StringExpr); !isDocString {
165165
// Insert a nil statement here
166166
break
167167
}

warn/warn_bazel_api_test.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,8 @@ def macro():
774774
}
775775

776776
func TestNativeShBinaryWarning(t *testing.T) {
777+
defer setUpFileReader(nil)()
778+
777779
checkFindingsAndFix(t, "native-sh-binary", `
778780
"""My file"""
779781
@@ -799,6 +801,8 @@ sh_binary()
799801
}
800802

801803
func TestNativeShLibraryWarning(t *testing.T) {
804+
defer setUpFileReader(nil)()
805+
802806
checkFindingsAndFix(t, "native-sh-library", `
803807
"""My file"""
804808
@@ -824,6 +828,8 @@ sh_library()
824828
}
825829

826830
func TestNativeShTestWarning(t *testing.T) {
831+
defer setUpFileReader(nil)()
832+
827833
checkFindingsAndFix(t, "native-sh-test", `
828834
"""My file"""
829835
@@ -848,6 +854,68 @@ sh_test()
848854
scopeBzl|scopeBuild)
849855
}
850856

857+
func TestNativeWarningLoadPlacedAfterFileDocstringWithComment(t *testing.T) {
858+
defer setUpFileReader(nil)()
859+
860+
checkFindingsAndFix(t, "native-sh-binary,native-java-binary", `
861+
# Copyright 2020 Google LLC
862+
#
863+
# Licensed under the Apache License, Version 2.0 (the "License");
864+
# you may not use this file except in compliance with the License.
865+
# You may obtain a copy of the License at
866+
#
867+
# https://www.apache.org/licenses/LICENSE-2.0
868+
#
869+
# Unless required by applicable law or agreed to in writing, software
870+
# distributed under the License is distributed on an "AS IS" BASIS,
871+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
872+
# See the License for the specific language governing permissions and
873+
# limitations under the License.
874+
875+
"""My file"""
876+
877+
def macro():
878+
native.sh_binary()
879+
native.java_binary()
880+
881+
java_binary()
882+
sh_binary()
883+
`, `
884+
# Copyright 2020 Google LLC
885+
#
886+
# Licensed under the Apache License, Version 2.0 (the "License");
887+
# you may not use this file except in compliance with the License.
888+
# You may obtain a copy of the License at
889+
#
890+
# https://www.apache.org/licenses/LICENSE-2.0
891+
#
892+
# Unless required by applicable law or agreed to in writing, software
893+
# distributed under the License is distributed on an "AS IS" BASIS,
894+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
895+
# See the License for the specific language governing permissions and
896+
# limitations under the License.
897+
898+
"""My file"""
899+
900+
load("@rules_java//java:java_binary.bzl", "java_binary")
901+
load("@rules_shell//shell:sh_binary.bzl", "sh_binary")
902+
903+
def macro():
904+
sh_binary()
905+
java_binary()
906+
907+
java_binary()
908+
sh_binary()
909+
`,
910+
[]string{
911+
fmt.Sprintf(`:18: Function "sh_binary" is not global anymore and needs to be loaded from "@rules_shell//shell:sh_binary.bzl".`),
912+
fmt.Sprintf(`:19: Function "java_binary" is not global anymore and needs to be loaded from "@rules_java//java:java_binary.bzl".`),
913+
fmt.Sprintf(`:21: Function "java_binary" is not global anymore and needs to be loaded from "@rules_java//java:java_binary.bzl".`),
914+
fmt.Sprintf(`:22: Function "sh_binary" is not global anymore and needs to be loaded from "@rules_shell//shell:sh_binary.bzl".`),
915+
},
916+
scopeBzl|scopeBuild)
917+
}
918+
851919
func TestKeywordParameters(t *testing.T) {
852920
checkFindingsAndFix(t, "keyword-positional-params", `
853921
foo(key = value)

0 commit comments

Comments
 (0)