Skip to content

Commit bc6bdf8

Browse files
feat: add http.NoBody (#34)
feat: add http.NoBody
1 parent 738e06f commit bc6bdf8

File tree

10 files changed

+127
-48
lines changed

10 files changed

+127
-48
lines changed

pkg/analyzer/analyzer.go

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const (
2020
CryptoHashFlag = "crypto-hash"
2121
HTTPMethodFlag = "http-method"
2222
HTTPStatusCodeFlag = "http-status-code"
23+
HTTPNoBodyFlag = "http-no-body"
2324
DefaultRPCPathFlag = "default-rpc-path"
2425
)
2526

@@ -38,6 +39,7 @@ func flags() flag.FlagSet {
3839
flags := flag.NewFlagSet("", flag.ExitOnError)
3940
flags.Bool(HTTPMethodFlag, true, "suggest the use of http.MethodXX")
4041
flags.Bool(HTTPStatusCodeFlag, true, "suggest the use of http.StatusXX")
42+
flags.Bool(HTTPNoBodyFlag, false, "suggest the use of http.NoBody")
4143
flags.Bool(TimeWeekdayFlag, false, "suggest the use of time.Weekday")
4244
flags.Bool(TimeMonthFlag, false, "suggest the use of time.Month")
4345
flags.Bool(TimeLayoutFlag, false, "suggest the use of time.Layout")
@@ -74,21 +76,29 @@ func run(pass *analysis.Pass) (interface{}, error) {
7476
}
7577

7678
case "NewRequest":
77-
if !lookupFlag(pass, HTTPMethodFlag) {
78-
return
79+
if lookupFlag(pass, HTTPMethodFlag) {
80+
if basicLit := getBasicLitFromArgs(n.Args, 3, 0, token.STRING); basicLit != nil {
81+
checkHTTPMethod(pass, basicLit)
82+
}
7983
}
8084

81-
if basicLit := getBasicLitFromArgs(n.Args, 3, 0, token.STRING); basicLit != nil {
82-
checkHTTPMethod(pass, basicLit)
85+
if lookupFlag(pass, HTTPNoBodyFlag) {
86+
if ident := getIdentFromArgs(n.Args, 3, 2); ident != nil {
87+
checkHTTPNoBody(pass, ident)
88+
}
8389
}
8490

8591
case "NewRequestWithContext":
86-
if !lookupFlag(pass, HTTPMethodFlag) {
87-
return
92+
if lookupFlag(pass, HTTPMethodFlag) {
93+
if basicLit := getBasicLitFromArgs(n.Args, 4, 1, token.STRING); basicLit != nil {
94+
checkHTTPMethod(pass, basicLit)
95+
}
8896
}
8997

90-
if basicLit := getBasicLitFromArgs(n.Args, 4, 1, token.STRING); basicLit != nil {
91-
checkHTTPMethod(pass, basicLit)
98+
if lookupFlag(pass, HTTPNoBodyFlag) {
99+
if ident := getIdentFromArgs(n.Args, 4, 3); ident != nil {
100+
checkHTTPNoBody(pass, ident)
101+
}
92102
}
93103
}
94104

@@ -173,6 +183,14 @@ func checkHTTPStatusCode(pass *analysis.Pass, basicLit *ast.BasicLit) {
173183
}
174184
}
175185

186+
func checkHTTPNoBody(pass *analysis.Pass, ident *ast.Ident) {
187+
currentVal := ident.Name
188+
189+
if newVal, ok := mapping.HTTPNoBody[currentVal]; ok {
190+
report(pass, ident.Pos(), currentVal, newVal)
191+
}
192+
}
193+
176194
func checkTimeWeekday(pass *analysis.Pass, pos token.Pos, currentVal string) {
177195
if newVal, ok := mapping.TimeWeekday[currentVal]; ok {
178196
report(pass, pos, currentVal, newVal)
@@ -226,6 +244,24 @@ func getBasicLitFromArgs(args []ast.Expr, count, idx int, typ token.Token) *ast.
226244
return basicLit
227245
}
228246

247+
// getIdentFromArgs gets the *ast.Ident of a function argument.
248+
//
249+
// Arguments:
250+
// - count - expected number of argument in function
251+
// - idx - index of the argument to get the *ast.Ident
252+
func getIdentFromArgs(args []ast.Expr, count, idx int) *ast.Ident {
253+
if len(args) != count {
254+
return nil
255+
}
256+
257+
ident, ok := args[idx].(*ast.Ident)
258+
if !ok {
259+
return nil
260+
}
261+
262+
return ident
263+
}
264+
229265
// getBasicLitFromElts gets the *ast.BasicLit of a struct elements.
230266
//
231267
// Arguments:

pkg/analyzer/analyzer_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ func TestUseStdlibVars(t *testing.T) {
3333
if err := a.Flags.Set(analyzer.DefaultRPCPathFlag, "true"); err != nil {
3434
t.Error(err)
3535
}
36+
if err := a.Flags.Set(analyzer.HTTPNoBodyFlag, "true"); err != nil {
37+
t.Error(err)
38+
}
3639

3740
analysistest.Run(t, analysistest.TestData(), a, pkgs...)
3841
}

pkg/analyzer/internal/gen.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,12 @@ func main() {
8383
templateName: "test-issue32.go.tmpl",
8484
fileName: "pkg/analyzer/testdata/src/a/http/issue32.go",
8585
},
86+
{
87+
mapping: mapping.HTTPNoBody,
88+
packageName: "http_test",
89+
templateName: "test-httpnobody.go.tmpl",
90+
fileName: "pkg/analyzer/testdata/src/a/http/nobody.go",
91+
},
8692
}
8793

8894
for _, operation := range operations {

pkg/analyzer/internal/mapping/mapping.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,3 +159,7 @@ var TimeLayout = map[string]string{
159159
time.StampMicro: "time.StampMicro",
160160
time.StampNano: "time.StampNano",
161161
}
162+
163+
var HTTPNoBody = map[string]string{
164+
"nil": "http.NoBody",
165+
}

pkg/analyzer/internal/template/test-httpmethod.go.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ const (
1818

1919
func _() {
2020
{{- range $key, $value := .Mapping }}
21-
_, _ = http.NewRequest("{{ $key }}", "", nil) // want `"{{ quoteMeta $key }}" can be replaced by {{ quoteMeta $value }}`
21+
_, _ = http.NewRequest("{{ $key }}", "", http.NoBody) // want `"{{ quoteMeta $key }}" can be replaced by {{ quoteMeta $value }}`
2222
{{- end }}
2323
}
2424

2525
func _() {
2626
{{- range $key, $value := .Mapping }}
27-
_, _ = http.NewRequestWithContext(nil, "{{ $key }}", "", nil) // want `"{{ quoteMeta $key }}" can be replaced by {{ quoteMeta $value }}`
27+
_, _ = http.NewRequestWithContext(nil, "{{ $key }}", "", http.NoBody) // want `"{{ quoteMeta $key }}" can be replaced by {{ quoteMeta $value }}`
2828
{{- end }}
2929
}
3030

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Code generated by usestdlibvars, DO NOT EDIT.
2+
3+
package {{ .PackageName }}
4+
5+
import "net/http"
6+
7+
func _() {
8+
{{- range $key, $value := .Mapping }}
9+
_, _ = http.NewRequest(http.MethodGet, "", {{ $key }}) // want `"{{ quoteMeta $key }}" can be replaced by {{ quoteMeta $value }}`
10+
{{- end }}
11+
}
12+
13+
func _() {
14+
{{- range $key, $value := .Mapping }}
15+
_, _ = http.NewRequestWithContext(nil, http.MethodGet, "", {{ $key }}) // want `"{{ quoteMeta $key }}" can be replaced by {{ quoteMeta $value }}`
16+
{{- end }}
17+
}

pkg/analyzer/internal/template/test-issue32.go.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ const (
1818

1919
func _() {
2020
{{- range $key, $value := .Mapping }}
21-
_, _ = http.NewRequest("{{ lower $key }}", "", nil) // want `"{{ quoteMeta $key }}" can be replaced by {{ quoteMeta $value }}`
21+
_, _ = http.NewRequest("{{ lower $key }}", "", http.NoBody) // want `"{{ quoteMeta $key }}" can be replaced by {{ quoteMeta $value }}`
2222
{{- end }}
2323
}
2424

2525
func _() {
2626
{{- range $key, $value := .Mapping }}
27-
_, _ = http.NewRequestWithContext(nil, "{{ lower $key }}", "", nil) // want `"{{ quoteMeta $key }}" can be replaced by {{ quoteMeta $value }}`
27+
_, _ = http.NewRequestWithContext(nil, "{{ lower $key }}", "", http.NoBody) // want `"{{ quoteMeta $key }}" can be replaced by {{ quoteMeta $value }}`
2828
{{- end }}
2929
}
3030

pkg/analyzer/testdata/src/a/http/issue32.go

Lines changed: 18 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/analyzer/testdata/src/a/http/method.go

Lines changed: 18 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/analyzer/testdata/src/a/http/nobody.go

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)