CLI: Skip print code or write file when performance flags found (#12682)

This commit is contained in:
fisker Cheung authored and GitHub committed 2022-04-25 08:44:22 +08:00
1 parent 2ef33f143d
commit 85e6e4f73f
4 files changed
+97 -2

No files matched your search

+3
View File
@@ -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.
+30 -2
View File
@@ -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.
@@ -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: [],
});
});
@@ -0,0 +1 @@
foo( bar )