-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
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
Update: autofix bug in lines-between-class-members (fixes #12391) #12632
Conversation
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.
LGTM. Thanks for working on this! I'd like at least one more pair of eyes on this before we merge.
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.
This is a very nice code but I think it can still remove comments in some cases, in particular when there are blank lines both before and after the comment?
/* eslint lines-between-class-members:["error", "never"] */
class MyClass {
myMethod1 (param) {
}
/**
* this comment will be removed?
*/
myMethod2 (param) {
}
}
Wondering if we should anyway merge this because it does fix the original issue (and these should be the most common scenarios), or maybe implement something similar to how padding-line-between-statements
handles comments.
Another solution might be to ignore class members that have comments between them and defer to the lines-around-comment
rule. It seems that these two rules can have some unavoidable conflicts anyway.
@mdjermanovic |
Thank for the change! Looks like the comments are completely safe now. There is another thing to note. In some extremely rare cases, this fix can change the behavior of this rule to report more or fewer warnings. The actual master version looks for comments before the next member (using This will almost always produce the same result at the end, but a class body can also contain useless empty members (semicolons), which don't exist in AST. An example where the actual version doesn't report warning, but the version from this PR does: /* eslint lines-between-class-members: ["error", "always"] */
class A {
foo() {}
/* comment */;
bar() {}
} This rule in general at the moment doesn't work well with these semicolons. I think it's okay to merge this PR as is (just mark with |
@mdjermanovic |
TIL! Nice catch, @mdjermanovic 👍 I'm personally not sure we should defer the fix until later. Would it be possible to check for semicolon tokens in addition to comments in this PR? |
May I ask some questions? The actual version doesn't report an error in case1. But I assume that this is a bug. ...right?
/* eslint lines-between-class-members: ["error", "always"] */
class A {
foo() {}
/* comment */; // No error, but here is no blank line.
bar() {}
} The PR version report an error in case1, but no error in case2.
/* eslint lines-between-class-members: ["error", "always"] */
class A {
foo() {}
/* comment */;
; // No error, but here is no blank line.
bar() {}
} So I need to add check for semicolon tokens. Do I get this right? |
In my opinion - yes, this is a bug, It seems that the actual master version doesn't handle semicolons well. In some cases, this rule can even remove them: /* eslint lines-between-class-members: ["error", "never"] */
class A {
foo() {}
;
bar() {}
} This is fixed to: /* eslint lines-between-class-members: ["error", "never"] */
class A {
foo() {}
bar() {}
} This doesn't look like something this rule should do,
While these semicolons are technically class members (I think), this rule should probably treat them as it treats comments - don't require/disallow empty lines around semicolons, but a line with a semicolon shouldn't be seen as an empty line. Maybe |
@mdjermanovic @kaicataldo
This is fixed to:
It's because the actual version inserts newline only after the last member node without considering trailing comments or semicolon. |
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.
Thanks for all the changes!
This PR now fixes 3 different bugs: #12391, semicolons, and "always"
autofix.
I left two notes for some minor edge cases.
@mdjermanovic |
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.
Looks good! Just to remove some probably extra chars in the tests, to avoid confusion about what was the intention of these tests.
@mdjermanovic |
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.
LGTM, thanks!
Thanks for contributing! |
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:
What changes did you make? (Give an overview)
This PR will fix three bugs in
lines-between-class-members
.Disallow lines-between-class-members deletes comments and docblocks between methods #12391
doesn't work well with
semicolons
.incorrect fixing trailing comments
Is there anything you'd like reviewers to focus on?
#12391