diff --git a/src/cli/util.js b/src/cli/util.js index cb9767822..8bcf4506f 100644 --- a/src/cli/util.js +++ b/src/cli/util.js @@ -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"); } } @@ -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); @@ -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; } @@ -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); } @@ -410,7 +412,7 @@ function formatFiles(context) { } if (fileIgnored) { - writeOutput({ formatted: input }, options); + writeOutput(context, { formatted: input }, options); return; } @@ -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); } }); } diff --git a/tests_integration/__tests__/__snapshots__/debug-check.js.snap b/tests_integration/__tests__/__snapshots__/debug-check.js.snap index 0f63c4825..4231b4bc9 100644 --- a/tests_integration/__tests__/__snapshots__/debug-check.js.snap +++ b/tests_integration/__tests__/__snapshots__/debug-check.js.snap @@ -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: diff --git a/tests_integration/__tests__/debug-check.js b/tests_integration/__tests__/debug-check.js index 9e02eb208..d0641ee64 100644 --- a/tests_integration/__tests__/debug-check.js +++ b/tests_integration/__tests__/debug-check.js @@ -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 + }); +}); diff --git a/tests_integration/cli/debug-check/issue-4599.js b/tests_integration/cli/debug-check/issue-4599.js new file mode 100644 index 000000000..aa92ee1f9 --- /dev/null +++ b/tests_integration/cli/debug-check/issue-4599.js @@ -0,0 +1 @@ +console.log("…");