1
1
package tenv
2
2
3
3
import (
4
+ "fmt"
4
5
"go/ast"
5
6
"strings"
6
7
@@ -51,15 +52,15 @@ func run(pass *analysis.Pass) (interface{}, error) {
51
52
}
52
53
53
54
func checkFuncDecl (pass * analysis.Pass , f * ast.FuncDecl , fileName string ) {
54
- argName , ok := targetRunner (f .Type .Params .List , fileName )
55
+ argName , ok := targetRunner (pass , f .Type .Params .List , fileName )
55
56
if ! ok {
56
57
return
57
58
}
58
59
checkStmts (pass , f .Body .List , f .Name .Name , argName )
59
60
}
60
61
61
62
func checkFuncLit (pass * analysis.Pass , f * ast.FuncLit , fileName string ) {
62
- argName , ok := targetRunner (f .Type .Params .List , fileName )
63
+ argName , ok := targetRunner (pass , f .Type .Params .List , fileName )
63
64
if ! ok {
64
65
return
65
66
}
@@ -97,11 +98,8 @@ func checkExprStmt(pass *analysis.Pass, stmt *ast.ExprStmt, funcName, argName st
97
98
if ! ok {
98
99
return false
99
100
}
100
- x , ok := fun .X .(* ast.Ident )
101
- if ! ok {
102
- return false
103
- }
104
- targetName := x .Name + "." + fun .Sel .Name
101
+ obj := pass .TypesInfo .ObjectOf (fun .Sel )
102
+ targetName := fmt .Sprintf ("%s.%s" , obj .Pkg ().Name (), obj .Name ())
105
103
if targetName == "os.Setenv" {
106
104
if argName == "" {
107
105
argName = "testing"
@@ -121,11 +119,8 @@ func checkArgs(pass *analysis.Pass, args []ast.Expr, funcName, argName string) {
121
119
if ! ok {
122
120
continue
123
121
}
124
- x , ok := fun .X .(* ast.Ident )
125
- if ! ok {
126
- continue
127
- }
128
- targetName := x .Name + "." + fun .Sel .Name
122
+ obj := pass .TypesInfo .ObjectOf (fun .Sel )
123
+ targetName := fmt .Sprintf ("%s.%s" , obj .Pkg ().Name (), obj .Name ())
129
124
if targetName == "os.Setenv" {
130
125
if argName == "" {
131
126
argName = "testing"
@@ -148,11 +143,8 @@ func checkIfStmt(pass *analysis.Pass, stmt *ast.IfStmt, funcName, argName string
148
143
if ! ok {
149
144
return false
150
145
}
151
- x , ok := fun .X .(* ast.Ident )
152
- if ! ok {
153
- return false
154
- }
155
- targetName := x .Name + "." + fun .Sel .Name
146
+ obj := pass .TypesInfo .ObjectOf (fun .Sel )
147
+ targetName := fmt .Sprintf ("%s.%s" , obj .Pkg ().Name (), obj .Name ())
156
148
if targetName == "os.Setenv" {
157
149
if argName == "" {
158
150
argName = "testing"
@@ -171,11 +163,8 @@ func checkAssignStmt(pass *analysis.Pass, stmt *ast.AssignStmt, funcName, argNam
171
163
if ! ok {
172
164
return false
173
165
}
174
- x , ok := fun .X .(* ast.Ident )
175
- if ! ok {
176
- return false
177
- }
178
- targetName := x .Name + "." + fun .Sel .Name
166
+ obj := pass .TypesInfo .ObjectOf (fun .Sel )
167
+ targetName := fmt .Sprintf ("%s.%s" , obj .Pkg ().Name (), obj .Name ())
179
168
if targetName == "os.Setenv" {
180
169
if argName == "" {
181
170
argName = "testing"
@@ -189,19 +178,19 @@ func checkForStmt(pass *analysis.Pass, stmt *ast.ForStmt, funcName, argName stri
189
178
checkStmts (pass , stmt .Body .List , funcName , argName )
190
179
}
191
180
192
- func targetRunner (params []* ast.Field , fileName string ) (string , bool ) {
181
+ func targetRunner (pass * analysis. Pass , params []* ast.Field , fileName string ) (string , bool ) {
193
182
for _ , p := range params {
194
183
switch typ := p .Type .(type ) {
195
184
case * ast.StarExpr :
196
- if checkStarExprTarget (typ ) {
185
+ if checkStarExprTarget (pass , typ ) {
197
186
if len (p .Names ) == 0 {
198
187
return "" , false
199
188
}
200
189
argName := p .Names [0 ].Name
201
190
return argName , true
202
191
}
203
192
case * ast.SelectorExpr :
204
- if checkSelectorExprTarget (typ ) {
193
+ if checkSelectorExprTarget (pass , typ ) {
205
194
if len (p .Names ) == 0 {
206
195
return "" , false
207
196
}
@@ -216,29 +205,19 @@ func targetRunner(params []*ast.Field, fileName string) (string, bool) {
216
205
return "" , false
217
206
}
218
207
219
- func checkStarExprTarget (typ * ast.StarExpr ) bool {
208
+ func checkStarExprTarget (pass * analysis. Pass , typ * ast.StarExpr ) bool {
220
209
selector , ok := typ .X .(* ast.SelectorExpr )
221
210
if ! ok {
222
211
return false
223
212
}
224
- x , ok := selector .X .(* ast.Ident )
225
- if ! ok {
226
- return false
227
- }
228
- targetName := x .Name + "." + selector .Sel .Name
229
- switch targetName {
213
+ switch pass .TypesInfo .TypeOf (selector ).String () {
230
214
case "testing.T" , "testing.B" :
231
215
return true
232
216
default :
233
217
return false
234
218
}
235
219
}
236
220
237
- func checkSelectorExprTarget (typ * ast.SelectorExpr ) bool {
238
- x , ok := typ .X .(* ast.Ident )
239
- if ! ok {
240
- return false
241
- }
242
- targetName := x .Name + "." + typ .Sel .Name
243
- return targetName == "testing.TB"
221
+ func checkSelectorExprTarget (pass * analysis.Pass , typ * ast.SelectorExpr ) bool {
222
+ return pass .TypesInfo .TypeOf (typ ).String () == "testing.TB"
244
223
}
0 commit comments