@@ -44,10 +44,12 @@ func run(pass *analysis.Pass) (interface{}, error) {
4444 if importStmt .Name == nil {
4545 return
4646 }
47+
4748 alias := importStmt .Name .Name
4849 if alias == "" {
4950 return
5051 }
52+
5153 aliasSlice := strings .Split (alias , "_" )
5254 path := strings .ReplaceAll (importStmt .Path .Value , "\" " , "" )
5355 // replace all separators with `/` for normalization
@@ -60,49 +62,42 @@ func run(pass *analysis.Pass) (interface{}, error) {
6062 if ! checkVersion (aliasSlice [len (aliasSlice )- 1 ], pathSlice ) {
6163 applicableAlias := getAliasFix (pathSlice )
6264 _ , 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- )
65+ pass .Report (analysis.Diagnostic {
66+ Pos : node .Pos (),
67+ Message : fmt .Sprintf ("version %q not specified in alias %q for import path %q" ,
68+ pathSlice [versionIndex ], alias , path ),
69+ SuggestedFixes : []analysis.SuggestedFix {{
70+ Message : fmt .Sprintf ("should replace %q with %q" , alias , applicableAlias ),
71+ TextEdits : []analysis.TextEdit {{
72+ Pos : importStmt .Pos (),
73+ End : importStmt .Name .End (),
74+ NewText : []byte (applicableAlias ),
75+ }},
76+ }},
77+ })
78+
8179 return
8280 }
83- if error := checkAliasName (aliasSlice , pathSlice , pass ); error != nil {
81+
82+ if err := checkAliasName (aliasSlice , pathSlice , pass ); err != nil {
8483 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- )
84+ pass .Report (analysis.Diagnostic {
85+ Pos : node .Pos (),
86+ Message : err .Error (),
87+ SuggestedFixes : []analysis.SuggestedFix {{
88+ Message : fmt .Sprintf ("should replace %q with %q" , alias , applicableAlias ),
89+ TextEdits : []analysis.TextEdit {{
90+ Pos : importStmt .Pos (),
91+ End : importStmt .Name .End (),
92+ NewText : []byte (applicableAlias ),
93+ }},
94+ }},
95+ })
96+
10397 return
10498 }
10599 })
100+
106101 return nil , nil
107102}
108103
@@ -112,13 +107,14 @@ func checkVersion(aliasLastWord string, pathSlice []string) bool {
112107 if ! versionExists {
113108 return true
114109 }
115- return aliasLastWord == pathSlice [versionPos ]
116110
111+ return aliasLastWord == pathSlice [versionPos ]
117112}
118113
119114// checkAliasName check consistency in alias name
120115func checkAliasName (aliasSlice []string , pathSlice []string , pass * analysis.Pass ) error {
121116 lastUsedWordIndex := - 1
117+
122118 for _ , name := range aliasSlice {
123119 // we don't check version rule here
124120 if strings .HasPrefix (name , "v" ) || name == "" {
@@ -146,15 +142,18 @@ func checkAliasName(aliasSlice []string, pathSlice []string, pass *analysis.Pass
146142
147143func getAliasFix (pathSlice []string ) string {
148144 versionExists , versionPos := packageVersion (pathSlice )
145+
149146 if ! versionExists {
150147 return pathSlice [len (pathSlice )- 1 ]
151148 }
149+
152150 if versionPos == len (pathSlice )- 1 {
153151 applicableAlias := pathSlice [len (pathSlice )- 2 ] + "_" + pathSlice [versionPos ]
154152 return applicableAlias
155153 }
156154
157155 applicableAlias := pathSlice [len (pathSlice )- 1 ] + "_" + pathSlice [versionPos ]
156+
158157 return applicableAlias
159158}
160159
@@ -166,6 +165,7 @@ func packageVersion(pathSlice []string) (bool, int) {
166165 return true , pos
167166 }
168167 }
168+
169169 return false , 0
170170}
171171
@@ -176,5 +176,6 @@ func searchString(slice []string, word string) int {
176176 return pos
177177 }
178178 }
179+
179180 return len (slice )
180181}
0 commit comments