Skip to content

Commit e6e0588

Browse files
authored
feat: add missing newlines between validation in groups
1 parent e667433 commit e6e0588

12 files changed

+177
-101
lines changed

rules/sort-decorators.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,11 @@ export default createEslintRule<Options, MESSAGE_ID>({
7878

7979
let options = complete(context.options.at(0), settings, defaultOptions)
8080
validateCustomSortConfiguration(options)
81-
validateGroupsConfiguration(
82-
options.groups,
83-
['unknown'],
84-
Object.keys(options.customGroups),
85-
)
81+
validateGroupsConfiguration({
82+
allowedCustomGroups: Object.keys(options.customGroups),
83+
allowedPredefinedGroups: ['unknown'],
84+
options,
85+
})
8686

8787
return {
8888
Decorator: decorator => {

rules/sort-heritage-clauses.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,11 @@ export default createEslintRule<Options, MESSAGE_ID>({
8383

8484
let options = complete(context.options.at(0), settings, defaultOptions)
8585
validateCustomSortConfiguration(options)
86-
validateGroupsConfiguration(
87-
options.groups,
88-
['unknown'],
89-
Object.keys(options.customGroups),
90-
)
86+
validateGroupsConfiguration({
87+
allowedCustomGroups: Object.keys(options.customGroups),
88+
allowedPredefinedGroups: ['unknown'],
89+
options,
90+
})
9191

9292
return {
9393
TSInterfaceDeclaration: declaration =>

rules/sort-imports.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,8 @@ export default createEslintRule<Options, MESSAGE_ID>({
135135
} as const),
136136
)
137137

138-
validateGroupsConfiguration(
139-
options.groups,
140-
[
138+
validateGroupsConfiguration({
139+
allowedPredefinedGroups: [
141140
'side-effect-style',
142141
'external-type',
143142
'internal-type',
@@ -157,11 +156,12 @@ export default createEslintRule<Options, MESSAGE_ID>({
157156
'style',
158157
'type',
159158
],
160-
[
159+
allowedCustomGroups: [
161160
...Object.keys(options.customGroups.type ?? {}),
162161
...Object.keys(options.customGroups.value ?? {}),
163162
],
164-
)
163+
options,
164+
})
165165
validateCustomSortConfiguration(options)
166166
validateNewlinesAndPartitionConfiguration(options)
167167

rules/sort-jsx-props.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,11 @@ export default createEslintRule<Options, MESSAGE_ID>({
8484
let settings = getSettings(context.settings)
8585
let options = complete(context.options.at(0), settings, defaultOptions)
8686
validateCustomSortConfiguration(options)
87-
validateGroupsConfiguration(
88-
options.groups,
89-
['multiline', 'shorthand', 'unknown'],
90-
Object.keys(options.customGroups),
91-
)
87+
validateGroupsConfiguration({
88+
allowedPredefinedGroups: ['multiline', 'shorthand', 'unknown'],
89+
allowedCustomGroups: Object.keys(options.customGroups),
90+
options,
91+
})
9292
validateNewlinesAndPartitionConfiguration(options)
9393

9494
let sourceCode = getSourceCode(context)

rules/sort-union-types.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,8 @@ export let sortUnionOrIntersectionTypes = <MessageIds extends string>({
155155

156156
let options = complete(context.options.at(0), settings, defaultOptions)
157157
validateCustomSortConfiguration(options)
158-
validateGroupsConfiguration(
159-
options.groups,
160-
[
158+
validateGroupsConfiguration({
159+
allowedPredefinedGroups: [
161160
'intersection',
162161
'conditional',
163162
'function',
@@ -172,8 +171,9 @@ export let sortUnionOrIntersectionTypes = <MessageIds extends string>({
172171
'tuple',
173172
'union',
174173
],
175-
[],
176-
)
174+
allowedCustomGroups: [],
175+
options,
176+
})
177177
validateNewlinesAndPartitionConfiguration(options)
178178

179179
let sourceCode = getSourceCode(context)

test/utils/validate-generated-groups-configuration.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,22 @@ describe('validate-generated-groups-configuration', () => {
8787
}),
8888
).toThrow('Invalid group(s): myCustomGroup')
8989
})
90+
91+
it('throws an error with consecutive newlines objects', () => {
92+
expect(() => {
93+
validateGeneratedGroupsConfiguration({
94+
options: {
95+
groups: [
96+
{ newlinesBetween: 'always' },
97+
{ newlinesBetween: 'always' },
98+
],
99+
customGroups: [],
100+
},
101+
selectors: [],
102+
modifiers: [],
103+
})
104+
}).toThrow("Consecutive 'newlinesBetween' objects are not allowed")
105+
})
90106
})
91107

92108
let getAllNonEmptyCombinations = (array: string[]): string[][] => {

test/utils/validate-groups-configuration.test.ts

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,36 +14,46 @@ import { validateGroupsConfiguration } from '../../utils/validate-groups-configu
1414
describe('validate-groups-configuration', () => {
1515
it('throws an error when an invalid group is provided', () => {
1616
expect(() => {
17-
validateGroupsConfiguration(
18-
['predefinedGroup', ['customGroup', 'invalidGroup1'], 'invalidGroup2'],
19-
['predefinedGroup'],
20-
['customGroup'],
21-
)
17+
validateGroupsConfiguration({
18+
options: {
19+
groups: [
20+
'predefinedGroup',
21+
['customGroup', 'invalidGroup1'],
22+
'invalidGroup2',
23+
],
24+
},
25+
allowedPredefinedGroups: ['predefinedGroup'],
26+
allowedCustomGroups: ['customGroup'],
27+
})
2228
}).toThrow('Invalid group(s): invalidGroup1, invalidGroup2')
2329
})
2430

2531
it('throws an error when a duplicate group is provided', () => {
2632
expect(() => {
27-
validateGroupsConfiguration(
28-
['predefinedGroup', 'predefinedGroup'],
29-
['predefinedGroup'],
30-
[],
31-
)
33+
validateGroupsConfiguration({
34+
options: {
35+
groups: ['predefinedGroup', 'predefinedGroup'],
36+
},
37+
allowedPredefinedGroups: ['predefinedGroup'],
38+
allowedCustomGroups: [],
39+
})
3240
}).toThrow('Duplicated group(s): predefinedGroup')
3341
})
3442

3543
it('throws an error with consecutive newlines objects', () => {
3644
expect(() => {
37-
validateGroupsConfiguration(
38-
[
39-
'a',
40-
{ newlinesBetween: 'always' },
41-
{ newlinesBetween: 'always' },
42-
'b',
43-
],
44-
['a', 'b'],
45-
[],
46-
)
45+
validateGroupsConfiguration({
46+
options: {
47+
groups: [
48+
'a',
49+
{ newlinesBetween: 'always' },
50+
{ newlinesBetween: 'always' },
51+
'b',
52+
],
53+
},
54+
allowedPredefinedGroups: ['a', 'b'],
55+
allowedCustomGroups: [],
56+
})
4757
}).toThrow("Consecutive 'newlinesBetween' objects are not allowed")
4858
})
4959
})
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { describe, expect, it } from 'vitest'
2+
3+
import { validateNewlinesBetweenInsideGroups } from '../../utils/validate-newlines-between-inside-groups'
4+
5+
describe('validate-newlines-between-inside-groups', () => {
6+
it('throws an error with consecutive newlines objects', () => {
7+
expect(() => {
8+
validateNewlinesBetweenInsideGroups({
9+
groups: [{ newlinesBetween: 'always' }, { newlinesBetween: 'always' }],
10+
})
11+
}).toThrow("Consecutive 'newlinesBetween' objects are not allowed")
12+
})
13+
})

utils/validate-generated-groups-configuration.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import type {
44
GroupsOptions,
55
} from '../types/common-options'
66

7-
import { validateNoDuplicatedGroups } from './validate-groups-configuration'
7+
import { validateNewlinesBetweenInsideGroups } from './validate-newlines-between-inside-groups'
8+
import { validateNoDuplicatedGroups } from './validate-no-duplicated-groups'
89

910
interface ValidateGenerateGroupsConfigurationParameters {
1011
options: {
@@ -36,7 +37,8 @@ export let validateGeneratedGroupsConfiguration = ({
3637
if (invalidGroups.length > 0) {
3738
throw new Error(`Invalid group(s): ${invalidGroups.join(', ')}`)
3839
}
39-
validateNoDuplicatedGroups(options.groups)
40+
validateNoDuplicatedGroups(options)
41+
validateNewlinesBetweenInsideGroups(options)
4042
}
4143

4244
let isPredefinedGroup = (
Lines changed: 33 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,58 @@
1-
import type { NewlinesBetweenOption } from '../types/common-options'
1+
import type { GroupsOptions } from '../types/common-options'
22

3+
import { validateNewlinesBetweenInsideGroups } from './validate-newlines-between-inside-groups'
4+
import { validateNoDuplicatedGroups } from './validate-no-duplicated-groups'
35
import { isNewlinesBetweenOption } from './is-newlines-between-option'
46

5-
type Group = { newlinesBetween: NewlinesBetweenOption } | string[] | string
7+
interface ValidateGroupsConfigurationParameters {
8+
options: {
9+
groups: GroupsOptions<string>
10+
}
11+
allowedPredefinedGroups: string[]
12+
allowedCustomGroups: string[]
13+
}
614

715
/**
816
* Throws an error if one of the following conditions is met:
917
* - One or more groups specified in `groups` are not predefined nor specified
1018
* in `customGroups`
1119
* - A group is specified in `groups` more than once
12-
* @param {Group[]} groups - The groups to validate.
13-
* @param {string[]} allowedPredefinedGroups - An array of predefined group
20+
* @param {object} parameters - Parameters object.
21+
* @param {object} parameters.options - Options containing the groups to validate.
22+
* @param {string[]} parameters.allowedPredefinedGroups - An array of predefined
23+
* group names that are considered valid.
24+
* @param {string[]} parameters.allowedCustomGroups - An array of custom group
1425
* names that are considered valid.
15-
* @param {string[]} allowedCustomGroups - An array of custom group names that
16-
* are considered valid.
17-
* @throws Will throw an error if invalid or duplicated groups are found.
26+
* @throws Error Will throw an error if invalid or duplicated groups are found.
1827
*/
19-
export let validateGroupsConfiguration = (
20-
groups: Group[],
21-
allowedPredefinedGroups: string[],
22-
allowedCustomGroups: string[],
23-
): void => {
28+
export let validateGroupsConfiguration = ({
29+
allowedPredefinedGroups,
30+
allowedCustomGroups,
31+
options,
32+
}: ValidateGroupsConfigurationParameters): void => {
2433
let allowedGroupsSet = new Set([
2534
...allowedPredefinedGroups,
2635
...allowedCustomGroups,
2736
])
2837
let invalidGroups: string[] = []
29-
let isPreviousElementNewlinesBetween = false
30-
for (let groupElement of groups) {
38+
39+
for (let groupElement of options.groups) {
3140
if (isNewlinesBetweenOption(groupElement)) {
32-
// There should not be two consecutive `newlinesBetween` objects
33-
if (isPreviousElementNewlinesBetween) {
34-
throw new Error("Consecutive 'newlinesBetween' objects are not allowed")
35-
}
36-
isPreviousElementNewlinesBetween = true
37-
} else {
38-
isPreviousElementNewlinesBetween = false
39-
let groupElements = Array.isArray(groupElement)
40-
? groupElement
41-
: [groupElement]
42-
for (let group of groupElements) {
43-
if (!allowedGroupsSet.has(group)) {
44-
invalidGroups.push(group)
45-
}
41+
continue
42+
}
43+
let groupElements = Array.isArray(groupElement)
44+
? groupElement
45+
: [groupElement]
46+
for (let group of groupElements) {
47+
if (!allowedGroupsSet.has(group)) {
48+
invalidGroups.push(group)
4649
}
4750
}
4851
}
4952
if (invalidGroups.length > 0) {
5053
throw new Error(`Invalid group(s): ${invalidGroups.join(', ')}`)
5154
}
52-
validateNoDuplicatedGroups(groups)
53-
}
54-
55-
/**
56-
* Throws an error if a group is specified more than once
57-
* @param {Group[]} groups - The groups to check for duplicates.
58-
* @throws Will throw an error if duplicated groups are found.
59-
*/
60-
export let validateNoDuplicatedGroups = (groups: Group[]): void => {
61-
let flattenGroups = groups.flat()
62-
let seenGroups = new Set<string>()
63-
let duplicatedGroups = new Set<string>()
64-
65-
for (let group of flattenGroups) {
66-
if (isNewlinesBetweenOption(group)) {
67-
continue
68-
}
69-
if (seenGroups.has(group)) {
70-
duplicatedGroups.add(group)
71-
} else {
72-
seenGroups.add(group)
73-
}
74-
}
7555

76-
if (duplicatedGroups.size > 0) {
77-
throw new Error(`Duplicated group(s): ${[...duplicatedGroups].join(', ')}`)
78-
}
56+
validateNoDuplicatedGroups(options)
57+
validateNewlinesBetweenInsideGroups(options)
7958
}

0 commit comments

Comments
 (0)