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 21, 2024
1 parent 4d93f01 commit f09460e
Showing 1 changed file with 49 additions and 49 deletions.
98 changes: 49 additions & 49 deletions walk/walk.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,65 +117,65 @@ func Walk(c *config.Config, cexts []config.Configurer, dirs []string, mode Mode,

updateRels := NewUpdateFilter(c.RepoRoot, dirs, mode)

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, 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, knownDirectives map[string]bool, updateRels *UpdateFilter, wf WalkFunc, dir, rel string, updateParent bool) {
haveError := false

c = configure(cexts, knownDirectives, c, rel, f)
wc := getWalkConfig(c)
// 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
}

if wc.isExcluded(rel, ".") {
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
}

var subdirs, regularFiles []string
for _, ent := range ents {
base := ent.Name()
ent := resolveFileInfo(wc, dir, rel, ent)
switch {
case ent == nil:
continue
case ent.IsDir():
subdirs = append(subdirs, base)
default:
regularFiles = append(regularFiles, base)
}
}
c = configure(cexts, knownDirectives, c, rel, f)
wc := getWalkConfig(c)

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)
}
if wc.isExcluded(rel, ".") {
return
}

var subdirs, regularFiles []string
for _, ent := range ents {
base := ent.Name()
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, 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 f09460e

Please sign in to comment.