-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add missing newlines between validation in groups
- Loading branch information
Showing
12 changed files
with
177 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
test/utils/validate-newlines-between-inside-groups.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { describe, expect, it } from 'vitest' | ||
|
||
import { validateNewlinesBetweenInsideGroups } from '../../utils/validate-newlines-between-inside-groups' | ||
|
||
describe('validate-newlines-between-inside-groups', () => { | ||
it('throws an error with consecutive newlines objects', () => { | ||
expect(() => { | ||
validateNewlinesBetweenInsideGroups({ | ||
groups: [{ newlinesBetween: 'always' }, { newlinesBetween: 'always' }], | ||
}) | ||
}).toThrow("Consecutive 'newlinesBetween' objects are not allowed") | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,58 @@ | ||
import type { NewlinesBetweenOption } from '../types/common-options' | ||
import type { GroupsOptions } from '../types/common-options' | ||
|
||
import { validateNewlinesBetweenInsideGroups } from './validate-newlines-between-inside-groups' | ||
import { validateNoDuplicatedGroups } from './validate-no-duplicated-groups' | ||
import { isNewlinesBetweenOption } from './is-newlines-between-option' | ||
|
||
type Group = { newlinesBetween: NewlinesBetweenOption } | string[] | string | ||
interface ValidateGroupsConfigurationParameters { | ||
options: { | ||
groups: GroupsOptions<string> | ||
} | ||
allowedPredefinedGroups: string[] | ||
allowedCustomGroups: string[] | ||
} | ||
|
||
/** | ||
* Throws an error if one of the following conditions is met: | ||
* - One or more groups specified in `groups` are not predefined nor specified | ||
* in `customGroups` | ||
* - A group is specified in `groups` more than once | ||
* @param {Group[]} groups - The groups to validate. | ||
* @param {string[]} allowedPredefinedGroups - An array of predefined group | ||
* @param {object} parameters - Parameters object. | ||
* @param {object} parameters.options - Options containing the groups to validate. | ||
* @param {string[]} parameters.allowedPredefinedGroups - An array of predefined | ||
* group names that are considered valid. | ||
* @param {string[]} parameters.allowedCustomGroups - An array of custom group | ||
* names that are considered valid. | ||
* @param {string[]} allowedCustomGroups - An array of custom group names that | ||
* are considered valid. | ||
* @throws Will throw an error if invalid or duplicated groups are found. | ||
* @throws Error Will throw an error if invalid or duplicated groups are found. | ||
*/ | ||
export let validateGroupsConfiguration = ( | ||
groups: Group[], | ||
allowedPredefinedGroups: string[], | ||
allowedCustomGroups: string[], | ||
): void => { | ||
export let validateGroupsConfiguration = ({ | ||
allowedPredefinedGroups, | ||
allowedCustomGroups, | ||
options, | ||
}: ValidateGroupsConfigurationParameters): void => { | ||
let allowedGroupsSet = new Set([ | ||
...allowedPredefinedGroups, | ||
...allowedCustomGroups, | ||
]) | ||
let invalidGroups: string[] = [] | ||
let isPreviousElementNewlinesBetween = false | ||
for (let groupElement of groups) { | ||
|
||
for (let groupElement of options.groups) { | ||
if (isNewlinesBetweenOption(groupElement)) { | ||
// There should not be two consecutive `newlinesBetween` objects | ||
if (isPreviousElementNewlinesBetween) { | ||
throw new Error("Consecutive 'newlinesBetween' objects are not allowed") | ||
} | ||
isPreviousElementNewlinesBetween = true | ||
} else { | ||
isPreviousElementNewlinesBetween = false | ||
let groupElements = Array.isArray(groupElement) | ||
? groupElement | ||
: [groupElement] | ||
for (let group of groupElements) { | ||
if (!allowedGroupsSet.has(group)) { | ||
invalidGroups.push(group) | ||
} | ||
continue | ||
} | ||
let groupElements = Array.isArray(groupElement) | ||
? groupElement | ||
: [groupElement] | ||
for (let group of groupElements) { | ||
if (!allowedGroupsSet.has(group)) { | ||
invalidGroups.push(group) | ||
} | ||
} | ||
} | ||
if (invalidGroups.length > 0) { | ||
throw new Error(`Invalid group(s): ${invalidGroups.join(', ')}`) | ||
} | ||
validateNoDuplicatedGroups(groups) | ||
} | ||
|
||
/** | ||
* Throws an error if a group is specified more than once | ||
* @param {Group[]} groups - The groups to check for duplicates. | ||
* @throws Will throw an error if duplicated groups are found. | ||
*/ | ||
export let validateNoDuplicatedGroups = (groups: Group[]): void => { | ||
let flattenGroups = groups.flat() | ||
let seenGroups = new Set<string>() | ||
let duplicatedGroups = new Set<string>() | ||
|
||
for (let group of flattenGroups) { | ||
if (isNewlinesBetweenOption(group)) { | ||
continue | ||
} | ||
if (seenGroups.has(group)) { | ||
duplicatedGroups.add(group) | ||
} else { | ||
seenGroups.add(group) | ||
} | ||
} | ||
|
||
if (duplicatedGroups.size > 0) { | ||
throw new Error(`Duplicated group(s): ${[...duplicatedGroups].join(', ')}`) | ||
} | ||
validateNoDuplicatedGroups(options) | ||
validateNewlinesBetweenInsideGroups(options) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import type { GroupsOptions } from '../types/common-options' | ||
|
||
import { isNewlinesBetweenOption } from './is-newlines-between-option' | ||
|
||
export let validateNewlinesBetweenInsideGroups = ({ | ||
groups, | ||
}: { | ||
groups: GroupsOptions<string> | ||
}): void => { | ||
let isPreviousElementNewlinesBetween = false | ||
for (let groupElement of groups) { | ||
if (!isNewlinesBetweenOption(groupElement)) { | ||
isPreviousElementNewlinesBetween = false | ||
continue | ||
} | ||
// There should not be two consecutive `newlinesBetween` objects | ||
if (isPreviousElementNewlinesBetween) { | ||
throw new Error("Consecutive 'newlinesBetween' objects are not allowed") | ||
} | ||
isPreviousElementNewlinesBetween = true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import type { GroupsOptions } from '../types/common-options' | ||
|
||
import { isNewlinesBetweenOption } from './is-newlines-between-option' | ||
|
||
/** | ||
* Throws an error if a group is specified more than once | ||
* @param {object} parameters - Parameters object. | ||
* @param {GroupsOptions} parameters.groups - The groups to check for duplicates. | ||
* @throws Error Will throw an error if duplicated groups are found. | ||
*/ | ||
export let validateNoDuplicatedGroups = ({ | ||
groups, | ||
}: { | ||
groups: GroupsOptions<string> | ||
}): void => { | ||
let flattenGroups = groups.flat() | ||
let seenGroups = new Set<string>() | ||
let duplicatedGroups = new Set<string>() | ||
|
||
for (let group of flattenGroups) { | ||
if (isNewlinesBetweenOption(group)) { | ||
continue | ||
} | ||
if (seenGroups.has(group)) { | ||
duplicatedGroups.add(group) | ||
} else { | ||
seenGroups.add(group) | ||
} | ||
} | ||
|
||
if (duplicatedGroups.size > 0) { | ||
throw new Error(`Duplicated group(s): ${[...duplicatedGroups].join(', ')}`) | ||
} | ||
} |