Skip to content

Commit

Permalink
Fix: improve error message on --print-config (fixes #11874) (#11885)
Browse files Browse the repository at this point in the history
  • Loading branch information
mysticatea authored and not-an-aardvark committed Jun 25, 2019
1 parent 056c2aa commit 13f0418
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 2 deletions.
2 changes: 1 addition & 1 deletion bin/eslint.js
Expand Up @@ -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);
Expand Down
7 changes: 7 additions & 0 deletions lib/cli-engine/cli-engine.js
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions 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
11 changes: 11 additions & 0 deletions tests/lib/cli-engine/cli-engine.js
Expand Up @@ -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", () => {
Expand Down
2 changes: 1 addition & 1 deletion tests/lib/cli.js
Expand Up @@ -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}`);

Expand Down

0 comments on commit 13f0418

Please sign in to comment.