Skip to content

Remove tag if new tag value is empty.#120

Merged
fatih merged 1 commit intofatih:mainfrom
madelinekalil:remove-empty-tags
Apr 18, 2025
Merged

Remove tag if new tag value is empty.#120
fatih merged 1 commit intofatih:mainfrom
madelinekalil:remove-empty-tags

Conversation

@madelinekalil
Copy link
Contributor

If the new tag value is the empty string,
set the Tag to be nil. Having empty but
non-nil tags causes formatting issues
that leave trailing whitespace.

Copy link
Collaborator

@findleyr findleyr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, with one nit. Thanks for tracking this down!

}

f.Tag.Value = res
if res == "" {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be clearer to defer mutating f.Tag until this point. In other words, on line 104 above, just do something like

curTag := ""
if f.Tag != nil {
  curTag = f.Tag.Value
}

And then here, do:

if res == "" {
  f.Tag = nil
} else {
  if f.Tag == nil {
    f.Tag = &ast.BasicLit{}
  }
  f.Tag.Value = res
}

The advantage of this is that we don't run the risk of leaving an empty, non-nil tag in the tree if we encounter an error. Also, it generally is easier to follow the code if state mutation is left until the end.

If the new tag value is the empty string,
set the Tag to be nil. Having empty but
non-nil tags causes formatting issues
that leave trailing whitespace.
@findleyr
Copy link
Collaborator

@fatih this is just a bug fix. If you're OK with it, can you approve?

@fatih
Copy link
Owner

fatih commented Apr 18, 2025

@findleyr are you OK if I add you as a collaborator so you can approve and merge PR's?

@fatih fatih merged commit 6c6ab72 into fatih:main Apr 18, 2025
1 check passed
@findleyr
Copy link
Collaborator

@fatih sure, I can do that. I probably won't be able to offer much support, but can help fix bugs related to our usage.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants