Navigation Menu

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

Fix: multiline-comment-style rule add extra space after * (fixes #12785) #12823

Merged
merged 5 commits into from Jan 27, 2020
Merged

Fix: multiline-comment-style rule add extra space after * (fixes #12785) #12823

merged 5 commits into from Jan 27, 2020

Conversation

karthikkp
Copy link
Contributor

Prerequisites checklist

  • I have read the contributing guidelines.
  • The team has reached consensus on the changes proposed in this pull request. If not, I understand that the evaluation process will begin with this pull request and won't be merged until the team has reached consensus.

What is the purpose of this pull request? (put an "X" next to item)

[ ] Documentation update
[x] Bug fix (template)
[ ] New rule (template)
[ ] Changes an existing rule (template)
[ ] Add autofixing to a rule
[ ] Add a CLI option
[ ] Add something to the core
[ ] Other, please explain:

fixes #12785

What changes did you make? (Give an overview)

Added the extra white space which was removed here https://github.com/eslint/eslint/pull/12316/files#diff-e8cce2004dd5e0386021b0e80370c8ebL190

Is there anything you'd like reviewers to focus on?

No

@eslint-deprecated eslint-deprecated bot added the triage An ESLint team member will look at this issue soon label Jan 22, 2020
Copy link
Member

@kaicataldo kaicataldo left a comment

Choose a reason for hiding this comment

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

Thanks for working on this! This looks like a regression I introduced in #12316. As you can see from the tests, the current behavior is intentional. If the original comment didn't have a leading offset space, it currently tries to preserve that formatting when it autofixes.

I think the correct fix here would be to check if any line starts with a / character and, if there isn't leading whitespace being added, set the offset to a single space character to all lines. This would allow the rule to function as it does now for the majority of cases and handle this edge case gracefully.

@kaicataldo kaicataldo added accepted There is consensus among the team that this change meets the criteria for inclusion autofix This change is related to ESLint's autofixing capabilities bug ESLint is working incorrectly rule Relates to ESLint's core rules and removed triage An ESLint team member will look at this issue soon labels Jan 22, 2020
@kaicataldo
Copy link
Member

Additionally, we require tests for all changes. Feel free to start with the case reported in the issue!

@karthikkp
Copy link
Contributor Author

Thanks for working on this! This looks like a regression I introduced in #12316. As you can see from the tests, the current behavior is intentional. If the original comment didn't have a leading offset space, it currently tries to preserve that formatting when it autofixes.

I think the correct fix here would be to check if any line starts with a / character and, if there isn't leading whitespace being added, set the offset to a single space character to all lines. This would allow the rule to function as it does now for the majority of cases and handle this edge case gracefully.

Actually the problem is with the regular expression here https://github.com/eslint/eslint/pull/12316/files#diff-e8cce2004dd5e0386021b0e80370c8ebR316 and not the diff in the description. If we split the regular expression into two parts, one for the prefix before * and one after, I can get it to work. Do you want me to do that instead of "If the original comment didn't have a leading offset space, it currently tries to preserve that formatting". That way it code will be generic and we may not have to just check that one edge case specifically.

Mean time I will try this and let you know

@kaicataldo
Copy link
Member

To be clear, the behavior before the PR mentioned in the description was very different how it is now.

Can you share an example of what you're thinking? I'm not sure I'm following. I think it's working correctly for other cases besides this one edge case, and we should keep the current behavior the same (which should be easy to verify since this rule is tested fairly well).

@karthikkp
Copy link
Contributor Author

So what I am saying is, in the diff pasted, this problem occurs when the preceeding line also does not have * and initial offset hence is an empty string. So I can just add a check to see if the initial offset is an empty string and if it is just set initialoffset to a string with a space.

@kaicataldo
Copy link
Member

kaicataldo commented Jan 22, 2020

@karthikkp If I'm understanding correctly, that would cause the tests I linked above to fail. The rule tries to preserve the leading whitespace (or lack thereof) when it's converting the comment, so it's sometimes correct for the initial offset to be an empty string. I think this is the correct behavior (and was intentional when I made that change). Are there any other cases where malformed code can occur other than when there's no leading whitespace and the first character of a comment line is a / character?

@karthikkp
Copy link
Contributor Author

Oh okay got it. Then I think handling the edge case alone will be the way to go. Will do that

Copy link
Member

@kaicataldo kaicataldo left a comment

Choose a reason for hiding this comment

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

Thanks for a working on this! Do you mind updating the PR title? I believe it's currently failing the commit message check because it is too long.

@@ -1379,6 +1379,25 @@ ${" "}
{ messageId: "missingStar", line: 4 },
{ messageId: "endNewline", line: 4 }
]
},
Copy link
Member

Choose a reason for hiding this comment

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

Can we make this test suite a bit more robust by adding other kinds of formatted comments being formatted to starred-block?

Some examples:

////This comment is in
//`separate-lines` format.

// // This comment is in
// `separate-lines` format.


/*
  * // a line comment
 *some.code();
 */


/*
// a line comment
 * some.code();
 */

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The first two are not converted to starred block comments in the current version. So the fix that I am working should not change that behavior right?

Copy link
Member

Choose a reason for hiding this comment

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

Oh, interesting. Yes, that makes sense to me. Thanks for adding these tests.

Copy link
Member

@yeonjuan yeonjuan left a comment

Choose a reason for hiding this comment

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

Thanks for working on this! I left a question about the result of fixing.

lib/rules/multiline-comment-style.js Outdated Show resolved Hide resolved
Copy link
Member

@yeonjuan yeonjuan left a comment

Choose a reason for hiding this comment

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

Thanks for the changes! I left a minor suggestion.

lib/rules/multiline-comment-style.js Outdated Show resolved Hide resolved
@karthikkp karthikkp changed the title Fix: multiline-comment-style rule missed an extra space after * (fixes #12785) Fix: multiline-comment-style rule add extra space after * (fixes #12785) Jan 27, 2020
Copy link
Member

@kaicataldo kaicataldo left a comment

Choose a reason for hiding this comment

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

LGTM, thank you!

@@ -1379,6 +1379,25 @@ ${" "}
{ messageId: "missingStar", line: 4 },
{ messageId: "endNewline", line: 4 }
]
},
Copy link
Member

Choose a reason for hiding this comment

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

Oh, interesting. Yes, that makes sense to me. Thanks for adding these tests.

@karthikkp
Copy link
Contributor Author

@yeonjuan The changes you had requested have been made 👍

Copy link
Member

@yeonjuan yeonjuan left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

@kaicataldo kaicataldo merged commit 533c114 into eslint:master Jan 27, 2020
@kaicataldo
Copy link
Member

Thanks for contributing to ESLint!

montmanu pushed a commit to montmanu/eslint that referenced this pull request Mar 4, 2020
…nt#12785) (eslint#12823)

* Fix: multiline-comment-style rule missed an extra space after *  (fixes eslint#12785)

* logic changed to handled only the edge case

* test case added for the edge case

* checks full offset and apply space after * only if there already isn't white space after it.

* addeed new test case
@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Jul 27, 2020
@eslint-deprecated eslint-deprecated bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Jul 27, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion autofix This change is related to ESLint's autofixing capabilities bug ESLint is working incorrectly rule Relates to ESLint's core rules
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[multline-comment-style] fix breaks code
3 participants