Skip to content

Commit 41063b7

Browse files
NoamTDmvdan
authored andcommitted
cue/load: clean up some var names and code organization
This CL: - improves naming of a few methods - removes an unnecessary method - moves some var declarations closer to their usage Signed-off-by: Noam Dolovich <[email protected]> Change-Id: Ia94b73088355565a94461db95806232eb7842fa3 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1193882 TryBot-Result: CUEcueckoo <[email protected]> Unity-Result: CUE porcuepine <[email protected]> Reviewed-by: Daniel Martí <[email protected]>
1 parent 35af5a1 commit 41063b7

File tree

3 files changed

+12
-20
lines changed

3 files changed

+12
-20
lines changed

cue/load/config.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ func (c Config) complete() (cfg *Config, err error) {
371371
if c.ModuleRoot == "" {
372372
// Only consider the current directory by default
373373
c.ModuleRoot = c.Dir
374-
if root := c.findRoot(c.Dir); root != "" {
374+
if root := c.findModRoot(c.Dir); root != "" {
375375
c.ModuleRoot = root
376376
}
377377
} else if !filepath.IsAbs(c.ModuleRoot) {
@@ -442,19 +442,18 @@ func (c *Config) loadModule() error {
442442
return nil
443443
}
444444

445-
func (c Config) isRoot(dir string) bool {
446-
fs := &c.fileSystem
445+
func (c Config) isModRoot(dir string) bool {
447446
// Note: cue.mod used to be a file. We still allow both to match.
448-
_, err := fs.stat(filepath.Join(dir, modDir))
447+
_, err := c.fileSystem.stat(filepath.Join(dir, modDir))
449448
return err == nil
450449
}
451450

452-
// findRoot returns the module root that's ancestor
451+
// findModRoot returns the module root that's ancestor
453452
// of the given absolute directory path, or "" if none was found.
454-
func (c Config) findRoot(absDir string) string {
453+
func (c Config) findModRoot(absDir string) string {
455454
abs := absDir
456455
for {
457-
if c.isRoot(abs) {
456+
if c.isModRoot(abs) {
458457
return abs
459458
}
460459
d := filepath.Dir(abs)

cue/load/fs.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,6 @@ func (fs *fileSystem) init(cwd string, overlay map[string]Source) error {
134134
return nil
135135
}
136136

137-
func (fs *fileSystem) joinPath(elem ...string) string {
138-
return filepath.Join(elem...)
139-
}
140-
141137
func (fs *fileSystem) makeAbs(path string) string {
142138
if filepath.IsAbs(path) {
143139
return path
@@ -270,7 +266,7 @@ func (fs *fileSystem) walkRec(path string, entry iofs.DirEntry, f walkFunc) erro
270266
}
271267

272268
for _, entry := range dir {
273-
filename := fs.joinPath(path, entry.Name())
269+
filename := filepath.Join(path, entry.Name())
274270
err = fs.walkRec(filename, entry, f)
275271
if err != nil {
276272
if !entry.IsDir() || err != skipDir {
@@ -335,7 +331,7 @@ func (fs *ioFS) ReadDir(name string) ([]iofs.DirEntry, error) {
335331
return fs.fs.readDir(fpath)
336332
}
337333

338-
// ReadDir implements [io/fs.ReadFileFS].
334+
// ReadFile implements [io/fs.ReadFileFS].
339335
func (fs *ioFS) ReadFile(name string) ([]byte, error) {
340336
fpath, err := fs.absPathFromFSPath(name)
341337
if err != nil {

cue/load/import.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ func (l *loader) importPkg(pos token.Pos, p *build.Instance) []*build.Instance {
131131
// have the same module scope and that there are no invalid modules.
132132
inModule := false // if pkg == "_"
133133
for _, d := range dirs {
134-
if l.cfg.findRoot(d[1]) != "" {
134+
if l.cfg.findModRoot(d[1]) != "" {
135135
inModule = true
136136
break
137137
}
@@ -156,7 +156,7 @@ func (l *loader) importPkg(pos token.Pos, p *build.Instance) []*build.Instance {
156156
fp.add(dir, &bf, importComment)
157157
}
158158

159-
if p.PkgName == "" || !inModule || l.cfg.isRoot(dir) || dir == d[0] {
159+
if p.PkgName == "" || !inModule || l.cfg.isModRoot(dir) || dir == d[0] {
160160
break
161161
}
162162

@@ -259,23 +259,20 @@ func (l *loader) newRelInstance(pos token.Pos, path, pkgName string) *build.Inst
259259
panic(fmt.Errorf("non-relative import path %q passed to newRelInstance", path))
260260
}
261261

262-
var err errors.Error
263-
dir := path
264-
265262
p := l.cfg.Context.NewInstance(path, l.loadFunc)
266263
p.PkgName = pkgName
267264
p.DisplayPath = filepath.ToSlash(path)
268265
// p.ImportPath = string(dir) // compute unique ID.
269266
p.Root = l.cfg.ModuleRoot
270267
p.Module = l.cfg.Module
271268

272-
dir = filepath.Join(l.cfg.Dir, filepath.FromSlash(path))
273-
269+
var err errors.Error
274270
if path != cleanImport(path) {
275271
err = errors.Append(err, l.errPkgf(nil,
276272
"non-canonical import path: %q should be %q", path, pathpkg.Clean(path)))
277273
}
278274

275+
dir := filepath.Join(l.cfg.Dir, filepath.FromSlash(path))
279276
if importPath, e := l.importPathFromAbsDir(fsPath(dir), path); e != nil {
280277
// Detect later to keep error messages consistent.
281278
} else {

0 commit comments

Comments
 (0)