Skip to content

Commit

Permalink
fix exploration of top-level func assignments
Browse files Browse the repository at this point in the history
  • Loading branch information
engelmi committed Nov 16, 2022
1 parent a3e4cec commit 380f5af
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 21 deletions.
34 changes: 23 additions & 11 deletions pkg/domain/goModuleAnalyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,33 +59,45 @@ func ExploreFunctions(filePath string) (functions []gosrc.Function, err error) {
case *ast.FuncDecl:
functions = append(functions, exploreFunction(declType, fs))
case *ast.GenDecl:
functions = append(functions, exploreGeneralDeclaration(declType, fs))
functions = append(functions, exploreGeneralDeclaration(declType, fs)...)
default:
}
}
return functions, nil
}

func exploreGeneralDeclaration(genDecl *ast.GenDecl, fs *token.FileSet) gosrc.Function {
from := fs.File(genDecl.Pos()).Position(genDecl.Pos())
to := fs.File(genDecl.End()).Position(genDecl.End())

function := gosrc.Function{
Name: string(gosrc.ExprTypeGenDecl),
Position: gosrc.NewPosition(from, to),
}
func exploreGeneralDeclaration(genDecl *ast.GenDecl, fs *token.FileSet) []gosrc.Function {
functions := []gosrc.Function{}

for _, spec := range genDecl.Specs {
switch specType := spec.(type) {
case *ast.ValueSpec:
for _, value := range specType.Values {

for i := range specType.Names {
name := specType.Names[i].Name
value := specType.Values[i]

// only add and explore if it is a function
if _, ok := value.(*ast.FuncLit); !ok {
continue
}

from := fs.File(value.Pos()).Position(value.Pos())
to := fs.File(value.End()).Position(value.End())

function := gosrc.Function{
Name: name,
Position: gosrc.NewPosition(from, to),
}

function.Statements = append(function.Statements, exploreExpression(value, nil, fs)...)
functions = append(functions, function)
}
default:
}
}

return function
return functions
}

func exploreFunction(toExplore *ast.FuncDecl, fs *token.FileSet) gosrc.Function {
Expand Down
54 changes: 44 additions & 10 deletions pkg/domain/goModuleAnalyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ func TestExploreFunctions(t *testing.T) {
name: "Successfully analyzes hello world example",
file: "hello_world_go",
expectFuns: []gosrc.Function{
{Name: string(gosrc.ExprTypeGenDecl), Position: gosrc.Position{LineFrom: 3, LineTo: 5, ColumnFrom: 1, ColumnTo: 2}},
{
Name: "main",
Position: gosrc.Position{LineFrom: 7, LineTo: 9, ColumnFrom: 1, ColumnTo: 2},
Expand All @@ -54,7 +53,6 @@ func TestExploreFunctions(t *testing.T) {
name: "Successfully analyzes hello world example with if statement",
file: "hello_world_with_if_go",
expectFuns: []gosrc.Function{
{Name: string(gosrc.ExprTypeGenDecl), Position: gosrc.Position{LineFrom: 3, LineTo: 6, ColumnFrom: 1, ColumnTo: 2}},
{
Name: "main",
Position: gosrc.Position{LineFrom: 8, LineTo: 12, ColumnFrom: 1, ColumnTo: 2},
Expand All @@ -79,7 +77,6 @@ func TestExploreFunctions(t *testing.T) {
name: "Successfully analyzes hello world example with if-else statement",
file: "hello_world_with_if_else_go",
expectFuns: []gosrc.Function{
{Name: string(gosrc.ExprTypeGenDecl), Position: gosrc.Position{LineFrom: 3, LineTo: 6, ColumnFrom: 1, ColumnTo: 2}},
{
Name: "main",
Position: gosrc.Position{LineFrom: 8, LineTo: 14, ColumnFrom: 1, ColumnTo: 2},
Expand Down Expand Up @@ -112,7 +109,6 @@ func TestExploreFunctions(t *testing.T) {
name: "Successfully analyzes hello world example with nested if statements",
file: "hello_world_with_nested_if_go",
expectFuns: []gosrc.Function{
{Name: string(gosrc.ExprTypeGenDecl), Position: gosrc.Position{LineFrom: 3, LineTo: 6, ColumnFrom: 1, ColumnTo: 2}},
{
Name: "main",
Position: gosrc.Position{LineFrom: 8, LineTo: 27, ColumnFrom: 1, ColumnTo: 2},
Expand Down Expand Up @@ -199,7 +195,6 @@ func TestExploreFunctions(t *testing.T) {
name: "Successfully analyzes example with for statement",
file: "for_go",
expectFuns: []gosrc.Function{
{Name: string(gosrc.ExprTypeGenDecl), Position: gosrc.Position{LineFrom: 3, LineTo: 5, ColumnFrom: 1, ColumnTo: 2}},
{
Name: "main",
Position: gosrc.Position{LineFrom: 7, LineTo: 12, ColumnFrom: 1, ColumnTo: 2},
Expand Down Expand Up @@ -227,7 +222,6 @@ func TestExploreFunctions(t *testing.T) {
name: "Successfully analyzes example with range statement",
file: "range_go",
expectFuns: []gosrc.Function{
{Name: string(gosrc.ExprTypeGenDecl), Position: gosrc.Position{LineFrom: 3, LineTo: 5, ColumnFrom: 1, ColumnTo: 2}},
{
Name: "main",
Position: gosrc.Position{LineFrom: 7, LineTo: 12, ColumnFrom: 1, ColumnTo: 2},
Expand Down Expand Up @@ -255,7 +249,6 @@ func TestExploreFunctions(t *testing.T) {
name: "Successfully analyzes example with type switch",
file: "type_switch_go",
expectFuns: []gosrc.Function{
{Name: string(gosrc.ExprTypeGenDecl), Position: gosrc.Position{LineFrom: 3, LineTo: 5, ColumnFrom: 1, ColumnTo: 2}},
{
Name: "main",
Position: gosrc.Position{LineFrom: 7, LineTo: 20, ColumnFrom: 1, ColumnTo: 2},
Expand Down Expand Up @@ -319,7 +312,6 @@ func TestExploreFunctions(t *testing.T) {
name: "Successfully analyzes example with switch-case",
file: "switch_go",
expectFuns: []gosrc.Function{
{Name: string(gosrc.ExprTypeGenDecl), Position: gosrc.Position{LineFrom: 3, LineTo: 5, ColumnFrom: 1, ColumnTo: 2}},
{
Name: "main",
Position: gosrc.Position{LineFrom: 7, LineTo: 20, ColumnFrom: 1, ColumnTo: 2},
Expand Down Expand Up @@ -383,7 +375,6 @@ func TestExploreFunctions(t *testing.T) {
name: "Successfully analyzes example with defer",
file: "defer_go",
expectFuns: []gosrc.Function{
{Name: string(gosrc.ExprTypeGenDecl), Position: gosrc.Position{LineFrom: 3, LineTo: 5, ColumnFrom: 1, ColumnTo: 2}},
{
Name: "main",
Position: gosrc.Position{LineFrom: 7, LineTo: 14, ColumnFrom: 1, ColumnTo: 2},
Expand Down Expand Up @@ -417,7 +408,6 @@ func TestExploreFunctions(t *testing.T) {
name: "Successfully analyzes example with func declaration",
file: "func_go",
expectFuns: []gosrc.Function{
{Name: string(gosrc.ExprTypeGenDecl), Position: gosrc.Position{LineFrom: 3, LineTo: 5, ColumnFrom: 1, ColumnTo: 2}},
{
Name: "main",
Position: gosrc.Position{LineFrom: 7, LineTo: 12, ColumnFrom: 1, ColumnTo: 2},
Expand Down Expand Up @@ -451,6 +441,50 @@ func TestExploreFunctions(t *testing.T) {
},
expectErr: false,
},
{
name: "someother",
file: "func_decl_go",
expectFuns: []gosrc.Function{
{
Name: "main",
Position: gosrc.Position{LineFrom: 7, LineTo: 11, ColumnFrom: 1, ColumnTo: 2},
Statements: []gosrc.Statement{
{
Type: gosrc.ExprTypeExpr,
Position: gosrc.Position{LineFrom: 8, LineTo: 8, ColumnFrom: 2, ColumnTo: 28},
},
{
Type: gosrc.ExprTypeExpr,
Position: gosrc.Position{LineFrom: 9, LineTo: 9, ColumnFrom: 2, ColumnTo: 9},
},
{
Type: gosrc.ExprTypeExpr,
Position: gosrc.Position{LineFrom: 10, LineTo: 10, ColumnFrom: 2, ColumnTo: 13},
},
},
},
{
Name: "DoSth",
Position: gosrc.Position{LineFrom: 13, LineTo: 15, ColumnFrom: 24, ColumnTo: 2},
Statements: []gosrc.Statement{
{
Type: gosrc.ExprTypeExpr,
Position: gosrc.Position{LineFrom: 14, LineTo: 14, ColumnFrom: 2, ColumnTo: 47},
},
},
},
{
Name: "DoSthElse",
Position: gosrc.Position{LineFrom: 15, LineTo: 17, ColumnFrom: 4, ColumnTo: 2},
Statements: []gosrc.Statement{
{
Type: gosrc.ExprTypeExpr,
Position: gosrc.Position{LineFrom: 16, LineTo: 16, ColumnFrom: 2, ColumnTo: 51},
},
},
},
},
},
}

for _, testcase := range testcases {
Expand Down

0 comments on commit 380f5af

Please sign in to comment.