Skip to content

Commit

Permalink
feat: adds newlines between option
Browse files Browse the repository at this point in the history
  • Loading branch information
hugop95 authored Oct 24, 2024
1 parent 3ceb793 commit a748aaa
Show file tree
Hide file tree
Showing 22 changed files with 1,404 additions and 171 deletions.
14 changes: 14 additions & 0 deletions docs/content/rules/sort-interfaces.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,18 @@ interface User {

Each group of members (separated by empty lines) is treated independently, and the order within each group is preserved.

### newlinesBetween

<sub>default: `'ignore'`</sub>

Specifies how new lines should be handled between interface groups.

- `ignore` — Do not report errors related to new lines 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`.

### groupKind

<sub>default: `'mixed'`</sub>
Expand Down Expand Up @@ -363,6 +375,7 @@ Determines the matcher used for patterns in the `partitionByComment`, `ignorePat
specialCharacters: 'keep',
ignorePattern: [],
partitionByNewLine: false,
newlinesBetween: 'ignore',
optionalityOrder: 'ignore',
matcher: 'minimatch',
groups: [],
Expand Down Expand Up @@ -393,6 +406,7 @@ Determines the matcher used for patterns in the `partitionByComment`, `ignorePat
specialCharacters: 'keep',
ignorePattern: [],
partitionByNewLine: false,
newlinesBetween: 'ignore',
optionalityOrder: 'ignore',
matcher: 'minimatch',
groups: [],
Expand Down
18 changes: 16 additions & 2 deletions docs/content/rules/sort-intersection-types.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,18 @@ type Employee =
Each group of intersection types (separated by empty lines) is treated independently, and the order within each group is preserved.
### newlinesBetween
<sub>default: `'ignore'`</sub>
Specifies how new lines should be handled between intersection type groups.
- `ignore` — Do not report errors related to new lines 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`.
### groups
<sub>
Expand Down Expand Up @@ -294,8 +306,9 @@ Determines the matcher used for patterns in the `partitionByComment` option.
order: 'asc',
ignoreCase: true,
specialCharacters: 'keep',
partitionByComment: false,
partitionByNewLine: false,
partitionByNewLine: false,
newlinesBetween: 'ignore',
matcher: 'minimatch',
groups: [],
},
Expand All @@ -322,8 +335,9 @@ Determines the matcher used for patterns in the `partitionByComment` option.
order: 'asc',
ignoreCase: true,
specialCharacters: 'keep',
partitionByNewLine: false,
partitionByComment: false,
partitionByNewLine: false,
newlinesBetween: 'ignore',
matcher: 'minimatch',
groups: [],
},
Expand Down
14 changes: 14 additions & 0 deletions docs/content/rules/sort-object-types.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,18 @@ type User = {

Each group of members (separated by empty lines) is treated independently, and the order within each group is preserved.

### newlinesBetween

<sub>default: `'ignore'`</sub>

Specifies how new lines should be handled between object type groups.

- `ignore` — Do not report errors related to new lines 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`.

### groupKind

<sub>default: `'mixed'`</sub>
Expand Down Expand Up @@ -320,6 +332,7 @@ Determines the matcher used for patterns in the `partitionByComment` and `custom
specialCharacters: 'keep',
partitionByComment: false,
partitionByNewLine: false,
newlinesBetween: 'ignore',
matcher: 'minimatch',
groups: [],
customGroups: {},
Expand Down Expand Up @@ -349,6 +362,7 @@ Determines the matcher used for patterns in the `partitionByComment` and `custom
specialCharacters: 'keep',
partitionByComment: false,
partitionByNewLine: false,
newlinesBetween: 'ignore',
matcher: 'minimatch',
groups: [],
customGroups: {},
Expand Down
14 changes: 14 additions & 0 deletions docs/content/rules/sort-objects.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,18 @@ const user = {

Each group of keys (separated by empty lines) is treated independently, and the order within each group is preserved.

### newlinesBetween

<sub>default: `'ignore'`</sub>

Specifies how new lines should be handled between object groups.

- `ignore` — Do not report errors related to new lines 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`.

### styledComponents

<sub>default: `true`</sub>
Expand Down Expand Up @@ -362,6 +374,7 @@ Determines the matcher used for patterns in the `partitionByComment`, `ignorePat
specialCharacters: 'keep',
partitionByComment: false,
partitionByNewLine: false,
newlinesBetween: 'ignore',
styledComponents: true,
ignorePattern: [],
matcher: 'minimatch',
Expand Down Expand Up @@ -393,6 +406,7 @@ Determines the matcher used for patterns in the `partitionByComment`, `ignorePat
specialCharacters: 'keep',
partitionByComment: false,
partitionByNewLine: false,
newlinesBetween: 'ignore',
styledComponents: true,
ignorePattern: [],
matcher: 'minimatch',
Expand Down
18 changes: 16 additions & 2 deletions docs/content/rules/sort-union-types.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,18 @@ type CarBrand =
Each group of union types (separated by empty lines) is treated independently, and the order within each group is preserved.
### newlinesBetween
<sub>default: `'ignore'`</sub>
Specifies how new lines should be handled between union type groups.
- `ignore` — Do not report errors related to new lines 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`.
### groups
<sub>
Expand Down Expand Up @@ -314,8 +326,9 @@ Determines the matcher used for patterns in the `partitionByComment` option.
order: 'asc',
ignoreCase: true,
specialCharacters: 'keep',
partitionByNewLine: false,
partitionByComment: false,
partitionByNewLine: false,
newlinesBetween: 'ignore',
matcher: 'minimatch',
groups: [],
},
Expand All @@ -342,8 +355,9 @@ Determines the matcher used for patterns in the `partitionByComment` option.
order: 'asc',
ignoreCase: true,
specialCharacters: 'keep',
partitionByNewLine: false,
partitionByComment: false,
partitionByNewLine: false,
newlinesBetween: 'ignore',
matcher: 'minimatch',
groups: [],
},
Expand Down
122 changes: 25 additions & 97 deletions rules/sort-imports.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { TSESTree } from '@typescript-eslint/types'
import type { TSESLint } from '@typescript-eslint/utils'

import { builtinModules } from 'node:module'

Expand All @@ -9,11 +8,11 @@ import { validateGroupsConfiguration } from '../utils/validate-groups-configurat
import { getOptionsWithCleanGroups } from '../utils/get-options-with-clean-groups'
import { sortNodesByGroups } from '../utils/sort-nodes-by-groups'
import { getCommentsBefore } from '../utils/get-comments-before'
import { makeNewlinesFixes } from '../utils/make-newlines-fixes'
import { getNewlinesErrors } from '../utils/get-newlines-errors'
import { createEslintRule } from '../utils/create-eslint-rule'
import { getLinesBetween } from '../utils/get-lines-between'
import { getGroupNumber } from '../utils/get-group-number'
import { getSourceCode } from '../utils/get-source-code'
import { getNodeRange } from '../utils/get-node-range'
import { rangeToDiff } from '../utils/range-to-diff'
import { getSettings } from '../utils/get-settings'
import { useGroups } from '../utils/use-groups'
Expand Down Expand Up @@ -596,28 +595,19 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
)
}

let numberOfEmptyLinesBetween = getLinesBetween(
sourceCode,
left,
right,
)
if (
options.newlinesBetween === 'never' &&
numberOfEmptyLinesBetween > 0
) {
messageIds.push('extraSpacingBetweenImports')
}

if (options.newlinesBetween === 'always') {
if (leftNum < rightNum && numberOfEmptyLinesBetween === 0) {
messageIds.push('missedSpacingBetweenImports')
} else if (
numberOfEmptyLinesBetween > 1 ||
(leftNum === rightNum && numberOfEmptyLinesBetween > 0)
) {
messageIds.push('extraSpacingBetweenImports')
}
}
messageIds = [
...messageIds,
...getNewlinesErrors({
left,
leftNum,
right,
rightNum,
sourceCode,
missedSpacingError: 'missedSpacingBetweenImports',
extraSpacingError: 'extraSpacingBetweenImports',
options,
}),
]

for (let messageId of messageIds) {
context.report({
Expand All @@ -629,78 +619,16 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
rightGroup: right.group,
},
node: right.node,
fix: fixer => {
let newlinesFixes: TSESLint.RuleFix[] = []

for (let max = sortedNodes.length, i = 0; i < max; i++) {
let node = sortedNodes.at(i)!
let nextNode = sortedNodes.at(i + 1)

if (options.newlinesBetween === 'ignore' || !nextNode) {
continue
}

let nodeGroupNumber = getGroupNumber(options.groups, node)
let nextNodeGroupNumber = getGroupNumber(
options.groups,
nextNode,
)
let currentNodeRange = getNodeRange(
nodeList.at(i)!.node,
sourceCode,
options,
)
let nextNodeRange =
getNodeRange(
nodeList.at(i + 1)!.node,
sourceCode,
options,
).at(0)! - 1

let linesBetweenImports = getLinesBetween(
sourceCode,
nodeList.at(i)!,
nodeList.at(i + 1)!,
)

if (
(options.newlinesBetween === 'always' &&
nodeGroupNumber === nextNodeGroupNumber &&
linesBetweenImports !== 0) ||
(options.newlinesBetween === 'never' &&
linesBetweenImports > 0)
) {
newlinesFixes.push(
fixer.removeRange([
currentNodeRange.at(1)!,
nextNodeRange,
]),
)
}
if (
options.newlinesBetween === 'always' &&
nodeGroupNumber !== nextNodeGroupNumber
) {
if (linesBetweenImports > 1) {
newlinesFixes.push(
fixer.replaceTextRange(
[currentNodeRange.at(1)!, nextNodeRange],
'\n',
),
)
} else if (linesBetweenImports === 0) {
newlinesFixes.push(
fixer.insertTextAfterRange(currentNodeRange, '\n'),
)
}
}
}

return [
...makeFixes(fixer, nodeList, sortedNodes, sourceCode),
...newlinesFixes,
]
},
fix: fixer => [
...makeFixes(fixer, nodeList, sortedNodes, sourceCode),
...makeNewlinesFixes(
fixer,
nodeList,
sortedNodes,
sourceCode,
options,
),
],
})
}
})
Expand Down
Loading

0 comments on commit a748aaa

Please sign in to comment.