Skip to content

Commit

Permalink
feat: sort alphabetically with co-located hooks for scripts (#632)
Browse files Browse the repository at this point in the history
<!-- πŸ‘‹ 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 #499
- [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

Fixes #499 and adds tests πŸ˜„ 

I'm not 100% sold on how I did the implementation, and I'm curious to
know your thoughts on it.
  • Loading branch information
sasial-dev authored Nov 30, 2024
1 parent 100ce08 commit 4ccae4f
Show file tree
Hide file tree
Showing 3 changed files with 173 additions and 8 deletions.
2 changes: 2 additions & 0 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
"npmpackagejsonlintrc",
"outro",
"packagejson",
"postbuild",
"postwatch",
"quickstart",
"ruleset",
"tsup",
Expand Down
58 changes: 50 additions & 8 deletions src/rules/sort-collections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,56 @@ export const rule = createRule<Options>({
toSort.includes(key.value)
) {
const currentOrder = collection.properties;
const desiredOrder = currentOrder
.slice()
.sort((a, b) =>
(a.key as AST.JSONStringLiteral).value >
(b.key as AST.JSONStringLiteral).value
? 1
: -1,
);
const scripts = new Set(
currentOrder.map(
(prop) => (prop.key as AST.JSONStringLiteral).value,
),
);

const desiredOrder = currentOrder.slice().sort((a, b) => {
let aKey = (a.key as AST.JSONStringLiteral).value;
let bKey = (b.key as AST.JSONStringLiteral).value;

if (key.value !== "scripts") {
return aKey > bKey ? 1 : -1;
} else {
let modifier = 0;

if (
aKey.startsWith("pre") &&
scripts.has(aKey.substring(3))
) {
aKey = aKey.substring(3);
modifier -= 1;
} else if (
aKey.startsWith("post") &&
scripts.has(aKey.substring(4))
) {
aKey = aKey.substring(4);
modifier += 1;
}

if (
bKey.startsWith("pre") &&
scripts.has(bKey.substring(3))
) {
bKey = bKey.substring(3);
modifier += 1;
} else if (
bKey.startsWith("post") &&
scripts.has(bKey.substring(4))
) {
bKey = bKey.substring(4);
modifier -= 1;
}

if (aKey === bKey) {
return modifier;
}

return aKey > bKey ? 1 : -1;
}
});
if (
currentOrder.some(
(property, i) => desiredOrder[i] !== property,
Expand Down
121 changes: 121 additions & 0 deletions src/tests/rules/sort-collections.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,94 @@ ruleTester.run("sort-collections", rule, {
"build": "webpack",
"watch": "webpack-dev-server"
}
}`,
},
{
code: `{
"scripts": {
"build": "webpack",
"postwatch": "echo test",
"prebuild": "echo test",
"watch": "webpack-dev-server"
}
}`,
errors: [
{
message: "Package scripts are not alphabetized",
type: "JSONProperty",
},
],
filename: "package.json",
output: `{
"scripts": {
"prebuild": "echo test",
"build": "webpack",
"watch": "webpack-dev-server",
"postwatch": "echo test"
}
}`,
},
{
code: `{
"scripts": {
"postbuild": "echo test",
"build": "echo test"
}
}`,
errors: [
{
message: "Package scripts are not alphabetized",
type: "JSONProperty",
},
],
filename: "package.json",
output: `{
"scripts": {
"build": "echo test",
"postbuild": "echo test"
}
}`,
},
{
code: `{
"scripts": {
"build": "echo test",
"prebuild": "echo test"
}
}`,
errors: [
{
message: "Package scripts are not alphabetized",
type: "JSONProperty",
},
],
filename: "package.json",
output: `{
"scripts": {
"prebuild": "echo test",
"build": "echo test"
}
}`,
},
{
code: `{
"scripts": {
"prebuild": "echo test",
"postbuild": "echo test"
}
}`,
errors: [
{
message: "Package scripts are not alphabetized",
type: "JSONProperty",
},
],
filename: "package.json",
output: `{
"scripts": {
"postbuild": "echo test",
"prebuild": "echo test"
}
}`,
},
{
Expand Down Expand Up @@ -87,5 +175,38 @@ ruleTester.run("sort-collections", rule, {
filename: "not-a-package.json",
options: [["devDependencies"]],
},
{
code: `{
"scripts": {
"lint": "echo test",
"poster": "echo test"
}
}`,
},
{
code: `{
"scripts": {
"pretest": "echo test",
"watch": "echo test"
}
}`,
},
{
code: `{
"scripts": {
"postwatch": "echo test",
"pretest": "echo test"
}
}`,
},
{
code: `{
"scripts": {
"prebuild": "echo test",
"build": "echo test",
"postbuild": "echo test"
}
}`,
},
],
});

0 comments on commit 4ccae4f

Please sign in to comment.