github.com/servernoj/jade@v0.0.0-20231225191405-efec98d19db1/cmd/jade/go_ast_unbk.go (about)

     1  package main
     2  
     3  import (
     4  	"go/ast"
     5  )
     6  
     7  func (a *goAST) checkUnresolvedBlock() {
     8  	var dsSl = []*ast.DeclStmt{}
     9  	var ds *ast.DeclStmt
    10  
    11  	ast.Inspect(a.node, func(n ast.Node) bool {
    12  		if n != nil {
    13  			switch xds := n.(type) {
    14  			case *ast.DeclStmt:
    15  				if gd, ok := xds.Decl.(*ast.GenDecl); ok {
    16  					ast.Inspect(gd, func(n ast.Node) bool {
    17  						if n != nil {
    18  							switch x := n.(type) {
    19  							case *ast.Ident:
    20  								if x.Name == "block" {
    21  									// dsSl = append(dsSl, xds)
    22  									ds = xds
    23  								}
    24  								// case *ast.CallExpr:
    25  								// 	return false
    26  							}
    27  						}
    28  						return true
    29  					})
    30  				}
    31  			case *ast.CallExpr:
    32  				if len(xds.Args) == 1 {
    33  					if i, ok := xds.Args[0].(*ast.Ident); ok {
    34  						if i.Name == "block" {
    35  							if len(dsSl) > 0 && dsSl[len(dsSl)-1] == ds {
    36  								dsSl = dsSl[:len(dsSl)-1]
    37  							}
    38  						}
    39  					}
    40  				}
    41  			}
    42  		}
    43  		return true
    44  	})
    45  	ast.Inspect(a.node, func(n ast.Node) bool {
    46  		if n != nil {
    47  			switch x := n.(type) {
    48  			// case *ast.CallExpr:
    49  			// 	return false
    50  			case *ast.BlockStmt:
    51  				for i, v := range x.List {
    52  					if ds, ok := v.(*ast.DeclStmt); ok {
    53  						for _, v := range dsSl {
    54  							if ds == v {
    55  								x.List = append(x.List[:i], x.List[i+1:]...)
    56  							}
    57  						}
    58  					}
    59  				}
    60  			}
    61  		}
    62  		return true
    63  	})
    64  }