diff --git a/bin/eslint.js b/bin/eslint.js index 1c41bafd245..b6b14d3a2ba 100755 --- a/bin/eslint.js +++ b/bin/eslint.js @@ -45,7 +45,7 @@ process.once("uncaughtException", err => { const pkg = require("../package.json"); console.error("\nOops! Something went wrong! :("); - console.error(`\nESLint: ${pkg.version}.\n${template(err.messageData || {})}`); + console.error(`\nESLint: ${pkg.version}.\n\n${template(err.messageData || {})}`); } else { console.error(err.stack); diff --git a/lib/cli-engine/cli-engine.js b/lib/cli-engine/cli-engine.js index d298898c0d7..81e1b07c83e 100644 --- a/lib/cli-engine/cli-engine.js +++ b/lib/cli-engine/cli-engine.js @@ -904,6 +904,13 @@ class CLIEngine { const { configArrayFactory, options } = internalSlotsMap.get(this); const absolutePath = path.resolve(options.cwd, filePath); + if (directoryExists(absolutePath)) { + throw Object.assign( + new Error("'filePath' should not be a directory path."), + { messageTemplate: "print-config-with-directory-path" } + ); + } + return configArrayFactory .getConfigArrayForFile(absolutePath) .extractConfig(absolutePath) diff --git a/messages/print-config-with-directory-path.txt b/messages/print-config-with-directory-path.txt new file mode 100644 index 00000000000..1afc9b1e88b --- /dev/null +++ b/messages/print-config-with-directory-path.txt @@ -0,0 +1,2 @@ +The '--print-config' CLI option requires a path to a source code file rather than a directory. +See also: https://eslint.org/docs/user-guide/command-line-interface#--print-config diff --git a/tests/lib/cli-engine/cli-engine.js b/tests/lib/cli-engine/cli-engine.js index cb9221bbf80..9e54fc7c227 100644 --- a/tests/lib/cli-engine/cli-engine.js +++ b/tests/lib/cli-engine/cli-engine.js @@ -3459,6 +3459,17 @@ describe("CLIEngine", () => { assert.deepStrictEqual(actualConfig, expectedConfig); }); + it("should throw an error if a directory path was given.", () => { + const engine = new CLIEngine(); + + try { + engine.getConfigForFile("."); + } catch (error) { + assert.strictEqual(error.messageTemplate, "print-config-with-directory-path"); + return; + } + assert.fail("should throw an error"); + }); }); describe("isPathIgnored", () => { diff --git a/tests/lib/cli.js b/tests/lib/cli.js index 0e24b143649..baae75b53cb 100644 --- a/tests/lib/cli.js +++ b/tests/lib/cli.js @@ -1084,7 +1084,7 @@ describe("cli", () => { describe("when passing --print-config", () => { it("should print out the configuration", () => { - const filePath = getFixturePath("files"); + const filePath = getFixturePath("xxxx"); const exitCode = cli.execute(`--print-config ${filePath}`);