-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from zimmski/noop-all-the-way
Noop all the way
- Loading branch information
Showing
14 changed files
with
92 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package astutil | ||
|
||
import ( | ||
"go/ast" | ||
"go/token" | ||
) | ||
|
||
// CreateNoopOfStatement creates a syntactically safe noop statement out of a given statement. | ||
func CreateNoopOfStatement(stmt ast.Stmt) ast.Stmt { | ||
return CreateNoopOfStatements([]ast.Stmt{stmt}) | ||
} | ||
|
||
// CreateNoopOfStatement creates a syntactically safe noop statement out of a given statement. | ||
func CreateNoopOfStatements(stmts []ast.Stmt) ast.Stmt { | ||
var ids []ast.Expr | ||
for _, stmt := range stmts { | ||
ids = append(ids, IdentifiersInStatement(stmt)...) | ||
} | ||
|
||
if len(ids) == 0 { | ||
return &ast.EmptyStmt{ | ||
Semicolon: token.NoPos, | ||
} | ||
} | ||
|
||
lhs := make([]ast.Expr, len(ids)) | ||
for i := range ids { | ||
lhs[i] = ast.NewIdent("_") | ||
} | ||
|
||
return &ast.AssignStmt{ | ||
Lhs: lhs, | ||
Rhs: ids, | ||
Tok: token.ASSIGN, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package astutil | ||
|
||
import ( | ||
"go/ast" | ||
) | ||
|
||
// IdentifiersInStatement returns all identifiers with their found in a statement. | ||
func IdentifiersInStatement(stmt ast.Stmt) []ast.Expr { | ||
w := &identifierWalker{} | ||
|
||
ast.Walk(w, stmt) | ||
|
||
return w.identifiers | ||
} | ||
|
||
type identifierWalker struct { | ||
identifiers []ast.Expr | ||
} | ||
|
||
func (w *identifierWalker) Visit(node ast.Node) ast.Visitor { | ||
switch n := node.(type) { | ||
case *ast.Ident: | ||
w.identifiers = append(w.identifiers, n) | ||
|
||
return nil | ||
case *ast.SelectorExpr: | ||
w.identifiers = append(w.identifiers, n) | ||
|
||
return nil | ||
} | ||
|
||
return w | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,7 @@ func main() { | |
case i == 2: | ||
fmt.Println(i * 2) | ||
default: | ||
|
||
_, _ = fmt.Println, i | ||
} | ||
|
||
i++ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,7 @@ func main() { | |
} else if i == 2 { | ||
fmt.Println(i * 2) | ||
} else { | ||
|
||
_, _ = fmt.Println, i | ||
} | ||
|
||
i++ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters