Skip to content

Commit

Permalink
Support ForStmt (#36)
Browse files Browse the repository at this point in the history
* support for stmt

Signed-off-by: sivchari <shibuuuu5@gmail.com>

* new release

Signed-off-by: sivchari <shibuuuu5@gmail.com>

---------

Signed-off-by: sivchari <shibuuuu5@gmail.com>
  • Loading branch information
sivchari committed Jun 5, 2024
1 parent d926185 commit ae0d83c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
2 changes: 1 addition & 1 deletion release/RELEASE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
tag: v1.8.1
tag: v1.9.1
prerelease: false

releaseNoteGenerator:
Expand Down
8 changes: 6 additions & 2 deletions tenv.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package tenv

import (
"fmt"
"go/ast"
"strings"

Expand Down Expand Up @@ -82,6 +81,8 @@ func checkStmts(pass *analysis.Pass, stmts []ast.Stmt, funcName, argName string)
if !checkAssignStmt(pass, stmt, funcName, argName) {
continue
}
case *ast.ForStmt:
checkForStmt(pass, stmt, funcName, argName)
}
}
}
Expand All @@ -95,7 +96,6 @@ func checkExprStmt(pass *analysis.Pass, stmt *ast.ExprStmt, funcName, argName st
if !ok {
return false
}
fmt.Println(pass.TypesInfo.Types[fun.Sel].Value)
x, ok := fun.X.(*ast.Ident)
if !ok {
return false
Expand Down Expand Up @@ -160,6 +160,10 @@ func checkAssignStmt(pass *analysis.Pass, stmt *ast.AssignStmt, funcName, argNam
return true
}

func checkForStmt(pass *analysis.Pass, stmt *ast.ForStmt, funcName, argName string) {
checkStmts(pass, stmt.Body.List, funcName, argName)
}

func targetRunner(params []*ast.Field, fileName string) (string, bool) {
for _, p := range params {
switch typ := p.Type.(type) {
Expand Down
29 changes: 29 additions & 0 deletions testdata/src/a/a_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package a

import (
"fmt"
"os"
"testing"
)
Expand Down Expand Up @@ -66,3 +67,31 @@ func TestEmpty(t *testing.T) {
func TestEmptyTB(t *testing.T) {
func(testing.TB) {}(t)
}

func TestTDD(t *testing.T) {
for _, tt := range []struct {
name string
}{
{"test"},
} {
t.Run(tt.name, func(t *testing.T) {
os.Setenv("a", "b") // want "os\\.Setenv\\(\\) can be replaced by `t\\.Setenv\\(\\)` in anonymous function"
err := os.Setenv("a", "b") // want "os\\.Setenv\\(\\) can be replaced by `t\\.Setenv\\(\\)` in anonymous function"
_ = err
if err := os.Setenv("a", "b"); err != nil { // want "os\\.Setenv\\(\\) can be replaced by `t\\.Setenv\\(\\)` in anonymous function"
_ = err
}
})
}
}

func TestLoop(t *testing.T) {
for i := 0; i < 3; i++ {
os.Setenv(fmt.Sprintf("a%d", i), "b") // want "os\\.Setenv\\(\\) can be replaced by `t\\.Setenv\\(\\)` in TestLoop"
err := os.Setenv(fmt.Sprintf("a%d", i), "b") // want "os\\.Setenv\\(\\) can be replaced by `t\\.Setenv\\(\\)` in TestLoop"
_ = err
if err := os.Setenv(fmt.Sprintf("a%d", i), "b"); err != nil { // want "os\\.Setenv\\(\\) can be replaced by `t\\.Setenv\\(\\)` in TestLoop"
_ = err
}
}
}

0 comments on commit ae0d83c

Please sign in to comment.