Skip to content
Permalink
Browse files

allow more stmtList substitution scenarios

Hit this when trying to replace:

	err = F()
	if err != nil {
		...
	}

With:

	if err := F(); err != nil {
		...
	}

The command below failed. Add a small test case and fix it.
  • Loading branch information...
mvdan committed Mar 6, 2019
1 parent 54a0031 commit 492ba69e3162cee27095d4099de2bbfa47b3a8ec
Showing with 15 additions and 1 deletion.
  1. +5 −0 match_test.go
  2. +10 −1 subst.go
@@ -740,6 +740,11 @@ func TestMatch(t *testing.T) {
`f(c, d)`,
`f2(x, c, d)`,
},
{
[]string{"-x", "err = f(); if err != nil { $*then }", "-s", "if err := f(); err != nil { $then }", "-w"},
`{ err = f(); if err != nil { handle(err); }; }`,
`{ if err := f(); err != nil { handle(err); }; }`,
},
}
for i, tc := range tests {
t.Run(fmt.Sprintf("%03d", i), func(t *testing.T) {
@@ -36,7 +36,16 @@ func (m *matcher) fillValues(node ast.Node, values map[string]ast.Node) {
prev := values[info.name]
switch prev.(type) {
case exprList:
node = exprList([]ast.Expr{node.(*ast.Ident)})
node = exprList([]ast.Expr{
node.(*ast.Ident),
})
case stmtList:
if ident, ok := node.(*ast.Ident); ok {
node = &ast.ExprStmt{X: ident}
}
node = stmtList([]ast.Stmt{
node.(*ast.ExprStmt),
})
}
m.substNode(node, prev)
return true

0 comments on commit 492ba69

Please sign in to comment.
You can’t perform that action at this time.