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: allow linting the empty string from stdin (fixes #9515) #9517

Merged
merged 1 commit into from Oct 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 7 additions & 5 deletions lib/cli.js
Expand Up @@ -144,6 +144,8 @@ const cli = {

const files = currentOptions._;

const useStdin = typeof text === "string";

if (currentOptions.version) { // version from package.json

log.info(`v${require("../package.json").version}`);
Expand All @@ -153,7 +155,7 @@ const cli = {
log.error("The --print-config option must be used with exactly one file name.");
return 1;
}
if (text) {
if (useStdin) {
log.error("The --print-config option is not available for piped-in code.");
return 1;
}
Expand All @@ -164,27 +166,27 @@ const cli = {

log.info(JSON.stringify(fileConfig, null, " "));
return 0;
} else if (currentOptions.help || (!files.length && !text)) {
} else if (currentOptions.help || (!files.length && !useStdin)) {

log.info(options.generateHelp());

} else {

debug(`Running on ${text ? "text" : "files"}`);
debug(`Running on ${useStdin ? "text" : "files"}`);

if (currentOptions.fix && currentOptions.fixDryRun) {
log.error("The --fix option and the --fix-dry-run option cannot be used together.");
return 1;
}

if (text && currentOptions.fix) {
if (useStdin && currentOptions.fix) {
log.error("The --fix option is not available for piped-in code; use --fix-dry-run instead.");
return 1;
}

const engine = new CLIEngine(translateOptions(currentOptions));

const report = text ? engine.executeOnText(text, currentOptions.stdinFilename, true) : engine.executeOnFiles(files);
const report = useStdin ? engine.executeOnText(text, currentOptions.stdinFilename, true) : engine.executeOnFiles(files);

if (currentOptions.fix) {
debug("Fix mode enabled - applying fixes");
Expand Down
7 changes: 7 additions & 0 deletions tests/lib/cli.js
Expand Up @@ -101,6 +101,13 @@ describe("cli", () => {
assert.strictEqual(result, 1);
});

it("should not print debug info when passed the empty string as text", () => {
const result = cli.execute(["--stdin", "--no-eslintrc"], "");

assert.strictEqual(result, 0);
assert.isTrue(log.info.notCalled);
});

it("should return no error when --ext .js2 is specified", () => {
const filePath = getFixturePath("files");
const result = cli.execute(`--ext .js2 ${filePath}`);
Expand Down