2020-04-05 01:20:50 -05:00
|
|
|
// 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 12:13:07 -05:00
|
|
|
Doc: "check for import order",
|
2020-04-05 01:20:50 -05:00
|
|
|
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 12:13:07 -05:00
|
|
|
switch {
|
|
|
|
case importHasPrefix(val, "code.gitea.io"):
|
2020-04-05 01:20:50 -05:00
|
|
|
lvl = 2
|
2020-08-15 12:13:07 -05:00
|
|
|
case strings.Contains(val, "."):
|
2020-04-05 01:20:50 -05:00
|
|
|
lvl = 3
|
2020-08-15 12:13:07 -05:00
|
|
|
default:
|
2020-04-05 01:20:50 -05:00
|
|
|
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)
|
|
|
|
}
|