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 · 2 comments
Open

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

jba opened this issue Feb 10, 2018 · 2 comments
Labels
Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@jba
Copy link
Contributor

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
Copy link

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 This label describes issues relating to any tools in the x/tools repository. label Sep 12, 2019
@garlic0708
Copy link

Simply appending to *FieldList.List yields the same result. So I assume it's more likely an issue within go/ast package?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

4 participants