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

New: rule lines-between-class-members(fixes #5949). #9141

Merged
merged 25 commits into from Oct 2, 2017

Conversation

aladdin-add
Copy link
Member

@aladdin-add aladdin-add commented Aug 22, 2017

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?

@aladdin-add aladdin-add changed the title New: rule lines-between-class-methods(fixes #5949). WIP: New: rule lines-between-class-methods(fixes #5949). Aug 22, 2017
@not-an-aardvark not-an-aardvark added accepted There is consensus among the team that this change meets the criteria for inclusion feature This change adds a new feature to ESLint rule Relates to ESLint's core rules labels Aug 24, 2017
@aladdin-add aladdin-add force-pushed the issue5949 branch 4 times, most recently from ea57a82 to c0ec4bf Compare August 27, 2017 12:40
@kaicataldo
Copy link
Member

Thanks for working on this! Anything we can do to help? Looks like it hasn't been touched in a little while.

@aladdin-add
Copy link
Member Author

aladdin-add commented Sep 4, 2017

I was so busy these days, so sorry for the delay.
image

in current implement, to fix the problem, I just replace the text between methods, to one or two linebreak as needed. But seems like it cannot preserve comments. any suggestions?

@not-an-aardvark
Copy link
Member

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.

@aladdin-add
Copy link
Member Author

almost finished, but for the docs. lol.

@kaicataldo
Copy link
Member

@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" }] }
Copy link
Member

@mysticatea mysticatea Sep 5, 2017

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() {}
}

Copy link
Member Author

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!

Copy link
Member

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?

Copy link
Member Author

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.

Copy link
Member

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.

Copy link
Member

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.

Copy link
Member

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;
Copy link
Member

@mysticatea mysticatea Sep 5, 2017

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.

@aladdin-add aladdin-add changed the title WIP: New: rule lines-between-class-methods(fixes #5949). New: rule lines-between-class-members(fixes #5949). Sep 5, 2017
@eslint eslint deleted a comment from eslintbot Sep 9, 2017
@eslint eslint deleted a comment from eslintbot Sep 9, 2017
@eslint eslint deleted a comment from eslintbot Sep 9, 2017
@eslintbot
Copy link

LGTM

@eslintbot
Copy link

LGTM

@eslintbot
Copy link

LGTM

@eslintbot
Copy link

LGTM

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.

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{
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 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(){}
}

@eslintbot
Copy link

LGTM

@eslintbot
Copy link

LGTM

@eslintbot
Copy link

LGTM

Copy link
Member

@btmills btmills left a 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]) {
Copy link
Member

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?

Copy link
Member Author

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.

@eslintbot
Copy link

LGTM

Copy link
Member

@btmills btmills 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 @aladdin-add!

@eslintbot
Copy link

LGTM

@not-an-aardvark not-an-aardvark merged commit ee99876 into eslint:master Oct 2, 2017
@aladdin-add aladdin-add deleted the issue5949 branch October 2, 2017 16:51
@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Apr 1, 2018
@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 Apr 1, 2018
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 feature This change adds a new feature to ESLint rule Relates to ESLint's core rules
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants