Skip to content

Commit fc6f8cd

Browse files
authored
feat: sort eslint rules (#158)
* feat: sort eslint rules * fix: use `sortObjectBy`
1 parent c35e306 commit fc6f8cd

10 files changed

+76
-8
lines changed

index.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,14 @@ const sortEslintConfig = onObject(
7575
onArray(overrides => overrides.map(sortEslintConfig)),
7676
),
7777
overProperty('parserOptions', sortObject),
78-
overProperty('rules', sortObject),
78+
overProperty(
79+
'rules',
80+
sortObjectBy(
81+
(rule1, rule2) =>
82+
rule1.split('/').length - rule2.split('/').length ||
83+
rule1.localeCompare(rule2),
84+
),
85+
),
7986
overProperty('settings', sortObject),
8087
]),
8188
)

tests/eslint.js

+19-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,25 @@ test('eslintConfig', macro.sortObject, {
2525
value: keysToObject(['z', ...baseEslintConfigKeys, 'a']),
2626
})
2727

28-
for (const key of ['env', 'globals', 'parserOptions', 'rules', 'settings']) {
28+
test('eslintConfig.rules', macro.sortObject, {
29+
path: 'eslintConfig.rules',
30+
expect: 'snapshot',
31+
value: keysToObject([
32+
'z',
33+
'unknown-plugin/depth-1/depth-2',
34+
'unicorn/new-for-builtins',
35+
'unicorn/prefer-includes',
36+
'for-direction',
37+
'array-callback-return',
38+
'yoda',
39+
'prettier/prettier',
40+
'react/display-name',
41+
'react/jsx-key',
42+
'a',
43+
]),
44+
})
45+
46+
for (const key of ['env', 'globals', 'parserOptions', 'settings']) {
2947
const path = ['eslintConfig', key].join('.')
3048
test(path, macro.sortObjectAlphabetically, { path })
3149
}

tests/snapshots/cli.js.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
The actual snapshot is saved in `cli.js.snap`.
44

5-
Generated by [AVA](https://ava.li).
5+
Generated by [AVA](https://avajs.dev).
66

77
## run `cli --check` on 1 bad file
88

tests/snapshots/cli.js.snap

25 Bytes
Binary file not shown.

tests/snapshots/deps.js.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
The actual snapshot is saved in `deps.js.snap`.
44

5-
Generated by [AVA](https://ava.li).
5+
Generated by [AVA](https://avajs.dev).
66

77
## peerDependenciesMeta
88

tests/snapshots/eslint.js.md

+44-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
The actual snapshot is saved in `eslint.js.snap`.
44

5-
Generated by [AVA](https://ava.li).
5+
Generated by [AVA](https://avajs.dev).
66

77
## eslintConfig
88

@@ -93,3 +93,46 @@ Generated by [AVA](https://ava.li).
9393
]␊
9494
}␊
9595
}`
96+
97+
## eslintConfig.rules
98+
99+
> Should sort `eslintConfig.rules` as object.
100+
101+
{
102+
input: `{␊
103+
"eslintConfig": {␊
104+
"rules": {␊
105+
"z": "z",␊
106+
"unknown-plugin/depth-1/depth-2": "unknown-plugin/depth-1/depth-2",␊
107+
"unicorn/new-for-builtins": "unicorn/new-for-builtins",␊
108+
"unicorn/prefer-includes": "unicorn/prefer-includes",␊
109+
"for-direction": "for-direction",␊
110+
"array-callback-return": "array-callback-return",␊
111+
"yoda": "yoda",␊
112+
"prettier/prettier": "prettier/prettier",␊
113+
"react/display-name": "react/display-name",␊
114+
"react/jsx-key": "react/jsx-key",␊
115+
"a": "a"␊
116+
}␊
117+
}␊
118+
}`,
119+
options: undefined,
120+
output: `{␊
121+
"eslintConfig": {␊
122+
"rules": {␊
123+
"a": "a",␊
124+
"array-callback-return": "array-callback-return",␊
125+
"for-direction": "for-direction",␊
126+
"yoda": "yoda",␊
127+
"z": "z",␊
128+
"prettier/prettier": "prettier/prettier",␊
129+
"react/display-name": "react/display-name",␊
130+
"react/jsx-key": "react/jsx-key",␊
131+
"unicorn/new-for-builtins": "unicorn/new-for-builtins",␊
132+
"unicorn/prefer-includes": "unicorn/prefer-includes",␊
133+
"unknown-plugin/depth-1/depth-2": "unknown-plugin/depth-1/depth-2"␊
134+
}␊
135+
}␊
136+
}`,
137+
pretty: true,
138+
}

tests/snapshots/eslint.js.snap

261 Bytes
Binary file not shown.

tests/snapshots/fields.js.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
The actual snapshot is saved in `fields.js.snap`.
44

5-
Generated by [AVA](https://ava.li).
5+
Generated by [AVA](https://avajs.dev).
66

77
## badges
88

tests/snapshots/main.js.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
The actual snapshot is saved in `main.js.snap`.
44

5-
Generated by [AVA](https://ava.li).
5+
Generated by [AVA](https://avajs.dev).
66

77
## default sortOrder
88

tests/snapshots/prettier.js.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
The actual snapshot is saved in `prettier.js.snap`.
44

5-
Generated by [AVA](https://ava.li).
5+
Generated by [AVA](https://avajs.dev).
66

77
## prettier
88

0 commit comments

Comments
 (0)