gitea-vet/checks/imports.go
Jason Song 4c10dca949
All checks were successful
continuous-integration/drone/push Build is passing
Check SPDX-License-Identifier (#21)
Check `SPDX-License-Identifier` in file headers.

Related to https://github.com/go-gitea/gitea/pull/21840

Co-authored-by: Jason Song <i@wolfogre.com>
Reviewed-on: #21
Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
Reviewed-by: Xinyu Zhou <i@sourcehut.net>
Co-authored-by: Jason Song <wolfogre@noreply.gitea.io>
Co-committed-by: Jason Song <wolfogre@noreply.gitea.io>
2022-12-01 23:58:05 +08:00

46 lines
818 B
Go

// Copyright 2020 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package checks
import (
"strings"
"golang.org/x/tools/go/analysis"
)
var Imports = &analysis.Analyzer{
Name: "imports",
Doc: "check for import order",
Run: runImports,
}
func runImports(pass *analysis.Pass) (interface{}, error) {
for _, file := range pass.Files {
level := 0
for _, im := range file.Imports {
var lvl int
val := im.Path.Value
switch {
case importHasPrefix(val, "code.gitea.io"):
lvl = 2
case strings.Contains(val, "."):
lvl = 3
default:
lvl = 1
}
if lvl < level {
pass.Reportf(file.Pos(), "Imports are sorted wrong")
break
}
level = lvl
}
}
return nil, nil
}
func importHasPrefix(s, p string) bool {
return strings.HasPrefix(s, "\""+p)
}