Skip to content

Commit 3607976

Browse files
authored
feat: add suggested fixes support (#100)
1 parent a7b3ee4 commit 3607976

22 files changed

+5515
-91
lines changed

.github/workflows/ci.yaml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,21 @@ on:
77
branches: [ main ]
88

99
env:
10-
GO_VERSION: 1.20.6
10+
GO_VERSION: stable
1111

1212
jobs:
1313
run:
1414
runs-on: ubuntu-latest
1515
strategy:
1616
matrix:
17-
go: [ '1.20', '1.21', '1.22' ]
17+
go: [ stable, oldstable ]
1818

1919
steps:
2020
- name: Checkout code
21-
uses: actions/checkout@v3
21+
uses: actions/checkout@v4
2222

2323
- name: Install Go
24-
uses: actions/setup-go@v3
24+
uses: actions/setup-go@v5
2525
with:
2626
go-version: ${{ matrix.go }}
2727

@@ -35,7 +35,7 @@ jobs:
3535
run: go test -v -race ./...
3636

3737
- name: Lint
38-
uses: golangci/golangci-lint-action@v3.2.0
38+
uses: golangci/golangci-lint-action@v6
3939
with:
4040
version: latest
4141
args: --timeout 5m
@@ -44,10 +44,10 @@ jobs:
4444
needs: run
4545
runs-on: windows-latest
4646
steps:
47-
- uses: actions/checkout@v3
47+
- uses: actions/checkout@v4
4848

4949
- name: Install Go
50-
uses: actions/setup-go@v3
50+
uses: actions/setup-go@v5
5151
with:
5252
go-version: ${{ env.GO_VERSION }}
5353

@@ -58,10 +58,10 @@ jobs:
5858
needs: run
5959
runs-on: macos-latest
6060
steps:
61-
- uses: actions/checkout@v3
61+
- uses: actions/checkout@v4
6262

6363
- name: Install Go
64-
uses: actions/setup-go@v3
64+
uses: actions/setup-go@v5
6565
with:
6666
go-version: ${{ env.GO_VERSION }}
6767

pkg/analyzer/analyzer.go

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package analyzer
22

33
import (
44
"flag"
5+
"fmt"
56
"go/ast"
67
"go/token"
78
"strings"
@@ -364,55 +365,55 @@ func checkHTTPMethod(pass *analysis.Pass, basicLit *ast.BasicLit) {
364365
key := strings.ToUpper(currentVal)
365366

366367
if newVal, ok := mapping.HTTPMethod[key]; ok {
367-
report(pass, basicLit.Pos(), currentVal, newVal)
368+
report(pass, basicLit, currentVal, newVal)
368369
}
369370
}
370371

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

374375
if newVal, ok := mapping.HTTPStatusCode[currentVal]; ok {
375-
report(pass, basicLit.Pos(), currentVal, newVal)
376+
report(pass, basicLit, currentVal, newVal)
376377
}
377378
}
378379

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

382383
if newVal, ok := mapping.TimeWeekday[currentVal]; ok {
383-
report(pass, basicLit.Pos(), currentVal, newVal)
384+
report(pass, basicLit, currentVal, newVal)
384385
}
385386
}
386387

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

390391
if newVal, ok := mapping.TimeMonth[currentVal]; ok {
391-
report(pass, basicLit.Pos(), currentVal, newVal)
392+
report(pass, basicLit, currentVal, newVal)
392393
}
393394
}
394395

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

398399
if newVal, ok := mapping.TimeLayout[currentVal]; ok {
399-
report(pass, basicLit.Pos(), currentVal, newVal)
400+
report(pass, basicLit, currentVal, newVal)
400401
}
401402
}
402403

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

406407
if newVal, ok := mapping.CryptoHash[currentVal]; ok {
407-
report(pass, basicLit.Pos(), currentVal, newVal)
408+
report(pass, basicLit, currentVal, newVal)
408409
}
409410
}
410411

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

414415
if newVal, ok := mapping.RPCDefaultPath[currentVal]; ok {
415-
report(pass, basicLit.Pos(), currentVal, newVal)
416+
report(pass, basicLit, currentVal, newVal)
416417
}
417418
}
418419

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

424425
if newVal, ok := mapping.SQLIsolationLevel[currentVal]; ok {
425-
report(pass, basicLit.Pos(), currentVal, newVal)
426+
report(pass, basicLit, currentVal, newVal)
426427
}
427428
}
428429

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

432433
if newVal, ok := mapping.TLSSignatureScheme[currentVal]; ok {
433-
report(pass, basicLit.Pos(), currentVal, newVal)
434+
report(pass, basicLit, currentVal, newVal)
434435
}
435436
}
436437

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

440441
if newVal, ok := mapping.ConstantKind[currentVal]; ok {
441-
report(pass, basicLit.Pos(), currentVal, newVal)
442+
report(pass, basicLit, currentVal, newVal)
442443
}
443444
}
444445

@@ -514,6 +515,16 @@ func getBasicLitValue(basicLit *ast.BasicLit) string {
514515
return val.String()
515516
}
516517

517-
func report(pass *analysis.Pass, pos token.Pos, currentVal, newVal string) {
518-
pass.Reportf(pos, "%q can be replaced by %s", currentVal, newVal)
518+
func report(pass *analysis.Pass, rg analysis.Range, currentVal, newVal string) {
519+
pass.Report(analysis.Diagnostic{
520+
Pos: rg.Pos(),
521+
Message: fmt.Sprintf("%q can be replaced by %s", currentVal, newVal),
522+
SuggestedFixes: []analysis.SuggestedFix{{
523+
TextEdits: []analysis.TextEdit{{
524+
Pos: rg.Pos(),
525+
End: rg.End(),
526+
NewText: []byte(newVal),
527+
}},
528+
}},
529+
})
519530
}

pkg/analyzer/analyzer_test.go

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,6 @@ var flags = []string{
1919
analyzer.ConstantKindFlag,
2020
}
2121

22-
var pkgs = []string{
23-
"a/crypto",
24-
"a/http",
25-
"a/rpc",
26-
"a/time",
27-
"a/sql",
28-
"a/tls",
29-
"a/constant",
30-
}
31-
3222
func TestUseStdlibVars(t *testing.T) {
3323
a := analyzer.New()
3424

@@ -38,5 +28,21 @@ func TestUseStdlibVars(t *testing.T) {
3828
}
3929
}
4030

41-
analysistest.Run(t, analysistest.TestData(), a, pkgs...)
31+
testCases := []struct {
32+
dir string
33+
}{
34+
{dir: "a/crypto"},
35+
{dir: "a/http"},
36+
{dir: "a/rpc"},
37+
{dir: "a/time"},
38+
{dir: "a/sql"},
39+
{dir: "a/tls"},
40+
{dir: "a/constant"},
41+
}
42+
43+
for _, test := range testCases {
44+
t.Run(test.dir, func(t *testing.T) {
45+
analysistest.RunWithSuggestedFixes(t, analysistest.TestData(), a, test.dir)
46+
})
47+
}
4248
}

0 commit comments

Comments
 (0)