Skip to content

Commit

Permalink
reorganize code and function calls
Browse files Browse the repository at this point in the history
  • Loading branch information
peczenyj committed Jun 5, 2024
1 parent aba4893 commit e39f61f
Showing 1 changed file with 27 additions and 16 deletions.
43 changes: 27 additions & 16 deletions analyzer/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,37 +66,48 @@ func (ta *ttempdirAnalyzer) Run(pass *analysis.Pass) (interface{}, error) {
inspect.Preorder(nodeFilter, func(node ast.Node) {
switch function := node.(type) {
case *ast.FuncDecl:
ta.checkFuncDecl(pass, function)
ta.checkFunctionDeclaration(pass, function)
case *ast.FuncLit:
ta.checkFuncLit(pass, function)
ta.checkFunctionLiteral(pass, function, "anonymous function")
}
})

return nil, nil
}

func (ta *ttempdirAnalyzer) checkFuncDecl(pass *analysis.Pass, function *ast.FuncDecl) {
ta.checkGenericFunctionCall(pass, function.Pos(), function.Type, function.Body, function.Name.Name)
}
func (ta *ttempdirAnalyzer) checkFunctionDeclaration(pass *analysis.Pass,
function *ast.FuncDecl,
) {
// rewrite function declaration as function literal
functionLiteral := &ast.FuncLit{
Type: function.Type,
Body: function.Body,
}

func (ta *ttempdirAnalyzer) checkFuncLit(pass *analysis.Pass, function *ast.FuncLit) {
ta.checkGenericFunctionCall(pass, function.Pos(), function.Type, function.Body, "anonymous function")
ta.checkFunctionLiteral(pass, functionLiteral, function.Name.Name)
}

func (ta *ttempdirAnalyzer) checkGenericFunctionCall(pass *analysis.Pass,
functionPosition token.Pos,
functionType *ast.FuncType,
functionBody *ast.BlockStmt,
func (ta *ttempdirAnalyzer) checkFunctionLiteral(pass *analysis.Pass,
function *ast.FuncLit,
targetFunctionName string,
) {
fileName := pass.Fset.File(functionPosition).Name()
if variableOrPackageName, found := ta.targetRunner(functionType.Params, fileName); found {
variableOrPackageName, found := ta.targetRunner(function.Type.Params,
isFilenameFollowingTestingConventions(pass, function.Pos()),
)

if found {
reporterBuilder := newReporterBuilder(pass, variableOrPackageName, targetFunctionName)

ta.checkStmts(reporterBuilder, functionBody.List)
ta.checkStmts(reporterBuilder, function.Body.List)
}
}

func isFilenameFollowingTestingConventions(pass *analysis.Pass, pos token.Pos) bool {
fileName := pass.Fset.File(pos).Name()

return strings.HasSuffix(fileName, "_test.go")
}

func (ta *ttempdirAnalyzer) checkStmts(reporterBuilder ReporterBuilder,
stmts []ast.Stmt,
) {
Expand Down Expand Up @@ -211,7 +222,7 @@ func (ta *ttempdirAnalyzer) checkIdentifiers(reporter Reporter,

func (ta *ttempdirAnalyzer) targetRunner(
functionTypeParams *ast.FieldList,
fileName string,
isTestFile bool,
) (variableOrPackageName string, found bool) {
for _, field := range functionTypeParams.List {
switch typ := field.Type.(type) {
Expand All @@ -226,7 +237,7 @@ func (ta *ttempdirAnalyzer) targetRunner(
}
}

if ta.all && strings.HasSuffix(fileName, "_test.go") {
if ta.all && isTestFile {
return "", true
}

Expand Down

0 comments on commit e39f61f

Please sign in to comment.