eslint-plugin-package-jsonc 2.0.1 → 3.0.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
@@ -1,6 +1,6 @@
1
1
  # eslint-plugin-package-jsonc
2
2
 
3
- An ESLint plugin that ensures `package.json` is consistent with `package.jsonc`.
3
+ An ESLint plugin that ensures `package.json` is consistent with `package.jsonc`. You can use comments and trailing commas in your `package.jsonc`, and it serves as the source of truth for validation purposes.
4
4
 
5
5
  ## Installation
6
6
 
@@ -8,11 +8,7 @@ An ESLint plugin that ensures `package.json` is consistent with `package.jsonc`.
8
8
  npm install --save-dev eslint-plugin-package-jsonc jsonc-eslint-parser
9
9
  ```
10
10
 
11
- ## Usage
12
-
13
- Add the plugin to your ESLint configuration:
14
-
15
- ### Flat Config (eslint.config.js)
11
+ ## Flat Config (eslint.config.js)
16
12
 
17
13
  ```javascript
18
14
  import jsoncParser from "jsonc-eslint-parser";
@@ -21,15 +17,10 @@ import packageJsonc from "eslint-plugin-package-jsonc";
21
17
  export default [
22
18
  // Configure the parser for JSON/JSONC files (handles comments, trailing commas)
23
19
  {
24
- files: ["**/*.json", "**/*.jsonc"],
20
+ files: ["package.jsonc"],
25
21
  languageOptions: {
26
22
  parser: jsoncParser,
27
23
  },
28
- },
29
- // Add package-jsonc/sync rule for package.jsonc files
30
- {
31
- // Use "**/package.jsonc" if you have packages in subdirectories (monorepo)
32
- files: ["package.jsonc"],
33
24
  plugins: {
34
25
  "package-jsonc": packageJsonc,
35
26
  },
@@ -40,37 +31,19 @@ export default [
40
31
  ];
41
32
  ```
42
33
 
43
- > **Note:** Use `"**/package.jsonc"` if you have packages in subdirectories (monorepo).
44
-
45
34
  ## Rule: `package-jsonc/sync`
46
35
 
47
- This rule ensures that `package.jsonc` is the single source of truth for your package configuration.
36
+ This rule validates that `package.jsonc` is the source of truth for your package configuration.
48
37
 
49
38
  It enforces the following:
50
39
 
51
- 1. **Invalid `package.jsonc`**: If `package.jsonc` contains invalid JSON (syntax errors), the rule reports an error. This is not auto-fixable.
52
- 2. **Inconsistent `package.json`**: If `package.jsonc` is valid, the rule compares it with `package.json`. If `package.json` is missing, invalid, or inconsistent with `package.jsonc`, the rule reports an error. **This is auto-fixable.**
53
-
54
- ### Fix Mode
40
+ 1. **Invalid `package.jsonc`**: If `package.jsonc` contains invalid JSON (syntax errors), the rule reports an error.
41
+ 2. **Inconsistent `package.json`**: If `package.jsonc` is valid, the rule compares it with `package.json`. If `package.json` is missing, invalid, or inconsistent with `package.jsonc`, the rule reports an error.
55
42
 
56
- When running ESLint with the `--fix` flag, the plugin will automatically generate or update `package.json` to match `package.jsonc`.
57
-
58
- ```bash
59
- eslint package.jsonc --fix
60
- ```
43
+ > **Note**: This rule is **not auto-fixable**. When an inconsistency is detected, you need to manually synchronize `package.json` with `package.jsonc`. This is intentional because package managers like pnpm may modify `package.json` directly, making automatic fixes unreliable.
61
44
 
62
45
  ## How It Works
63
46
 
64
- 1. When linting `package.jsonc`, it uses `jsonc-eslint-parser` to parse the content.
65
- 2. If `package.jsonc` is valid, it compares the parsed content with `package.json`.
66
- 3. If they differ (or `package.json` is missing/invalid), it reports an error.
67
- 4. In fix mode, it writes the normalized JSON from `package.jsonc` to `package.json`.
68
-
69
- ## Why Use This?
70
-
71
- - **Comments in package.json**: JSONC allows you to add comments to your package configuration while maintaining a standard `package.json` for npm compatibility.
72
- - **Single source of truth**: `package.jsonc` becomes the source of truth, and `package.json` is auto-generated.
73
-
74
- ## Hints
75
-
76
- You can use eslint-plugin-package-json as you let AI fix related errors in package.jsonc rather than package.json.
47
+ 1. When linting `package.jsonc`, it uses `jsonc-eslint-parser` to parse the content.
48
+ 2. If `package.jsonc` is valid, it compares the parsed content with `package.json`.
49
+ 3. If they differ (or `package.json` is missing/invalid), it reports an error.
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAQrC,QAAA,MAAM,MAAM,EAAE,MAAM,CAAC,MAgBpB,CAAC;AAOF,eAAe,MAAM,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAQrC,QAAA,MAAM,MAAM,EAAE,MAAM,CAAC,MAgBpB,CAAC;AAEF,eAAe,MAAM,CAAC"}
package/dist/index.js CHANGED
@@ -1,8 +1,8 @@
1
- import packageJsoncRule, { clearFixedFiles } from "./rules/package-jsonc.js";
1
+ import packageJsoncRule from "./rules/package-jsonc.js";
2
2
  // Plugin metadata - hardcoded to avoid dependency on package.json
3
3
  // This ensures the plugin works even when the project's package.json is missing
4
4
  const PLUGIN_NAME = "eslint-plugin-package-jsonc";
5
- const PLUGIN_VERSION = "2.0.1";
5
+ const PLUGIN_VERSION = "3.0.0";
6
6
  const plugin = {
7
7
  meta: {
8
8
  name: PLUGIN_NAME,
@@ -20,7 +20,5 @@ const plugin = {
20
20
  },
21
21
  },
22
22
  };
23
- // Export the clearFixedFiles function for testing
24
- plugin.clearFixedFiles = clearFixedFiles;
25
23
  export default plugin;
26
24
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,gBAAgB,EAAE,EAAE,eAAe,EAAE,MAAM,0BAA0B,CAAC;AAE7E,kEAAkE;AAClE,gFAAgF;AAChF,MAAM,WAAW,GAAG,6BAA6B,CAAC;AAClD,MAAM,cAAc,GAAG,OAAO,CAAC;AAE/B,MAAM,MAAM,GAAkB;IAC1B,IAAI,EAAE;QACF,IAAI,EAAE,WAAW;QACjB,OAAO,EAAE,cAAc;KAC1B;IACD,KAAK,EAAE;QACH,IAAI,EAAE,gBAAgB;KACzB;IACD,OAAO,EAAE;QACL,WAAW,EAAE;YACT,OAAO,EAAE,CAAC,eAAe,CAAC;YAC1B,KAAK,EAAE;gBACH,oBAAoB,EAAE,OAAO;aAChC;SACJ;KACJ;CACJ,CAAC;AAEF,kDAAkD;AAE9C,MACH,CAAC,eAAe,GAAG,eAAe,CAAC;AAEpC,eAAe,MAAM,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,gBAAgB,MAAM,0BAA0B,CAAC;AAExD,kEAAkE;AAClE,gFAAgF;AAChF,MAAM,WAAW,GAAG,6BAA6B,CAAC;AAClD,MAAM,cAAc,GAAG,OAAO,CAAC;AAE/B,MAAM,MAAM,GAAkB;IAC1B,IAAI,EAAE;QACF,IAAI,EAAE,WAAW;QACjB,OAAO,EAAE,cAAc;KAC1B;IACD,KAAK,EAAE;QACH,IAAI,EAAE,gBAAgB;KACzB;IACD,OAAO,EAAE;QACL,WAAW,EAAE;YACT,OAAO,EAAE,CAAC,eAAe,CAAC;YAC1B,KAAK,EAAE;gBACH,oBAAoB,EAAE,OAAO;aAChC;SACJ;KACJ;CACJ,CAAC;AAEF,eAAe,MAAM,CAAC"}
@@ -1,5 +1,4 @@
1
1
  import type { Rule } from "eslint";
2
2
  declare const rule: Rule.RuleModule;
3
- export declare function clearFixedFiles(): void;
4
3
  export default rule;
5
4
  //# sourceMappingURL=package-jsonc.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"package-jsonc.d.ts","sourceRoot":"","sources":["../../src/rules/package-jsonc.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AAiDnC,QAAA,MAAM,IAAI,EAAE,IAAI,CAAC,UA+FhB,CAAC;AAGF,wBAAgB,eAAe,IAAI,IAAI,CAEtC;AAED,eAAe,IAAI,CAAC"}
1
+ {"version":3,"file":"package-jsonc.d.ts","sourceRoot":"","sources":["../../src/rules/package-jsonc.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AA+BnC,QAAA,MAAM,IAAI,EAAE,IAAI,CAAC,UAuEhB,CAAC;AAEF,eAAe,IAAI,CAAC"}
@@ -1,8 +1,6 @@
1
1
  import { getStaticJSONValue, parseJSON } from "jsonc-eslint-parser";
2
2
  import * as fs from "node:fs";
3
3
  import path from "node:path";
4
- // Track which files have been fixed in this ESLint run to avoid circular fixes
5
- const fixedFiles = new Set();
6
4
  /**
7
5
  * Indentation level for JSON stringification.
8
6
  */
@@ -26,21 +24,6 @@ function parseJSONC(content) {
26
24
  function getNormalizedJson(object) {
27
25
  return `${JSON.stringify(object, null, JSON_INDENT)}\n`;
28
26
  }
29
- /**
30
- * Check if package.json needs to be updated.
31
- * This is called during the fix phase to avoid circular fixes.
32
- */
33
- function needsFix(packageJsonPath, normalizedJsonc) {
34
- try {
35
- const content = fs.readFileSync(packageJsonPath);
36
- const data = JSON.parse(content.toString());
37
- return normalizedJsonc !== getNormalizedJson(data);
38
- }
39
- catch {
40
- // File doesn't exist or is invalid
41
- return true;
42
- }
43
- }
44
27
  const rule = {
45
28
  meta: {
46
29
  type: "problem",
@@ -49,11 +32,11 @@ const rule = {
49
32
  category: "Possible Errors",
50
33
  recommended: true,
51
34
  },
52
- fixable: "code",
35
+ fixable: undefined,
53
36
  schema: [],
54
37
  messages: {
55
38
  invalidPackageJsonc: "package.jsonc is invalid.",
56
- inconsistentPackageJson: "package.json is inconsistent with package.jsonc. Run 'npx eslint package.jsonc --fix' to update it.",
39
+ inconsistentPackageJson: "package.json is inconsistent with package.jsonc.",
57
40
  },
58
41
  },
59
42
  create(context) {
@@ -98,31 +81,10 @@ const rule = {
98
81
  context.report({
99
82
  node,
100
83
  messageId: "inconsistentPackageJson",
101
- fix(fixer) {
102
- // Check if we've already fixed this file in this run
103
- if (fixedFiles.has(filename)) {
104
- return null;
105
- }
106
- // Check if fix is actually needed
107
- if (!needsFix(packageJsonPath, normalizedJsonc)) {
108
- fixedFiles.add(filename);
109
- return null;
110
- }
111
- // Apply the fix
112
- fixedFiles.add(filename);
113
- fs.writeFileSync(packageJsonPath, normalizedJsonc, "utf8");
114
- // Return null to indicate no source text changes
115
- // The actual fix happened to a different file (package.json)
116
- return null;
117
- },
118
84
  });
119
85
  },
120
86
  };
121
87
  },
122
88
  };
123
- // Export a function to clear the fixed files set (for testing)
124
- export function clearFixedFiles() {
125
- fixedFiles.clear();
126
- }
127
89
  export default rule;
128
90
  //# sourceMappingURL=package-jsonc.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"package-jsonc.js","sourceRoot":"","sources":["../../src/rules/package-jsonc.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,kBAAkB,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AACpE,OAAO,KAAK,EAAE,MAAM,SAAS,CAAC;AAC9B,OAAO,IAAI,MAAM,WAAW,CAAC;AAE7B,+EAA+E;AAC/E,MAAM,UAAU,GAAG,IAAI,GAAG,EAAU,CAAC;AAErC;;GAEG;AACH,MAAM,WAAW,GAAG,CAAC,CAAC;AAEtB;;;;;GAKG;AACH,SAAS,UAAU,CAAC,OAAe;IAC/B,MAAM,GAAG,GAAG,SAAS,CAAC,OAAO,CAAC,CAAC;IAC/B,OAAO,kBAAkB,CAAC,GAAG,CAA4B,CAAC;AAC9D,CAAC;AAED;;;;;GAKG;AACH,SAAS,iBAAiB,CAAC,MAAe;IACtC,OAAO,GAAG,IAAI,CAAC,SAAS,CAAC,MAAM,EAAE,IAAI,EAAE,WAAW,CAAC,IAAI,CAAC;AAC5D,CAAC;AAED;;;GAGG;AACH,SAAS,QAAQ,CAAC,eAAuB,EAAE,eAAuB;IAC9D,IAAI,CAAC;QACD,MAAM,OAAO,GAAG,EAAE,CAAC,YAAY,CAAC,eAAe,CAAC,CAAC;QACjD,MAAM,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,QAAQ,EAAE,CAA4B,CAAC;Q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EAAE,cAAc,CAAC,CAAC;QAE7D,OAAO;YACH,OAAO,CAAC,IAAI;gBACR,MAAM,EAAE,UAAU,EAAE,GAAG,OAAO,CAAC;gBAC/B,MAAM,YAAY,GAAG,UAAU,CAAC,OAAO,EAAE,CAAC;gBAE1C,IAAI,SAAkC,CAAC;gBACvC,IAAI,CAAC;oBACD,SAAS,GAAG,UAAU,CAAC,YAAY,CAAC,CAAC;gBACzC,CAAC;gBAAC,MAAM,CAAC;oBACL,OAAO,CAAC,MAAM,CAAC;wBACX,IAAI;wBACJ,SAAS,EAAE,qBAAqB;qBACnC,CAAC,CAAC;oBACH,OAAO;gBACX,CAAC;gBAED,MAAM,eAAe,GAAG,iBAAiB,CAAC,SAAS,CAAC,CAAC;gBAErD,iDAAiD;gBACjD,IAAI,YAAY,GAAG,KAAK,CAAC;gBACzB,IAAI,CAAC;oBACD,MAAM,kBAAkB,GAAG,EAAE,CAAC,YAAY,CAAC,eAAe,CAAC,CAAC;oBAC5D,MAAM,eAAe,GAAG,IAAI,CAAC,KAAK,CAC9B,kBAAkB,CAAC,QAAQ,EAAE,CACL,CAAC;oBAC7B,YAAY;wBACR,eAAe,KAAK,iBAAiB,CAAC,eAAe,CAAC,CAAC;gBAC/D,CAAC;gBAAC,MAAM,CAAC;oBACL,4CAA4C;gBAChD,CAAC;gBAED,IAAI,YAAY,EAAE,CAAC;oBACf,OAAO;gBACX,CAAC;gBAED,OAAO,CAAC,MAAM,CAAC;oBACX,IAAI;oBACJ,SAAS,EAAE,yBAAyB;iBACvC,CAAC,CAAC;YACP,CAAC;SACJ,CAAC;IACN,CAAC;CACJ,CAAC;AAEF,eAAe,IAAI,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-package-jsonc",
3
- "version": "2.0.1",
3
+ "version": "3.0.0",
4
4
  "description": "ESLint plugin to ensure package.json is consistent with package.jsonc",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",