Skip to content

Commit 4a3fbc8

Browse files
feat(order-properties): more precise error reporting (#328)
<!-- πŸ‘‹ Hi, thanks for sending a PR to eslint-plugin-package-json! πŸ’–. Please fill out all fields below and make sure each item is true and [x] checked. Otherwise we may not be able to review your PR. --> ## PR Checklist - [x] Addresses an existing open issue: fixes #320 - [x] That issue was marked as [`status: accepting prs`](https://github.com/JoshuaKGoldberg/eslint-plugin-package-json/issues?q=is%3Aopen+is%3Aissue+label%3A%22status%3A+accepting+prs%22) - [x] Steps in [CONTRIBUTING.md](https://github.com/JoshuaKGoldberg/eslint-plugin-package-json/blob/main/.github/CONTRIBUTING.md) were taken ## Overview <!-- Description of what is changed and how the code change does that. --> --------- Co-authored-by: Josh Goldberg ✨ <[email protected]>
1 parent 17f9c03 commit 4a3fbc8

File tree

2 files changed

+142
-37
lines changed

2 files changed

+142
-37
lines changed

β€Žsrc/rules/order-properties.ts

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -70,38 +70,39 @@ export const rule = createRule<Options>({
7070
const { properties } = ast.body[0].expression;
7171

7272
for (let i = 0; i < properties.length; i += 1) {
73-
if (
74-
(properties[i].key as JsonAST.JSONStringLiteral)
75-
.value !== orderedKeys[i]
76-
) {
77-
context.report({
78-
fix(fixer) {
79-
const { indent, type } = detectIndent(text);
80-
const endCharacters = text.endsWith("\n")
81-
? "\n"
82-
: "";
83-
const newline = detectNewline.graceful(text);
84-
let result =
85-
JSON.stringify(
86-
orderedSource,
87-
null,
88-
type === "tab" ? "\t" : indent,
89-
) + endCharacters;
90-
if (newline === "\r\n") {
91-
result = result.replace(/\n/g, newline);
92-
}
73+
const property = properties[i]
74+
.key as JsonAST.JSONStringLiteral;
75+
const { value } = property;
9376

94-
return fixer.replaceText(
95-
context.sourceCode.ast,
96-
result,
97-
);
98-
},
99-
message:
100-
"Package top-level properties are not ordered in the npm standard way. Run the ESLint auto-fixer to correct.",
101-
node: context.sourceCode.ast,
102-
});
103-
break;
77+
if (value === orderedKeys[i]) {
78+
continue;
10479
}
80+
81+
context.report({
82+
fix(fixer) {
83+
const { indent, type } = detectIndent(text);
84+
const endCharacters = text.endsWith("\n")
85+
? "\n"
86+
: "";
87+
const newline = detectNewline.graceful(text);
88+
let result =
89+
JSON.stringify(
90+
orderedSource,
91+
null,
92+
type === "tab" ? "\t" : indent,
93+
) + endCharacters;
94+
if (newline === "\r\n") {
95+
result = result.replace(/\n/g, newline);
96+
}
97+
98+
return fixer.replaceText(
99+
context.sourceCode.ast,
100+
result,
101+
);
102+
},
103+
loc: properties[i].loc,
104+
message: `Package top-level property "${value}" is not ordered in the npm standard way. Run the ESLint auto-fixer to correct.`,
105+
});
105106
}
106107
},
107108
};

β€Žsrc/tests/rules/order-properties.test.ts

Lines changed: 111 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,23 @@ ruleTester.run("order-properties", rule, {
1818
errors: [
1919
{
2020
message:
21-
"Package top-level properties are not ordered in the npm standard way. Run the ESLint auto-fixer to correct.",
21+
'Package top-level property "main" is not ordered in the npm standard way. Run the ESLint auto-fixer to correct.',
22+
},
23+
{
24+
message:
25+
'Package top-level property "homepage" is not ordered in the npm standard way. Run the ESLint auto-fixer to correct.',
26+
},
27+
{
28+
message:
29+
'Package top-level property "version" is not ordered in the npm standard way. Run the ESLint auto-fixer to correct.',
30+
},
31+
{
32+
message:
33+
'Package top-level property "name" is not ordered in the npm standard way. Run the ESLint auto-fixer to correct.',
34+
},
35+
{
36+
message:
37+
'Package top-level property "repository" is not ordered in the npm standard way. Run the ESLint auto-fixer to correct.',
2238
},
2339
],
2440
filename: "package.json",
@@ -49,7 +65,15 @@ ruleTester.run("order-properties", rule, {
4965
errors: [
5066
{
5167
message:
52-
"Package top-level properties are not ordered in the npm standard way. Run the ESLint auto-fixer to correct.",
68+
'Package top-level property "main" is not ordered in the npm standard way. Run the ESLint auto-fixer to correct.',
69+
},
70+
{
71+
message:
72+
'Package top-level property "version" is not ordered in the npm standard way. Run the ESLint auto-fixer to correct.',
73+
},
74+
{
75+
message:
76+
'Package top-level property "repository" is not ordered in the npm standard way. Run the ESLint auto-fixer to correct.',
5377
},
5478
],
5579
filename: "package.json",
@@ -80,7 +104,23 @@ ruleTester.run("order-properties", rule, {
80104
errors: [
81105
{
82106
message:
83-
"Package top-level properties are not ordered in the npm standard way. Run the ESLint auto-fixer to correct.",
107+
'Package top-level property "main" is not ordered in the npm standard way. Run the ESLint auto-fixer to correct.',
108+
},
109+
{
110+
message:
111+
'Package top-level property "homepage" is not ordered in the npm standard way. Run the ESLint auto-fixer to correct.',
112+
},
113+
{
114+
message:
115+
'Package top-level property "version" is not ordered in the npm standard way. Run the ESLint auto-fixer to correct.',
116+
},
117+
{
118+
message:
119+
'Package top-level property "name" is not ordered in the npm standard way. Run the ESLint auto-fixer to correct.',
120+
},
121+
{
122+
message:
123+
'Package top-level property "repository" is not ordered in the npm standard way. Run the ESLint auto-fixer to correct.',
84124
},
85125
],
86126
filename: "package.json",
@@ -111,7 +151,23 @@ ruleTester.run("order-properties", rule, {
111151
errors: [
112152
{
113153
message:
114-
"Package top-level properties are not ordered in the npm standard way. Run the ESLint auto-fixer to correct.",
154+
'Package top-level property "main" is not ordered in the npm standard way. Run the ESLint auto-fixer to correct.',
155+
},
156+
{
157+
message:
158+
'Package top-level property "homepage" is not ordered in the npm standard way. Run the ESLint auto-fixer to correct.',
159+
},
160+
{
161+
message:
162+
'Package top-level property "version" is not ordered in the npm standard way. Run the ESLint auto-fixer to correct.',
163+
},
164+
{
165+
message:
166+
'Package top-level property "name" is not ordered in the npm standard way. Run the ESLint auto-fixer to correct.',
167+
},
168+
{
169+
message:
170+
'Package top-level property "repository" is not ordered in the npm standard way. Run the ESLint auto-fixer to correct.',
115171
},
116172
],
117173
filename: "package.json",
@@ -142,7 +198,23 @@ ruleTester.run("order-properties", rule, {
142198
errors: [
143199
{
144200
message:
145-
"Package top-level properties are not ordered in the npm standard way. Run the ESLint auto-fixer to correct.",
201+
'Package top-level property "main" is not ordered in the npm standard way. Run the ESLint auto-fixer to correct.',
202+
},
203+
{
204+
message:
205+
'Package top-level property "homepage" is not ordered in the npm standard way. Run the ESLint auto-fixer to correct.',
206+
},
207+
{
208+
message:
209+
'Package top-level property "version" is not ordered in the npm standard way. Run the ESLint auto-fixer to correct.',
210+
},
211+
{
212+
message:
213+
'Package top-level property "name" is not ordered in the npm standard way. Run the ESLint auto-fixer to correct.',
214+
},
215+
{
216+
message:
217+
'Package top-level property "repository" is not ordered in the npm standard way. Run the ESLint auto-fixer to correct.',
146218
},
147219
],
148220
filename: "package.json",
@@ -174,7 +246,23 @@ ruleTester.run("order-properties", rule, {
174246
errors: [
175247
{
176248
message:
177-
"Package top-level properties are not ordered in the npm standard way. Run the ESLint auto-fixer to correct.",
249+
'Package top-level property "main" is not ordered in the npm standard way. Run the ESLint auto-fixer to correct.',
250+
},
251+
{
252+
message:
253+
'Package top-level property "homepage" is not ordered in the npm standard way. Run the ESLint auto-fixer to correct.',
254+
},
255+
{
256+
message:
257+
'Package top-level property "version" is not ordered in the npm standard way. Run the ESLint auto-fixer to correct.',
258+
},
259+
{
260+
message:
261+
'Package top-level property "name" is not ordered in the npm standard way. Run the ESLint auto-fixer to correct.',
262+
},
263+
{
264+
message:
265+
'Package top-level property "repository" is not ordered in the npm standard way. Run the ESLint auto-fixer to correct.',
178266
},
179267
],
180268
filename: "package.json",
@@ -206,7 +294,23 @@ ruleTester.run("order-properties", rule, {
206294
errors: [
207295
{
208296
message:
209-
"Package top-level properties are not ordered in the npm standard way. Run the ESLint auto-fixer to correct.",
297+
'Package top-level property "main" is not ordered in the npm standard way. Run the ESLint auto-fixer to correct.',
298+
},
299+
{
300+
message:
301+
'Package top-level property "homepage" is not ordered in the npm standard way. Run the ESLint auto-fixer to correct.',
302+
},
303+
{
304+
message:
305+
'Package top-level property "version" is not ordered in the npm standard way. Run the ESLint auto-fixer to correct.',
306+
},
307+
{
308+
message:
309+
'Package top-level property "name" is not ordered in the npm standard way. Run the ESLint auto-fixer to correct.',
310+
},
311+
{
312+
message:
313+
'Package top-level property "repository" is not ordered in the npm standard way. Run the ESLint auto-fixer to correct.',
210314
},
211315
],
212316
filename: "package.json",

0 commit comments

Comments
Β (0)