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

1. Fixed a bug when adding extra spaces after the parameter name. 2. Fixed a bug when using comment-like text in string literals. #1301

Merged
merged 3 commits into from
Oct 17, 2024

Conversation

PeJetuz
Copy link

@PeJetuz PeJetuz commented Jul 4, 2024

@pnatashap @yegor256
Please review, this should resolve the issue 1079.

Vladimir.Shapkin added 2 commits July 4, 2024 17:42
The @param regex allows varying numbers of spaces before and after the @param tag, while the @SInCE tag only allows one space before and one after it.
The error no longer appears for string literals similar to comments.
@PeJetuz
Copy link
Author

PeJetuz commented Aug 15, 2024

@yegor256 @pnatashap ping

@yegor256
Copy link
Owner

@PeJetuz can you please merge master into your branch, since there are merge conflicts now?

@PeJetuz
Copy link
Author

PeJetuz commented Aug 27, 2024

@yegor256 done.

@PeJetuz
Copy link
Author

PeJetuz commented Sep 16, 2024

@yegor256 Hi!, the branch has been updated.

@yegor256
Copy link
Owner

@volodya-lombrozo can you please help and review this pull request? It's pretty large and I'm afraid of merging it without a proper review.

Copy link

@volodya-lombrozo volodya-lombrozo left a comment

Choose a reason for hiding this comment

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

@PeJetuz I have left just a few small suggestions. Also, it would be nice to have a short description of the changes made in this PR description; otherwise, it's hard to trace the original problem you're trying to pinpoint. Anyway, thanks so much for your contribution!

