Skip to content

Commit a73a2f2

Browse files
committed
refactor: move the "groups" duplicates/existence validation logic up to the schema
1 parent 30b4dcf commit a73a2f2

File tree

2 files changed

+16
-66
lines changed

2 files changed

+16
-66
lines changed

src/rules/order.js

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -590,18 +590,14 @@ function getRequireBlock(node) {
590590

591591
const types = ['builtin', 'external', 'internal', 'unknown', 'parent', 'sibling', 'index', 'object', 'type'];
592592

593-
// Creates an object with type-rank pairs.
594-
// Example: { index: 0, sibling: 1, parent: 1, external: 1, builtin: 2, internal: 2 }
595-
// Will throw an error if it contains a type that does not exist, or has a duplicate
593+
/**
594+
* Creates an object with type-rank pairs.
595+
*
596+
* Example: { index: 0, sibling: 1, parent: 1, external: 1, builtin: 2, internal: 2 }
597+
*/
596598
function convertGroupsToRanks(groups) {
597599
const rankObject = groups.reduce(function (res, group, index) {
598600
[].concat(group).forEach(function (groupItem) {
599-
if (types.indexOf(groupItem) === -1) {
600-
throw new Error(`Incorrect configuration of the rule: Unknown type \`${JSON.stringify(groupItem)}\``);
601-
}
602-
if (res[groupItem] !== undefined) {
603-
throw new Error(`Incorrect configuration of the rule: \`${groupItem}\` is duplicated`);
604-
}
605601
res[groupItem] = index * 2;
606602
});
607603
return res;
@@ -858,6 +854,17 @@ module.exports = {
858854
properties: {
859855
groups: {
860856
type: 'array',
857+
uniqueItems: true,
858+
items: {
859+
oneOf: [
860+
{ enum: types },
861+
{
862+
type: 'array',
863+
uniqueItems: true,
864+
items: { enum: types },
865+
},
866+
],
867+
},
861868
},
862869
pathGroupsExcludedImportTypes: {
863870
type: 'array',

tests/src/rules/order.js

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1643,63 +1643,6 @@ ruleTester.run('order', rule, {
16431643
message: '`async` import should occur before import of `path`',
16441644
}],
16451645
}),
1646-
// Setting the order for an unknown type
1647-
// should make the rule trigger an error and do nothing else
1648-
test({
1649-
code: `
1650-
var async = require('async');
1651-
var index = require('./');
1652-
`,
1653-
options: [{ groups: [
1654-
'index',
1655-
['sibling', 'parent', 'UNKNOWN', 'internal'],
1656-
] }],
1657-
errors: [{
1658-
message: 'Incorrect configuration of the rule: Unknown type `"UNKNOWN"`',
1659-
}],
1660-
}),
1661-
// Type in an array can't be another array, too much nesting
1662-
test({
1663-
code: `
1664-
var async = require('async');
1665-
var index = require('./');
1666-
`,
1667-
options: [{ groups: [
1668-
'index',
1669-
['sibling', 'parent', ['builtin'], 'internal'],
1670-
] }],
1671-
errors: [{
1672-
message: 'Incorrect configuration of the rule: Unknown type `["builtin"]`',
1673-
}],
1674-
}),
1675-
// No numbers
1676-
test({
1677-
code: `
1678-
var async = require('async');
1679-
var index = require('./');
1680-
`,
1681-
options: [{ groups: [
1682-
'index',
1683-
['sibling', 'parent', 2, 'internal'],
1684-
] }],
1685-
errors: [{
1686-
message: 'Incorrect configuration of the rule: Unknown type `2`',
1687-
}],
1688-
}),
1689-
// Duplicate
1690-
test({
1691-
code: `
1692-
var async = require('async');
1693-
var index = require('./');
1694-
`,
1695-
options: [{ groups: [
1696-
'index',
1697-
['sibling', 'parent', 'parent', 'internal'],
1698-
] }],
1699-
errors: [{
1700-
message: 'Incorrect configuration of the rule: `parent` is duplicated',
1701-
}],
1702-
}),
17031646
// Mixing require and import should have import up top
17041647
test({
17051648
code: `

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy