diff --git a/changelog_unreleased/cli/12682.md b/changelog_unreleased/cli/12682.md new file mode 100644 index 000000000..135d87bb8 --- /dev/null +++ b/changelog_unreleased/cli/12682.md @@ -0,0 +1,3 @@ +#### Skip print code or write file when performance flags found (#12682 by @fisker) + +When `--debug-benchmark` or `--debug-repeat` is used, the CLI will skip print the formatted code to the screen or write files. diff --git a/src/cli/format.js b/src/cli/format.js index 5e25bd4ca..5f41e5aec 100644 --- a/src/cli/format.js +++ b/src/cli/format.js @@ -103,6 +103,16 @@ function listDifferent(context, input, options, filename) { return true; } +function getPerformanceTestFlag(context) { + if (context.argv.debugBenchmark) { + return "--debug-benchmark"; + } + + if (context.argv.debugRepeat > 0) { + return "--debug-repeat"; + } +} + function format(context, input, opt) { if (!opt.parser && !opt.filepath) { throw new errors.UndefinedParserError( @@ -267,7 +277,17 @@ async function formatStdin(context) { return; } - writeOutput(context, format(context, input, options), options); + const formatted = format(context, input, options); + + const performanceTestFlag = getPerformanceTestFlag(context); + if (performanceTestFlag) { + context.logger.log( + `'${performanceTestFlag}' option found, skipped print code to screen.` + ); + return; + } + + writeOutput(context, formatted, options); } catch (error) { handleError(context, relativeFilepath || "stdin", error); } @@ -280,7 +300,7 @@ async function formatFiles(context) { let numberOfUnformattedFilesFound = 0; - if (context.argv.check) { + if (context.argv.check && !getPerformanceTestFlag(context)) { context.logger.log("Checking formatting..."); } @@ -369,6 +389,14 @@ async function formatFiles(context) { printedFilename.clear(); } + const performanceTestFlag = getPerformanceTestFlag(context); + if (performanceTestFlag) { + context.logger.log( + `'${performanceTestFlag}' option found, skipped print code or write files.` + ); + return; + } + if (context.argv.write) { // Don't write the file if it won't change in order not to invalidate // mtime based caches. diff --git a/tests/integration/__tests__/performance-flags.js b/tests/integration/__tests__/performance-flags.js new file mode 100644 index 000000000..bf6dd34e8 --- /dev/null +++ b/tests/integration/__tests__/performance-flags.js @@ -0,0 +1,63 @@ +"use strict"; + +const runPrettier = require("../run-prettier.js"); + +describe("should not write file or print code when `--debug-benchmark` or `--debug-repeat` found", () => { + // Can't test `--debug-benchmark`, since it requires `benchmark` package + runPrettier( + "cli/performance-flags", + ["--debug-repeat", "2", "--parser", "babel"], + { + input: "foo( bar )", + } + ).test({ + stderr: "", + status: 0, + stdout: "'--debug-repeat' option found, skipped print code to screen.\n", + write: [], + }); + + runPrettier("cli/performance-flags", [ + "fixture.js", + "--debug-repeat", + "2", + "--parser", + "babel", + ]).test({ + stderr: "", + status: 0, + stdout: + "'--debug-repeat' option found, skipped print code or write files.\n", + write: [], + }); + + runPrettier("cli/performance-flags", [ + "fixture.js", + "--debug-repeat", + "2", + "--parser", + "babel", + "--write", + ]).test({ + stderr: "", + status: 0, + stdout: + "'--debug-repeat' option found, skipped print code or write files.\n", + write: [], + }); + + runPrettier("cli/performance-flags", [ + "fixture.js", + "--debug-repeat", + "2", + "--parser", + "babel", + "--check", + ]).test({ + stderr: "", + status: 0, + stdout: + "'--debug-repeat' option found, skipped print code or write files.\n", + write: [], + }); +}); diff --git a/tests/integration/cli/performance-flags/fixture.js b/tests/integration/cli/performance-flags/fixture.js new file mode 100644 index 000000000..a994de17a --- /dev/null +++ b/tests/integration/cli/performance-flags/fixture.js @@ -0,0 +1 @@ +foo( bar )