Skip to content

Commit

Permalink
feat: allow to enter new lines between in groups
Browse files Browse the repository at this point in the history
  • Loading branch information
hugop95 authored Jan 3, 2025
1 parent a0b0a58 commit 4045595
Show file tree
Hide file tree
Showing 37 changed files with 930 additions and 96 deletions.
2 changes: 1 addition & 1 deletion docs/content/rules/sort-classes.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ Specifies how new lines should be handled between class member groups.
- `always` — Enforce one new line between each group, and forbid new lines inside a group.
- `never` — No new lines are allowed in object types.

This options is only applicable when `partitionByNewLine` is `false`.
This option is only applicable when `partitionByNewLine` is `false`.

### ignoreCallbackDependenciesPatterns

Expand Down
25 changes: 24 additions & 1 deletion docs/content/rules/sort-imports.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,11 @@ Specifies how new lines should be handled between import groups.
- `always` — Enforce one new line between each group, and forbid new lines inside a group.
- `never` — No new lines are allowed in the entire import section.

This options is only applicable when `partitionByNewLine` is `false`.
You can also enforce the newline behavior between two specific groups through the `groups` options.

See the [`groups`](#newlines-between-groups) option.

This option is only applicable when `partitionByNewLine` is `false`.

### maxLineLength

Expand Down Expand Up @@ -333,6 +337,25 @@ import type { Details } from './data'
import type { BaseOptions } from './index.d.ts'
```

#### Newlines between groups

You may place `newlinesBetween` objects between your groups to enforce the newline behavior between two specific groups.

See the [`newlinesBetween`](#newlinesbetween) option.

This feature is only applicable when `partitionByNewLine` is false.

```ts
{
newlinesBetween: 'always',
groups: [
'a',
{ newlinesBetween: 'never' }, // Overrides the global newlinesBetween option
'b',
]
}
```

### customGroups

<sub>
Expand Down
2 changes: 1 addition & 1 deletion docs/content/rules/sort-interfaces.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ Specifies how new lines should be handled between interface groups.
- `always` — Enforce one new line between each group, and forbid new lines inside a group.
- `never` — No new lines are allowed between interface members.

This options is only applicable when `partitionByNewLine` is `false`.
This option is only applicable when `partitionByNewLine` is `false`.

### [DEPRECATED] groupKind

Expand Down
2 changes: 1 addition & 1 deletion docs/content/rules/sort-intersection-types.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ Specifies how new lines should be handled between intersection type groups.
- `always` — Enforce one new line between each group, and forbid new lines inside a group.
- `never` — No new lines are allowed in intersection types.
This options is only applicable when `partitionByNewLine` is `false`.
This option is only applicable when `partitionByNewLine` is `false`.
### groups
Expand Down
2 changes: 1 addition & 1 deletion docs/content/rules/sort-modules.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ Specifies how new lines should be handled between module member groups.
- `always` — Enforce one new line between each group, and forbid new lines inside a group.
- `never` — No new lines are allowed in object types.

This options is only applicable when `partitionByNewLine` is `false`.
This option is only applicable when `partitionByNewLine` is `false`.

### groups

Expand Down
2 changes: 1 addition & 1 deletion docs/content/rules/sort-object-types.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ Specifies how new lines should be handled between object type groups.
- `always` — Enforce one new line between each group, and forbid new lines inside a group.
- `never` — No new lines are allowed in object types.

This options is only applicable when `partitionByNewLine` is `false`.
This option is only applicable when `partitionByNewLine` is `false`.

### [DEPRECATED] groupKind

Expand Down
2 changes: 1 addition & 1 deletion docs/content/rules/sort-objects.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ Specifies how new lines should be handled between object groups.
- `always` — Enforce one new line between each group, and forbid new lines inside a group.
- `never` — No new lines are allowed in objects.

This options is only applicable when `partitionByNewLine` is `false`.
This option is only applicable when `partitionByNewLine` is `false`.

### styledComponents

Expand Down
2 changes: 1 addition & 1 deletion docs/content/rules/sort-union-types.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ Specifies how new lines should be handled between union type groups.
- `always` — Enforce one new line between each group, and forbid new lines inside a group.
- `never` — No new lines are allowed in union types.
This options is only applicable when `partitionByNewLine` is `false`.
This option is only applicable when `partitionByNewLine` is `false`.
### groups
Expand Down
6 changes: 5 additions & 1 deletion rules/sort-classes/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,18 @@ export type SortClassesOptions = [
| string[]
| boolean
| string
groups: (
| { newlinesBetween: 'ignore' | 'always' | 'never' }
| Group[]
| Group
)[]
type: 'alphabetical' | 'line-length' | 'natural' | 'custom'
newlinesBetween: 'ignore' | 'always' | 'never'
specialCharacters: 'remove' | 'trim' | 'keep'
ignoreCallbackDependenciesPatterns: string[]
locales: NonNullable<Intl.LocalesArgument>
partitionByNewLine: boolean
customGroups: CustomGroup[]
groups: (Group[] | Group)[]
order: 'desc' | 'asc'
ignoreCase: boolean
alphabet: string
Expand Down
14 changes: 11 additions & 3 deletions rules/sort-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,15 @@ export type Options<T extends string[]> = [
value?: Record<T[number], string[] | string>
type?: Record<T[number], string[] | string>
}
groups: (
| { newlinesBetween: 'ignore' | 'always' | 'never' }
| Group<T>[]
| Group<T>
)[]
type: 'alphabetical' | 'line-length' | 'natural' | 'custom'
newlinesBetween: 'ignore' | 'always' | 'never'
specialCharacters: 'remove' | 'trim' | 'keep'
locales: NonNullable<Intl.LocalesArgument>
groups: (Group<T>[] | Group<T>)[]
environment: 'node' | 'bun'
partitionByNewLine: boolean
internalPattern: string[]
Expand Down Expand Up @@ -177,9 +181,13 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
: null

let isSideEffectOnlyGroup = (
group: undefined | string[] | string,
group:
| { newlinesBetween: 'ignore' | 'always' | 'never' }
| undefined
| string[]
| string,
): boolean => {
if (!group) {
if (!group || (typeof group === 'object' && 'newlinesBetween' in group)) {
return false
}
if (typeof group === 'string') {
Expand Down
6 changes: 5 additions & 1 deletion rules/sort-intersection-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,15 @@ type Options = [
| string[]
| boolean
| string
groups: (
| { newlinesBetween: 'ignore' | 'always' | 'never' }
| Group[]
| Group
)[]
type: 'alphabetical' | 'line-length' | 'natural' | 'custom'
newlinesBetween: 'ignore' | 'always' | 'never'
specialCharacters: 'remove' | 'trim' | 'keep'
locales: NonNullable<Intl.LocalesArgument>
groups: (Group[] | Group)[]
partitionByNewLine: boolean
order: 'desc' | 'asc'
ignoreCase: boolean
Expand Down
6 changes: 5 additions & 1 deletion rules/sort-modules/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,16 @@ export type SortModulesOptions = [
| string[]
| boolean
| string
groups: (
| { newlinesBetween: 'ignore' | 'always' | 'never' }
| Group[]
| Group
)[]
type: 'alphabetical' | 'line-length' | 'natural' | 'custom'
newlinesBetween: 'ignore' | 'always' | 'never'
specialCharacters: 'remove' | 'trim' | 'keep'
locales: NonNullable<Intl.LocalesArgument>
customGroups: CustomGroup[]
groups: (Group[] | Group)[]
partitionByNewLine: boolean
order: 'desc' | 'asc'
ignoreCase: boolean
Expand Down
6 changes: 5 additions & 1 deletion rules/sort-object-types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ export type Options = Partial<{
declarationMatchesPattern?: string
allNamesMatchPattern?: string
}
groups: (
| { newlinesBetween: 'ignore' | 'always' | 'never' }
| Group[]
| Group
)[]
type: 'alphabetical' | 'line-length' | 'unsorted' | 'natural' | 'custom'
customGroups: Record<string, string[] | string> | CustomGroup[]
/**
Expand All @@ -30,7 +35,6 @@ export type Options = Partial<{
newlinesBetween: 'ignore' | 'always' | 'never'
specialCharacters: 'remove' | 'trim' | 'keep'
locales: NonNullable<Intl.LocalesArgument>
groups: (Group[] | Group)[]
partitionByNewLine: boolean
/**
* @deprecated for {@link `useConfigurationIf.declarationMatchesPattern`}
Expand Down
6 changes: 5 additions & 1 deletion rules/sort-objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,17 @@ type Options = Partial<{
callingFunctionNamePattern?: string
allNamesMatchPattern?: string
}
groups: (
| { newlinesBetween: 'ignore' | 'always' | 'never' }
| Group[]
| Group
)[]
type: 'alphabetical' | 'line-length' | 'unsorted' | 'natural' | 'custom'
destructuredObjects: { groups: boolean } | boolean
customGroups: Record<string, string[] | string>
newlinesBetween: 'ignore' | 'always' | 'never'
specialCharacters: 'remove' | 'trim' | 'keep'
locales: NonNullable<Intl.LocalesArgument>
groups: (Group[] | Group)[]
partitionByNewLine: boolean
objectDeclarations: boolean
styledComponents: boolean
Expand Down
6 changes: 5 additions & 1 deletion rules/sort-union-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,15 @@ type Options = [
| string[]
| boolean
| string
groups: (
| { newlinesBetween: 'ignore' | 'always' | 'never' }
| Group[]
| Group
)[]
type: 'alphabetical' | 'line-length' | 'natural' | 'custom'
newlinesBetween: 'ignore' | 'always' | 'never'
specialCharacters: 'remove' | 'trim' | 'keep'
locales: NonNullable<Intl.LocalesArgument>
groups: (Group[] | Group)[]
partitionByNewLine: boolean
order: 'desc' | 'asc'
ignoreCase: boolean
Expand Down
86 changes: 86 additions & 0 deletions test/rules/sort-classes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4427,6 +4427,92 @@ describe(ruleName, () => {
valid: [],
},
)

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: [
'a',
{ newlinesBetween: 'always' },
'b',
{ newlinesBetween: 'always' },
'c',
{ newlinesBetween: 'never' },
'd',
{ newlinesBetween: 'ignore' },
'e',
],
newlinesBetween: 'always',
},
],
errors: [
{
data: {
right: 'b',
left: 'a',
},
messageId: 'missedSpacingBetweenClassMembers',
},
{
data: {
right: 'c',
left: 'b',
},
messageId: 'extraSpacingBetweenClassMembers',
},
{
data: {
right: 'd',
left: 'c',
},
messageId: 'extraSpacingBetweenClassMembers',
},
],
output: dedent`
class Class {
a: string
b: string
c: string
d: string
e: string
}
`,
code: dedent`
class Class {
a: string
b: string
c: string
d: string
e: string
}
`,
},
],
valid: [],
},
)
})

describe(`${ruleName}(${type}): sorts inline elements correctly`, () => {
Expand Down
Loading

0 comments on commit 4045595

Please sign in to comment.