diff --git a/index.js b/index.js index 5e905521..8037c32f 100644 --- a/index.js +++ b/index.js @@ -29,7 +29,15 @@ const overProperty = (property, over) => object => ? Object.assign(object, { [property]: over(object[property]) }) : object const sortGitHooks = sortObjectBy(gitHooks) -const sortESLintConfig = sortObjectBy([ + +// https://github.com/eslint/eslint/blob/acc0e47572a9390292b4e313b4a4bf360d236358/conf/config-schema.js +const eslintBaseConfigProperties = [ + // `files` and `excludedFiles` are only on `overrides[]` + // for easier sort `overrides[]`, + // add them to here, so we don't need sort `overrides[]` twice + 'files', + 'excludedFiles', + // baseConfig 'env', 'parser', 'parserOptions', @@ -42,7 +50,21 @@ const sortESLintConfig = sortObjectBy([ 'processor', 'noInlineConfig', 'reportUnusedDisableDirectives', -]) +] +const sortEslintConfig = onObject( + pipe([ + sortObjectBy(eslintBaseConfigProperties), + overProperty('env', sortObject), + overProperty('globals', sortObject), + overProperty( + 'overrides', + onArray(overrides => overrides.map(sortEslintConfig)), + ), + overProperty('parserOptions', sortObject), + overProperty('rules', sortObject), + overProperty('settings', sortObject), + ]), +) const sortVSCodeBadgeObject = sortObjectBy(['description', 'url', 'href']) const sortPrettierConfigKeys = onObject(config => @@ -183,7 +205,7 @@ const fields = [ { key: 'browserslist' }, { key: 'xo', over: sortObject }, { key: 'prettier', over: sortPrettierConfig }, - { key: 'eslintConfig', over: sortESLintConfig }, + { key: 'eslintConfig', over: sortEslintConfig }, { key: 'eslintIgnore' }, { key: 'stylelint' }, { key: 'ava', over: sortObject }, diff --git a/test.js b/test.js index 2adbab5b..b2e50833 100644 --- a/test.js +++ b/test.js @@ -291,18 +291,100 @@ testField('husky', [ }, ]) -testField('eslintConfig', [ - { - value: { - overrides: [], - extends: ['standard', 'plugin:prettier/recommended', 'prettier/standard'], - [UNKNOWN]: UNKNOWN, - rules: {}, - plugins: ['prettier'], +// eslint +const sortedEslintConfig = sortPackageJson({ + eslintConfig: { + overrides: [ + { + z: 'z', + a: 'a', + files: '*.js', + excludedFiles: '*.exclude.js', + rules: { + z: 'z', + a: 'a', + semi: 'error', + }, + }, + ], + extends: ['standard', 'plugin:prettier/recommended', 'prettier/standard'], + z: 'z', + a: 'a', + rules: { + z: 'z', + a: 'a', + semi: 'error', + }, + env: { + z: 'z', + a: 'a', + browser: true, + node: true, }, - expect: ['plugins', 'extends', 'rules', 'overrides', UNKNOWN], + globals: { + z: 'z', + a: 'a', + var1: 'writable', + var2: 'readonly', + }, + parserOptions: { + z: 'z', + a: 'a', + ecmaVersion: 6, + sourceType: 'module', + ecmaFeatures: { + jsx: true, + }, + }, + settings: { + z: 'z', + a: 'a', + 'import/extensions': '', + }, + plugins: ['a', 'z', 'prettier'], }, -]) +}).eslintConfig +assert.deepStrictEqual( + Object.keys(sortedEslintConfig), + [ + 'env', + 'parserOptions', + 'settings', + 'plugins', + 'extends', + 'rules', + 'overrides', + 'globals', + 'a', + 'z', + ], + 'eslintConfig field should sorted', +) +assert.deepStrictEqual( + Object.keys(sortedEslintConfig.parserOptions), + ['a', 'ecmaFeatures', 'ecmaVersion', 'sourceType', 'z'], + 'eslintConfig.parserOptions should sorted', +) +assert.deepStrictEqual( + Object.keys(sortedEslintConfig.rules), + ['a', 'semi', 'z'], + 'eslintConfig.rules should sorted', +) +assert.deepStrictEqual( + Object.keys(sortedEslintConfig.settings), + ['a', 'import/extensions', 'z'], + 'eslintConfig.settings should sorted', +) +assert.deepStrictEqual( + Object.keys(sortedEslintConfig.overrides[0]), + ['files', 'excludedFiles', 'rules', 'a', 'z'], + 'eslintConfig.overrides[0] should sorted', +) +assert.deepStrictEqual( + Object.keys(sortedEslintConfig.overrides[0].rules), + ['a', 'semi', 'z'], + 'eslintConfig.overrides[0].rules should sorted', +) // prettier const sortedPrettierConfig = sortPackageJson({