Skip to content

Commit

Permalink
add test cases for the goanalysis package
Browse files Browse the repository at this point in the history
  • Loading branch information
Pantani authored and Pantani committed Feb 10, 2024
1 parent a252c1a commit 603e4fa
Show file tree
Hide file tree
Showing 2 changed files with 427 additions and 38 deletions.
24 changes: 13 additions & 11 deletions official/wasm/pkg/goanalysis/goanalysis.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,17 @@ func AppendCode(fileContent, functionName, codeToInsert string) (modifiedContent
return "", err
}

// Parse the Go code to insert.
insertionExpr, err := parser.ParseExpr(codeToInsert)
if err != nil {
return "", err
}

found := false
ast.Inspect(f, func(n ast.Node) bool {
if funcDecl, ok := n.(*ast.FuncDecl); ok {
// Check if the function has the name you want to replace.
if funcDecl.Name.Name == functionName {
// Insert the code before the end or return of the function.
insertionExpr, err := parser.ParseExpr(codeToInsert)
if err != nil {
return false
}
// Check if there is a return statement in the function.
if len(funcDecl.Body.List) > 0 {
lastStmt := funcDecl.Body.List[len(funcDecl.Body.List)-1]
Expand Down Expand Up @@ -208,17 +209,18 @@ func ReplaceCode(fileContent, oldFunctionName, newFunction string) (modifiedCont
return "", err
}

// Parse the content of the new function into an ast.File.
newFuncContent := fmt.Sprintf("package p; func _() { %s }", strings.TrimSpace(newFunction))
newFile, err := parser.ParseFile(fileSet, "", newFuncContent, parser.ParseComments)
if err != nil {
return "", err
}

found := false
ast.Inspect(f, func(n ast.Node) bool {
if funcDecl, ok := n.(*ast.FuncDecl); ok {
// Check if the function has the name you want to replace.
if funcDecl.Name.Name == oldFunctionName {
// Parse the content of the new function into an ast.File.
newFuncContent := fmt.Sprintf("package p; func _() { %s }", strings.TrimSpace(newFunction))
newFile, err := parser.ParseFile(fileSet, "", newFuncContent, parser.ParseComments)
if err != nil {
return false
}
// Take the body of the new function from the parsed file.
newFunctionBody := newFile.Decls[0].(*ast.FuncDecl).Body
// Replace the function body with the body of the new function.
Expand Down
Loading

0 comments on commit 603e4fa

Please sign in to comment.