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

Update: Allows for grouped sort-imports (fixes #12951) #12956

Closed
wants to merge 1 commit into from

Conversation

dmitov
Copy link

@dmitov dmitov commented Feb 23, 2020

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 an item)

  • Documentation update
  • 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)

I've added a new option to the sort-imports rule, called ignoreBlankLines, which is true by default. When set to false, the alphabetical sort error will be surpassed if there is a blank line before the statement. Meaning that there could be grouped imports, like mentioned in the issue.

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

Maybe the default to true option could be done better, but the tests are working nice, so I suppose it's fine.

@jsf-clabot
Copy link

jsf-clabot commented Feb 23, 2020

CLA assistant check
All committers have signed the CLA.

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

Hello, @dmitov Thanks for the proposal! (#12951)
And please note that we need time to get the consensus of ESlint members on the proposal. :) You can find more about the consensus process here.

@yeonjuan yeonjuan added enhancement This change enhances an existing feature of ESLint evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion rule Relates to ESLint's core rules and removed triage An ESLint team member will look at this issue soon labels Feb 24, 2020
@dmitov
Copy link
Author

dmitov commented Feb 24, 2020

@yeonjuan Thanks for your response! I'm familiar with the consensus process and I hope this will pass it, or inspire someone else to implement it in the core.

@dmitov dmitov removed the request for review from g-plane February 24, 2020 18:23
@mdjermanovic mdjermanovic added accepted There is consensus among the team that this change meets the criteria for inclusion and removed evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion labels Apr 2, 2020
@aladdin-add aladdin-add self-requested a review April 15, 2020 06:13
@@ -36,6 +36,7 @@ This rule accepts an object with its properties as
* `ignoreCase` (default: `false`)
* `ignoreDeclarationSort` (default: `false`)
* `ignoreMemberSort` (default: `false`)
* `ignoreBlankLines` (default: `true`)
Copy link
Member

Choose a reason for hiding this comment

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

this might be a breaking change, as we follow semantic-versioning-policy

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 it wouldn't be breaking, given the description in #12951, but this option's name is definitely confusing and we should change it.

Copy link
Member

Choose a reason for hiding this comment

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

it is a breaking change, as users have to update their config to keep the current behaviour.
am I misunderstanding something?

Copy link
Member

Choose a reason for hiding this comment

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

This will be more clear with allowSeparatedGroups name.

Per the original proposal, ignoreBlankLines: true was supposed to keep the current behavior, so it wouldn't be necessary to update configs.

I believe the logic for ignoreBlankLines was: blank lines should normally reset sorting, but by default this rule ignores them.

Copy link
Member

Choose a reason for hiding this comment

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

I see, thanks! ❤️

@@ -36,6 +36,7 @@ This rule accepts an object with its properties as
* `ignoreCase` (default: `false`)
* `ignoreDeclarationSort` (default: `false`)
* `ignoreMemberSort` (default: `false`)
* `ignoreBlankLines` (default: `true`)
Copy link
Member

Choose a reason for hiding this comment

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

We should rename this option to allowSeparatedGroups, default false.

(the same applies to the rest of documentation, and the code of course)

@@ -186,6 +200,45 @@ import {b, a, c} from 'foo.js'

Default is `false`.

### `ignoreBlankLines`

When `true` the rule ignores the blank lines between the imports.
Copy link
Member

Choose a reason for hiding this comment

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

Along with the option's name, we should also change this description since this option doesn't check blank lines (#12951 comment)


When `true` the rule ignores the blank lines between the imports.

Examples of **incorrect** code for this rule with the default `{ "ignoreBlankLines": true }` option:
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 please add an example with a statement between two imports, and with a comment between imports, both without any blank lines.

(same examples in incorrect and correct)

* @param {ASTNode} node2 the ImportDeclaration node.
* @returns {number} number of lines.
*/
function getLinesBetweenMembers(node1, node2) {
Copy link
Member

Choose a reason for hiding this comment

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

Perhaps we should rename this function to something like getNumberOfLinesBetween (as in function's description), because it doesn't return lines?

Comment on lines +124 to +125
* @param {ASTNode} node1 the ImportDeclaration node.
* @param {ASTNode} node2 the ImportDeclaration node.
Copy link
Member

Choose a reason for hiding this comment

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

This function assumes that node1 appears after node2 in the source code, so it would be nice to rename these params to make it clear. Maybe declaration and previousDeclaration ?

"import b from 'foo.js';\n\n" +
"import a from 'bar.js';",
output: null,
errors: [expectedError]
Copy link
Member

Choose a reason for hiding this comment

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

It would be nice to have an invalid test where the option is explicitly set to its default value.

Can you please also add some tests with multiline declarations and some tests where both declarations are on the same line? This is to ensure that the code that counts lines and checks the result works well.

Also, tests with a comment between, and tests with a statement between instead of blank lines.

@nzakas
Copy link
Member

nzakas commented Jun 11, 2020

It looks like this pull request has been abandoned, so I'm closing it.

Note: the issue that this pull request addresses is accepted (#12951), so anyone who wants to work on this change may do so and submit another pull request.

@nzakas nzakas closed this Jun 11, 2020
@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Dec 9, 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 Dec 9, 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 enhancement This change enhances an existing feature of ESLint rule Relates to ESLint's core rules
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[sort-imports] Allow the users to group their imports in a simple way
6 participants