eslint-plugin-package-json 0.50.0 → 0.51.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/CHANGELOG.md CHANGED
@@ -1,11 +1,17 @@
1
1
  # Changelog
2
2
 
3
- # [0.50.0](https://github.com/JoshuaKGoldberg/eslint-plugin-package-json/compare/v0.49.0...v0.50.0) (2025-08-05)
3
+ # [0.51.0](https://github.com/JoshuaKGoldberg/eslint-plugin-package-json/compare/v0.50.0...v0.51.0) (2025-08-05)
4
+
5
+
6
+ ### Features
7
+
8
+ * add `ignorePrivate` option to all `require-*` rules ([#1158](https://github.com/JoshuaKGoldberg/eslint-plugin-package-json/issues/1158)) ([055009b](https://github.com/JoshuaKGoldberg/eslint-plugin-package-json/commit/055009bf864c9f8db153bf0c5bb9568d023abe12)), closes [#1092](https://github.com/JoshuaKGoldberg/eslint-plugin-package-json/issues/1092)
4
9
 
10
+ # [0.50.0](https://github.com/JoshuaKGoldberg/eslint-plugin-package-json/compare/v0.49.0...v0.50.0) (2025-08-05)
5
11
 
6
12
  ### Features
7
13
 
8
- * add new `require-` rules for `bugs`, `bundleDependencies`, `dependencies`, and more ([#1197](https://github.com/JoshuaKGoldberg/eslint-plugin-package-json/issues/1197)) ([0a06664](https://github.com/JoshuaKGoldberg/eslint-plugin-package-json/commit/0a0666404d5659ea8eb8717516aa3c3b6374af26)), closes [#862](https://github.com/JoshuaKGoldberg/eslint-plugin-package-json/issues/862) [#863](https://github.com/JoshuaKGoldberg/eslint-plugin-package-json/issues/863) [#811](https://github.com/JoshuaKGoldberg/eslint-plugin-package-json/issues/811) [#809](https://github.com/JoshuaKGoldberg/eslint-plugin-package-json/issues/809) [#801](https://github.com/JoshuaKGoldberg/eslint-plugin-package-json/issues/801) [#797](https://github.com/JoshuaKGoldberg/eslint-plugin-package-json/issues/797)
14
+ - add new `require-` rules for `bugs`, `bundleDependencies`, `dependencies`, and more ([#1197](https://github.com/JoshuaKGoldberg/eslint-plugin-package-json/issues/1197)) ([0a06664](https://github.com/JoshuaKGoldberg/eslint-plugin-package-json/commit/0a0666404d5659ea8eb8717516aa3c3b6374af26)), closes [#862](https://github.com/JoshuaKGoldberg/eslint-plugin-package-json/issues/862) [#863](https://github.com/JoshuaKGoldberg/eslint-plugin-package-json/issues/863) [#811](https://github.com/JoshuaKGoldberg/eslint-plugin-package-json/issues/811) [#809](https://github.com/JoshuaKGoldberg/eslint-plugin-package-json/issues/809) [#801](https://github.com/JoshuaKGoldberg/eslint-plugin-package-json/issues/801) [#797](https://github.com/JoshuaKGoldberg/eslint-plugin-package-json/issues/797)
9
15
 
10
16
  # [0.49.0](https://github.com/JoshuaKGoldberg/eslint-plugin-package-json/compare/v0.48.0...v0.49.0) (2025-08-05)
11
17
 
package/README.md CHANGED
@@ -101,6 +101,40 @@ module.exports = {
101
101
  };
102
102
  ```
103
103
 
104
+ ### Settings
105
+
106
+ Some rules can be configured in ESLint shared settings.
107
+ You can set them in `settings.packageJson` in an ESLint flat config.
108
+
109
+ Example:
110
+
111
+ ```ts
112
+ // eslint.config.ts
113
+ import packageJson from "eslint-plugin-package-json";
114
+
115
+ export default {
116
+ plugins: {
117
+ "package-json": packageJson,
118
+ },
119
+ rules: {
120
+ // `description` won't be required in package.json with `"private": true`
121
+ "package-json/require-description": "error",
122
+ },
123
+ settings: {
124
+ packageJson: {
125
+ enforceForPrivate: false,
126
+ },
127
+ },
128
+ };
129
+ ```
130
+
131
+ #### `enforceForPrivate`
132
+
133
+ **Type:** `boolean`
134
+
135
+ Determines whether `require-*` rules, if used, should enforce the presence of the corresponding property in package.json files with `"private": true`.
136
+ By default, all `require-*` rules except for [`require-name`](docs/rules/require-name.md) and [`require-version`](docs/rules/require-version.md) will report if the corresponding property is missing in package.json with `"private": true`.
137
+
104
138
  ### Usage Alongside Prettier
105
139
 
106
140
  **[`prettier-plugin-packagejson`](https://github.com/matzkoh/prettier-plugin-packagejson)** is a [Prettier plugin](https://prettier.io/docs/en/plugins) that enforces the same `package.json` keys ordering as the [`order-properties`](docs/rules/order-properties.md) and [sort-collections](docs/rules/sort-collections.md) rules with default options.
@@ -14,8 +14,20 @@ interface JsonAstBodyStatement extends ESTree.ExpressionStatement {
14
14
  interface PackageJsonAst extends AST$1.Program {
15
15
  body: [JsonAstBodyStatement];
16
16
  }
17
+ interface PackageJsonPluginSettings {
18
+ /**
19
+ * Whether `require-*` rules, if used, should enforce the presence of
20
+ * the corresponding property *in package.json files with `"private": true`*.
21
+ *
22
+ * If not specified, it will not enforce the presence only of `name` and `version` properties.
23
+ */
24
+ enforceForPrivate?: boolean;
25
+ }
17
26
  interface PackageJsonRuleContext<Options extends unknown[] = unknown[]> extends Rule.RuleContext {
18
27
  options: Options;
28
+ settings: {
29
+ packageJson?: PackageJsonPluginSettings;
30
+ };
19
31
  sourceCode: PackageJsonSourceCode;
20
32
  }
21
33
  interface PackageJsonRuleModule<Options extends unknown[] = unknown[]> {
@@ -30,4 +42,4 @@ declare function createRule<Options extends unknown[]>(rule: PackageJsonRuleModu
30
42
  meta: Rule.RuleMetaData;
31
43
  };
32
44
 
33
- export { type JsonAstBodyExpression, type JsonAstBodyProperty, type JsonAstBodyStatement, type PackageJsonAst, type PackageJsonRuleContext, type PackageJsonRuleModule, type PackageJsonSourceCode, createRule };
45
+ export { type JsonAstBodyExpression, type JsonAstBodyProperty, type JsonAstBodyStatement, type PackageJsonAst, type PackageJsonPluginSettings, type PackageJsonRuleContext, type PackageJsonRuleModule, type PackageJsonSourceCode, createRule };
package/lib/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import * as jsonc_eslint_parser from 'jsonc-eslint-parser';
2
2
  import { PackageJsonRuleModule } from './createRule.js';
3
+ export { PackageJsonPluginSettings } from './createRule.js';
3
4
  import { plugin } from './plugin.js';
4
5
  import 'estree';
5
6
  import 'eslint';
@@ -1,9 +1,15 @@
1
- import { PackageJsonRuleModule } from '../createRule.js';
1
+ import * as eslint from 'eslint';
2
+ import * as jsonc_eslint_parser from 'jsonc-eslint-parser';
3
+ import { PackageJsonRuleContext } from '../createRule.js';
2
4
  import 'estree';
3
- import 'eslint';
4
- import 'jsonc-eslint-parser';
5
5
 
6
- /** All require- flavor rules */
7
- declare const rules: Record<string, PackageJsonRuleModule<unknown[]>>;
6
+ declare const rules: {
7
+ [k: string]: {
8
+ create(context: PackageJsonRuleContext<[({
9
+ ignorePrivate?: boolean;
10
+ } | undefined)?]>): jsonc_eslint_parser.RuleListener;
11
+ meta: eslint.Rule.RuleMetaData;
12
+ };
13
+ };
8
14
 
9
15
  export { rules };
@@ -1,31 +1,28 @@
1
- import { createSimpleRequirePropertyRule } from "../utils/createSimpleRequirePropertyRule.js";
1
+ import {
2
+ createSimpleRequirePropertyRule
3
+ } from "../utils/createSimpleRequirePropertyRule.js";
2
4
  const properties = [
3
- ["author", false],
4
- ["bugs", false],
5
- ["bundleDependencies", false],
6
- ["dependencies", false],
7
- ["devDependencies", false],
8
- ["description", true],
9
- ["engines", false],
10
- ["files", false],
11
- ["keywords", false],
12
- ["name", true],
13
- ["optionalDependencies", false],
14
- ["peerDependencies", false],
15
- ["type", true],
16
- ["types", false],
17
- ["version", true]
18
- // TODO: More to come!
5
+ ["author"],
6
+ ["bugs"],
7
+ ["bundleDependencies"],
8
+ ["dependencies"],
9
+ ["description", { isRecommended: true }],
10
+ ["devDependencies"],
11
+ ["engines"],
12
+ ["files"],
13
+ ["keywords"],
14
+ ["name", { ignorePrivateDefault: true, isRecommended: true }],
15
+ ["optionalDependencies"],
16
+ ["peerDependencies"],
17
+ ["type", { isRecommended: true }],
18
+ ["types"],
19
+ ["version", { ignorePrivateDefault: true, isRecommended: true }]
19
20
  ];
20
- const rules = properties.reduce(
21
- (acc, [propertyName, isRecommended]) => {
22
- acc[`require-${propertyName}`] = createSimpleRequirePropertyRule(
23
- propertyName,
24
- isRecommended
25
- );
26
- return acc;
27
- },
28
- {}
21
+ const rules = Object.fromEntries(
22
+ properties.map(([propertyName, options]) => [
23
+ `require-${propertyName}`,
24
+ createSimpleRequirePropertyRule(propertyName, options)
25
+ ])
29
26
  );
30
27
  export {
31
28
  rules
@@ -3,15 +3,28 @@ import * as jsonc_eslint_parser from 'jsonc-eslint-parser';
3
3
  import { PackageJsonRuleContext } from '../createRule.js';
4
4
  import 'estree';
5
5
 
6
+ interface CreateRequirePropertyRuleOptions {
7
+ /**
8
+ * The default value of `ignorePrivate` rule option.
9
+ */
10
+ ignorePrivateDefault?: boolean;
11
+ /**
12
+ * Whether the rule should be included in the recommended config.
13
+ */
14
+ isRecommended?: boolean;
15
+ }
16
+ type Options = [{
17
+ ignorePrivate?: boolean;
18
+ }?];
6
19
  /**
7
20
  * Given a top-level property name, create a rule that requires that property to be present.
8
21
  * Optionally, include it in the recommended config.
9
22
  * Note: this will only create a basic require rule, with no options. If you need
10
23
  * to create a more complex rule, create it in its own file.
11
24
  */
12
- declare const createSimpleRequirePropertyRule: (propertyName: string, isRecommended?: boolean) => {
13
- create(context: PackageJsonRuleContext<unknown[]>): jsonc_eslint_parser.RuleListener;
25
+ declare const createSimpleRequirePropertyRule: (propertyName: string, { ignorePrivateDefault, isRecommended, }?: CreateRequirePropertyRuleOptions) => {
26
+ create(context: PackageJsonRuleContext<Options>): jsonc_eslint_parser.RuleListener;
14
27
  meta: eslint.Rule.RuleMetaData;
15
28
  };
16
29
 
17
- export { createSimpleRequirePropertyRule };
30
+ export { type CreateRequirePropertyRuleOptions, createSimpleRequirePropertyRule };
@@ -1,10 +1,20 @@
1
1
  import { createRule } from "../createRule.js";
2
2
  import { isJSONStringLiteral } from "./predicates.js";
3
- const createSimpleRequirePropertyRule = (propertyName, isRecommended = false) => {
3
+ const createSimpleRequirePropertyRule = (propertyName, {
4
+ ignorePrivateDefault = false,
5
+ isRecommended
6
+ } = {}) => {
4
7
  return createRule({
5
8
  create(context) {
9
+ const enforceForPrivate = context.settings.packageJson?.enforceForPrivate;
10
+ const ignorePrivate = context.options[0]?.ignorePrivate ?? (typeof enforceForPrivate === "boolean" ? !enforceForPrivate : ignorePrivateDefault);
6
11
  return {
7
12
  "Program > JSONExpressionStatement > JSONObjectExpression"(node) {
13
+ if (ignorePrivate && node.properties.some(
14
+ (property) => isJSONStringLiteral(property.key) && property.key.value === "private" && property.value.type === "JSONLiteral" && property.value.value === true
15
+ )) {
16
+ return;
17
+ }
8
18
  if (!node.properties.some(
9
19
  (property) => isJSONStringLiteral(property.key) && property.key.value === propertyName
10
20
  )) {
@@ -25,7 +35,18 @@ const createSimpleRequirePropertyRule = (propertyName, isRecommended = false) =>
25
35
  messages: {
26
36
  missing: "Property '{{property}}' is required."
27
37
  },
28
- schema: [],
38
+ schema: [
39
+ {
40
+ additionalProperties: false,
41
+ properties: {
42
+ ignorePrivate: {
43
+ default: ignorePrivateDefault,
44
+ type: "boolean"
45
+ }
46
+ },
47
+ type: "object"
48
+ }
49
+ ],
29
50
  type: "suggestion"
30
51
  }
31
52
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-package-json",
3
- "version": "0.50.0",
3
+ "version": "0.51.0",
4
4
  "description": "Rules for consistent, readable, and valid package.json files. 🗂️",
5
5
  "homepage": "https://github.com/JoshuaKGoldberg/eslint-plugin-package-json#readme",
6
6
  "bugs": {