New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x/tools/go/ast/astutil: Apply: extraneous comma after replaced parameter #23771

Open
jba opened this Issue Feb 10, 2018 · 0 comments

Comments

Projects
None yet
2 participants
@jba
Copy link

jba commented Feb 10, 2018

Not sure if this is a bug in astutil.Apply or format.Node.
This function:

func applyBug() {
    src := `                                                                                                                                                                                                                                  
package p                                                                                                                                                                                                                                     
import foo "bar"                                                                                                                                                                                                                              
func f(x foo.t) {}                                                                                                                                                                                                                            
`
    fset := token.NewFileSet()
    file, err := parser.ParseFile(fset, "", src, 0)
    if err != nil {
        panic(err)
    }
    pre := func(c *astutil.Cursor) bool {
        if sel, ok := c.Node().(*ast.SelectorExpr); ok {
            if id, ok := sel.X.(*ast.Ident); ok && id.Obj == nil && id.Name == "foo" {
                c.Replace(&ast.Ident{Name: "foo_t"})
            }
        }
        return true
    }
    astutil.Apply(file, pre, nil)
    var buf bytes.Buffer
    if err := format.Node(&buf, fset, file); err != nil {
        panic(err)
    }
    fmt.Println(buf.String())
}

yields this output:

package p

import foo "bar"

func f(x foo_t,) {}

Note the comma at the end of the function param list.

Reparsing the result with a new FileSet and then re-formatting fixes the problem.

@gopherbot gopherbot added this to the Unreleased milestone Feb 10, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment