Skip to content

Commit

Permalink
Encorce filePath/FilePath casing in bufstyle
Browse files Browse the repository at this point in the history
  • Loading branch information
bufdev committed Nov 21, 2023
1 parent 9491df8 commit 1e7f739
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
14 changes: 7 additions & 7 deletions private/bufpkg/bufimage/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ func newImage(files []ImageFile, reorder bool) (*image, error) {
return nil, errors.New("image contains no files")
}
pathToImageFile := make(map[string]ImageFile, len(files))
type commitAndFilepath struct {
type commitAndFilePath struct {
commit string
filepath string
filePath string
}
identityStringToCommitAndFilepath := make(map[string]commitAndFilepath)
identityStringToCommitAndFilePath := make(map[string]commitAndFilePath)
for _, file := range files {
path := file.Path()
if _, ok := pathToImageFile[path]; ok {
Expand All @@ -44,18 +44,18 @@ func newImage(files []ImageFile, reorder bool) (*image, error) {
pathToImageFile[path] = file
if moduleIdentity := file.ModuleIdentity(); moduleIdentity != nil {
identityString := moduleIdentity.IdentityString()
existing, ok := identityStringToCommitAndFilepath[identityString]
existing, ok := identityStringToCommitAndFilePath[identityString]
if ok {
if existing.commit != file.Commit() {
return nil, fmt.Errorf(
"files with different commits for the same module %s: %s:%s and %s:%s",
identityString, existing.filepath, existing.commit, path, file.Commit(),
identityString, existing.filePath, existing.commit, path, file.Commit(),
)
}
} else {
identityStringToCommitAndFilepath[identityString] = commitAndFilepath{
identityStringToCommitAndFilePath[identityString] = commitAndFilePath{
commit: file.Commit(),
filepath: path,
filePath: path,
}
}
}
Expand Down
19 changes: 19 additions & 0 deletions private/bufpkg/bufstyle/analyzers.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,24 @@ func newAnalyzers() []*analysis.Analyzer {
return nil, nil
},
},
{
Name: "FILEPATH_CASING",
Doc: "Verifies filePath or FilePath is used, not filepath or Filepath.",
Run: func(pass *analysis.Pass) (interface{}, error) {
if typesInfo := pass.TypesInfo; typesInfo != nil {
for _, object := range pass.TypesInfo.Defs {
if object != nil {
if strings.Contains(object.Name(), "Filepath") {
pass.Reportf(object.Pos(), `Use "FilePath" instead of "Filepath" in name %q`, object.Name())
}
if strings.Contains(object.Name(), "filepath") {
pass.Reportf(object.Pos(), `Use "filePath" instead of "filepath" in name %q`, object.Name())
}
}
}
}
return nil, nil
},
},
}
}

0 comments on commit 1e7f739

Please sign in to comment.