Skip to content

Commit 94b4799

Browse files
silvenonJounQin
andauthored
fix(cli): do not crash on no rules configured (#328)
Co-authored-by: JounQin <[email protected]>
1 parent cdc4a5c commit 94b4799

File tree

5 files changed

+24
-2
lines changed

5 files changed

+24
-2
lines changed

.changeset/lazy-dingos-cheat.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"eslint-config-prettier": patch
3+
---
4+
5+
fix(cli): do not crash on no rules configured

bin/cli.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,13 @@ if (module === require.main) {
6060
})
6161
)
6262
.then((configs) => {
63-
const rules = configs.flatMap(({ rules }, index) =>
64-
Object.entries(rules).map((entry) => [...entry, args[index]])
63+
const rules = configs.flatMap(
64+
(
65+
// Initializing config and rules for files that aren't included in the flat config,
66+
// which is a very unlikely scenario, but should still be treated as a success
67+
{ rules = {} } = {},
68+
index
69+
) => Object.entries(rules).map((entry) => [...entry, args[index]])
6570
);
6671
const result = processRules(rules);
6772
if (result.stderr) {

test/cli.test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
"use strict";
22

3+
const childProcess = require("node:child_process");
4+
const path = require("node:path");
5+
36
const cli = require("../bin/cli");
47

58
const offPatterns = [0, "off", [0], ["off"], ["off", "never"]];
@@ -76,6 +79,14 @@ test("no results", () => {
7679
`);
7780
});
7881

82+
test("empty config", (done) => {
83+
childProcess.exec(
84+
`node ${path.resolve("bin/cli.js")} index.js`,
85+
{ cwd: path.resolve(__dirname, "fixtures/empty-config") },
86+
done
87+
);
88+
});
89+
7990
test("conflicting options", () => {
8091
const rules = ["strict", ["curly", "multi-line"]];
8192
expect(cli.processRules(createRules(rules, "error"))).toMatchInlineSnapshot(`
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default [{}];

test/fixtures/empty-config/index.js

Whitespace-only changes.

0 commit comments

Comments
 (0)