Skip to content

fix(#1363): package-comments ignores //nolint directives #1366

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

Merged
merged 19 commits into from
May 23, 2025
Merged

Conversation

chavacava
Copy link
Collaborator

@chavacava chavacava commented May 21, 2025

Closes #1363

Root cause of the bug: when checking package comments, the rule doesn't skip directive-comments at the right place.

@chavacava chavacava marked this pull request as draft May 22, 2025 08:43
Copy link
Contributor

@ccoVeille ccoVeille left a comment

Choose a reason for hiding this comment

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

That's a lot of changes 😱

@chavacava
Copy link
Collaborator Author

That's a lot of changes 😱

Yes... I think I hit a bug in dots.Resolve when it runs on Windows. I don´t have a windows machine so... I tried to catch the bug by relying on the GH CI...

@chavacava chavacava marked this pull request as ready for review May 22, 2025 15:01
Copy link
Contributor

@ccoVeille ccoVeille left a comment

Choose a reason for hiding this comment

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

I would say OK, but I cannot swear I reviewed everything

@chavacava
Copy link
Collaborator Author

I would say OK, but I cannot swear I reviewed everything

Thanks! The PR seems "complicated" but it does very few things:

  1. Fix package-comments ignores //nolint directives #1363 by checking if the block comment attached to package declaration is not "empty" (after removing empty lines and directives)
  2. Add a test case: the very same of package-comments ignores //nolint directives #1363
  3. Add a new helper method in test utils to test a rule against a list of files (vs the usual against a single file): this is where it seems to be a lot of changes but it's just the original helper functions with some extra for-range to iterate over the list of files. (easier to read with a side-by-side diff or by directly reading the final file)

@ccoVeille
Copy link
Contributor

Thanks for the details, I assumed it was something like that, yes

If you plan to squash your commits into one, what you wrote could be used as a good commit message

return true
}

return commentGroup.Text() == ""
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can be one-liner:

Suggested change
return commentGroup.Text() == ""
return commentGroup == nil || commentGroup.Text() == ""

@@ -0,0 +1,17 @@
package test
Copy link
Collaborator

Choose a reason for hiding this comment

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

  1. Tests for package-comments is inside the testdata/golint directory.
  2. I assume we can add simply testdata/golint/package_doc7.go instead of modifying test suite:
//nolint:dupl
package foo

Before the fix:

$ go test ./...
?       github.com/mgechev/revive       [no test files]
ok      github.com/mgechev/revive/cli   0.504s
ok      github.com/mgechev/revive/config        1.181s
ok      github.com/mgechev/revive/formatter     0.855s
?       github.com/mgechev/revive/internal/astutils     [no test files]
?       github.com/mgechev/revive/internal/ifelse       [no test files]
?       github.com/mgechev/revive/internal/typeparams   [no test files]
ok      github.com/mgechev/revive/lint  0.674s
ok      github.com/mgechev/revive/logging       0.315s
ok      github.com/mgechev/revive/revivelib     1.050s
ok      github.com/mgechev/revive/rule  1.498s
--- FAIL: TestAll (0.90s)
    --- FAIL: TestAll/package_doc7.go (0.00s)
        golint_test.go:71: Unexpected problem at package_doc7.go:1: package comment should be of the form "Package foo ..."
FAIL
FAIL    github.com/mgechev/revive/test  2.314s
FAIL

After applying the fix:

$ go test ./...
?       github.com/mgechev/revive       [no test files]
ok      github.com/mgechev/revive/cli   (cached)
ok      github.com/mgechev/revive/config        (cached)
ok      github.com/mgechev/revive/formatter     (cached)
?       github.com/mgechev/revive/internal/astutils     [no test files]
?       github.com/mgechev/revive/internal/ifelse       [no test files]
?       github.com/mgechev/revive/internal/typeparams   [no test files]
ok      github.com/mgechev/revive/lint  (cached)
ok      github.com/mgechev/revive/logging       (cached)
ok      github.com/mgechev/revive/revivelib     0.326s
ok      github.com/mgechev/revive/rule  (cached)
ok      github.com/mgechev/revive/test  1.407s

See my example in #1371

@chavacava chavacava merged commit f531af3 into master May 23, 2025
8 checks passed
@chavacava chavacava deleted the fix-1363 branch May 26, 2025 11:19
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.

package-comments ignores //nolint directives
3 participants