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(cli): regression for --debug-check + --list-different #4600

Merged
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
22 changes: 12 additions & 10 deletions src/cli/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,13 @@ function logFileInfoOrDie(context) {
);
}

function writeOutput(result, options) {
function writeOutput(context, result, options) {
// Don't use `console.log` here since it adds an extra newline at the end.
process.stdout.write(result.formatted);
process.stdout.write(
context.argv["debug-check"] ? result.filepath : result.formatted
);

if (options.cursorOffset >= 0) {
if (options && options.cursorOffset >= 0) {
process.stderr.write(result.cursorOffset + "\n");
}
}
Expand Down Expand Up @@ -195,7 +197,7 @@ function format(context, input, opt) {
);
}
}
return { formatted: opt.filepath || "(stdin)\n" };
return { formatted: pp, filepath: opt.filepath || "(stdin)\n" };
}

return prettier.formatWithCursor(input, opt);
Expand Down Expand Up @@ -308,7 +310,7 @@ function formatStdin(context) {

thirdParty.getStream(process.stdin).then(input => {
if (relativeFilepath && ignorer.filter([relativeFilepath]).length === 0) {
writeOutput({ formatted: input }, {});
writeOutput(context, { formatted: input });
return;
}

Expand All @@ -319,7 +321,7 @@ function formatStdin(context) {
return;
}

writeOutput(format(context, input, options), options);
writeOutput(context, format(context, input, options), options);
} catch (error) {
handleError(context, "stdin", error);
}
Expand Down Expand Up @@ -410,7 +412,7 @@ function formatFiles(context) {
}

if (fileIgnored) {
writeOutput({ formatted: input }, options);
writeOutput(context, { formatted: input }, options);
return;
}

Expand Down Expand Up @@ -465,13 +467,13 @@ function formatFiles(context) {
context.logger.log(`${chalk.grey(filename)} ${Date.now() - start}ms`);
}
} else if (context.argv["debug-check"]) {
if (output) {
context.logger.log(output);
if (result.filepath) {
context.logger.log(result.filepath);
} else {
process.exitCode = 2;
}
} else if (!context.argv["list-different"]) {
writeOutput(result, options);
writeOutput(context, result, options);
}
});
}
Expand Down
9 changes: 9 additions & 0 deletions tests_integration/__tests__/__snapshots__/debug-check.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ exports[`checks stdin with --debug-check (write) 1`] = `Array []`;

exports[`doesn't crash when --debug-check is passed (write) 1`] = `Array []`;

exports[`should not exit non-zero for already prettified code with --debug-check + --list-different (stderr) 1`] = `""`;

exports[`should not exit non-zero for already prettified code with --debug-check + --list-different (stdout) 1`] = `
"issue-4599.js
"
`;

exports[`should not exit non-zero for already prettified code with --debug-check + --list-different (write) 1`] = `Array []`;

exports[`show diff for 2+ error files with --debug-check (stderr) 1`] = `
"[error] a.debug-check: prettier(input) !== prettier(prettier(input))
[error] Index:
Expand Down
10 changes: 10 additions & 0 deletions tests_integration/__tests__/debug-check.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,13 @@ describe("show diff for 2+ error files with --debug-check", () => {
status: "non-zero"
});
});

describe("should not exit non-zero for already prettified code with --debug-check + --list-different", () => {
runPrettier("cli/debug-check", [
"issue-4599.js",
"--debug-check",
"--list-different"
]).test({
status: 0
});
});
1 change: 1 addition & 0 deletions tests_integration/cli/debug-check/issue-4599.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log("…");