Skip to content

Commit

Permalink
refactor: remove nested walk function literal
Browse files Browse the repository at this point in the history
  • Loading branch information
jbedard committed Aug 22, 2024
1 parent 5cd3717 commit a1ae6fc
Showing 1 changed file with 55 additions and 55 deletions.
110 changes: 55 additions & 55 deletions walk/walk.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,72 +122,72 @@ func Walk(c *config.Config, cexts []config.Configurer, dirs []string, mode Mode,
log.Printf("error loading .bazelignore: %v", err)
}

var visit func(*config.Config, string, string, bool)
visit = func(c *config.Config, dir, rel string, updateParent bool) {
haveError := false

// TODO: OPT: ReadDir stats all the files, which is slow. We just care about
// names and modes, so we should use something like
// golang.org/x/tools/internal/fastwalk to speed this up.
ents, err := os.ReadDir(dir)
if err != nil {
log.Print(err)
return
}
visit(c, cexts, isBazelIgnored, knownDirectives, updateRels, wf, c.RepoRoot, "", false)
}

f, err := loadBuildFile(c, rel, dir, ents)
if err != nil {
log.Print(err)
if c.Strict {
// TODO(https://github.com/bazelbuild/bazel-gazelle/issues/1029):
// Refactor to accumulate and propagate errors to main.
log.Fatal("Exit as strict mode is on")
}
haveError = true
}
func visit(c *config.Config, cexts []config.Configurer, isBazelIgnored isIgnoredFunc, knownDirectives map[string]bool, updateRels *UpdateFilter, wf WalkFunc, dir, rel string, updateParent bool) {
haveError := false

if isBazelIgnored(rel) {
return
// TODO: OPT: ReadDir stats all the files, which is slow. We just care about
// names and modes, so we should use something like
// golang.org/x/tools/internal/fastwalk to speed this up.
ents, err := os.ReadDir(dir)
if err != nil {
log.Print(err)
return
}

f, err := loadBuildFile(c, rel, dir, ents)
if err != nil {
log.Print(err)
if c.Strict {
// TODO(https://github.com/bazelbuild/bazel-gazelle/issues/1029):
// Refactor to accumulate and propagate errors to main.
log.Fatal("Exit as strict mode is on")
}
haveError = true
}

c = configure(cexts, knownDirectives, c, rel, f)
wc := getWalkConfig(c)
if isBazelIgnored(rel) {
return
}

if wc.isExcluded(rel, ".") {
return
}
c = configure(cexts, knownDirectives, c, rel, f)
wc := getWalkConfig(c)

var subdirs, regularFiles []string
for _, ent := range ents {
base := ent.Name()
if isBazelIgnored(path.Join(rel, base)) || wc.isExcluded(rel, base) {
continue
}
ent := resolveFileInfo(wc, dir, rel, ent)
switch {
case ent == nil:
continue
case ent.IsDir():
subdirs = append(subdirs, base)
default:
regularFiles = append(regularFiles, base)
}
}
if wc.isExcluded(rel, ".") {
return
}

shouldUpdate := updateRels.shouldUpdate(rel, updateParent)
for _, sub := range subdirs {
if subRel := path.Join(rel, sub); updateRels.shouldVisit(subRel, shouldUpdate) {
visit(c, filepath.Join(dir, sub), subRel, shouldUpdate)
}
var subdirs, regularFiles []string
for _, ent := range ents {
base := ent.Name()
if isBazelIgnored(path.Join(rel, base)) || wc.isExcluded(rel, base) {
continue
}
ent := resolveFileInfo(wc, dir, rel, ent)
switch {
case ent == nil:
continue
case ent.IsDir():
subdirs = append(subdirs, base)
default:
regularFiles = append(regularFiles, base)
}
}

update := !haveError && !wc.ignore && shouldUpdate
if updateRels.shouldCall(rel, updateParent) {
genFiles := findGenFiles(wc, f)
wf(dir, rel, c, update, f, subdirs, regularFiles, genFiles)
shouldUpdate := updateRels.shouldUpdate(rel, updateParent)
for _, sub := range subdirs {
if subRel := path.Join(rel, sub); updateRels.shouldVisit(subRel, shouldUpdate) {
visit(c, cexts, isBazelIgnored, knownDirectives, updateRels, wf, filepath.Join(dir, sub), subRel, shouldUpdate)
}
}
visit(c, c.RepoRoot, "", false)

update := !haveError && !wc.ignore && shouldUpdate
if updateRels.shouldCall(rel, updateParent) {
genFiles := findGenFiles(wc, f)
wf(dir, rel, c, update, f, subdirs, regularFiles, genFiles)
}
}

// An UpdateFilter tracks which directories need to be updated
Expand Down

0 comments on commit a1ae6fc

Please sign in to comment.