Skip to content

Commit 396ed8a

Browse files
fiskerkeithamus
authored andcommitted
feat: sort eslintConfig deeper (#128)
1 parent d02d8ef commit 396ed8a

File tree

2 files changed

+117
-13
lines changed

2 files changed

+117
-13
lines changed

index.js

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,15 @@ const overProperty = (property, over) => object =>
3030
? Object.assign(object, { [property]: over(object[property]) })
3131
: object
3232
const sortGitHooks = sortObjectBy(gitHooks)
33-
const sortESLintConfig = sortObjectBy([
33+
34+
// https://github.com/eslint/eslint/blob/acc0e47572a9390292b4e313b4a4bf360d236358/conf/config-schema.js
35+
const eslintBaseConfigProperties = [
36+
// `files` and `excludedFiles` are only on `overrides[]`
37+
// for easier sort `overrides[]`,
38+
// add them to here, so we don't need sort `overrides[]` twice
39+
'files',
40+
'excludedFiles',
41+
// baseConfig
3442
'env',
3543
'parser',
3644
'parserOptions',
@@ -43,7 +51,21 @@ const sortESLintConfig = sortObjectBy([
4351
'processor',
4452
'noInlineConfig',
4553
'reportUnusedDisableDirectives',
46-
])
54+
]
55+
const sortEslintConfig = onObject(
56+
pipe([
57+
sortObjectBy(eslintBaseConfigProperties),
58+
overProperty('env', sortObject),
59+
overProperty('globals', sortObject),
60+
overProperty(
61+
'overrides',
62+
onArray(overrides => overrides.map(sortEslintConfig)),
63+
),
64+
overProperty('parserOptions', sortObject),
65+
overProperty('rules', sortObject),
66+
overProperty('settings', sortObject),
67+
]),
68+
)
4769
const sortVSCodeBadgeObject = sortObjectBy(['description', 'url', 'href'])
4870

4971
const sortPrettierConfig = onObject(
@@ -192,7 +214,7 @@ const fields = [
192214
{ key: 'browserslist' },
193215
{ key: 'xo', over: sortObject },
194216
{ key: 'prettier', over: sortPrettierConfig },
195-
{ key: 'eslintConfig', over: sortESLintConfig },
217+
{ key: 'eslintConfig', over: sortEslintConfig },
196218
{ key: 'eslintIgnore' },
197219
{ key: 'stylelint' },
198220
{ key: 'ava', over: sortObject },

test.js

Lines changed: 92 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -291,18 +291,100 @@ testField('husky', [
291291
},
292292
])
293293

294-
testField('eslintConfig', [
295-
{
296-
value: {
297-
overrides: [],
298-
extends: ['standard', 'plugin:prettier/recommended', 'prettier/standard'],
299-
[UNKNOWN]: UNKNOWN,
300-
rules: {},
301-
plugins: ['prettier'],
294+
// eslint
295+
const sortedEslintConfig = sortPackageJson({
296+
eslintConfig: {
297+
overrides: [
298+
{
299+
z: 'z',
300+
a: 'a',
301+
files: '*.js',
302+
excludedFiles: '*.exclude.js',
303+
rules: {
304+
z: 'z',
305+
a: 'a',
306+
semi: 'error',
307+
},
308+
},
309+
],
310+
extends: ['standard', 'plugin:prettier/recommended', 'prettier/standard'],
311+
z: 'z',
312+
a: 'a',
313+
rules: {
314+
z: 'z',
315+
a: 'a',
316+
semi: 'error',
317+
},
318+
env: {
319+
z: 'z',
320+
a: 'a',
321+
browser: true,
322+
node: true,
302323
},
303-
expect: ['plugins', 'extends', 'rules', 'overrides', UNKNOWN],
324+
globals: {
325+
z: 'z',
326+
a: 'a',
327+
var1: 'writable',
328+
var2: 'readonly',
329+
},
330+
parserOptions: {
331+
z: 'z',
332+
a: 'a',
333+
ecmaVersion: 6,
334+
sourceType: 'module',
335+
ecmaFeatures: {
336+
jsx: true,
337+
},
338+
},
339+
settings: {
340+
z: 'z',
341+
a: 'a',
342+
'import/extensions': '',
343+
},
344+
plugins: ['a', 'z', 'prettier'],
304345
},
305-
])
346+
}).eslintConfig
347+
assert.deepStrictEqual(
348+
Object.keys(sortedEslintConfig),
349+
[
350+
'env',
351+
'parserOptions',
352+
'settings',
353+
'plugins',
354+
'extends',
355+
'rules',
356+
'overrides',
357+
'globals',
358+
'a',
359+
'z',
360+
],
361+
'eslintConfig field should sorted',
362+
)
363+
assert.deepStrictEqual(
364+
Object.keys(sortedEslintConfig.parserOptions),
365+
['a', 'ecmaFeatures', 'ecmaVersion', 'sourceType', 'z'],
366+
'eslintConfig.parserOptions should sorted',
367+
)
368+
assert.deepStrictEqual(
369+
Object.keys(sortedEslintConfig.rules),
370+
['a', 'semi', 'z'],
371+
'eslintConfig.rules should sorted',
372+
)
373+
assert.deepStrictEqual(
374+
Object.keys(sortedEslintConfig.settings),
375+
['a', 'import/extensions', 'z'],
376+
'eslintConfig.settings should sorted',
377+
)
378+
assert.deepStrictEqual(
379+
Object.keys(sortedEslintConfig.overrides[0]),
380+
['files', 'excludedFiles', 'rules', 'a', 'z'],
381+
'eslintConfig.overrides[0] should sorted',
382+
)
383+
assert.deepStrictEqual(
384+
Object.keys(sortedEslintConfig.overrides[0].rules),
385+
['a', 'semi', 'z'],
386+
'eslintConfig.overrides[0].rules should sorted',
387+
)
306388

307389
// prettier
308390
const sortedPrettierConfig = sortPackageJson({

0 commit comments

Comments
 (0)