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

buildozer: Remove unused loads with comments #896

Merged
merged 3 commits into from
Dec 14, 2020

Conversation

vladmos
Copy link
Member

@vladmos vladmos commented Sep 3, 2020

Previously buildozer refused to remove an unused load if it had any comment, now it works only with a special "@unused" comment.

edit/fix.go Outdated
continue
}

if len(all) == 0 {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do you test len(all)?

Copy link
Member Author

Choose a reason for hiding this comment

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

If all is not empty I can just attach the comments to the previous expression. My idea was that if there's an after-comment and we keep it, it belongs to the whole load-block and shouldn't be separated by an empty line: the following

load(":a.bzl", "used")
load(":b.bzl", "unused")
# foo

becomes

load(":a.bzl", "used")
# foo

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think the comment should be attached to another node. If we keep the comment, that means we consider it was incorrectly attached. So moving it to a block-comment might be better?

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

edit/fix.go Outdated
@@ -396,6 +375,16 @@ func cleanUnusedLoads(f *build.File) bool {
all = append(all, load)
} else {
fixed = true
// If the load statement contains a post-comment, keep it
if len(load.Comment().After) == 0 {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we do the same for Comment().Before?

Copy link
Member Author

@vladmos vladmos Sep 3, 2020

Choose a reason for hiding this comment

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

I'm not sure, there are indeed cases when a before-comment belongs only to one statement and to all of them. The fact that load statements are not separated by empty lines (unless they have comments) and that there's a (non-default) fix that orders them lexicographically makes it hard to determine which is the case:

# Bar definitions:
load(":bar.bzl", "bar")
load(":foo.bzl", "foo")

vs

# All definitions:
load(":bar.bzl", "bar")
load(":foo.bzl", "foo")

Copy link
Member Author

Choose a reason for hiding this comment

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

Changed to keeping all comments (except suffix).

@vladmos vladmos merged commit 174cbb4 into bazelbuild:master Dec 14, 2020
@vladmos vladmos deleted the fix_unused_loads branch December 14, 2020 19:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants