diff --git a/changelog_unreleased/cli/10924.md b/changelog_unreleased/cli/10924.md new file mode 100644 index 000000000000..22486a31d107 --- /dev/null +++ b/changelog_unreleased/cli/10924.md @@ -0,0 +1,3 @@ +#### Infer parser for `.stylelintrc` (#10924 by @SevenOutman) + +A `.stylelintrc` file (without extension) is handled using `json` and `yaml` parsers. diff --git a/src/language-yaml/embed.js b/src/language-yaml/embed.js index cf246b8e02d8..cc3e0722c67d 100644 --- a/src/language-yaml/embed.js +++ b/src/language-yaml/embed.js @@ -3,11 +3,11 @@ function embed(path, print, textToDoc, options) { const node = path.getValue(); - // Try to format `.prettierrc` as `json` first + // Try to format `.prettierrc` and `.stylelintrc` as `json` first if ( node.type === "root" && options.filepath && - /(?:[/\\]|^)\.prettierrc$/.test(options.filepath) + /(?:[/\\]|^)\.(?:prettier|stylelint)rc$/.test(options.filepath) ) { return textToDoc(options.originalText, { ...options, parser: "json" }); } diff --git a/src/language-yaml/index.js b/src/language-yaml/index.js index f406e04e57a3..600ed41939eb 100644 --- a/src/language-yaml/index.js +++ b/src/language-yaml/index.js @@ -14,6 +14,7 @@ const languages = [ filenames: [ ...data.filenames.filter((filename) => filename !== "yarn.lock"), ".prettierrc", + ".stylelintrc", ], })), ]; diff --git a/tests/integration/__tests__/__snapshots__/support-info.js.snap b/tests/integration/__tests__/__snapshots__/support-info.js.snap index 550ac7eec464..7d2c25bc5d36 100644 --- a/tests/integration/__tests__/__snapshots__/support-info.js.snap +++ b/tests/integration/__tests__/__snapshots__/support-info.js.snap @@ -743,7 +743,8 @@ exports[`CLI --support-info (stdout) 1`] = ` \\".clang-tidy\\", \\".gemrc\\", \\"glide.lock\\", - \\".prettierrc\\" + \\".prettierrc\\", + \\".stylelintrc\\" ], \\"linguistLanguageId\\": 407, \\"name\\": \\"YAML\\", diff --git a/tests/integration/__tests__/with-parser-inference.js b/tests/integration/__tests__/with-parser-inference.js index 4e0c0cb3f732..ad200e6a883f 100644 --- a/tests/integration/__tests__/with-parser-inference.js +++ b/tests/integration/__tests__/with-parser-inference.js @@ -28,6 +28,18 @@ describe("infers parser from filename", () => { ).toEqual("{}\n"); }); + test("json from .stylelintrc", () => { + expect( + prettier.format(" { } ", { filepath: "x/y/.stylelintrc" }) + ).toEqual("{}\n"); + }); + + test("yaml from .stylelintrc", () => { + expect( + prettier.format(" extends: '' ", { filepath: "x/y/.stylelintrc" }) + ).toEqual('extends: ""\n'); + }); + test("babel from Jakefile", () => { expect( prettier.format("let foo = ( x = 1 ) => x", { filepath: "x/y/Jakefile" })