Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: sort alphabetically with co-located hooks for scripts #632

Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"npmpackagejsonlintrc",
"outro",
"packagejson",
"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 @@ -29,14 +29,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;
sasial-dev marked this conversation as resolved.
Show resolved Hide resolved

if (key.value !== "scripts") {
sasial-dev marked this conversation as resolved.
Show resolved Hide resolved
return aKey > bKey ? 1 : -1;
} else {
let modifier = 0;
sasial-dev marked this conversation as resolved.
Show resolved Hide resolved

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
49 changes: 49 additions & 0 deletions src/tests/rules/sort-collections.test.ts
sasial-dev marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,31 @@ 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"
}
}`,
},
{
Expand Down Expand Up @@ -87,5 +112,29 @@ 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"
}
}`,
},
],
});