|
| 1 | +// Copyright Project Contour Authors |
| 2 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +// you may not use this file except in compliance with the License. |
| 4 | +// You may obtain a copy of the License at |
| 5 | +// |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +package importalias |
| 15 | + |
| 16 | +import ( |
| 17 | + "fmt" |
| 18 | + "go/ast" |
| 19 | + "regexp" |
| 20 | + "strings" |
| 21 | + |
| 22 | + "golang.org/x/tools/go/analysis" |
| 23 | + "golang.org/x/tools/go/analysis/passes/inspect" |
| 24 | + "golang.org/x/tools/go/ast/inspector" |
| 25 | +) |
| 26 | + |
| 27 | +// Analyzer for import aliases |
| 28 | +var Analyzer = &analysis.Analyzer{ |
| 29 | + Name: "importalias", |
| 30 | + Doc: "Checks import aliases have consistent names", |
| 31 | + Run: run, |
| 32 | + Requires: []*analysis.Analyzer{inspect.Analyzer}, |
| 33 | +} |
| 34 | + |
| 35 | +func run(pass *analysis.Pass) (interface{}, error) { |
| 36 | + inspector := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) |
| 37 | + nodeFilter := []ast.Node{ // filter needed nodes: visit only them |
| 38 | + (*ast.ImportSpec)(nil), |
| 39 | + } |
| 40 | + |
| 41 | + inspector.Preorder(nodeFilter, func(node ast.Node) { |
| 42 | + importStmt := node.(*ast.ImportSpec) |
| 43 | + |
| 44 | + if importStmt.Name == nil { |
| 45 | + return |
| 46 | + } |
| 47 | + alias := importStmt.Name.Name |
| 48 | + if alias == "" { |
| 49 | + return |
| 50 | + } |
| 51 | + aliasSlice := strings.Split(alias, "_") |
| 52 | + path := strings.ReplaceAll(importStmt.Path.Value, "\"", "") |
| 53 | + // replace all separators with `/` for normalization |
| 54 | + path = strings.ReplaceAll(path, "_", "/") |
| 55 | + path = strings.ReplaceAll(path, ".", "/") |
| 56 | + path = strings.ReplaceAll(path, "-", "") |
| 57 | + // omit the domain name in path |
| 58 | + pathSlice := strings.Split(path, "/")[1:] |
| 59 | + |
| 60 | + if !checkVersion(aliasSlice[len(aliasSlice)-1], pathSlice) { |
| 61 | + applicableAlias := getAliasFix(pathSlice) |
| 62 | + _, versionIndex := packageVersion(pathSlice) |
| 63 | + pass.Report( |
| 64 | + analysis.Diagnostic{ |
| 65 | + Pos: node.Pos(), |
| 66 | + Message: fmt.Sprintf("version %q not specified in alias %q for import path %q", pathSlice[versionIndex], alias, path), |
| 67 | + SuggestedFixes: []analysis.SuggestedFix{ |
| 68 | + { |
| 69 | + Message: fmt.Sprintf("should replace %q with %q", alias, applicableAlias), |
| 70 | + TextEdits: []analysis.TextEdit{ |
| 71 | + { |
| 72 | + Pos: importStmt.Pos(), |
| 73 | + End: importStmt.Name.End(), |
| 74 | + NewText: []byte(applicableAlias), |
| 75 | + }, |
| 76 | + }, |
| 77 | + }, |
| 78 | + }, |
| 79 | + }, |
| 80 | + ) |
| 81 | + return |
| 82 | + } |
| 83 | + if error := checkAliasName(aliasSlice, pathSlice, pass); error != nil { |
| 84 | + applicableAlias := getAliasFix(pathSlice) |
| 85 | + pass.Report( |
| 86 | + analysis.Diagnostic{ |
| 87 | + Pos: node.Pos(), |
| 88 | + Message: error.Error(), |
| 89 | + SuggestedFixes: []analysis.SuggestedFix{ |
| 90 | + { |
| 91 | + Message: fmt.Sprintf("should replace %q with %q", alias, applicableAlias), |
| 92 | + TextEdits: []analysis.TextEdit{ |
| 93 | + { |
| 94 | + Pos: importStmt.Pos(), |
| 95 | + End: importStmt.Name.End(), |
| 96 | + NewText: []byte(applicableAlias), |
| 97 | + }, |
| 98 | + }, |
| 99 | + }, |
| 100 | + }, |
| 101 | + }, |
| 102 | + ) |
| 103 | + return |
| 104 | + } |
| 105 | + }) |
| 106 | + return nil, nil |
| 107 | +} |
| 108 | + |
| 109 | +// checkVersion checks that if package name starts with `v` it's included in alias name |
| 110 | +func checkVersion(aliasLastWord string, pathSlice []string) bool { |
| 111 | + versionExists, versionPos := packageVersion(pathSlice) |
| 112 | + if !versionExists { |
| 113 | + return true |
| 114 | + } |
| 115 | + return aliasLastWord == pathSlice[versionPos] |
| 116 | + |
| 117 | +} |
| 118 | + |
| 119 | +// checkAliasName check consistency in alias name |
| 120 | +func checkAliasName(aliasSlice []string, pathSlice []string, pass *analysis.Pass) error { |
| 121 | + lastUsedWordIndex := -1 |
| 122 | + for _, name := range aliasSlice { |
| 123 | + // we don't check version rule here |
| 124 | + if strings.HasPrefix(name, "v") || name == "" { |
| 125 | + continue |
| 126 | + } |
| 127 | + usedWordIndex := searchString(pathSlice, name) |
| 128 | + |
| 129 | + if usedWordIndex == len(pathSlice) { |
| 130 | + return fmt.Errorf("alias %q does not contain any words from import path %q", strings.Join(aliasSlice, "_"), strings.Join(pathSlice, "/")) |
| 131 | + } |
| 132 | + |
| 133 | + if usedWordIndex <= lastUsedWordIndex { |
| 134 | + return fmt.Errorf("alias %q does not match word order from import path %q", strings.Join(aliasSlice, "_"), strings.Join(pathSlice, "/")) |
| 135 | + } |
| 136 | + |
| 137 | + lastUsedWordIndex = usedWordIndex |
| 138 | + } |
| 139 | + |
| 140 | + if lastUsedWordIndex == -1 { |
| 141 | + return fmt.Errorf("alias %q uses words that are not in path %q", strings.Join(aliasSlice, "_"), strings.Join(pathSlice, "/")) |
| 142 | + } |
| 143 | + |
| 144 | + return nil |
| 145 | +} |
| 146 | + |
| 147 | +func getAliasFix(pathSlice []string) string { |
| 148 | + versionExists, versionPos := packageVersion(pathSlice) |
| 149 | + if !versionExists { |
| 150 | + return pathSlice[len(pathSlice)-1] |
| 151 | + } |
| 152 | + if versionPos == len(pathSlice)-1 { |
| 153 | + applicableAlias := pathSlice[len(pathSlice)-2] + "_" + pathSlice[versionPos] |
| 154 | + return applicableAlias |
| 155 | + } |
| 156 | + |
| 157 | + applicableAlias := pathSlice[len(pathSlice)-1] + "_" + pathSlice[versionPos] |
| 158 | + return applicableAlias |
| 159 | +} |
| 160 | + |
| 161 | +// packageVersion returns if some version specification exists in import path and it's position |
| 162 | +func packageVersion(pathSlice []string) (bool, int) { |
| 163 | + for pos, value := range pathSlice { |
| 164 | + r, _ := regexp.Compile("^v[0-9]+$") |
| 165 | + if r.MatchString(value) { |
| 166 | + return true, pos |
| 167 | + } |
| 168 | + } |
| 169 | + return false, 0 |
| 170 | +} |
| 171 | + |
| 172 | +func searchString(slice []string, word string) int { |
| 173 | + for pos, value := range slice { |
| 174 | + r, _ := regexp.Compile(word + "(s)?") |
| 175 | + if r.MatchString(value) { |
| 176 | + return pos |
| 177 | + } |
| 178 | + } |
| 179 | + return len(slice) |
| 180 | +} |
0 commit comments