Skip to content

Commit 71162a9

Browse files
committed
fix issue with empty nodes
1 parent f69bf80 commit 71162a9

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

strparse.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,18 @@ func Stmt(s string) ast.Stmt {
4141
if err != nil {
4242
return BadStmt
4343
}
44-
return node.Decls[0].(*ast.FuncDecl).Body.List[0]
44+
fn := node.Decls[0].(*ast.FuncDecl)
45+
if len(fn.Body.List) != 1 {
46+
return BadStmt
47+
}
48+
return fn.Body.List[0]
4549
}
4650

4751
// Decl parses single declaration node from s.
4852
// In case of parse error, BadDecl is returned.
4953
func Decl(s string) ast.Decl {
5054
node, err := parser.ParseFile(token.NewFileSet(), "", "package main;"+s, 0)
51-
if err != nil {
55+
if err != nil || len(node.Decls) != 1 {
5256
return BadDecl
5357
}
5458
return node.Decls[0]

0 commit comments

Comments
 (0)