Skip to content

Commit cc47a16

Browse files
feat: add pkgFunArgs
1 parent 6863d7f commit cc47a16

File tree

1 file changed

+76
-70
lines changed

1 file changed

+76
-70
lines changed

pkg/analyzer/analyzer.go

Lines changed: 76 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -60,84 +60,17 @@ func run(pass *analysis.Pass) (interface{}, error) {
6060
insp.Preorder(types, func(node ast.Node) {
6161
switch n := node.(type) {
6262
case *ast.CallExpr:
63-
selectorExpr, ok := n.Fun.(*ast.SelectorExpr)
63+
fun, ok := n.Fun.(*ast.SelectorExpr)
6464
if !ok {
6565
return
6666
}
6767

68-
ident, ok := selectorExpr.X.(*ast.Ident)
68+
pkg, ok := fun.X.(*ast.Ident)
6969
if !ok {
7070
return
7171
}
7272

73-
switch ident.Name {
74-
case "http":
75-
switch selectorExpr.Sel.Name {
76-
case "NewRequest":
77-
if !lookupFlag(pass, HTTPMethodFlag) {
78-
return
79-
}
80-
81-
if basicLit := getBasicLitFromArgs(n.Args, 3, 0, token.STRING); basicLit != nil {
82-
checkHTTPMethod(pass, basicLit)
83-
}
84-
85-
case "NewRequestWithContext":
86-
if !lookupFlag(pass, HTTPMethodFlag) {
87-
return
88-
}
89-
90-
if basicLit := getBasicLitFromArgs(n.Args, 4, 1, token.STRING); basicLit != nil {
91-
checkHTTPMethod(pass, basicLit)
92-
}
93-
94-
case "Error":
95-
if !lookupFlag(pass, HTTPStatusCodeFlag) {
96-
return
97-
}
98-
99-
if basicLit := getBasicLitFromArgs(n.Args, 3, 2, token.INT); basicLit != nil {
100-
checkHTTPStatusCode(pass, basicLit)
101-
}
102-
103-
case "StatusText":
104-
if !lookupFlag(pass, HTTPStatusCodeFlag) {
105-
return
106-
}
107-
108-
if basicLit := getBasicLitFromArgs(n.Args, 1, 0, token.INT); basicLit != nil {
109-
checkHTTPStatusCode(pass, basicLit)
110-
}
111-
112-
case "Redirect":
113-
if !lookupFlag(pass, HTTPStatusCodeFlag) {
114-
return
115-
}
116-
117-
if basicLit := getBasicLitFromArgs(n.Args, 4, 3, token.INT); basicLit != nil {
118-
checkHTTPStatusCode(pass, basicLit)
119-
}
120-
121-
case "RedirectHandler":
122-
if !lookupFlag(pass, HTTPStatusCodeFlag) {
123-
return
124-
}
125-
126-
if basicLit := getBasicLitFromArgs(n.Args, 2, 1, token.INT); basicLit != nil {
127-
checkHTTPStatusCode(pass, basicLit)
128-
}
129-
}
130-
default:
131-
if selectorExpr.Sel.Name == "WriteHeader" {
132-
if !lookupFlag(pass, HTTPStatusCodeFlag) {
133-
return
134-
}
135-
136-
if basicLit := getBasicLitFromArgs(n.Args, 1, 0, token.INT); basicLit != nil {
137-
checkHTTPStatusCode(pass, basicLit)
138-
}
139-
}
140-
}
73+
pkgFunArgs(pass, pkg, fun, n.Args)
14174

14275
case *ast.BasicLit:
14376
if lookupFlag(pass, TimeWeekdayFlag) {
@@ -281,6 +214,79 @@ func run(pass *analysis.Pass) (interface{}, error) {
281214
return nil, nil
282215
}
283216

217+
// pkgFunArgs checks arguments of function from package.
218+
func pkgFunArgs(pass *analysis.Pass, pkg *ast.Ident, fun *ast.SelectorExpr, args []ast.Expr) {
219+
switch pkg.Name {
220+
case "http":
221+
switch fun.Sel.Name {
222+
case "NewRequest":
223+
if !lookupFlag(pass, HTTPMethodFlag) {
224+
return
225+
}
226+
227+
if basicLit := getBasicLitFromArgs(args, 3, 0, token.STRING); basicLit != nil {
228+
checkHTTPMethod(pass, basicLit)
229+
}
230+
231+
case "NewRequestWithContext":
232+
if !lookupFlag(pass, HTTPMethodFlag) {
233+
return
234+
}
235+
236+
if basicLit := getBasicLitFromArgs(args, 4, 1, token.STRING); basicLit != nil {
237+
checkHTTPMethod(pass, basicLit)
238+
}
239+
240+
case "Error":
241+
if !lookupFlag(pass, HTTPStatusCodeFlag) {
242+
return
243+
}
244+
245+
if basicLit := getBasicLitFromArgs(args, 3, 2, token.INT); basicLit != nil {
246+
checkHTTPStatusCode(pass, basicLit)
247+
}
248+
249+
case "StatusText":
250+
if !lookupFlag(pass, HTTPStatusCodeFlag) {
251+
return
252+
}
253+
254+
if basicLit := getBasicLitFromArgs(args, 1, 0, token.INT); basicLit != nil {
255+
checkHTTPStatusCode(pass, basicLit)
256+
}
257+
258+
case "Redirect":
259+
if !lookupFlag(pass, HTTPStatusCodeFlag) {
260+
return
261+
}
262+
263+
if basicLit := getBasicLitFromArgs(args, 4, 3, token.INT); basicLit != nil {
264+
checkHTTPStatusCode(pass, basicLit)
265+
}
266+
267+
case "RedirectHandler":
268+
if !lookupFlag(pass, HTTPStatusCodeFlag) {
269+
return
270+
}
271+
272+
if basicLit := getBasicLitFromArgs(args, 2, 1, token.INT); basicLit != nil {
273+
checkHTTPStatusCode(pass, basicLit)
274+
}
275+
}
276+
default:
277+
if fun.Sel.Name == "WriteHeader" {
278+
if !lookupFlag(pass, HTTPStatusCodeFlag) {
279+
return
280+
}
281+
282+
if basicLit := getBasicLitFromArgs(args, 1, 0, token.INT); basicLit != nil {
283+
checkHTTPStatusCode(pass, basicLit)
284+
}
285+
}
286+
}
287+
}
288+
289+
// ifstmt checks X and Y in if-statement.
284290
func ifstmt(pass *analysis.Pass, x *ast.SelectorExpr, y *ast.BasicLit) {
285291
switch x.Sel.Name {
286292
case "StatusCode":

0 commit comments

Comments
 (0)