Skip to content

Commit e9d4ee6

Browse files
committed
test: add em
1 parent db08e57 commit e9d4ee6

File tree

7 files changed

+128
-7
lines changed

7 files changed

+128
-7
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ Top-level keys are sorted according to a style commonly seen in the packages of
5353
'author',
5454
'homepage',
5555
'bugs',
56+
'main',
5657
'engines',
5758
'scripts',
5859
'files',

lib/index.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@
88
The above copyright notice and this permission notice shall be
99
included in all copies or substantial portions of this Source Code Form.
1010
*/
11-
const { 'json-stringify': parser } = require('prettier/parser-babylon');
11+
const { parsers } = require('prettier/parser-babylon');
1212

1313
const { sort } = require('./sort');
1414

15+
const { 'json-stringify': parser } = parsers;
1516
const rePkg = /package\.json$/;
1617

1718
const format = (input) => {
18-
const result = sort(input);
19+
const result = JSON.stringify(sort(input), null, 2);
1920
return result;
2021
};
2122

@@ -24,10 +25,11 @@ exports.parsers = {
2425
...parser,
2526
preprocess(input, options) {
2627
const { filepath } = options;
27-
const text = parser.preprocess(input, options);
28+
const text = parser.preprocess ? parser.preprocess(input, options) : input;
2829

2930
if (rePkg.test(filepath)) {
30-
return format(text);
31+
const result = format(text);
32+
return result;
3133
}
3234

3335
return text;

lib/sort.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@ const primary = [
1717
'author',
1818
'homepage',
1919
'bugs',
20+
'main',
2021
'engines',
2122
'scripts',
2223
'files',
24+
'keywords',
2325
'peerDependencies',
2426
'dependencies',
25-
'devDependencies',
26-
'keywords'
27+
'devDependencies'
2728
];
2829

2930
const sort = (input) => {
@@ -38,6 +39,8 @@ const sort = (input) => {
3839
result[key] = pkg[key];
3940
}
4041
}
42+
43+
return result;
4144
};
4245

4346
module.exports = { sort };

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"author": "shellscape",
88
"homepage": "https://github.com/shellscape/prettier-plugin-package",
99
"bugs": "https://github.com/shellscape/prettier-plugin-package/issues",
10+
"main": "lib/index.js",
1011
"engines": {
1112
"node": ">= 8.0.0"
1213
},

test/snapshots/test.js.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# Snapshot report for `test/test.js`
2+
3+
The actual snapshot is saved in `test.js.snap`.
4+
5+
Generated by [AVA](https://ava.li).
6+
7+
## randomize
8+
9+
> Snapshot 1
10+
11+
`{␊
12+
"name": "prettier-plugin-package",␊
13+
"version": "0.1.0",␊
14+
"description": "An opinionated package.json formatter plugin for Prettier",␊
15+
"license": "MPL-2.0",␊
16+
"repository": "shellscape/prettier-plugin-package",␊
17+
"author": "shellscape",␊
18+
"homepage": "https://github.com/shellscape/prettier-plugin-package",␊
19+
"bugs": "https://github.com/shellscape/prettier-plugin-package/issues",␊
20+
"main": "lib/index.js",␊
21+
"engines": {␊
22+
"node": ">= 8.0.0"␊
23+
},␊
24+
"scripts": {␊
25+
"ci:coverage": "nyc npm run ci:test && nyc report --reporter=text-lcov > coverage.lcov",␊
26+
"ci:lint": "npm run lint && npm run security",␊
27+
"ci:lint:commits": "commitlint --from=${CIRCLE_BRANCH} --to=${CIRCLE_SHA1}",␊
28+
"ci:test": "npm run test -- --verbose",␊
29+
"commitlint": "commitlint",␊
30+
"commitmsg": "commitlint -e $GIT_PARAMS",␊
31+
"lint": "eslint --fix --cache lib test",␊
32+
"lint-staged": "lint-staged",␊
33+
"security": "npm audit",␊
34+
"test": "ava"␊
35+
},␊
36+
"files": [␊
37+
"lib/",␊
38+
"README.md",␊
39+
"LICENSE"␊
40+
],␊
41+
"keywords": [␊
42+
"package",␊
43+
"package.json",␊
44+
"plugin",␊
45+
"prettier"␊
46+
],␊
47+
"peerDependencies": {␊
48+
"prettier": "^1.18.2"␊
49+
},␊
50+
"dependencies": {},␊
51+
"devDependencies": {␊
52+
"@commitlint/cli": "^8.1.0",␊
53+
"@commitlint/config-conventional": "^8.1.0",␊
54+
"ava": "^2.2.0",␊
55+
"eslint-config-shellscape": "^2.0.2",␊
56+
"execa": "^2.0.3",␊
57+
"lint-staged": "^9.2.0",␊
58+
"nyc": "^14.1.0",␊
59+
"pre-commit": "^1.2.2",␊
60+
"prettier": "^1.18.2"␊
61+
},␊
62+
"ava": {␊
63+
"files": [␊
64+
"!**/fixtures/**"␊
65+
]␊
66+
},␊
67+
"lint-staged": {␊
68+
"*.js": [␊
69+
"eslint --fix",␊
70+
"git add"␊
71+
]␊
72+
},␊
73+
"nyc": {␊
74+
"include": [␊
75+
"lib/*.js"␊
76+
],␊
77+
"exclude": [␊
78+
"test/"␊
79+
]␊
80+
},␊
81+
"pre-commit": "lint-staged"␊
82+
}␊
83+
`

test/snapshots/test.js.snap

794 Bytes
Binary file not shown.

test/test.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,34 @@
1+
const { join } = require('path');
2+
13
const test = require('ava');
4+
const prettier = require('prettier');
5+
6+
const pkg = require('../package.json');
7+
8+
const shuffle = (arr) => {
9+
const result = arr.slice();
10+
for (let i = result.length - 1; i > 0; i--) {
11+
const rand = Math.floor(Math.random() * (i + 1));
12+
[result[i], result[rand]] = [result[rand], result[i]];
13+
}
14+
return result;
15+
};
16+
17+
test('randomize', (t) => {
18+
const options = {
19+
filepath: join(__dirname, 'package.json'),
20+
parser: 'json-stringify',
21+
plugins: ['.']
22+
};
23+
const keys = shuffle(Object.keys(pkg));
24+
const fixture = {};
25+
26+
for (const key of keys) {
27+
fixture[key] = pkg[key];
28+
}
29+
30+
const input = JSON.stringify(fixture, null, 2);
31+
const output = prettier.format(input, options);
232

3-
test('default', (t) => t.pass());
33+
t.snapshot(output);
34+
});

0 commit comments

Comments
 (0)