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

Wrong output when the file isn't gofmt'd #39

Open
Carpetsmoker opened this Issue Mar 13, 2019 · 2 comments

Comments

Projects
None yet
1 participant
@Carpetsmoker
Copy link

Carpetsmoker commented Mar 13, 2019

With the following file:

$ cat tags.go
package a
type x struct {
    Foo int
    bar int
}

The tool gives the wrong results.

Should be start=2:

$ gomodifytags -format json -file tags.go -transform snakecase -line 3,4 -add-tags json -add-options json=
{
  "start": 3,
  "end": 4,
  "lines": [
    "type x struct {",
    "\tFoo int `json:\"foo\"`"
  ]
}

Should be start=2, and one line is missing from the output:

$ gomodifytags -format json -file tags.go -transform snakecase -line 3,3 -add-tags json -add-options json=
{
  "start": 3,
  "end": 3,
  "lines": [
    "type x struct {"
  ]
}

start is correct, but the last line (}) missing:

$ gomodifytags -format json -file tags.go -transform snakecase -offset 23 -add-tags json -add-options json=
{
  "start": 2,
  "end": 5,
  "lines": [
    "",
    "type x struct {",
    "\tFoo int `json:\"foo\"`",
    "\tbar int `json:\"bar\"`"
  ]
}

It seems that the tool operates on the file as if they're gofmt'd.

@Carpetsmoker

This comment has been minimized.

Copy link
Author

Carpetsmoker commented Mar 13, 2019

The problem is that it parses the file, does the modifications, and then calls format.Node() on the result which gofmt's the source. It then bases the offsets on the result, rather than the original.

I'm not sure if there a straight-forward way to print out an ast tree with the positions as they were found in the original source.

@Carpetsmoker

This comment has been minimized.

Copy link
Author

Carpetsmoker commented Mar 13, 2019

There's another issues due to the same cause; say you have:

type foo struct {
	foo string
}

And now you add another field, and end up with (note the blank line before }):

type foo struct {
	foo string
	bar string

}

You use :GoModifyTags, but it returns offset is not inside a struct.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.