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

[java] Fix #2996 - CommentSize suppression #4939

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

oowekyala
Copy link
Member

@oowekyala oowekyala commented Apr 6, 2024

Describe the PR

Suppressing violations of comment related rules doesn't work well right now, because they are reported against the compilation unit node. This adds some utilities to be able to report violations on a specific token. The RuleContext finds the closest node to the token and uses that to look for suppression annotations.

See also #2996 (comment)

Draft because probably other comment rules have the same problem.

Related issues

Ready?

  • Added unit tests for fixed bug/feature
  • Passing all unit tests
  • Complete build ./mvnw clean verify passes (checked automatically by github actions)
  • Added (in-code) documentation (if needed)

@pmd-test
Copy link

pmd-test commented Apr 9, 2024

1 Message
📖 Compared to master:
This changeset changes 0 violations,
introduces 0 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 0 errors and 0 configuration errors.
Download full report as build artifact

Generated by 🚫 Danger

Comment on lines +159 to +160
public void addViolationWithPosition(Reportable reportable, AstInfo<?> astInfo, FileLocation location,
String message, Object... formatArgs) {
Copy link
Member Author

Choose a reason for hiding this comment

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

One problem I have with this is the unreadability of these overloads... Maybe this API could have a makeover? Maybe we should write it like that:

ctx.report(at(node), fmt("msg", a, b))
ctx.report(at(token, astInfo, loc), msg, a, b)

// or

ctx.at(node).warnWithDefaultMsg();
ctx.at(node).warnWithDefaultMsg(arg1, arg2);
ctx.at(token, astInfo, loc).warnWithDefaultMsg();
ctx.at(token, astInfo, loc).warnWithDefaultMsg();
ctx.at(token, astInfo, loc).warn("Custom msg {0}", arg);

We could also probably dispense with the token and astInfo parameters with some magic (store the AstInfo in the RuleContext) and get

ctx.at(loc).warn(...);

But apparently changing the construction of the RuleContext would require changing a bunch of test code... Just a small hurdle though if this is what we want.

@jsotuyod @adangel any thoughts?

Copy link
Member

Choose a reason for hiding this comment

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

Maybe we could implement it with a builder? Which could look like (similar to your suggestions):

ctx.newViolation(message).withArgs(arg1, arg2).at(node);
ctx.newViolation(message).withArgs(arg1, arg2).at(token, astInfo);

In this example, at(Reportable) would actually add the ruleviolation to the rule context. We could also have a separate, explicit build method, which would allow to refine the location:

ctx.newViolation(message).withArgs(arg1, arg2).at(node).atBeginLine(4).build();

This could be an additional API first, as an alternative to the overloads addViolation...

We could do it slightly different:

new ViolationMessageBuilder().with(message).withArgs(arg).at(node).build(ctx);

Which would be then more separated from RuleContext, but honestly, I like it that the entry point for reporting violations is in rulecontext.

Copy link
Member

Choose a reason for hiding this comment

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

Late to the party, but here are some thoughts:

  1. I support the fluent API, I think it's a neat solution. Moreover, it's very flexible looking forward for adding things like [core] Reduce priority levels and add confidence value #4327)
  2. I feel shaping up that in this PR would be muddying the waters / delay getting this fixed, I'd recommend a separate PR.
  3. Any fluent API needs to be flagged @Experimental, I'd not commit our first iteration to a stable public API.
  4. Starting at RuleContext is the cleanest approach, I like it. This is irrespective of wether that is passed down directly or still obtained through asCtx(data) (again, separate issue / PR eventually).
  5. I'd have the finalizer method be a verb (not at!), descriptive (ie: warn() or report()) and return void to avoid any ambiguity (I've this ViolationMessage object, how do I report it? what do I do with it now?
  6. SLF4J v2 introduced it's own fluent API which may work as reference / inspiration: https://slf4j.org/manual.html#fluent

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.

[java] CommentSize rule violation is not suppressed at method level
4 participants