Skip to content

SONARJAVA-5537 implement S7476: comments must start with correct number of slashes#5151

Merged
erwan-serandour merged 9 commits intomasterfrom
erwan/S7476
May 14, 2025
Merged

SONARJAVA-5537 implement S7476: comments must start with correct number of slashes#5151
erwan-serandour merged 9 commits intomasterfrom
erwan/S7476

Conversation

@erwan-serandour
Copy link
Copy Markdown
Contributor

@erwan-serandour erwan-serandour commented May 14, 2025

SONARJAVA-5537

Part of

Copy link
Copy Markdown
Contributor

@romainbrenguier romainbrenguier left a comment

Choose a reason for hiding this comment

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

There is nothing really blocking, but I think we coud have a quickfix for this.

public class CommentsMustStartWithCorrectNumberOfSlashesCheck extends IssuableSubscriptionVisitor {
private static final String BEFORE_JAVA_23 = "A single-line comment should start with exactly two slashes, no more.";
private static final String AFTER_JAVA_23 = "Markdown documentation should start with exactly three slashes, no more.";
private static final String SLASHES_BEFORE_JAVA_23 = "///";
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Maybe add something like INCORRECT_ as prefix to the name, to make the purpose clearer.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

👍

public void visitTrivia(SyntaxTrivia syntaxTrivia) {
if (syntaxTrivia.isComment(SyntaxTrivia.CommentKind.LINE) && syntaxTrivia.comment().startsWith(SLASHES_BEFORE_JAVA_23)) {
var span = LineSpan.fromComment(syntaxTrivia, 0, 0, SLASHES_BEFORE_JAVA_23.length());
((DefaultJavaFileScannerContext) this.context).reportIssue(issueSingleLine(span, BEFORE_JAVA_23));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Since it's used several times, could you declare a variable for ((DefaultJavaFileScannerContext) this.context) or a method to report issues.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

👍

public void visitTrivia(SyntaxTrivia syntaxTrivia) {
if (syntaxTrivia.isComment(SyntaxTrivia.CommentKind.LINE) && syntaxTrivia.comment().startsWith(SLASHES_BEFORE_JAVA_23)) {
var span = LineSpan.fromComment(syntaxTrivia, 0, 0, SLASHES_BEFORE_JAVA_23.length());
((DefaultJavaFileScannerContext) this.context).reportIssue(issueSingleLine(span, BEFORE_JAVA_23));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Since the reportIssue method used is declared in DefaultModuleScannerContext, it would make more sense to cast to that.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

👍

}

private AnalyzerMessage issueSingleLine(LineSpan span, String message) {
AnalyzerMessage.TextSpan textSpan = new AnalyzerMessage.TextSpan(span.line, span.start, span.line, span.end);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

you could use var here, since you used it in other places

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

👍

"ruleSpecification": "RSPEC-7476",
"sqKey": "S7476",
"scope": "All",
"quickfix": "unknown",
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

could we consider a quickfix for this?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The quickfix api does not support trivia nodes. It would mean changing it. Quite some work and I don't think it is worth it for the rule.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We can keep it unknown for now.

@sonarqube-next
Copy link
Copy Markdown

@erwan-serandour erwan-serandour merged commit cedbc59 into master May 14, 2025
15 of 16 checks passed
@erwan-serandour erwan-serandour deleted the erwan/S7476 branch May 14, 2025 15:50
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.

2 participants