eslint-plugin-package-json 1.3.0 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -46,7 +46,7 @@ See [Getting Started](https://eslint-plugin-package-json.dev/getting-started) fo
46
46
  | [no-local-dependencies](https://eslint-plugin-package-json.dev/rules/no-local-dependencies) | Requires that dependencies do not use local file paths, which will likely result in errors when installing from a registry. | | | |
47
47
  | [no-redundant-files](https://eslint-plugin-package-json.dev/rules/no-redundant-files) | Prevents adding unnecessary / redundant files. | ✅ 📦 | | 💡 |
48
48
  | [no-redundant-publishConfig](https://eslint-plugin-package-json.dev/rules/no-redundant-publishConfig) | Warns when publishConfig.access is used in unscoped packages. | ✅ 📦 | | 💡 |
49
- | [order-properties](https://eslint-plugin-package-json.dev/rules/order-properties) | Package properties should be declared in standard order | 🎨 | 🔧 | |
49
+ | [order-properties](https://eslint-plugin-package-json.dev/rules/order-properties) | Enforces that package properties are declared in a consistent order. | 🎨 | 🔧 | |
50
50
  | [repository-shorthand](https://eslint-plugin-package-json.dev/rules/repository-shorthand) | Enforce either object or shorthand declaration for repository. | ✅ 📦 | 🔧 | |
51
51
  | [require-attribution](https://eslint-plugin-package-json.dev/rules/require-attribution) | Ensures that proper attribution is included, requiring that either `author` or `contributors` is defined, and that if `contributors` is present, it should include at least one contributor. | ✅ 📦 | | 💡 |
52
52
  | [restrict-dependency-ranges](https://eslint-plugin-package-json.dev/rules/restrict-dependency-ranges) | Restricts the range of dependencies to allow or disallow specific types of ranges. | | | 💡 |
@@ -47,15 +47,14 @@ const rule = createRule({
47
47
  }
48
48
  },
49
49
  "Program > JSONExpressionStatement > JSONObjectExpression > JSONProperty[key.value=files]"(node) {
50
- if (node.value.type === "JSONArrayExpression") {
51
- const seen = /* @__PURE__ */ new Set();
52
- const elements = node.value.elements;
53
- entryCache.files = elements;
54
- for (const [index, element] of elements.entries()) if (isNotNullish(element) && isJSONStringLiteral(element)) {
55
- if (seen.has(element.value)) report(elements, index, "duplicate");
56
- else seen.add(element.value);
57
- for (const defaultFile of defaultFiles) if (defaultFile.test(element.value)) report(elements, index, "unnecessaryDefault");
58
- }
50
+ if (node.value.type !== "JSONArrayExpression") return;
51
+ const seen = /* @__PURE__ */ new Set();
52
+ const elements = node.value.elements;
53
+ entryCache.files = elements;
54
+ for (const [index, element] of elements.entries()) if (isNotNullish(element) && isJSONStringLiteral(element)) {
55
+ if (seen.has(element.value)) report(elements, index, "duplicate");
56
+ else seen.add(element.value);
57
+ for (const defaultFile of defaultFiles) if (defaultFile.test(element.value)) report(elements, index, "unnecessaryDefault");
59
58
  }
60
59
  },
61
60
  "Program > JSONExpressionStatement > JSONObjectExpression > JSONProperty[key.value=main]"(node) {
@@ -38,10 +38,10 @@ const rule = createRule({
38
38
  defaultOptions: [{ order: "sort-package-json" }],
39
39
  docs: {
40
40
  category: "Stylistic",
41
- description: "Package properties should be declared in standard order"
41
+ description: "Enforces that package properties are declared in a consistent order."
42
42
  },
43
43
  fixable: "code",
44
- messages: { incorrectOrder: "Top-level property \"{{property}}\" is not ordered in the standard way." },
44
+ messages: { incorrectOrder: "Top-level property `{{property}}` is not ordered in the standard way." },
45
45
  schema: [{
46
46
  additionalProperties: false,
47
47
  properties: { order: {
@@ -15,7 +15,7 @@ const rule = createRule({
15
15
  "Program > JSONExpressionStatement > JSONObjectExpression > JSONProperty[key.value=contributors]"(node) {
16
16
  contributorsPropertyNode = node;
17
17
  const contributorsValue = node.value;
18
- if (contributorsValue.type !== "JSONArrayExpression" || !contributorsValue.elements.some((element) => !!element)) context.report({
18
+ if (contributorsValue.type !== "JSONArrayExpression" || contributorsValue.elements.every((element) => !element)) context.report({
19
19
  messageId: "noContributors",
20
20
  node
21
21
  });
@@ -102,7 +102,7 @@ const rule = createRule({
102
102
  });
103
103
  break;
104
104
  }
105
- if (!rangeTypes.find((rangeType) => {
105
+ if (!rangeTypes.some((rangeType) => {
106
106
  switch (rangeType) {
107
107
  case "caret": return isCaretRange;
108
108
  case "pin": return isPinned;
@@ -7,7 +7,7 @@ const rule = createRule({
7
7
  create(context) {
8
8
  const blockedProperties = context.options[0]?.blockedProperties ?? defaultBlockedProperties;
9
9
  return { "Program > JSONExpressionStatement > JSONObjectExpression"(node) {
10
- if (!node.properties.some((property) => isJSONStringLiteral(property.key) && property.key.value === "private" && property.value.type === "JSONLiteral" && property.value.value === true)) return;
10
+ if (node.properties.every((property) => !(isJSONStringLiteral(property.key) && property.key.value === "private" && property.value.type === "JSONLiteral" && property.value.value === true))) return;
11
11
  for (const property of node.properties) if (isJSONStringLiteral(property.key) && blockedProperties.includes(property.key.value)) {
12
12
  const isEmpty = property.value.type === "JSONArrayExpression" && property.value.elements.length === 0 || property.value.type === "JSONObjectExpression" && property.value.properties.length === 0;
13
13
  context.report({
@@ -1,10 +1,33 @@
1
1
  import { PackageJsonRuleModule } from "../createRule.mjs";
2
2
 
3
3
  //#region src/rules/sort-collections.d.ts
4
- declare const rule: PackageJsonRuleModule<[(string[] | undefined)?], [{
5
- readonly description: "Array of package properties to require sorting.";
4
+ declare const rule: PackageJsonRuleModule<[((string | {
5
+ key: string;
6
+ order: string[];
7
+ })[] | undefined)?], [{
8
+ readonly description: "Array of package properties to require sorting. Provide a string to sort that collection lexicographically (lifecycle-aware for `scripts`), or an object to sort it by a specified order.";
6
9
  readonly items: {
7
- readonly type: "string";
10
+ readonly anyOf: readonly [{
11
+ readonly type: "string";
12
+ }, {
13
+ readonly additionalProperties: false;
14
+ readonly properties: {
15
+ readonly key: {
16
+ readonly description: "The collection property to sort.";
17
+ readonly type: "string";
18
+ };
19
+ readonly order: {
20
+ readonly description: "The order to sort the collection by. Keys not listed are appended in lexicographical order.";
21
+ readonly items: {
22
+ readonly type: "string";
23
+ };
24
+ readonly type: "array";
25
+ readonly uniqueItems: true;
26
+ };
27
+ };
28
+ readonly required: readonly ["key", "order"];
29
+ readonly type: "object";
30
+ }];
8
31
  };
9
32
  readonly type: "array";
10
33
  }]>;
@@ -1,4 +1,6 @@
1
1
  import { createRule } from "../createRule.mjs";
2
+ import detectIndent from "detect-indent";
3
+ import { detectNewlineGraceful } from "detect-newline";
2
4
  import sortPackageJson from "sort-package-json";
3
5
  //#region src/rules/sort-collections.ts
4
6
  const defaultCollections = new Set([
@@ -14,7 +16,7 @@ const defaultCollections = new Set([
14
16
  ]);
15
17
  const rule = createRule({
16
18
  create(context) {
17
- const toSort = context.options[0] ? new Set(context.options[0]) : defaultCollections;
19
+ const toSort = new Map((context.options[0] ?? Array.from(defaultCollections)).map((entry) => typeof entry === "string" ? [entry, null] : [entry.key, entry.order]));
18
20
  return { "JSONProperty:exit"(node) {
19
21
  const { key: nodeKey, value: collection } = node;
20
22
  if (nodeKey.type !== "JSONLiteral" || collection.type !== "JSONObjectExpression") return;
@@ -22,27 +24,47 @@ const rule = createRule({
22
24
  for (let currNode = node.parent; currNode; currNode = currNode.parent) if (currNode.type === "JSONProperty" && currNode.key.type === "JSONLiteral") keyPartsReversed.push(currNode.key.value);
23
25
  else if (currNode.type === "JSONArrayExpression") return;
24
26
  const key = keyPartsReversed.reverse().join(".");
25
- if (!toSort.has(key)) return;
27
+ const customOrder = toSort.get(key);
28
+ if (customOrder === void 0) return;
26
29
  const currentOrder = collection.properties;
27
- let desiredOrder;
28
- if (keyPartsReversed.at(-1) === "scripts") {
30
+ const isScripts = keyPartsReversed.at(-1) === "scripts";
31
+ let naturalCompare;
32
+ if (isScripts) {
29
33
  const scriptsSource = context.sourceCode.getText(node);
30
34
  const { scripts: sortedScripts } = sortPackageJson(JSON.parse(`{${scriptsSource}}`));
31
- const propertyNodeMap = Object.fromEntries(collection.properties.map((prop) => [prop.key.value, prop]));
32
- desiredOrder = Object.keys(sortedScripts).map((prop) => propertyNodeMap[prop]);
33
- } else desiredOrder = currentOrder.toSorted((a, b) => {
34
- return a.key.value > b.key.value ? 1 : -1;
35
+ const lifecycleIndex = new Map(Object.keys(sortedScripts).map((k, i) => [k, i]));
36
+ naturalCompare = (a, b) => (lifecycleIndex.get(a) ?? 0) - (lifecycleIndex.get(b) ?? 0);
37
+ } else naturalCompare = (a, b) => a > b ? 1 : -1;
38
+ const orderIndex = new Map((customOrder ?? []).map((k, i) => [k, i]));
39
+ const rank = (k) => orderIndex.get(k) ?? orderIndex.size;
40
+ const desiredOrder = currentOrder.toSorted((a, b) => {
41
+ const aKey = a.key.value;
42
+ const bKey = b.key.value;
43
+ const ai = rank(aKey);
44
+ const bi = rank(bKey);
45
+ if (ai !== bi) return ai - bi;
46
+ return naturalCompare(aKey, bKey);
35
47
  });
36
48
  if (currentOrder.some((property, i) => desiredOrder[i] !== property)) context.report({
37
49
  data: { key },
38
50
  fix(fixer) {
39
- return fixer.replaceText(collection, JSON.stringify(desiredOrder.reduce((out, property) => {
51
+ const { text } = context.sourceCode;
52
+ const { indent, type } = detectIndent(text);
53
+ const newline = detectNewlineGraceful(text);
54
+ const indentUnit = type === "tab" ? " " : indent || " ";
55
+ const jsonLines = JSON.stringify(desiredOrder.reduce((out, property) => {
40
56
  out[property.key.value] = JSON.parse(context.sourceCode.getText(property.value));
41
57
  return out;
42
- }, {}), null, 2).split("\n").join("\n "));
58
+ }, {}), null, indentUnit).split("\n");
59
+ const collectionStartLine = collection.loc.start.line;
60
+ const lineText = context.sourceCode.lines[collectionStartLine - 1];
61
+ const leadingWhitespaceMatch = /^\s*/.exec(lineText);
62
+ const leadingWhitespace = leadingWhitespaceMatch ? leadingWhitespaceMatch[0] : "";
63
+ const result = jsonLines.map((l, i) => i === 0 ? l : leadingWhitespace + l).join(newline);
64
+ return fixer.replaceText(collection, result);
43
65
  },
44
66
  loc: collection.loc,
45
- messageId: keyPartsReversed.at(-1) === "scripts" ? "unsortedScripts" : "unsortedKeys",
67
+ messageId: customOrder ? "unsortedOrder" : isScripts ? "unsortedScripts" : "unsortedKeys",
46
68
  node
47
69
  });
48
70
  } };
@@ -57,11 +79,28 @@ const rule = createRule({
57
79
  fixable: "code",
58
80
  messages: {
59
81
  unsortedKeys: "Entries in '{{ key }}' are not in lexicographical order",
82
+ unsortedOrder: "Entries in '{{ key }}' are not in the specified order",
60
83
  unsortedScripts: "Entries in 'scripts' are not in lexicographical order and grouped by lifecycles"
61
84
  },
62
85
  schema: [{
63
- description: "Array of package properties to require sorting.",
64
- items: { type: "string" },
86
+ description: "Array of package properties to require sorting. Provide a string to sort that collection lexicographically (lifecycle-aware for `scripts`), or an object to sort it by a specified order.",
87
+ items: { anyOf: [{ type: "string" }, {
88
+ additionalProperties: false,
89
+ properties: {
90
+ key: {
91
+ description: "The collection property to sort.",
92
+ type: "string"
93
+ },
94
+ order: {
95
+ description: "The order to sort the collection by. Keys not listed are appended in lexicographical order.",
96
+ items: { type: "string" },
97
+ type: "array",
98
+ uniqueItems: true
99
+ }
100
+ },
101
+ required: ["key", "order"],
102
+ type: "object"
103
+ }] },
65
104
  type: "array"
66
105
  }],
67
106
  type: "layout"
@@ -12,8 +12,8 @@ import { findRootSync } from "@altano/repository-tools";
12
12
  * @example '/a/b/c', 'd' => false
13
13
  */
14
14
  const pathEndsWith = (parent, child) => {
15
- const segments = parent.split(path.sep);
16
15
  if (parent === child) return true;
16
+ const segments = parent.split(path.sep);
17
17
  let pathToCheck = "";
18
18
  return segments.reverse().some((segment) => {
19
19
  pathToCheck = path.join(segment, pathToCheck);
@@ -16,7 +16,7 @@ const createSimpleRequirePropertyRule = (propertyName, { category, fixValue, ign
16
16
  const ignorePrivate = context.options[0]?.ignorePrivate ?? (typeof enforceForPrivate === "boolean" ? !enforceForPrivate : ignorePrivateDefault);
17
17
  return { "Program > JSONExpressionStatement > JSONObjectExpression"(node) {
18
18
  if (ignorePrivate && node.properties.some((property) => isJSONStringLiteral(property.key) && property.key.value === "private" && property.value.type === "JSONLiteral" && property.value.value === true)) return;
19
- if (!node.properties.some((property) => isJSONStringLiteral(property.key) && property.key.value === propertyName)) context.report({
19
+ if (node.properties.every((property) => !(isJSONStringLiteral(property.key) && property.key.value === propertyName))) context.report({
20
20
  data: { property: propertyName },
21
21
  fix: fixValue === void 0 ? void 0 : function* (fixer) {
22
22
  yield fixer.insertTextAfterRange([0, 1], `\n "${propertyName}": ${JSON.stringify(fixValue)}`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-package-json",
3
- "version": "1.3.0",
3
+ "version": "1.4.0",
4
4
  "description": "Rules for consistent, readable, and valid package.json files. 🗂️",
5
5
  "homepage": "https://github.com/michaelfaith/eslint-plugin-package-json#readme",
6
6
  "bugs": {
@@ -53,51 +53,52 @@
53
53
  "package-json-validator": "^1.5.0",
54
54
  "semver": "^7.7.3",
55
55
  "sort-object-keys": "^2.0.0",
56
- "sort-package-json": "^3.4.0"
56
+ "sort-package-json": "^4.0.0"
57
57
  },
58
58
  "devDependencies": {
59
- "@astrojs/starlight": "0.39.0",
59
+ "@astrojs/check": "0.9.9",
60
+ "@astrojs/starlight": "0.40.0",
60
61
  "@catppuccin/starlight": "2.0.1",
61
- "@eslint-community/eslint-plugin-eslint-comments": "4.7.0",
62
+ "@eslint-community/eslint-plugin-eslint-comments": "4.7.2",
62
63
  "@eslint/js": "10.0.1",
63
64
  "@eslint/json": "2.0.0",
64
65
  "@eslint/markdown": "8.0.1",
65
66
  "@ianvs/prettier-plugin-sort-imports": "4.7.1",
66
67
  "@types/estree": "1.0.8",
67
- "@types/node": "24.12.0",
68
+ "@types/node": "24.13.0",
68
69
  "@types/semver": "7.7.1",
69
- "@vitest/coverage-v8": "4.1.0",
70
- "@vitest/eslint-plugin": "1.6.1",
70
+ "@vitest/coverage-v8": "4.1.8",
71
+ "@vitest/eslint-plugin": "1.6.20",
71
72
  "astro": "6.4.2",
72
73
  "astro-og-canvas": "0.11.1",
73
74
  "canvaskit-wasm": "0.41.1",
74
75
  "console-fail-test": "0.6.0",
75
- "eslint": "10.4.0",
76
+ "eslint": "10.5.0",
76
77
  "eslint-doc-generator": "3.6.0",
77
- "eslint-plugin-eslint-plugin": "7.3.1",
78
- "eslint-plugin-jsdoc": "63.0.0",
78
+ "eslint-plugin-eslint-plugin": "7.4.0",
79
+ "eslint-plugin-jsdoc": "63.0.2",
79
80
  "eslint-plugin-jsonc": "3.2.0",
80
- "eslint-plugin-n": "18.0.0",
81
+ "eslint-plugin-n": "18.1.0",
81
82
  "eslint-plugin-node-dependencies": "2.2.0",
82
83
  "eslint-plugin-perfectionist": "5.9.0",
83
84
  "eslint-plugin-regexp": "3.1.0",
84
- "eslint-plugin-unicorn": "64.0.0",
85
+ "eslint-plugin-unicorn": "66.0.0",
85
86
  "eslint-plugin-yml": "3.4.0",
86
87
  "jiti": "2.7.0",
87
88
  "json-schema-to-ts": "3.1.1",
88
- "knip": "6.14.0",
89
- "prettier": "3.8.0",
90
- "prettier-plugin-curly": "0.4.0",
91
- "prettier-plugin-packagejson": "3.0.0",
92
- "prettier-plugin-sh": "0.18.0",
89
+ "knip": "6.16.0",
90
+ "prettier": "3.8.4",
91
+ "prettier-plugin-curly": "0.4.1",
92
+ "prettier-plugin-packagejson": "3.0.2",
93
+ "prettier-plugin-sh": "0.18.1",
93
94
  "pretty-quick": "4.2.2",
94
- "sharp": "0.34.5",
95
+ "sharp": "0.35.0",
95
96
  "simple-git-hooks": "2.13.1",
96
97
  "starlight-auto-sidebar": "0.4.0",
97
- "tsdown": "0.22.0",
98
- "typescript": "6.0.2",
99
- "typescript-eslint": "8.60.0",
100
- "vitest": "4.1.0"
98
+ "tsdown": "0.22.2",
99
+ "typescript": "6.0.3",
100
+ "typescript-eslint": "8.61.0",
101
+ "vitest": "4.1.8"
101
102
  },
102
103
  "peerDependencies": {
103
104
  "@eslint/json": ">=1.0.0",
@@ -118,10 +119,10 @@
118
119
  "lint": "eslint . --max-warnings 0",
119
120
  "lint:knip": "knip",
120
121
  "site:build": "astro build",
122
+ "site:check": "astro check --root site",
121
123
  "site:dev": "astro dev",
122
124
  "site:preview": "astro preview",
123
125
  "site:sync": "astro sync",
124
- "site:typecheck": "pnpm site:sync && tsc --project site/tsconfig.json",
125
126
  "test": "vitest",
126
127
  "typecheck": "tsc"
127
128
  }