Skip to content

Commit a00d31e

Browse files
authored
Support .lintstagedrc (#13081)
* Test `.lintstagedrc` JSON and YML formatting (TDD) * Support `.lintstagedrc` formatting * Update spell checking for `.lintstagedrc` * Update changelog * Update changelog
1 parent a043ac0 commit a00d31e

6 files changed

Lines changed: 35 additions & 3 deletions

File tree

changelog_unreleased/cli/13081.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#### Infer parser for `.lintstagedrc` (#13081 by @OrRosenblatt)
2+
3+
A `.lintstagedrc` file (without extension) is handled using `json` and `yaml` parsers.

cspell.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@
184184
"linearize",
185185
"linebreak",
186186
"linebreaks",
187+
"lintstaged",
188+
"lintstagedrc",
187189
"literalline",
188190
"loglevel",
189191
"lowercased",

src/language-yaml/embed.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
function embed(path, print, textToDoc, options) {
44
const node = path.getValue();
55

6-
// Try to format `.prettierrc` and `.stylelintrc` as `json` first
6+
// Try to format `.prettierrc`, `.stylelintrc`, and `.lintstagedrc` as `json` first
77
if (
88
node.type === "root" &&
99
options.filepath &&
10-
/(?:[/\\]|^)\.(?:prettier|stylelint)rc$/.test(options.filepath)
10+
/(?:[/\\]|^)\.(?:prettier|stylelint|lintstaged)rc$/.test(options.filepath)
1111
) {
1212
return textToDoc(options.originalText, { ...options, parser: "json" });
1313
}

src/language-yaml/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const languages = [
1515
...data.filenames.filter((filename) => filename !== "yarn.lock"),
1616
".prettierrc",
1717
".stylelintrc",
18+
".lintstagedrc",
1819
],
1920
})),
2021
];

tests/integration/__tests__/__snapshots__/support-info.js.snap

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,8 @@ exports[`CLI --support-info (stdout) 1`] = `
774774
"CITATION.cff",
775775
"glide.lock",
776776
".prettierrc",
777-
".stylelintrc"
777+
".stylelintrc",
778+
".lintstagedrc"
778779
],
779780
"linguistLanguageId": 407,
780781
"name": "YAML",

tests/integration/__tests__/with-parser-inference.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,29 @@ describe("infers parser from filename", () => {
7070
` + "\n"
7171
);
7272
});
73+
74+
test("json from .lintstagedrc", () => {
75+
expect(
76+
prettier.format(" { '*': 'your-cmd' } ", {
77+
filepath: "/path/to/.lintstagedrc",
78+
})
79+
).toBe('{ "*": "your-cmd" }\n');
80+
});
81+
82+
test("yaml from .lintstagedrc", () => {
83+
expect(
84+
prettier.format(
85+
/* indent */ `
86+
'*':
87+
- your-cmd
88+
`,
89+
{ filepath: "/path/to/.lintstagedrc" }
90+
)
91+
).toBe(
92+
outdent`
93+
"*":
94+
- your-cmd
95+
` + "\n"
96+
);
97+
});
7398
});

0 commit comments

Comments
 (0)