Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add suggested fixes support #100

Merged
merged 2 commits into from
Dec 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@ on:
branches: [ main ]

env:
GO_VERSION: 1.20.6
GO_VERSION: stable

jobs:
run:
runs-on: ubuntu-latest
strategy:
matrix:
go: [ '1.20', '1.21', '1.22' ]
go: [ stable, oldstable ]

steps:
- name: Checkout code
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Install Go
uses: actions/setup-go@v3
uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go }}

Expand All @@ -35,7 +35,7 @@ jobs:
run: go test -v -race ./...

- name: Lint
uses: golangci/golangci-lint-action@v3.2.0
uses: golangci/golangci-lint-action@v6
with:
version: latest
args: --timeout 5m
Expand All @@ -44,10 +44,10 @@ jobs:
needs: run
runs-on: windows-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Install Go
uses: actions/setup-go@v3
uses: actions/setup-go@v5
with:
go-version: ${{ env.GO_VERSION }}

Expand All @@ -58,10 +58,10 @@ jobs:
needs: run
runs-on: macos-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Install Go
uses: actions/setup-go@v3
uses: actions/setup-go@v5
with:
go-version: ${{ env.GO_VERSION }}

Expand Down
35 changes: 23 additions & 12 deletions pkg/analyzer/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package analyzer

import (
"flag"
"fmt"
"go/ast"
"go/token"
"strings"
Expand Down Expand Up @@ -364,55 +365,55 @@ func checkHTTPMethod(pass *analysis.Pass, basicLit *ast.BasicLit) {
key := strings.ToUpper(currentVal)

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

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

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

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

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

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

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

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

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

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

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

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

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

Expand All @@ -422,23 +423,23 @@ func checkSQLIsolationLevel(pass *analysis.Pass, basicLit *ast.BasicLit) {
currentVal := getBasicLitValue(basicLit)

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

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

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

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

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

Expand Down Expand Up @@ -514,6 +515,16 @@ func getBasicLitValue(basicLit *ast.BasicLit) string {
return val.String()
}

func report(pass *analysis.Pass, pos token.Pos, currentVal, newVal string) {
pass.Reportf(pos, "%q can be replaced by %s", currentVal, newVal)
func report(pass *analysis.Pass, rg analysis.Range, currentVal, newVal string) {
pass.Report(analysis.Diagnostic{
Pos: rg.Pos(),
Message: fmt.Sprintf("%q can be replaced by %s", currentVal, newVal),
SuggestedFixes: []analysis.SuggestedFix{{
TextEdits: []analysis.TextEdit{{
Pos: rg.Pos(),
End: rg.End(),
NewText: []byte(newVal),
}},
}},
})
}
28 changes: 17 additions & 11 deletions pkg/analyzer/analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,6 @@ var flags = []string{
analyzer.ConstantKindFlag,
}

var pkgs = []string{
"a/crypto",
"a/http",
"a/rpc",
"a/time",
"a/sql",
"a/tls",
"a/constant",
}

func TestUseStdlibVars(t *testing.T) {
a := analyzer.New()

Expand All @@ -38,5 +28,21 @@ func TestUseStdlibVars(t *testing.T) {
}
}

analysistest.Run(t, analysistest.TestData(), a, pkgs...)
testCases := []struct {
dir string
}{
{dir: "a/crypto"},
{dir: "a/http"},
{dir: "a/rpc"},
{dir: "a/time"},
{dir: "a/sql"},
{dir: "a/tls"},
{dir: "a/constant"},
}

for _, test := range testCases {
t.Run(test.dir, func(t *testing.T) {
analysistest.RunWithSuggestedFixes(t, analysistest.TestData(), a, test.dir)
})
}
}
Loading
Loading