Skip to content
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 · 1 comment
Open

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

jba opened this issue Feb 10, 2018 · 1 comment
Labels
Milestone

Comments

@jba
Copy link
Contributor

@jba 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
@xjtian

This comment has been minimized.

Copy link

@xjtian xjtian commented Jul 25, 2019

I've just run into this on go 1.12.1 on MacOS.

When using Cursor.Replace() to replace an *ast.Ident with another *ast.Ident, all the places where the Idents were replaced ended up with trailing commas. I'm printing the file with printer.Fprint.

The workaround in the issue worked for me though.

@gopherbot gopherbot added the Tools label Sep 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.