You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
890 B
47 lines
890 B
5 years ago
|
// 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",
|
||
4 years ago
|
Doc: "check for import order",
|
||
5 years ago
|
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
|
||
4 years ago
|
switch {
|
||
|
case importHasPrefix(val, "code.gitea.io"):
|
||
5 years ago
|
lvl = 2
|
||
4 years ago
|
case strings.Contains(val, "."):
|
||
5 years ago
|
lvl = 3
|
||
4 years ago
|
default:
|
||
5 years ago
|
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)
|
||
|
}
|