github.com/Liam-Williams/i18n4go@v0.2.7-0.20201028180611-670cbaceaa6b/common/ast.go (about)

     1  package common
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  
     7  	"go/ast"
     8  )
     9  
    10  func ImportsForASTFile(astFile *ast.File) (*ast.GenDecl, error) {
    11  	for _, declaration := range astFile.Decls {
    12  		decl, ok := declaration.(*ast.GenDecl)
    13  		if !ok || len(decl.Specs) == 0 {
    14  			continue
    15  		}
    16  
    17  		if _, ok = decl.Specs[0].(*ast.ImportSpec); ok {
    18  			return decl, nil
    19  		}
    20  	}
    21  
    22  	return nil, errors.New(fmt.Sprintf("Could not find imports for root node:\n\t%#v\n", astFile))
    23  }