Skip to content

Commit 13f0418

Browse files
mysticateanot-an-aardvark
authored andcommitted
Fix: improve error message on --print-config (fixes #11874) (#11885)
1 parent 056c2aa commit 13f0418

File tree

5 files changed

+22
-2
lines changed

5 files changed

+22
-2
lines changed

bin/eslint.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ process.once("uncaughtException", err => {
4545
const pkg = require("../package.json");
4646

4747
console.error("\nOops! Something went wrong! :(");
48-
console.error(`\nESLint: ${pkg.version}.\n${template(err.messageData || {})}`);
48+
console.error(`\nESLint: ${pkg.version}.\n\n${template(err.messageData || {})}`);
4949
} else {
5050

5151
console.error(err.stack);

lib/cli-engine/cli-engine.js

+7
Original file line numberDiff line numberDiff line change
@@ -904,6 +904,13 @@ class CLIEngine {
904904
const { configArrayFactory, options } = internalSlotsMap.get(this);
905905
const absolutePath = path.resolve(options.cwd, filePath);
906906

907+
if (directoryExists(absolutePath)) {
908+
throw Object.assign(
909+
new Error("'filePath' should not be a directory path."),
910+
{ messageTemplate: "print-config-with-directory-path" }
911+
);
912+
}
913+
907914
return configArrayFactory
908915
.getConfigArrayForFile(absolutePath)
909916
.extractConfig(absolutePath)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
The '--print-config' CLI option requires a path to a source code file rather than a directory.
2+
See also: https://eslint.org/docs/user-guide/command-line-interface#--print-config

tests/lib/cli-engine/cli-engine.js

+11
Original file line numberDiff line numberDiff line change
@@ -3459,6 +3459,17 @@ describe("CLIEngine", () => {
34593459
assert.deepStrictEqual(actualConfig, expectedConfig);
34603460
});
34613461

3462+
it("should throw an error if a directory path was given.", () => {
3463+
const engine = new CLIEngine();
3464+
3465+
try {
3466+
engine.getConfigForFile(".");
3467+
} catch (error) {
3468+
assert.strictEqual(error.messageTemplate, "print-config-with-directory-path");
3469+
return;
3470+
}
3471+
assert.fail("should throw an error");
3472+
});
34623473
});
34633474

34643475
describe("isPathIgnored", () => {

tests/lib/cli.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1084,7 +1084,7 @@ describe("cli", () => {
10841084

10851085
describe("when passing --print-config", () => {
10861086
it("should print out the configuration", () => {
1087-
const filePath = getFixturePath("files");
1087+
const filePath = getFixturePath("xxxx");
10881088

10891089
const exitCode = cli.execute(`--print-config ${filePath}`);
10901090

0 commit comments

Comments
 (0)