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
Conversation
@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. |
@@ -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`) |
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 might be a breaking change, as we follow semantic-versioning-policy
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 it wouldn't be breaking, given the description in #12951, but this option's name is definitely confusing and we should change it.
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.
it is a breaking change, as users have to update their config to keep the current behaviour.
am I misunderstanding something?
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 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.
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 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`) |
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.
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. |
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.
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: |
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 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) { |
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.
Perhaps we should rename this function to something like getNumberOfLinesBetween
(as in function's description), because it doesn't return lines?
* @param {ASTNode} node1 the ImportDeclaration node. | ||
* @param {ASTNode} node2 the ImportDeclaration node. |
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 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] |
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.
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.
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. |
Prerequisites checklist
What is the purpose of this pull request? (put an "X" next to an item)
What changes did you make? (Give an overview)
I've added a new option to the
sort-imports
rule, calledignoreBlankLines
, which istrue
by default. When set tofalse
, 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.