* @param patt Pattern for checking the contents of a tag in a string.
* @param rep Reference to a method for writing a message to the log.
*/
RequiredJavaDocTag(final String name, final Pattern patt,

Choose a reason for hiding this comment

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

@PeJetuz NIT: What do you think about placing all the parameters either on one line, or each parameter in a separate line for the sake of consistency?

RequiredJavaDocTag(final String name, final Pattern patt, final Reporter rep) {

or

RequiredJavaDocTag(
  final String name, 
  final Pattern patt, 
  final Reporter rep
) {

The same is relevant for all the rest methods in this class.
I understand, that the format you used comes from the old classes from this repository, so it's not really important and you can leave it as is.

Copy link
Author

Choose a reason for hiding this comment

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

@volodya-lombrozo Thanks! Done.


@Test
void success() {
final String[] lines = {" *", " * @since 0.3", " *"};

Choose a reason for hiding this comment

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

@PeJetuz NIT: Many redundant variables are used in this test. Fore example, you can remove lines variables entirely.

There is the explanation why it's bad: https://www.yegor256.com/2015/09/01/redundant-variables-are-evil.html

Also, there is a study that emphasises significant negative impact of a hight number of identifiers in the code.

Copy link
Author

Choose a reason for hiding this comment

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

@volodya-lombrozo Thanks! Done.


/**
* Valid multi line literal.
* @checkstyle ArrayTrailingCommaCheck (6 lines)

Choose a reason for hiding this comment

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

@PeJetuz Why do you need to add ArrayTrailingCommaCheck here?

Copy link
Author

Choose a reason for hiding this comment

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

@volodya-lombrozo Thanks! I added a description in the comments to the methods. Is this ok?

Choose a reason for hiding this comment

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

@PeJetuz Maybe we can just remove this ArrayTrailingCommaCheck and add one more comma to the last element?

public static final String[] MULTILINE_LITERAL = {
    " /**", " * @since 0.3.4.4.", " **/",
    " /**",
    " * @since 0.3.4.4.",
    " **/",
};

In this case you don't need to suppress this violation and to add explanation to it.

@@ -0,0 +1,5 @@
6:This kind of comment is not allowed.
9:First sentence in a comment should start with a capital letter.

Choose a reason for hiding this comment

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

@PeJetuz Could you help me here, please? I don't clearly understand why we have exactly this line numbers 6,9,9,15,19.

Line 6 - /* C-style comment */ - OK
Line 9 - /** first sentence in a comment should start with a capital letter */ - OK
Line 9 - Why did you duplicate it?
Line 15 - The end of the incorrect java doc, but not the line itself (why not the 14 line?)
Line 19 - Why not the 18?

Copy link
Author

@PeJetuz PeJetuz Oct 1, 2024

Choose a reason for hiding this comment

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

@volodya-lombrozo Thanks! Duplication occurs because we have this check triggered twice, once in the class com.qulice.checkstyle.MultiLineCommentCheck and the second time in the class com.qulice.checkstyle.SingleLineCommentCheck.
Unfortunately, I don't know where it would be better to place the description of this case.

}
}

public void setFormat(final String fmt) {

Choose a reason for hiding this comment

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

@PeJetuz I understand why you need to add setters, but in general it is a bad practice.
I suggest to add at least some javadocs that would explain why we need them and why we can't remove them from the code base. It will preserve them from the future removal. The same is relevant for SingleLineCommentCheck

Copy link
Author

Choose a reason for hiding this comment

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

@volodya-lombrozo Thanks! I added a description in the comments to the methods.

@Override
public void visitToken(final DetailAST ast) {
if (ast.getType() == TokenTypes.BLOCK_COMMENT_BEGIN) {
this.line = new StringBuilder(ast.getText());

Choose a reason for hiding this comment

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

@PeJetuz Why do you need to initialise this.line field exactly here? Maybe we can initialise it directly in the constructor or in the field declaration?

private final StringBuilder line = new StringBuilder();

By doing this you will be able just to append text to it. First of all, this should simplify the code, and what is the most important, you will make this.line field immutable. How Immutability Helps.

Copy link
Author

@PeJetuz PeJetuz Oct 1, 2024

Choose a reason for hiding this comment

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

@volodya-lombrozo Thanks! I described why we reinitialize the variable in the comment when declaring it. Unfortunately, I didn't find a place where it is done correctly. Is this ok?

}

@Test
void successWithSpaces() {

Choose a reason for hiding this comment

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

@PeJetuz NIT: I would highly recommend to use @ParameterizedTest from JUnit 5. This would allow to remove similar tests and consider more test cases. More about this type of testing is here.

Copy link
Author

Choose a reason for hiding this comment

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

@volodya-lombrozo Thanks! Done.

@PeJetuz
Copy link
Author

PeJetuz commented Sep 20, 2024

@volodya-lombrozo Thank you! I'll read the articles and fix.

@PeJetuz PeJetuz force-pushed the fix_space_in_tags branch 3 times, most recently from 7259ac0 to 4844907 Compare October 1, 2024 12:58
@PeJetuz PeJetuz changed the title Fix issue 1079 1. Fixed a bug when adding extra spaces after the parameter name 2. Fixed a bug when using comment-like text in string literals. Oct 1, 2024
@PeJetuz PeJetuz changed the title 1. Fixed a bug when adding extra spaces after the parameter name 2. Fixed a bug when using comment-like text in string literals. 1. Fixed a bug when adding extra spaces after the parameter name. 2. Fixed a bug when using comment-like text in string literals. Oct 1, 2024
@PeJetuz
Copy link
Author

PeJetuz commented Oct 1, 2024

@PeJetuz I have left just a few small suggestions. Also, it would be nice to have a short description of the changes made in this PR description; otherwise, it's hard to trace the original problem you're trying to pinpoint. Anyway, thanks so much for your contribution!

@volodya-lombrozo Thank you very much! I changed the commit message to describe the issues fixed. Is this okay?

Copy link

@volodya-lombrozo volodya-lombrozo left a comment

Choose a reason for hiding this comment

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

@PeJetuz Thank you for the clarification. It's almost finished. I've just left several small comments.


/**
* Valid multi line literal.
* @checkstyle ArrayTrailingCommaCheck (6 lines)

Choose a reason for hiding this comment

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

@PeJetuz Maybe we can just remove this ArrayTrailingCommaCheck and add one more comma to the last element?

public static final String[] MULTILINE_LITERAL = {
    " /**", " * @since 0.3.4.4.", " **/",
    " /**",
    " * @since 0.3.4.4.",
    " **/",
};

In this case you don't need to suppress this violation and to add explanation to it.

<module name="com.qulice.checkstyle.MultiLineCommentCheck">
<property name="format" value=" */\*\* +[^A-Z\{ ][\W\s\S]*"/>
<property name="message" value="First sentence in a comment should start with a capital letter."/>
<property name="id" value="FirstLetterInCommentShouldBeCapital3"/>

Choose a reason for hiding this comment

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

@PeJetuz I have some doubts about id properties here. Can we just omit them? Or at least to give them more meaningful names. FirstLetterInCommentShouldBeCapital2 -> FirstLetterInMultiLineCommenWithAsterisk. Or something like this. FirstLetterInCommentShouldBeCapital2 and FirstLetterInCommentShouldBeCapital3 look strange to me.

Copy link
Author

Choose a reason for hiding this comment

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

@volodya-lombrozo Thanks for your help with ArrayTrailingCommaCheck!
I renamed id and changed the message for this check. Thanks for pointing this out, I think the message should be more friendly now.

* during the class under test and the field is reinitialized with a new object.
*/
@SuppressWarnings("PMD.AvoidStringBufferField")
private StringBuilder line;

Choose a reason for hiding this comment

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

@PeJetuz It seems, that the line is not an appropriate name of variable here. Since it's a "MultilineComment", I would give it lines or text name. What do you think?

Copy link
Author

Choose a reason for hiding this comment

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

@volodya-lombrozo Absolutely agree. Fixed.

</module>
<module name="com.qulice.checkstyle.MultiLineCommentCheck">
<property name="format" value=" */\*\*\W+\* +[^A-Z\{ ][\W\s\S]*"/>
<property name="message" value="First sentence in a comment should start with a capital letter."/>

Choose a reason for hiding this comment

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

@PeJetuz I would highly recommend you to change this error message:

First sentence in a comment should start with a capital letter.

->

First sentence in a multiline comment should start with a capital letter.

It will greatly help to distinct these two cases. The same you can do in tests.

Copy link
Author

Choose a reason for hiding this comment

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

@volodya-lombrozo Thanks! I've changed the messages so they should be more friendly now.

* @param line   Line position.
* @param column Column position.
2. Fixed a bug when using comment-like text in string literals:
final String comment = "/* some text */";
Fixes for issue yegor256#1079
Copy link

@volodya-lombrozo volodya-lombrozo left a comment

Choose a reason for hiding this comment

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

@PeJetuz Looks good to me. Thank you for the contribution!
@yegor256 Could you merge these changes, please?

@yegor256
Copy link
Owner

@rultor merge

@rultor
Copy link
Collaborator

rultor commented Oct 17, 2024

@rultor merge

@yegor256 OK, I'll try to merge now. You can check the progress of the merge here.

@rultor rultor merged commit e8f1762 into yegor256:master Oct 17, 2024
8 checks passed
@rultor
Copy link
Collaborator

rultor commented Oct 17, 2024

@rultor merge

@yegor256 Done! FYI, the full log is here (took me 11min).

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.

4 participants