2020-04-05 11:50:50 +05:30
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package checks
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"golang.org/x/tools/go/analysis"
|
|
|
|
)
|
|
|
|
|
|
|
|
var Imports = &analysis.Analyzer{
|
|
|
|
Name: "imports",
|
2020-08-15 22:43:07 +05:30
|
|
|
Doc: "check for import order",
|
2020-04-05 11:50:50 +05:30
|
|
|
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
|
2020-08-15 22:43:07 +05:30
|
|
|
switch {
|
|
|
|
case importHasPrefix(val, "code.gitea.io"):
|
2020-04-05 11:50:50 +05:30
|
|
|
lvl = 2
|
2020-08-15 22:43:07 +05:30
|
|
|
case strings.Contains(val, "."):
|
2020-04-05 11:50:50 +05:30
|
|
|
lvl = 3
|
2020-08-15 22:43:07 +05:30
|
|
|
default:
|
2020-04-05 11:50:50 +05:30
|
|
|
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)
|
|
|
|
}
|