Skip to content

Commit

Permalink
feat: sort eslintConfig deeper (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker authored and keithamus committed Jan 8, 2020
1 parent d02d8ef commit 396ed8a
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 13 deletions.
28 changes: 25 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,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',
Expand All @@ -43,7 +51,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 sortPrettierConfig = onObject(
Expand Down Expand Up @@ -192,7 +214,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 },
Expand Down
102 changes: 92 additions & 10 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down

0 comments on commit 396ed8a

Please sign in to comment.