Skip to content

Commit c75a5eb

Browse files
committed
feat: add newlinesBetween option
1 parent 60cc19f commit c75a5eb

4 files changed

Lines changed: 46 additions & 10 deletions

File tree

rules/sort-named-exports.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,17 @@ import {
99
buildCustomGroupsArrayJsonSchema,
1010
partitionByCommentJsonSchema,
1111
partitionByNewLineJsonSchema,
12+
newlinesBetweenJsonSchema,
1213
commonJsonSchemas,
1314
groupsJsonSchema,
1415
} from '../utils/common-json-schemas'
16+
import {
17+
MISSED_SPACING_ERROR,
18+
EXTRA_SPACING_ERROR,
19+
GROUP_ORDER_ERROR,
20+
ORDER_ERROR,
21+
} from '../utils/report-errors'
22+
import { validateNewlinesAndPartitionConfiguration } from '../utils/validate-newlines-and-partition-configuration'
1523
import {
1624
singleCustomGroupJsonSchema,
1725
allModifiers,
@@ -23,7 +31,6 @@ import { validateCustomSortConfiguration } from '../utils/validate-custom-sort-c
2331
import { generatePredefinedGroups } from '../utils/generate-predefined-groups'
2432
import { getEslintDisabledLines } from '../utils/get-eslint-disabled-lines'
2533
import { isNodeEslintDisabled } from '../utils/is-node-eslint-disabled'
26-
import { GROUP_ORDER_ERROR, ORDER_ERROR } from '../utils/report-errors'
2734
import { doesCustomGroupMatch } from '../utils/does-custom-group-match'
2835
import { sortNodesByGroups } from '../utils/sort-nodes-by-groups'
2936
import { createEslintRule } from '../utils/create-eslint-rule'
@@ -37,6 +44,8 @@ import { complete } from '../utils/complete'
3744

3845
type MESSAGE_ID =
3946
| 'unexpectedNamedExportsGroupOrder'
47+
| 'missedSpacingBetweenNamedExports'
48+
| 'extraSpacingBetweenNamedExports'
4049
| 'unexpectedNamedExportsOrder'
4150

4251
/**
@@ -49,6 +58,7 @@ let defaultOptions: Required<Options[0]> = {
4958
specialCharacters: 'keep',
5059
partitionByNewLine: false,
5160
partitionByComment: false,
61+
newlinesBetween: 'ignore',
5262
type: 'alphabetical',
5363
ignoreAlias: false,
5464
groupKind: 'mixed',
@@ -75,6 +85,7 @@ export default createEslintRule<Options, MESSAGE_ID>({
7585
selectors: allSelectors,
7686
options,
7787
})
88+
validateNewlinesAndPartitionConfiguration(options)
7889

7990
let { sourceCode, id } = context
8091
let eslintDisabledLines = getEslintDisabledLines({
@@ -199,6 +210,8 @@ export default createEslintRule<Options, MESSAGE_ID>({
199210

200211
reportAllErrors<MESSAGE_ID>({
201212
availableMessageIds: {
213+
missedSpacingBetweenMembers: 'missedSpacingBetweenNamedExports',
214+
extraSpacingBetweenMembers: 'extraSpacingBetweenNamedExports',
202215
unexpectedGroupOrder: 'unexpectedNamedExportsGroupOrder',
203216
unexpectedOrder: 'unexpectedNamedExportsOrder',
204217
},
@@ -230,6 +243,7 @@ export default createEslintRule<Options, MESSAGE_ID>({
230243
}),
231244
partitionByComment: partitionByCommentJsonSchema,
232245
partitionByNewLine: partitionByNewLineJsonSchema,
246+
newlinesBetween: newlinesBetweenJsonSchema,
233247
groups: groupsJsonSchema,
234248
},
235249
additionalProperties: false,
@@ -238,15 +252,17 @@ export default createEslintRule<Options, MESSAGE_ID>({
238252
uniqueItems: true,
239253
type: 'array',
240254
},
255+
messages: {
256+
missedSpacingBetweenNamedExports: MISSED_SPACING_ERROR,
257+
extraSpacingBetweenNamedExports: EXTRA_SPACING_ERROR,
258+
unexpectedNamedExportsGroupOrder: GROUP_ORDER_ERROR,
259+
unexpectedNamedExportsOrder: ORDER_ERROR,
260+
},
241261
docs: {
242262
url: 'https://perfectionist.dev/rules/sort-named-exports',
243263
description: 'Enforce sorted named exports.',
244264
recommended: true,
245265
},
246-
messages: {
247-
unexpectedNamedExportsGroupOrder: GROUP_ORDER_ERROR,
248-
unexpectedNamedExportsOrder: ORDER_ERROR,
249-
},
250266
type: 'suggestion',
251267
fixable: 'code',
252268
},

rules/sort-named-exports/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { TSESTree } from '@typescript-eslint/types'
33

44
import type {
55
PartitionByCommentOption,
6+
NewlinesBetweenOption,
67
CustomGroupsOption,
78
CommonOptions,
89
GroupsOptions,
@@ -25,6 +26,7 @@ export type Options = Partial<
2526
groupKind: 'values-first' | 'types-first' | 'mixed'
2627
customGroups: CustomGroupsOption<SingleCustomGroup>
2728
partitionByComment: PartitionByCommentOption
29+
newlinesBetween: NewlinesBetweenOption
2830
groups: GroupsOptions<Group>
2931
partitionByNewLine: boolean
3032
ignoreAlias: boolean

rules/sort-named-imports.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,17 @@ import {
99
buildCustomGroupsArrayJsonSchema,
1010
partitionByCommentJsonSchema,
1111
partitionByNewLineJsonSchema,
12+
newlinesBetweenJsonSchema,
1213
commonJsonSchemas,
1314
groupsJsonSchema,
1415
} from '../utils/common-json-schemas'
16+
import {
17+
MISSED_SPACING_ERROR,
18+
EXTRA_SPACING_ERROR,
19+
GROUP_ORDER_ERROR,
20+
ORDER_ERROR,
21+
} from '../utils/report-errors'
22+
import { validateNewlinesAndPartitionConfiguration } from '../utils/validate-newlines-and-partition-configuration'
1523
import {
1624
singleCustomGroupJsonSchema,
1725
allModifiers,
@@ -23,7 +31,6 @@ import { validateCustomSortConfiguration } from '../utils/validate-custom-sort-c
2331
import { generatePredefinedGroups } from '../utils/generate-predefined-groups'
2432
import { getEslintDisabledLines } from '../utils/get-eslint-disabled-lines'
2533
import { isNodeEslintDisabled } from '../utils/is-node-eslint-disabled'
26-
import { GROUP_ORDER_ERROR, ORDER_ERROR } from '../utils/report-errors'
2734
import { doesCustomGroupMatch } from '../utils/does-custom-group-match'
2835
import { sortNodesByGroups } from '../utils/sort-nodes-by-groups'
2936
import { createEslintRule } from '../utils/create-eslint-rule'
@@ -37,6 +44,8 @@ import { complete } from '../utils/complete'
3744

3845
type MESSAGE_ID =
3946
| 'unexpectedNamedImportsGroupOrder'
47+
| 'missedSpacingBetweenNamedImports'
48+
| 'extraSpacingBetweenNamedImports'
4049
| 'unexpectedNamedImportsOrder'
4150

4251
/**
@@ -49,6 +58,7 @@ let defaultOptions: Required<Options[0]> = {
4958
specialCharacters: 'keep',
5059
partitionByNewLine: false,
5160
partitionByComment: false,
61+
newlinesBetween: 'ignore',
5262
type: 'alphabetical',
5363
ignoreAlias: false,
5464
groupKind: 'mixed',
@@ -78,6 +88,7 @@ export default createEslintRule<Options, MESSAGE_ID>({
7888
selectors: allSelectors,
7989
options,
8090
})
91+
validateNewlinesAndPartitionConfiguration(options)
8192

8293
let { sourceCode, id } = context
8394
let eslintDisabledLines = getEslintDisabledLines({
@@ -200,6 +211,8 @@ export default createEslintRule<Options, MESSAGE_ID>({
200211

201212
reportAllErrors<MESSAGE_ID>({
202213
availableMessageIds: {
214+
missedSpacingBetweenMembers: 'missedSpacingBetweenNamedImports',
215+
extraSpacingBetweenMembers: 'extraSpacingBetweenNamedImports',
203216
unexpectedGroupOrder: 'unexpectedNamedImportsGroupOrder',
204217
unexpectedOrder: 'unexpectedNamedImportsOrder',
205218
},
@@ -231,6 +244,7 @@ export default createEslintRule<Options, MESSAGE_ID>({
231244
}),
232245
partitionByComment: partitionByCommentJsonSchema,
233246
partitionByNewLine: partitionByNewLineJsonSchema,
247+
newlinesBetween: newlinesBetweenJsonSchema,
234248
groups: groupsJsonSchema,
235249
},
236250
additionalProperties: false,
@@ -239,15 +253,17 @@ export default createEslintRule<Options, MESSAGE_ID>({
239253
uniqueItems: true,
240254
type: 'array',
241255
},
256+
messages: {
257+
missedSpacingBetweenNamedImports: MISSED_SPACING_ERROR,
258+
extraSpacingBetweenNamedImports: EXTRA_SPACING_ERROR,
259+
unexpectedNamedImportsGroupOrder: GROUP_ORDER_ERROR,
260+
unexpectedNamedImportsOrder: ORDER_ERROR,
261+
},
242262
docs: {
243263
url: 'https://perfectionist.dev/rules/sort-named-imports',
244264
description: 'Enforce sorted named imports.',
245265
recommended: true,
246266
},
247-
messages: {
248-
unexpectedNamedImportsGroupOrder: GROUP_ORDER_ERROR,
249-
unexpectedNamedImportsOrder: ORDER_ERROR,
250-
},
251267
type: 'suggestion',
252268
fixable: 'code',
253269
},

rules/sort-named-imports/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { TSESTree } from '@typescript-eslint/types'
33

44
import type {
55
PartitionByCommentOption,
6+
NewlinesBetweenOption,
67
CustomGroupsOption,
78
CommonOptions,
89
GroupsOptions,
@@ -25,6 +26,7 @@ export type Options = Partial<
2526
groupKind: 'values-first' | 'types-first' | 'mixed'
2627
customGroups: CustomGroupsOption<SingleCustomGroup>
2728
partitionByComment: PartitionByCommentOption
29+
newlinesBetween: NewlinesBetweenOption
2830
groups: GroupsOptions<Group>
2931
partitionByNewLine: boolean
3032
ignoreAlias: boolean

0 commit comments

Comments
 (0)