Skip to content
Open
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
12 changes: 11 additions & 1 deletion edit/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,17 @@ func IndexOfRuleByName(f *build.File, name string) (int, *build.Rule) {
for i, stmt := range f.Stmt {
call, ok := stmt.(*build.CallExpr)
if !ok {
continue
// if stmt is not a CallExpr, check if it is an AssignExpr with a CallExpr
// on the right-hand side
as, ok := stmt.(*build.AssignExpr)
if !ok {
continue
}
if as.RHS != nil {
if call, ok = as.RHS.(*build.CallExpr); !ok {
continue
}
}
}
r := f.Rule(call)
start, _ := call.X.Span()
Expand Down
32 changes: 32 additions & 0 deletions edit/edit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -829,3 +829,35 @@ func TestInterpretLabelForWorkspaceLocation(t *testing.T) {
runTestInterpretLabelForWorkspaceLocation(t, "BUILD")
runTestInterpretLabelForWorkspaceLocation(t, "BUILD.bazel")
}

func TestFindRuleByName(t *testing.T) {
input := `load("foo.bzl", "bar")
rule(
name="r1",
srcs=["a.txt"]
)

a = rule2(
name="r2",
)
`

bld, err := build.Parse("BUILD", []byte(input))
if err != nil {
t.Error(err)
return
}
for _, name := range []string{"r1", "r2"} {
rule := FindRuleByName(bld, name)
if rule == nil {
t.Errorf("FindRuleByName: could not find rule with name '%s' in BUILD file <%s>", name, input)
return
}
}
// expect to not find:
rule := FindRuleByName(bld, "r3")
if rule != nil {
t.Errorf("FindRuleByName: found rule with name 'r3', expected not to find it")
return
}
}