Skip to content

Commit

Permalink
feat: improve new lines between inside groups option behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
hugop95 authored Jan 29, 2025
1 parent 0ec132a commit a682297
Show file tree
Hide file tree
Showing 20 changed files with 2,679 additions and 928 deletions.
3 changes: 2 additions & 1 deletion rules/sort-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { validateCustomSortConfiguration } from '../utils/validate-custom-sort-c
import { readClosestTsConfigByPath } from './sort-imports/read-closest-ts-config-by-path'
import { validateGroupsConfiguration } from '../utils/validate-groups-configuration'
import { getOptionsWithCleanGroups } from '../utils/get-options-with-clean-groups'
import { isNewlinesBetweenOption } from '../utils/is-newlines-between-option'
import { getEslintDisabledLines } from '../utils/get-eslint-disabled-lines'
import { getTypescriptImport } from './sort-imports/get-typescript-import'
import { isNodeEslintDisabled } from '../utils/is-node-eslint-disabled'
Expand Down Expand Up @@ -179,7 +180,7 @@ export default createEslintRule<Options, MESSAGE_ID>({
| string[]
| string,
): boolean => {
if (!group || (typeof group === 'object' && 'newlinesBetween' in group)) {
if (!group || isNewlinesBetweenOption(group)) {
return false
}
if (typeof group === 'string') {
Expand Down
272 changes: 200 additions & 72 deletions test/rules/sort-array-includes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1596,91 +1596,219 @@ describe(ruleName, () => {
},
)

ruleTester.run(
`${ruleName}(${type}): allows to use "newlinesBetween" inside groups`,
rule,
{
invalid: [
{
options: [
{
...options,
customGroups: [
{ elementNamePattern: 'a', groupName: 'a' },
{ elementNamePattern: 'b', groupName: 'b' },
{ elementNamePattern: 'c', groupName: 'c' },
{ elementNamePattern: 'd', groupName: 'd' },
{ elementNamePattern: 'e', groupName: 'e' },
],
groups: [
describe(`${ruleName}(${type}): "newlinesBetween" inside groups`, () => {
ruleTester.run(
`${ruleName}(${type}): handles "newlinesBetween" between consecutive groups`,
rule,
{
invalid: [
{
options: [
{
...options,
groups: [
'a',
{ newlinesBetween: 'always' },
'b',
{ newlinesBetween: 'always' },
'c',
{ newlinesBetween: 'never' },
'd',
{ newlinesBetween: 'ignore' },
'e',
],
customGroups: [
{ elementNamePattern: 'a', groupName: 'a' },
{ elementNamePattern: 'b', groupName: 'b' },
{ elementNamePattern: 'c', groupName: 'c' },
{ elementNamePattern: 'd', groupName: 'd' },
{ elementNamePattern: 'e', groupName: 'e' },
],
newlinesBetween: 'always',
},
],
errors: [
{
data: {
right: 'b',
left: 'a',
},
messageId: 'missedSpacingBetweenArrayIncludesMembers',
},
{
data: {
right: 'c',
left: 'b',
},
messageId: 'extraSpacingBetweenArrayIncludesMembers',
},
{
data: {
right: 'd',
left: 'c',
},
messageId: 'extraSpacingBetweenArrayIncludesMembers',
},
],
output: dedent`
[
'a',
{ newlinesBetween: 'always' },
'b',
{ newlinesBetween: 'always' },
'c',
{ newlinesBetween: 'never' },
'd',
{ newlinesBetween: 'ignore' },
'e',
],
newlinesBetween: 'always',
},
],
errors: [
{
data: {
right: 'b',
left: 'a',
},
messageId: 'missedSpacingBetweenArrayIncludesMembers',
},
{
data: {
right: 'c',
left: 'b',
},
messageId: 'extraSpacingBetweenArrayIncludesMembers',
},
{
data: {
right: 'd',
left: 'c',
},
messageId: 'extraSpacingBetweenArrayIncludesMembers',
},
],
output: dedent`
[
'a',
'b',
'c',
'd',
'e'
].includes(value)
`,
code: dedent`
[
'a',
'b',
'e'
].includes(value)
`,
code: dedent`
[
'a',
'b',
'c',
'd',
'c',
'd',
'e'
].includes(value)
`,
},
],
valid: [],
},
)

describe(`${ruleName}(${type}): "newlinesBetween" between non-consecutive groups`, () => {
for (let [globalNewlinesBetween, groupNewlinesBetween] of [
['always', 'never'] as const,
['always', 'ignore'] as const,
['never', 'always'] as const,
['ignore', 'always'] as const,
]) {
ruleTester.run(
`${ruleName}(${type}): enforces a newline if the global option is "${globalNewlinesBetween}" and the group option is "${groupNewlinesBetween}"`,
rule,
{
invalid: [
{
options: [
{
...options,
customGroups: [
{ elementNamePattern: 'a', groupName: 'a' },
{ elementNamePattern: 'b', groupName: 'b' },
{ groupName: 'unusedGroup', elementNamePattern: 'X' },
],
groups: [
'a',
'unusedGroup',
{ newlinesBetween: groupNewlinesBetween },
'b',
],
newlinesBetween: globalNewlinesBetween,
},
],
errors: [
{
data: {
right: 'b',
left: 'a',
},
messageId: 'missedSpacingBetweenArrayIncludesMembers',
},
],
output: dedent`
[
a,
b,
].includes(value)
`,
code: dedent`
[
a,
b,
].includes(value)
`,
},
],
valid: [],
},
)
}

for (let [globalNewlinesBetween, groupNewlinesBetween] of [
['ignore', 'never'] as const,
['never', 'ignore'] as const,
]) {
ruleTester.run(
`${ruleName}(${type}): does not enforce a newline if the global option is "${globalNewlinesBetween}" and the group option is "${groupNewlinesBetween}"`,
rule,
{
valid: [
{
options: [
{
...options,
customGroups: [
{ elementNamePattern: 'a', groupName: 'a' },
{ elementNamePattern: 'b', groupName: 'b' },
{ groupName: 'unusedGroup', elementNamePattern: 'X' },
],
groups: [
'a',
'unusedGroup',
{ newlinesBetween: groupNewlinesBetween },
'b',
],
newlinesBetween: globalNewlinesBetween,
},
],
code: dedent`
[
a,
'e'
].includes(value)
`,
},
],
valid: [],
},
)
b,
].includes(value)
`,
},
{
options: [
{
...options,
customGroups: [
{ elementNamePattern: 'a', groupName: 'a' },
{ elementNamePattern: 'b', groupName: 'b' },
{ groupName: 'unusedGroup', elementNamePattern: 'X' },
],
groups: [
'a',
'unusedGroup',
{ newlinesBetween: groupNewlinesBetween },
'b',
],
newlinesBetween: globalNewlinesBetween,
},
],
code: dedent`
[
a,
b,
].includes(value)
`,
},
],
invalid: [],
},
)
}
})
})

ruleTester.run(
`${ruleName}(${type}): handles newlines and comment after fixes`,
Expand Down
Loading

0 comments on commit a682297

Please sign in to comment.