Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: sort prettier field #121

Merged
merged 7 commits into from
Jan 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const gitHooks = require('git-hooks-list')

const hasOwnProperty = (object, property) =>
Object.prototype.hasOwnProperty.call(object, property)
const pipe = fns => x => fns.reduce((result, fn) => fn(result), x)
const onArray = fn => x => (Array.isArray(x) ? fn(x) : x)
const uniq = onArray(xs => xs.filter((x, i) => i === xs.indexOf(x)))
const sortArray = onArray(array => [...array].sort())
Expand Down Expand Up @@ -45,6 +46,26 @@ const sortESLintConfig = sortObjectBy([
'reportUnusedDisableDirectives',
])

const sortPrettierConfigKeys = onObject(config =>
sortObjectKeys(config, [
...Object.keys(config)
.filter(key => key !== 'overrides')
.sort(),
'overrides',
]),
)
const sortPrettierConfigOptions = pipe([
sortObject,
overProperty('options', sortObject),
])
const sortPrettierConfigOverrides = onArray(overrides =>
overrides.map(sortPrettierConfigOptions),
)
const sortPrettierConfig = pipe([
sortPrettierConfigKeys,
onObject(overProperty('overrides', sortPrettierConfigOverrides)),
])

// See https://docs.npmjs.com/misc/scripts
const defaultNpmScripts = new Set([
'install',
Expand Down Expand Up @@ -149,7 +170,7 @@ const fields = [
{ key: 'babel', over: sortObject },
{ key: 'browserslist' },
{ key: 'xo', over: sortObject },
{ key: 'prettier', over: sortObject },
{ key: 'prettier', over: sortPrettierConfig },
{ key: 'eslintConfig', over: sortESLintConfig },
{ key: 'eslintIgnore' },
{ key: 'stylelint' },
Expand Down
39 changes: 38 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,6 @@ for (const field of [
'browserify',
'babel',
'xo',
'prettier',
'ava',
'jest',
'mocha',
Expand Down Expand Up @@ -295,6 +294,44 @@ testField('eslintConfig', [
},
])

// prettier
const sortedPrettierConfig = sortPackageJson({
prettier: {
trailingComma: 'none',
z: 'z',
a: 'a',
overrides: [
{
options: {
trailingComma: 'all',
semi: false,
z: 'z',
a: 'a',
},
z: 'z',
a: 'a',
files: '*.js',
},
],
semi: false,
},
}).prettier
assert.deepStrictEqual(
Object.keys(sortedPrettierConfig),
['a', 'semi', 'trailingComma', 'z', 'overrides'],
'prettier field should sorted',
)
assert.deepStrictEqual(
Object.keys(sortedPrettierConfig.overrides[0]),
['a', 'files', 'options', 'z'],
'prettier.overrides should sorted',
)
assert.deepStrictEqual(
Object.keys(sortedPrettierConfig.overrides[0].options),
['a', 'semi', 'trailingComma', 'z'],
'prettier.overrides[].options should sorted',
)

testField('binary', [
{
value: {
Expand Down