diff --git a/changelog_unreleased/json/15947.md b/changelog_unreleased/json/15947.md new file mode 100644 index 000000000..59d9c68cb --- /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. + + +```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 6f7119e19..0de2c90ad 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 eade08a15..c63d6ac79 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"], diff --git a/tests/integration/__tests__/__snapshots__/support-info.js.snap b/tests/integration/__tests__/__snapshots__/support-info.js.snap index b1803945b..61c17b5dd 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 42d1649fa..3c99d1f44 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", );