Skip to content

Commit ca76f9b

Browse files
committed
refactor: use parse instead of preprocess
1 parent 8e93dc6 commit ca76f9b

File tree

6 files changed

+350
-294
lines changed

6 files changed

+350
-294
lines changed

assets/banner.ai

Lines changed: 282 additions & 249 deletions
Large diffs are not rendered by default.

lib/index.js

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,33 @@ const { scripts } = require('./scripts');
1414
const { sort } = require('./sort');
1515

1616
const { 'json-stringify': parser } = parsers;
17+
const { parse } = parser;
1718
const rePkg = /package\.json$/;
1819

19-
const format = (input) => {
20-
let pkg = JSON.parse(input);
21-
pkg = sort(pkg);
22-
pkg = scripts(pkg);
20+
const format = (properties) => {
21+
let props = sort(properties);
22+
props = scripts(props);
2323

24-
const result = JSON.stringify(pkg, null, 2);
25-
return result;
24+
return props;
2625
};
2726

28-
exports.parsers = {
29-
'json-stringify': {
30-
...parser,
31-
preprocess(input, options) {
32-
const { filepath } = options;
33-
const text = parser.preprocess ? parser.preprocess(input, options) : input;
34-
35-
if (rePkg.test(filepath)) {
36-
const result = format(text);
37-
return result;
27+
module.exports = {
28+
name: 'prettier-plugin-package',
29+
parsers: {
30+
'json-stringify': {
31+
...parser,
32+
parse(...args) {
33+
const [, , options] = args;
34+
const { filepath } = options;
35+
const ast = parse(...args);
36+
37+
if (rePkg.test(filepath)) {
38+
const { properties } = ast;
39+
ast.properties = format(properties);
40+
}
41+
42+
return ast;
3843
}
39-
40-
return text;
4144
}
4245
}
4346
};

lib/scripts.js

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,19 @@
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 sortScripts = (pkg) => {
12-
const { scripts } = pkg;
11+
const sort = (props) => {
12+
const scriptsIndex = props.findIndex((prop) => prop.key.value === 'scripts');
1313

14-
if (!scripts) {
15-
return pkg;
14+
if (scriptsIndex) {
15+
const scripts = props[scriptsIndex];
16+
scripts.value.properties.sort((a, b) =>
17+
a.key.value > b.key.value ? 1 : a.key.value < b.key.value ? -1 : 0
18+
);
19+
// eslint-disable-next-line no-param-reassign
20+
props[scriptsIndex] = scripts;
1621
}
1722

18-
const result = Object.assign({}, pkg, { scripts: {} });
19-
const keys = Object.keys(scripts).sort();
20-
21-
for (const key of keys) {
22-
if (scripts[key]) {
23-
result.scripts[key] = scripts[key];
24-
}
25-
}
26-
27-
return result;
23+
return props;
2824
};
2925

30-
module.exports = { scripts: sortScripts };
26+
module.exports = { scripts: sort };

lib/sort.js

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,25 @@ const primary = [
4545
'resolutions'
4646
];
4747

48-
const sort = (pkg) => {
49-
const result = {};
50-
const unknown = Object.keys(pkg)
51-
.filter((key) => !primary.includes(key))
52-
.sort();
53-
54-
for (const key of primary.concat(unknown)) {
55-
if (pkg[key]) {
56-
result[key] = pkg[key];
48+
const sort = (props) => {
49+
const unknown = [];
50+
const known = props.filter((prop) => {
51+
if (primary.includes(prop.key.value)) {
52+
return true;
5753
}
58-
}
54+
unknown.push(prop);
55+
return false;
56+
});
5957

60-
return result;
58+
known.sort((a, b) => {
59+
const aIndex = primary.indexOf(a.key.value);
60+
const bIndex = primary.indexOf(b.key.value);
61+
62+
return aIndex > bIndex ? 1 : aIndex < bIndex ? -1 : 0;
63+
});
64+
unknown.sort((a, b) => (a.key.value > b.key.value ? 1 : a.key.value < b.key.value ? -1 : 0));
65+
66+
return known.concat(unknown);
6167
};
6268

6369
module.exports = { sort };

test/fixtures/broken.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "prettier-plugin-package",
3+
"batmam": {]
4+
}

test/test.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
const { readFileSync } = require('fs');
12
const { join } = require('path');
23

34
const test = require('ava');
45
const prettier = require('prettier');
56

67
const pkg = require('./fixtures/fixture.json');
7-
8+
//
89
const shuffle = (arr) => {
910
const result = arr.slice();
1011
for (let i = result.length - 1; i > 0; i--) {
@@ -70,3 +71,16 @@ test('not package.json', (t) => {
7071

7172
t.is(input.trim(), output.trim());
7273
});
74+
75+
test('broken json', (t) => {
76+
const options = {
77+
filepath: 'broken.json',
78+
parser: 'json-stringify',
79+
plugins: ['.']
80+
};
81+
82+
const broken = readFileSync(join(__dirname, './fixtures/broken.json'), 'utf-8');
83+
const fail = () => prettier.format(broken, options);
84+
85+
t.throws(fail);
86+
});

0 commit comments

Comments
 (0)