Skip to content

Commit 7d70c62

Browse files
kucebpapb
andauthored
feat: merge sort-package-json settings (#2)
* major: merge sort-package-json settings * fix typo BREAKING CHANGE: merge package-json-sort-order into sort-package-json setting Co-Authored-By: Pedro Augusto de Paula Barbosa <papb1996@gmail.com>
1 parent badb09b commit 7d70c62

File tree

7 files changed

+55
-27
lines changed

7 files changed

+55
-27
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,22 +63,22 @@ eslint --ext .js,.json,.eslintrc,.babelrc --fix .
6363
"json/sort-package-json": true,
6464
"json/ignore-files": ["**/package-lock.json"],
6565
"json/json-with-comments-files": ["**/tsconfig.json", ".vscode/**"],
66-
"json/package-json-sort-order": "pro"
66+
"json/package-json-sort-order": "standard"
6767
}
6868
```
6969
> Note: glob patterns use [`minimatch`](https://github.com/isaacs/minimatch/) against pathnames relative to the project root (cwd)
7070
7171
### `package-json-sort-order`
7272
You can configure the exact sort order of your `package.json` files (or turn it off entirely with the `sort-package-json` setting)
7373

74-
By default the sort order is `"pro"` (will change to "standard" in next major version)
74+
> By default the sort order is `"standard"`
7575
7676
#### Available sorting options
7777

78-
**"pro"**: places scripts and depenedencies at the top, reducing need to scroll down to view them. Pros only.
79-
8078
**"standard"**: default from [`sort-package-json`](https://github.com/keithamus/sort-package-json). This is a sane, standard order.
8179

80+
**"pro"**: places scripts and dependencies at the top, reducing need to scroll down to view them. Pros only.
81+
8282
**["your", "custom", "order", "here"]**: provide an array to manually set the sort order.
8383

8484
### Settings examples

docs/logo.gvdesign

4.69 KB
Binary file not shown.

docs/logo.svg

Lines changed: 1 addition & 0 deletions
Loading

lib/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ initializeEslintPlugin({
113113

114114
if (mode === 'package-json') {
115115
debug('sorting JSON')
116-
const sorted = formatJSON(sortPkgJSON(parsed, pluginSettings['package-json-sort-order']))
116+
const sorted = formatJSON(sortPkgJSON(parsed, pluginSettings['sort-package-json']))
117117

118118
if (sorted !== formatted) {
119119

@@ -186,7 +186,7 @@ function getMode (pluginSettings, filenameOrOptions) {
186186
}
187187
}
188188

189-
if (pluginSettings[SETTINGS['sort-package-json']]) {
189+
if (pluginSettings[SETTINGS['sort-package-json']] !== false) {
190190
if (['package.json', '__package.json'].includes(basename)) {
191191
return 'package-json'
192192
}

lib/settings.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,8 @@ const schema = {
1616
],
1717
},
1818
'sort-package-json': {
19-
type: 'boolean',
20-
default: true,
21-
},
22-
'ignore-files': {
23-
type: 'array',
24-
default: [
25-
'**/package-lock.json',
26-
],
27-
},
28-
'package-json-sort-order': {
29-
type: ['array'],
30-
default: 'pro',
19+
type: ['array', 'boolean'],
20+
default: 'standard',
3121
presets: {
3222
'standard': [],
3323
'pro': [
@@ -56,6 +46,17 @@ const schema = {
5646
],
5747
},
5848
},
49+
'ignore-files': {
50+
type: 'array',
51+
default: [
52+
'**/package-lock.json',
53+
],
54+
},
55+
'package-json-sort-order': {
56+
type: 'boolean',
57+
deprecated: true,
58+
renamed: 'sort-package-json',
59+
},
5960
}
6061

6162
function getSetting (settings, name) {

test/__snapshots__/main.spec.js.snap

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,14 @@ exports[`main spec configuration ignores json-with-comments-files (for now) 1`]
66
To remove this warning add \\".vscode/settings.json\\" to your \`.eslintignore\` file"
77
`;
88

