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
New: rule lines-between-class-members(fixes #5949). #9141
Conversation
ea57a82
to
c0ec4bf
Compare
Thanks for working on this! Anything we can do to help? Looks like it hasn't been touched in a little while. |
Instead of replacing the text with two linebreaks, would it work to just insert a single linebreak after the first method? That way, you wouldn't need to delete any existing text between the methods. |
c0ec4bf
to
9a5c9cf
Compare
almost finished, but for the docs. lol. |
@aladdin-add No need to apologize! Just checking in :) |
{ code: "class foo{ bar(){\n}\n\nbaz(){}}", options: [{ multiline: "always" }] }, | ||
{ code: "class foo{ bar(){\n}\nbaz(){}}", options: [{ multiline: "never" }] }, | ||
{ code: "class foo{ bar(){}\n\nbaz(){}}", options: [{ singleline: "always" }] }, | ||
{ code: "class foo{ bar(){}\nbaz(){}}", options: [{ singleline: "never" }] } |
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.
I'd like to see more tests, especially for semicolons and comments.
class A {
a() {}
;
b() {}
}
class B {
a() {}
// comment
b() {}
}
class C {
a() {}
/**
* comment
*/
b() {}
}
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.
class A { a() {} ; b() {} }
should also add tests for semi? this is not a necessary and looks strange!
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.
Yeah, it's strange, but it's valid syntax. This rule should work well even if semicolons exist.
In semantics, the semicolon is an empty member. So if this rule is checking blank lines between class members, this rule should check blank lines between a method and a semicolon, and between consecutive semicolons. But, this rule seems to check only between methods, so maybe it should ignore blank lines between a method and a semicolon, and between consecutive semicolons.
But I'm not sure if this rule should check only between methods. In this direction, we have to add rules as many as the combination of the number of class member types.
- lines-between-class-empty-members
- lines-between-class-methods
- lines-between-class-method-and-empty
- lines-between-class-fields (in future)
- lines-between-class-field-and-empty (in future)
- lines-between-class-field-and-method (in future)
I'm imaging a rule like padding-line-between-statements rule. Thought?
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.
👍 . a semicolon can be seen as a method? if not, I think the rulename lines-between-class-methods
is a little misleading.
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.
I think empty members are rare and not very useful, and it doesn't seem like people have a lot of different preferences for enforcing newlines between class members. So I'm wondering if it would be better to keep just line-between-class-methods
for now, and evaluate proposals for other class members separately if people want more customizability.
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.
My opinion is not strongly. So I'm OK if the rule checks only methods and just ignores blank lines between a method and a semicolon, and between consecutive semicolons. But, I'm guessing lines-between-class-methods
is going to be deprecated in future by adding fields.
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.
What if we call it lines-between-class-members
instead? That way, we can add options to it for fields in the future.
|
||
// only check padding between methods definition. | ||
if (body[i].type !== "MethodDefinition" || body[i + 1].type !== "MethodDefinition") { | ||
break; |
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.
I guess this should be continue
.
4d9ecb6
to
9bd2866
Compare
ed9d8b7
to
3f9d163
Compare
LGTM |
069b7d1
to
796985e
Compare
LGTM |
796985e
to
c43e1dd
Compare
LGTM |
c43e1dd
to
42ea9ee
Compare
LGTM |
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 small comment about the docs, but otherwise this LGTM. Thanks for all your hard work on this!
|
||
```js | ||
/* eslint lines-between-class-members: ["error", "always", { exceptAfterSingleLine: true }]*/ | ||
class Foo{ |
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.
Can we make this example more clear? It's not obvious how it's different from the default option.
Maybe something like:
class Foo{
bar(){} // single line class member
baz(){
// multi line class member
}
qux(){}
}
LGTM |
LGTM |
LGTM |
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.
Nice work on this, @aladdin-add! I have just one request for an additional test, then once that's done LGTM!
for (let i = 0; i < body.length - 1; i++) { | ||
|
||
// only check padding lines after class members(skip empty). | ||
if (body[i]) { |
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.
Can you add a test that covers this check?
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.
@btmills I found this check is unused -- it is always true. removed it and added some tests.
LGTM |
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 @aladdin-add!
3ba392a
to
870e2e5
Compare
LGTM |
What is the purpose of this pull request? (put an "X" next to item)
[x] New rule (template)
What changes did you make? (Give an overview)
fixes ##5949
Is there anything you'd like reviewers to focus on?