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
base: master
Are you sure you want to change the base?
Conversation
Generated by 🚫 Danger |
public void addViolationWithPosition(Reportable reportable, AstInfo<?> astInfo, FileLocation location, | ||
String message, Object... formatArgs) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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:
- 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)
- I feel shaping up that in this PR would be muddying the waters / delay getting this fixed, I'd recommend a separate PR.
- Any fluent API needs to be flagged
@Experimental
, I'd not commit our first iteration to a stable public API. - Starting at
RuleContext
is the cleanest approach, I like it. This is irrespective of wether that is passed down directly or still obtained throughasCtx(data)
(again, separate issue / PR eventually). - I'd have the finalizer method be a verb (not
at
!), descriptive (ie:warn()
orreport()
) and returnvoid
to avoid any ambiguity (I've thisViolationMessage
object, how do I report it? what do I do with it now? - SLF4J v2 introduced it's own fluent API which may work as reference / inspiration: https://slf4j.org/manual.html#fluent
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?
./mvnw clean verify
passes (checked automatically by github actions)