Repository: prettier/prettier (JavaScript)
Date: 2024-01-17 09:29:19+00:00
Task type: Bug Fix

Issue:
Prettier 3.2.3 incorrectly formats `.estlintrc.json`
I'll check how it's loaded.


Issue discussion:
Confirmed, we should not treat it as jsonc.
 https://github.com/eslint/eslintrc/blob/a61384731aff386a8260a80d9710c912e4f62aaa/lib/config-array-factory.js#L188-L203
Before we work on it, let's double check and make sure [these files](https://github.com/prettier/prettier/blob/6c6a332171f590a38c369673662d1a5baf765167/tests/integration/__tests__/__snapshots__/support-info.js.snap#L524-L538) are valid jsonc files. 
- [x] `.babelrc`, JSON5 https://github.com/babel/babel/blob/218faee4351345415b40b23a9e0102f628d45108/packages/babel-core/src/config/files/configuration.ts#L158
- [ ] `.devcontainer.json`
- [x] `.eslintrc.json`， JSON  https://github.com/eslint/eslintrc/blob/a61384731aff386a8260a80d9710c912e4f62aaa/lib/config-array-factory.js#L188-L203
- [x] `.jscsrc`, uses jsonlint https://github.com/jscs-dev/node-jscs/blob/38d33a0e455d965106ad3c8948c870f8f4e5dda9/lib/cli-config.js#L81
- [x] `.jshintrc`, JSON https://github.com/jshint/jshint/blob/0a5644f8f529e252e7dd0c0d54334ae435b13de0/src/cli.js#L538
- [x] `.jslintrc`, JSON https://github.com/reid/node-jslint/blob/1eefcc48116fe4fbc3b585ebd2922b3509f4f4de/lib/options.js#L31C49-L31C49
- [ ] `.swcrc`
- [x] `api-extractor.json`, it uses [jju](https://github.com/microsoft/rushstack/blob/fa6fbb1284cc4cfa4f2da6ec6c6cffc63fdc9822/libraries/node-core-library/src/JsonFile.ts#L218) not sure about the format.
- [x] `devcontainer.json` JSONC, https://github.com/devcontainers/cli/blob/e50dad80c154cdc23d4b371a2117b4ba46cc02a3/src/spec-node/featuresCLI/resolveDependencies.ts#L71C44-L71C44
- [x] `jsconfig.json`, JSONC
- [x] `language-configuration.json`, JSONC
- [x] `tsconfig.json`, JSONC
- [x] `tslint.json`, JSON https://github.com/palantir/tslint/blob/285fc1db18d1fd24680d6a2282c6445abf1566ee/src/configuration.ts#L268

Solution patch:
diff --git a/changelog_unreleased/json/15947.md b/changelog_unreleased/json/15947.md
new file mode 100644
index 000000000000..59d9c68cbbbf
--- /dev/null
+++ b/changelog_unreleased/json/15947.md
@@ -0,0 +1,14 @@
+#### Fix incorrect parser inference (#15947 by @fisker)
+
+Files like `.eslintrc.json` were incorrectly formatted as JSONC files.
+
+<!-- prettier-ignore -->
+```jsx
+// Input
+prettier --file-info .eslintrc.json
+{ "ignored": false, "inferredParser": "jsonc" }
+
+// Prettier main
+prettier --file-info .eslintrc.json
+{ "ignored": false, "inferredParser": "json" }
+```
diff --git a/cspell.json b/cspell.json
index 6f7119e195f8..0de2c90adfa5 100644
--- a/cspell.json
+++ b/cspell.json
@@ -156,6 +156,7 @@
         "jscodeshift",
         "jsesc",
         "jsfmt",
+        "jslintrc",
         "jsonata",
         "judgements",
         "kalmanb",
diff --git a/src/language-json/languages.evaluate.js b/src/language-json/languages.evaluate.js
index eade08a15e09..c63d6ac798c4 100644
--- a/src/language-json/languages.evaluate.js
+++ b/src/language-json/languages.evaluate.js
@@ -14,10 +14,26 @@ const languages = [
     parsers: ["json"],
     vscodeLanguageIds: ["json"],
     extensions: data.extensions.filter((extension) => extension !== ".jsonl"),
+    filenames: [
+      ...data.filenames,
+      ".babelrc",
+      ".jscsrc",
+      ".jshintrc",
+      ".jslintrc",
+      ".swcrc",
+    ],
   })),
   createLanguage(linguistLanguages["JSON with Comments"], () => ({
     parsers: ["jsonc"],
     vscodeLanguageIds: ["jsonc"],
+    // Too many filenames from `linguist-languages` are not a valid JSONC files.
+    // https://github.com/prettier/prettier/issues/15945#issuecomment-1895371835
+    filenames: [
+      "devcontainer.json",
+      "jsconfig.json",
+      "language-configuration.json",
+      "tsconfig.json",
+    ],
   })),
   createLanguage(linguistLanguages.JSON5, () => ({
     parsers: ["json5"],


Test patch:
diff --git a/tests/integration/__tests__/__snapshots__/support-info.js.snap b/tests/integration/__tests__/__snapshots__/support-info.js.snap
index b1803945bed9..61c17b5ddc18 100644
--- a/tests/integration/__tests__/__snapshots__/support-info.js.snap
+++ b/tests/integration/__tests__/__snapshots__/support-info.js.snap
@@ -488,7 +488,12 @@ exports[`CLI --support-info (stdout) 1`] = `
         "Pipfile.lock",
         "composer.lock",
         "flake.lock",
-        "mcmod.info"
+        "mcmod.info",
+        ".babelrc",
+        ".jscsrc",
+        ".jshintrc",
+        ".jslintrc",
+        ".swcrc"
       ],
       "linguistLanguageId": 174,
       "name": "JSON",
@@ -522,19 +527,10 @@ exports[`CLI --support-info (stdout) 1`] = `
         ".sublime_session"
       ],
       "filenames": [
-        ".babelrc",
-        ".devcontainer.json",
-        ".eslintrc.json",
-        ".jscsrc",
-        ".jshintrc",
-        ".jslintrc",
-        ".swcrc",
-        "api-extractor.json",
         "devcontainer.json",
         "jsconfig.json",
         "language-configuration.json",
-        "tsconfig.json",
-        "tslint.json"
+        "tsconfig.json"
       ],
       "group": "JSON",
       "linguistLanguageId": 423,
diff --git a/tests/integration/__tests__/with-parser-inference.js b/tests/integration/__tests__/with-parser-inference.js
index 42d1649fac16..3c99d1f444bd 100644
--- a/tests/integration/__tests__/with-parser-inference.js
+++ b/tests/integration/__tests__/with-parser-inference.js
@@ -66,8 +66,8 @@ describe("infers parser from filename", () => {
         {
           "jsc": {
             // Requires v1.2.50 or upper and requires target to be es2016 or upper.
-            "keepClassNames": false,
-          },
+            "keepClassNames": false
+          }
         }
       ` + "\n",
     );