9-
exports[`main spec errors bad settings json-with-comments-filenames 1`] = `
9+
exports[`main spec errors bad settings @deprecate json-with-comments-filenames 1`] = `
1010
"Eslint Settings Error: [json]:
1111
Using deprecated settings key: \\"json/json-with-comments-filenames\\"
1212
Please rename this settings key to: \\"json/json-with-comments-files\\""
1313
`;
14+
15+
exports[`main spec errors bad settings @deprecate package-json-sort-order 1`] = `
16+
"Eslint Settings Error: [json]:
17+
Using deprecated settings key: \\"json/package-json-sort-order\\"
18+
Please rename this settings key to: \\"json/sort-package-json\\""
19+
`;

test/main.spec.js

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const getFormatted = async (filename) => {
1414
}
1515

1616
const getFormattedAndSorted = async (filename) => {
17-
return formatJSON(sortPkgJSON(parseJSON(await getFormatted(filename)), settings.getDefault('package-json-sort-order')))
17+
return formatJSON(sortPkgJSON(parseJSON(await getFormatted(filename)), settings.getDefault('sort-package-json')))
1818
}
1919

2020
function execute (file, baseConfig) {
@@ -208,15 +208,20 @@ describe('main spec', () => {
208208

209209
// starts out of order
210210
expectOrder(_.keys(JSON.parse(await getFormatted(filename))), [
211-
'license',
212-
'dependencies',
211+
'keywords',
212+
'version',
213213
])
214214

215215
expect(result.output).toBe(await getFormattedAndSorted(filename))
216216
const resultOrder = _.keys(JSON.parse(result.output))
217217

218218
// ends up in order
219-
expectOrder(resultOrder, ['dependencies', 'license'])
219+
expectOrder(resultOrder, [
220+
'version',
221+
'keywords',
222+
'license',
223+
'dependencies',
224+
])
220225
})
221226

222227
describe('configuration', () => {
@@ -298,7 +303,7 @@ describe('main spec', () => {
298303
const filename = './fixtures/sort-order/__package.json'
299304
const { result } = execute(filename, {
300305
fix: true,
301-
settings: { 'json/package-json-sort-order': ['foo', 'bar'] },
306+
settings: { 'json/sort-package-json': ['foo', 'bar'] },
302307
})
303308

304309
expectOrder(_.keys(parseJSON(result.output)), ['foo', 'bar'])
@@ -309,7 +314,7 @@ describe('main spec', () => {
309314
const { result } = execute(filename, {
310315
fix: true,
311316
// standard order is default from sort-package-json package
312-
settings: { 'json/package-json-sort-order': 'standard' },
317+
settings: { 'json/sort-package-json': 'pro' },
313318
})
314319

315320
// starts out of order
@@ -322,7 +327,11 @@ describe('main spec', () => {
322327
const resultOrder = _.keys(JSON.parse(result.output))
323328

324329
// ends up in order
325-
expectOrder(resultOrder, ['license', 'dependencies'])
330+
expectOrder(resultOrder, [
331+
'dependencies',
332+
'license',
333+
])
334+
326335
})
327336

328337
})
@@ -424,7 +433,7 @@ describe('main spec', () => {
424433
})
425434

426435
describe('bad settings', () => {
427-
it('json-with-comments-filenames', async () => {
436+
it('@deprecate json-with-comments-filenames', async () => {
428437
const filename = './fixtures/demo.json'
429438
const { result } = execute(filename, {
430439
fix: true,
@@ -434,6 +443,17 @@ describe('main spec', () => {
434443
expect(result.warningCount).toBe(1)
435444
expect(result.messages[0].message).toMatchSnapshot()
436445
})
446+
447+
it('@deprecate package-json-sort-order', async () => {
448+
const filename = './fixtures/sort-order/__package.json'
449+
const { result } = execute(filename, {
450+
fix: true,
451+
settings: { 'json/package-json-sort-order': ['foo', 'bar'] },
452+
})
453+
454+
expect(result.warningCount).toBe(1)
455+
expect(result.messages[0].message).toMatchSnapshot()
456+
})
437457
})
438458
})
439459

0 commit comments

Comments
 (0)