Skip to content

Commit

Permalink
feat: sort eslint rules (#158)
Browse files Browse the repository at this point in the history
* feat: sort eslint rules

* fix: use `sortObjectBy`
  • Loading branch information
fisker authored Feb 19, 2020
1 parent c35e306 commit fc6f8cd
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 8 deletions.
9 changes: 8 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,14 @@ const sortEslintConfig = onObject(
onArray(overrides => overrides.map(sortEslintConfig)),
),
overProperty('parserOptions', sortObject),
overProperty('rules', sortObject),
overProperty(
'rules',
sortObjectBy(
(rule1, rule2) =>
rule1.split('/').length - rule2.split('/').length ||
rule1.localeCompare(rule2),
),
),
overProperty('settings', sortObject),
]),
)
Expand Down
20 changes: 19 additions & 1 deletion tests/eslint.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,25 @@ test('eslintConfig', macro.sortObject, {
value: keysToObject(['z', ...baseEslintConfigKeys, 'a']),
})

for (const key of ['env', 'globals', 'parserOptions', 'rules', 'settings']) {
test('eslintConfig.rules', macro.sortObject, {
path: 'eslintConfig.rules',
expect: 'snapshot',
value: keysToObject([
'z',
'unknown-plugin/depth-1/depth-2',
'unicorn/new-for-builtins',
'unicorn/prefer-includes',
'for-direction',
'array-callback-return',
'yoda',
'prettier/prettier',
'react/display-name',
'react/jsx-key',
'a',
]),
})

for (const key of ['env', 'globals', 'parserOptions', 'settings']) {
const path = ['eslintConfig', key].join('.')
test(path, macro.sortObjectAlphabetically, { path })
}
Expand Down
2 changes: 1 addition & 1 deletion tests/snapshots/cli.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

Generated by [AVA](https://ava.li).
Generated by [AVA](https://avajs.dev).

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

Expand Down
Binary file modified tests/snapshots/cli.js.snap
Binary file not shown.
2 changes: 1 addition & 1 deletion tests/snapshots/deps.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

Generated by [AVA](https://ava.li).
Generated by [AVA](https://avajs.dev).

## peerDependenciesMeta

Expand Down
45 changes: 44 additions & 1 deletion tests/snapshots/eslint.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

Generated by [AVA](https://ava.li).
Generated by [AVA](https://avajs.dev).

## eslintConfig

Expand Down Expand Up @@ -93,3 +93,46 @@ Generated by [AVA](https://ava.li).
]␊
}␊
}`

## eslintConfig.rules

> Should sort `eslintConfig.rules` as object.
{
input: `{␊
"eslintConfig": {␊
"rules": {␊
"z": "z",␊
"unknown-plugin/depth-1/depth-2": "unknown-plugin/depth-1/depth-2",␊
"unicorn/new-for-builtins": "unicorn/new-for-builtins",␊
"unicorn/prefer-includes": "unicorn/prefer-includes",␊
"for-direction": "for-direction",␊
"array-callback-return": "array-callback-return",␊
"yoda": "yoda",␊
"prettier/prettier": "prettier/prettier",␊
"react/display-name": "react/display-name",␊
"react/jsx-key": "react/jsx-key",␊
"a": "a"␊
}␊
}␊
}`,
options: undefined,
output: `{␊
"eslintConfig": {␊
"rules": {␊
"a": "a",␊
"array-callback-return": "array-callback-return",␊
"for-direction": "for-direction",␊
"yoda": "yoda",␊
"z": "z",␊
"prettier/prettier": "prettier/prettier",␊
"react/display-name": "react/display-name",␊
"react/jsx-key": "react/jsx-key",␊
"unicorn/new-for-builtins": "unicorn/new-for-builtins",␊
"unicorn/prefer-includes": "unicorn/prefer-includes",␊
"unknown-plugin/depth-1/depth-2": "unknown-plugin/depth-1/depth-2"␊
}␊
}␊
}`,
pretty: true,
}
Binary file modified tests/snapshots/eslint.js.snap
Binary file not shown.
2 changes: 1 addition & 1 deletion tests/snapshots/fields.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

Generated by [AVA](https://ava.li).
Generated by [AVA](https://avajs.dev).

## badges

Expand Down
2 changes: 1 addition & 1 deletion tests/snapshots/main.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

Generated by [AVA](https://ava.li).
Generated by [AVA](https://avajs.dev).

## default sortOrder

Expand Down
2 changes: 1 addition & 1 deletion tests/snapshots/prettier.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

Generated by [AVA](https://ava.li).
Generated by [AVA](https://avajs.dev).

## prettier

Expand Down

0 comments on commit fc6f8cd

Please sign in to comment.