func detectNamingConvention()

in language/go/config.go [681:755]


func detectNamingConvention(c *config.Config, rootFile *rule.File) namingConvention {
	if !c.IndexLibraries {
		// Indexing is disabled, which usually means speed is important and I/O
		// should be minimized. Let's not open extra files or directories.
		return importNamingConvention
	}

	detectInFile := func(f *rule.File) namingConvention {
		for _, r := range f.Rules {
			// NOTE: map_kind is not supported. c.KindMap will not be accurate in
			// subdirectories.
			kind := r.Kind()
			name := r.Name()
			if kind != "alias" && name == defaultLibName {
				// Assume any kind of rule with the name "go_default_library" is some
				// kind of go library. The old version of go_proto_library used this
				// name, and it's possible with map_kind as well.
				return goDefaultLibraryNamingConvention
			} else if isGoLibrary(kind) && name == path.Base(r.AttrString("importpath")) {
				return importNamingConvention
			}
		}
		return unknownNamingConvention
	}

	detectInDir := func(dir, rel string) namingConvention {
		var f *rule.File
		for _, name := range c.ValidBuildFileNames {
			fpath := filepath.Join(dir, name)
			data, err := ioutil.ReadFile(fpath)
			if err != nil {
				continue
			}
			f, err = rule.LoadData(fpath, rel, data)
			if err != nil {
				continue
			}
		}
		if f == nil {
			return unknownNamingConvention
		}
		return detectInFile(f)
	}

	nc := unknownNamingConvention
	if rootFile != nil {
		if rootNC := detectInFile(rootFile); rootNC != unknownNamingConvention {
			return rootNC
		}
	}

	infos, err := ioutil.ReadDir(c.RepoRoot)
	if err != nil {
		return importNamingConvention
	}
	for _, info := range infos {
		if !info.IsDir() {
			continue
		}
		dirName := info.Name()
		dirNC := detectInDir(filepath.Join(c.RepoRoot, dirName), dirName)
		if dirNC == unknownNamingConvention {
			continue
		}
		if nc != unknownNamingConvention && dirNC != nc {
			// Subdirectories use different conventions. Return the default.
			return importNamingConvention
		}
		nc = dirNC
	}
	if nc == unknownNamingConvention {
		return importNamingConvention
	}
	return nc
}