From a00d31e1a4f6db1e16fb7a6f48496fcfb430544d Mon Sep 17 00:00:00 2001 From: Or Rosenblatt Date: Sat, 9 Jul 2022 00:30:26 -0400 Subject: [PATCH] Support `.lintstagedrc` (#13081) * Test `.lintstagedrc` JSON and YML formatting (TDD) * Support `.lintstagedrc` formatting * Update spell checking for `.lintstagedrc` * Update changelog * Update changelog --- changelog_unreleased/cli/13081.md | 3 +++ cspell.json | 2 ++ src/language-yaml/embed.js | 4 +-- src/language-yaml/index.js | 1 + .../__snapshots__/support-info.js.snap | 3 ++- .../__tests__/with-parser-inference.js | 25 +++++++++++++++++++ 6 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 changelog_unreleased/cli/13081.md diff --git a/changelog_unreleased/cli/13081.md b/changelog_unreleased/cli/13081.md new file mode 100644 index 000000000..67acee028 --- /dev/null +++ b/changelog_unreleased/cli/13081.md @@ -0,0 +1,3 @@ +#### Infer parser for `.lintstagedrc` (#13081 by @OrRosenblatt) + +A `.lintstagedrc` file (without extension) is handled using `json` and `yaml` parsers. diff --git a/cspell.json b/cspell.json index 37f04c11c..8bd613b15 100644 --- a/cspell.json +++ b/cspell.json @@ -184,6 +184,8 @@ "linearize", "linebreak", "linebreaks", + "lintstaged", + "lintstagedrc", "literalline", "loglevel", "lowercased", diff --git a/src/language-yaml/embed.js b/src/language-yaml/embed.js index cc3e0722c..244eb07a2 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` and `.stylelintrc` as `json` first + // Try to format `.prettierrc`, `.stylelintrc`, and `.lintstagedrc` as `json` first if ( node.type === "root" && options.filepath && - /(?:[/\\]|^)\.(?:prettier|stylelint)rc$/.test(options.filepath) + /(?:[/\\]|^)\.(?:prettier|stylelint|lintstaged)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 600ed4193..0a6815457 100644 --- a/src/language-yaml/index.js +++ b/src/language-yaml/index.js @@ -15,6 +15,7 @@ const languages = [ ...data.filenames.filter((filename) => filename !== "yarn.lock"), ".prettierrc", ".stylelintrc", + ".lintstagedrc", ], })), ]; diff --git a/tests/integration/__tests__/__snapshots__/support-info.js.snap b/tests/integration/__tests__/__snapshots__/support-info.js.snap index 8e6f7d9e0..fa53d0982 100644 --- a/tests/integration/__tests__/__snapshots__/support-info.js.snap +++ b/tests/integration/__tests__/__snapshots__/support-info.js.snap @@ -774,7 +774,8 @@ exports[`CLI --support-info (stdout) 1`] = ` "CITATION.cff", "glide.lock", ".prettierrc", - ".stylelintrc" + ".stylelintrc", + ".lintstagedrc" ], "linguistLanguageId": 407, "name": "YAML", diff --git a/tests/integration/__tests__/with-parser-inference.js b/tests/integration/__tests__/with-parser-inference.js index 2e5d64527..f503eef40 100644 --- a/tests/integration/__tests__/with-parser-inference.js +++ b/tests/integration/__tests__/with-parser-inference.js @@ -70,4 +70,29 @@ describe("infers parser from filename", () => { ` + "\n" ); }); + + test("json from .lintstagedrc", () => { + expect( + prettier.format(" { '*': 'your-cmd' } ", { + filepath: "/path/to/.lintstagedrc", + }) + ).toBe('{ "*": "your-cmd" }\n'); + }); + + test("yaml from .lintstagedrc", () => { + expect( + prettier.format( + /* indent */ ` + '*': + - your-cmd + `, + { filepath: "/path/to/.lintstagedrc" } + ) + ).toBe( + outdent` + "*": + - your-cmd + ` + "\n" + ); + }); });