Skip to content

Commit

Permalink
feat: add gen
Browse files Browse the repository at this point in the history
  • Loading branch information
sashamelentyev committed Aug 7, 2022
1 parent 3619a08 commit 709520b
Show file tree
Hide file tree
Showing 17 changed files with 1,587 additions and 252 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/ci.yaml
Expand Up @@ -34,9 +34,6 @@ jobs:
- name: Go Mod
run: go mod download

- name: Go Generate
run: go generate ./... && git diff --exit-code

- name: Go Build
run: go build -v ./...

Expand Down
3 changes: 3 additions & 0 deletions main.go
Expand Up @@ -6,6 +6,9 @@ import (
"github.com/sashamelentyev/usestdlibvars/pkg/analyzer"
)

//go:generate go run pkg/analyzer/internal/gen.go
//go:generate gofmt -s -w .

func main() {
singlechecker.Main(analyzer.New())
}
16 changes: 9 additions & 7 deletions pkg/analyzer/analyzer.go
Expand Up @@ -9,6 +9,8 @@ import (
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/ast/inspector"

"github.com/sashamelentyev/usestdlibvars/pkg/analyzer/internal/mapping"
)

const (
Expand Down Expand Up @@ -158,45 +160,45 @@ func lookupFlag(pass *analysis.Pass, name string) bool {
func checkHTTPMethod(pass *analysis.Pass, basicLit *ast.BasicLit) {
currentVal := getBasicLitValue(basicLit)

if newVal, ok := httpMethod[currentVal]; ok {
if newVal, ok := mapping.HTTPMethod[currentVal]; ok {
report(pass, basicLit.Pos(), currentVal, newVal)
}
}

func checkHTTPStatusCode(pass *analysis.Pass, basicLit *ast.BasicLit) {
currentVal := getBasicLitValue(basicLit)

if newVal, ok := httpStatusCode[currentVal]; ok {
if newVal, ok := mapping.HTTPStatusCode[currentVal]; ok {
report(pass, basicLit.Pos(), currentVal, newVal)
}
}

func checkTimeWeekday(pass *analysis.Pass, pos token.Pos, currentVal string) {
if newVal, ok := timeWeekday[currentVal]; ok {
if newVal, ok := mapping.TimeWeekday[currentVal]; ok {
report(pass, pos, currentVal, newVal)
}
}

func checkTimeMonth(pass *analysis.Pass, pos token.Pos, currentVal string) {
if newVal, ok := timeMonth[currentVal]; ok {
if newVal, ok := mapping.TimeMonth[currentVal]; ok {
report(pass, pos, currentVal, newVal)
}
}

func checkTimeLayout(pass *analysis.Pass, pos token.Pos, currentVal string) {
if newVal, ok := timeLayout[currentVal]; ok {
if newVal, ok := mapping.TimeLayout[currentVal]; ok {
report(pass, pos, currentVal, newVal)
}
}

func checkCryptoHash(pass *analysis.Pass, pos token.Pos, currentVal string) {
if newVal, ok := cryptoHash[currentVal]; ok {
if newVal, ok := mapping.CryptoHash[currentVal]; ok {
report(pass, pos, currentVal, newVal)
}
}

func checkDefaultRPCPath(pass *analysis.Pass, pos token.Pos, currentVal string) {
if newVal, ok := defaultRPCPath[currentVal]; ok {
if newVal, ok := mapping.DefaultRPCPath[currentVal]; ok {
report(pass, pos, currentVal, newVal)
}
}
Expand Down
50 changes: 50 additions & 0 deletions pkg/analyzer/internal/gen.go
@@ -0,0 +1,50 @@
package main

import (
"embed"
"github.com/sashamelentyev/usestdlibvars/pkg/analyzer/internal/mapping"
"log"
"os"
"regexp"
"text/template"
)

type tmpl struct {
PackageName string
Mapping map[string]string
}

//go:embed template/*
var templateDir embed.FS

func main() {
t := template.Must(
template.New("template").
Funcs(map[string]any{"quoteMeta": regexp.QuoteMeta}).
ParseFS(templateDir, "template/*.tmpl"),
)
execute("pkg/analyzer/testdata/src/a/crypto/crypto.go", t, "template.tmpl", tmpl{PackageName: "crypto", Mapping: mapping.CryptoHash})
execute("pkg/analyzer/testdata/src/a/http/method.go", t, "httpmethod.tmpl", tmpl{PackageName: "http", Mapping: mapping.HTTPMethod})
execute("pkg/analyzer/testdata/src/a/http/statuscode.go", t, "httpstatuscode.tmpl", tmpl{PackageName: "http", Mapping: mapping.HTTPStatusCode})
execute("pkg/analyzer/testdata/src/a/rpc/rpc.go", t, "template.tmpl", tmpl{PackageName: "rpc", Mapping: mapping.DefaultRPCPath})
execute("pkg/analyzer/testdata/src/a/time/weekday.go", t, "template.tmpl", tmpl{PackageName: "time", Mapping: mapping.TimeWeekday})
execute("pkg/analyzer/testdata/src/a/time/month.go", t, "template.tmpl", tmpl{PackageName: "time", Mapping: mapping.TimeMonth})
execute("pkg/analyzer/testdata/src/a/time/layout.go", t, "template.tmpl", tmpl{PackageName: "time", Mapping: mapping.TimeLayout})
}

func execute(fileName string, t *template.Template, templateName string, data any) {
f, err := os.Create(fileName)
mustNil(err)
defer f.Close()
t.ExecuteTemplate(
f,
templateName,
data,
)
}

func mustNil(err error) {
if err != nil {
log.Panic(err)
}
}
@@ -1,4 +1,4 @@
package analyzer
package mapping

import (
"crypto"
Expand All @@ -8,7 +8,7 @@ import (
"time"
)

var cryptoHash = map[string]string{
var CryptoHash = map[string]string{
crypto.MD4.String(): "crypto.MD4.String()",
crypto.MD5.String(): "crypto.MD5.String()",
crypto.SHA1.String(): "crypto.SHA1.String()",
Expand All @@ -30,7 +30,7 @@ var cryptoHash = map[string]string{
crypto.BLAKE2b_512.String(): "crypto.BLAKE2b_512.String()",
}

var httpMethod = map[string]string{
var HTTPMethod = map[string]string{
http.MethodGet: "http.MethodGet",
http.MethodHead: "http.MethodHead",
http.MethodPost: "http.MethodPost",
Expand All @@ -42,7 +42,7 @@ var httpMethod = map[string]string{
http.MethodTrace: "http.MethodTrace",
}

var httpStatusCode = map[string]string{
var HTTPStatusCode = map[string]string{
strconv.Itoa(http.StatusContinue): "http.StatusContinue",
strconv.Itoa(http.StatusSwitchingProtocols): "http.StatusSwitchingProtocols",
strconv.Itoa(http.StatusProcessing): "http.StatusProcessing",
Expand Down Expand Up @@ -111,12 +111,12 @@ var httpStatusCode = map[string]string{
strconv.Itoa(http.StatusNetworkAuthenticationRequired): "http.StatusNetworkAuthenticationRequired",
}

var defaultRPCPath = map[string]string{
var DefaultRPCPath = map[string]string{
rpc.DefaultRPCPath: "rpc.DefaultRPCPath",
rpc.DefaultDebugPath: "rpc.DefaultDebugPath",
}

var timeWeekday = map[string]string{
var TimeWeekday = map[string]string{
time.Sunday.String(): "time.Sunday.String()",
time.Monday.String(): "time.Monday.String()",
time.Tuesday.String(): "time.Tuesday.String()",
Expand All @@ -126,7 +126,7 @@ var timeWeekday = map[string]string{
time.Saturday.String(): "time.Saturday.String()",
}

var timeMonth = map[string]string{
var TimeMonth = map[string]string{
time.January.String(): "time.January.String()",
time.February.String(): "time.February.String()",
time.March.String(): "time.March.String()",
Expand All @@ -141,7 +141,7 @@ var timeMonth = map[string]string{
time.December.String(): "time.December.String()",
}

var timeLayout = map[string]string{
var TimeLayout = map[string]string{
time.Layout: "time.Layout",
time.ANSIC: "time.ANSIC",
time.UnixDate: "time.UnixDate",
Expand Down
37 changes: 37 additions & 0 deletions pkg/analyzer/internal/template/httpmethod.tmpl
@@ -0,0 +1,37 @@
// Code generated by usestdlibvars, DO NOT EDIT.

package {{ .PackageName }}

import "net/http"

var (
{{- range $key, $value := .Mapping }}
_ = "{{ $key }}"
{{- end }}
)

const (
{{- range $key, $value := .Mapping }}
_ = "{{ $key }}"
{{- end }}
)

func _() {
{{- range $key, $value := .Mapping }}
_, _ = http.NewRequest("{{ $key }}", "", nil) // want `"{{ quoteMeta $key }}" can be replaced by {{ quoteMeta $value }}`
{{- end }}
}

func _() {
{{- range $key, $value := .Mapping }}
_, _ = http.NewRequestWithContext(nil, "{{ $key }}", "", nil) // want `"{{ quoteMeta $key }}" can be replaced by {{ quoteMeta $value }}`
{{- end }}
}

func _() {
{{- range $key, $value := .Mapping }}
_ = &http.Request{
Method: "{{ $key }}", // want `"{{ quoteMeta $key }}" can be replaced by {{ quoteMeta $value }}`
}
{{- end }}
}
31 changes: 31 additions & 0 deletions pkg/analyzer/internal/template/httpstatuscode.tmpl
@@ -0,0 +1,31 @@
// Code generated by usestdlibvars, DO NOT EDIT.

package {{ .PackageName }}

import "net/http"

var (
{{- range $key, $value := .Mapping }}
_ = {{ $key }}
{{- end }}
)

const (
{{- range $key, $value := .Mapping }}
_ = {{ $key }}
{{- end }}
)
{{ range $key, $value := .Mapping }}
func _() {
var w http.ResponseWriter
w.WriteHeader({{ $key }}) // want `"{{ quoteMeta $key }}" can be replaced by {{ quoteMeta $value }}`
}
{{ end -}}

{{ range $key, $value := .Mapping }}
func _() {
_ = &http.Response{
StatusCode: {{ $key }}, // want `"{{ quoteMeta $key }}" can be replaced by {{ quoteMeta $value }}`
}
}
{{ end -}}
33 changes: 33 additions & 0 deletions pkg/analyzer/internal/template/template.tmpl
@@ -0,0 +1,33 @@
// Code generated by usestdlibvars, DO NOT EDIT.

package {{ .PackageName }}

import "fmt"

var (
{{- range $key, $value := .Mapping }}
_ = "{{ $key }}" // want `"{{ quoteMeta $key }}" can be replaced by {{ quoteMeta $value }}`
{{- end }}
)

const (
{{- range $key, $value := .Mapping }}
_ = "{{ $key }}" // want `"{{ quoteMeta $key }}" can be replaced by {{ quoteMeta $value }}`
{{- end }}
)

func _() {
{{- range $key, $value := .Mapping }}
_ = func(s string)string{return s}("{{ $key }}") // want `"{{ quoteMeta $key }}" can be replaced by {{ quoteMeta $value }}`
_ = func(s string)string{return s}("text before key {{ $key }}")
_ = func(s string)string{return s}("{{ $key }} text after key")
{{- end }}
}

func _() {
{{- range $key, $value := .Mapping }}
_ = fmt.Sprint("{{ $key }}") // want `"{{ quoteMeta $key }}" can be replaced by {{ quoteMeta $value }}`
_ = fmt.Sprint("text before key {{ $key }}")
_ = fmt.Sprint("{{ $key }} text after key")
{{- end }}
}

0 comments on commit 709520b

Please sign in to comment.