Skip to content

Commit f9dcf06

Browse files
committed
feat!: move to ESM. Prettier v3
1 parent 2304351 commit f9dcf06

27 files changed

+442
-463
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ dist
1010
test/fixtures/**/dist
1111

1212
.eslintcache
13+
.claude

.prettierrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
{
2-
"plugins": ["./lib/index.js"]
2+
"plugins": ["./lib/index.js"],
3+
"singleQuote": true
34
}

lib/index.js

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,16 @@
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-
import babelParsers from "prettier/plugins/babel";
12-
import estreeParsers from "prettier/plugins/estree";
13-
import prettier from "prettier";
11+
import babelParsers from 'prettier/plugins/babel';
1412

15-
import { dependencies } from "./rules/dependencies.js";
16-
import { engines } from "./rules/engines.js";
17-
import { files } from "./rules/files.js";
18-
import { scripts } from "./rules/scripts.js";
19-
import { sort } from "./rules/sort.js";
13+
import { dependencies } from './rules/dependencies.js';
14+
import { engines } from './rules/engines.js';
15+
import { files } from './rules/files.js';
16+
import { scripts } from './rules/scripts.js';
17+
import { sort } from './rules/sort.js';
2018

2119
const { parsers } = babelParsers;
22-
const { "json-stringify": parser } = parsers;
20+
const { 'json-stringify': parser } = parsers;
2321
const { parse } = parser;
2422
const rePkg = /package\.json$/;
2523

@@ -34,28 +32,26 @@ const format = (properties) => {
3432
};
3533

3634
const plugin = {
37-
name: "prettier-plugin-package",
35+
name: 'prettier-plugin-package',
3836
parsers: {
39-
"json-stringify": {
37+
'json-stringify': {
4038
...parser,
4139
async parse(text, options) {
4240
const { filepath } = options;
4341
const ast = parse(text, options);
4442

4543
if (rePkg.test(filepath)) {
4644
const { properties, node } = ast;
47-
ast.properties = format(properties || node.properties);
48-
}
45+
const affectNode = !properties && !!node?.properties;
46+
const formatResult = format(properties || node.properties);
4947

50-
const formattedText = await prettier.format(text, {
51-
parser: "json-stringify",
52-
plugins: [babelParsers, estreeParsers],
53-
});
54-
console.log("Formatted text:", formattedText);
48+
if (affectNode) ast.node.properties = formatResult;
49+
else ast.properties = formatResult;
50+
}
5551

5652
return ast;
5753
},
58-
astFormat: "estree-json",
54+
astFormat: 'estree-json',
5955
},
6056
},
6157
};

lib/rules/dependencies.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const dependencyNames = [
1616
'peerDependencies',
1717
'dependencies',
1818
'devDependencies',
19-
'resolutions'
19+
'resolutions',
2020
];
2121

2222
const process = (props) =>
@@ -25,16 +25,18 @@ const process = (props) =>
2525
const { elements, properties } = prop.value;
2626

2727
if (elements) {
28-
elements.sort((a, b) => (a.value > b.value ? 1 : a.value < b.value ? -1 : 0));
28+
elements.sort((a, b) =>
29+
a.value > b.value ? 1 : a.value < b.value ? -1 : 0,
30+
);
2931
}
3032

3133
if (properties) {
3234
properties.sort((a, b) =>
33-
a.key.value > b.key.value ? 1 : a.key.value < b.key.value ? -1 : 0
35+
a.key.value > b.key.value ? 1 : a.key.value < b.key.value ? -1 : 0,
3436
);
3537
}
3638
}
3739
return prop;
3840
});
3941

40-
export { process as dependencies };
42+
export { process as dependencies };

lib/rules/engines.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ const process = (props) => {
1515
const [engines] = props.splice(enginesIndex, 1);
1616
const { properties } = engines.value;
1717

18-
properties.sort((a, b) => (a.key.value > b.key.value ? 1 : a.key.value < b.key.value ? -1 : 0));
18+
properties.sort((a, b) =>
19+
a.key.value > b.key.value ? 1 : a.key.value < b.key.value ? -1 : 0,
20+
);
1921

2022
engines.value.properties = properties;
2123

@@ -25,4 +27,4 @@ const process = (props) => {
2527
return props;
2628
};
2729

28-
export { process as engines};
30+
export { process as engines };

lib/rules/files.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,10 @@ const process = (props) => {
4242
return aValue.localeCompare(bValue);
4343
});
4444

45-
if (readme) {
46-
elements.push(readme);
47-
}
48-
49-
if (license) {
50-
elements.push(license);
51-
}
45+
if (license) elements.push(license);
46+
if (readme) elements.push(readme);
5247

5348
filesNode.value.elements = elements;
54-
5549
props.splice(filesIndex, 0, filesNode);
5650
}
5751

lib/rules/props.js

Lines changed: 0 additions & 74 deletions
This file was deleted.

lib/rules/scripts.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const process = (props) => {
1414
if (scriptsIndex >= 0) {
1515
const scripts = props[scriptsIndex];
1616
scripts.value.properties.sort((a, b) =>
17-
a.key.value > b.key.value ? 1 : a.key.value < b.key.value ? -1 : 0
17+
a.key.value > b.key.value ? 1 : a.key.value < b.key.value ? -1 : 0,
1818
);
1919
// eslint-disable-next-line no-param-reassign
2020
props[scriptsIndex] = scripts;

lib/rules/sort.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ const primary = [
5050

5151
// types
5252
'types',
53-
'typings'
53+
'typings',
5454
];
5555

5656
const sort = (props) => {
@@ -66,10 +66,13 @@ const sort = (props) => {
6666
known.sort((a, b) => {
6767
const aIndex = primary.indexOf(a.key.value);
6868
const bIndex = primary.indexOf(b.key.value);
69+
const result = aIndex > bIndex ? 1 : aIndex < bIndex ? -1 : 0;
6970

70-
return aIndex > bIndex ? 1 : aIndex < bIndex ? -1 : 0;
71+
return result;
7172
});
72-
unknown.sort((a, b) => (a.key.value > b.key.value ? 1 : a.key.value < b.key.value ? -1 : 0));
73+
unknown.sort((a, b) =>
74+
a.key.value > b.key.value ? 1 : a.key.value < b.key.value ? -1 : 0,
75+
);
7376

7477
return known.concat(unknown);
7578
};

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
},
2626
"files": [
2727
"lib/",
28-
"README.md",
29-
"LICENSE"
28+
"LICENSE",
29+
"README.md"
3030
],
3131
"keywords": [
3232
"package",

0 commit comments

Comments
 (0)