Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: improve error message on --print-config (fixes #11874) #11885

Merged
merged 2 commits into from Jun 25, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@
'--print-config' CLI option requires a path to a source code file rather than a directory.
mysticatea marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -3458,6 +3458,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