From c0f6f2da8b3422c65c9ab1f41c70e30e240bb5d0 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Tue, 16 May 2023 20:40:18 +0800 Subject: [PATCH] Clear filename before print ignored file code to screen (#14794) --- changelog_unreleased/cli/14794.md | 15 ++++++++++++++ src/cli/format.js | 7 +++---- .../line-after-filepath-with-errors.js.snap | 18 +++++++++++++---- .../__snapshots__/piped-output.js.snap | 20 +++++++++++++++---- .../__snapshots__/print-code.js.snap | 14 +++++++++++++ tests/integration/__tests__/print-code.js | 8 ++++++++ tests/integration/cli-worker.js | 13 ++++++++++++ .../cli/print-code/.prettierignore | 1 + tests/integration/cli/print-code/ignored.js | 2 ++ .../integration/cli/print-code/not-ignored.js | 2 ++ 10 files changed, 88 insertions(+), 12 deletions(-) create mode 100644 changelog_unreleased/cli/14794.md create mode 100644 tests/integration/__tests__/__snapshots__/print-code.js.snap create mode 100644 tests/integration/__tests__/print-code.js create mode 100644 tests/integration/cli/print-code/.prettierignore create mode 100644 tests/integration/cli/print-code/ignored.js create mode 100644 tests/integration/cli/print-code/not-ignored.js diff --git a/changelog_unreleased/cli/14794.md b/changelog_unreleased/cli/14794.md new file mode 100644 index 000000000..c82d9bd8f --- /dev/null +++ b/changelog_unreleased/cli/14794.md @@ -0,0 +1,15 @@ +#### Clear filename before print ignored file code to screen (#14794 by @fisker) + + +```sh +# Input +echo test.js > .prettierignore +echo code > test.js +prettier ./test.js + +# Prettier stable +test.jscode + +# Prettier main +code +``` diff --git a/src/cli/format.js b/src/cli/format.js index ca4809518..d589a17bb 100644 --- a/src/cli/format.js +++ b/src/cli/format.js @@ -370,6 +370,7 @@ async function formatFiles(context) { } if (isFileIgnored) { + printedFilename?.clear(); writeOutput(context, { formatted: input }, options); continue; } @@ -399,10 +400,8 @@ async function formatFiles(context) { const isDifferent = output !== input; let shouldSetCache = !isDifferent; - if (printedFilename) { - // Remove previously printed filename to log it with duration. - printedFilename.clear(); - } + // Remove previously printed filename to log it with duration. + printedFilename?.clear(); if (performanceTestFlag) { context.logger.log( diff --git a/tests/integration/__tests__/__snapshots__/line-after-filepath-with-errors.js.snap b/tests/integration/__tests__/__snapshots__/line-after-filepath-with-errors.js.snap index 1571cd02c..f571b2fe4 100644 --- a/tests/integration/__tests__/__snapshots__/line-after-filepath-with-errors.js.snap +++ b/tests/integration/__tests__/__snapshots__/line-after-filepath-with-errors.js.snap @@ -52,14 +52,18 @@ exports[`Line breaking after filepath with errors (stdout) 1`] = ` "invalid-1.js invalid-2.js invalid-2.unknown -valid-1.jsfunction foo() {}" +valid-1.js +[[called readline.clearLine(process.stdout)]] +function foo() {}" `; exports[`Line breaking after filepath with errors (stdout) 2`] = ` "invalid-1.js invalid-2.js invalid-2.unknown -valid-1.js" +valid-1.js +[[called readline.clearLine(process.stdout)]] +" `; exports[`Line breaking after filepath with errors (stdout) 3`] = ` @@ -67,13 +71,19 @@ exports[`Line breaking after filepath with errors (stdout) 3`] = ` invalid-1.js invalid-2.js invalid-2.unknown -valid-1.jsAll matched files use Prettier code style!" +valid-1.js +[[called readline.clearLine(process.stdout)]] +All matched files use Prettier code style!" `; exports[`Line breaking after filepath with errors (stdout) 4`] = ` "invalid-1.js invalid-2.js -invalid-2.unknownvalid-1.jsvalid-1.js 0ms" +invalid-2.unknown +[[called readline.clearLine(process.stdout)]] +valid-1.js +[[called readline.clearLine(process.stdout)]] +valid-1.js 0ms" `; exports[`Line breaking after filepath with errors (write) 1`] = `[]`; diff --git a/tests/integration/__tests__/__snapshots__/piped-output.js.snap b/tests/integration/__tests__/__snapshots__/piped-output.js.snap index 860df516f..2f1fd39f5 100644 --- a/tests/integration/__tests__/__snapshots__/piped-output.js.snap +++ b/tests/integration/__tests__/__snapshots__/piped-output.js.snap @@ -6,7 +6,9 @@ exports[`no file diffs with --check + formatted file (stderr) 2`] = `""`; exports[`no file diffs with --check + formatted file (stdout) 1`] = ` "Checking formatting... -formatted.jsAll matched files use Prettier code style!" +formatted.js +[[called readline.clearLine(process.stdout)]] +All matched files use Prettier code style!" `; exports[`no file diffs with --check + formatted file (stdout) 2`] = ` @@ -22,7 +24,11 @@ exports[`no file diffs with --list-different + formatted file (stderr) 1`] = `"" exports[`no file diffs with --list-different + formatted file (stderr) 2`] = `""`; -exports[`no file diffs with --list-different + formatted file (stdout) 1`] = `"formatted.js"`; +exports[`no file diffs with --list-different + formatted file (stdout) 1`] = ` +"formatted.js +[[called readline.clearLine(process.stdout)]] +" +`; exports[`no file diffs with --list-different + formatted file (stdout) 2`] = `""`; @@ -42,7 +48,9 @@ exports[`output with --check + unformatted differs when piped (stderr) 2`] = ` exports[`output with --check + unformatted differs when piped (stdout) 1`] = ` "Checking formatting... -unformatted.js" +unformatted.js +[[called readline.clearLine(process.stdout)]] +" `; exports[`output with --check + unformatted differs when piped (stdout) 2`] = `"Checking formatting..."`; @@ -71,7 +79,11 @@ exports[`output with --list-different + unformatted differs when piped (stderr) exports[`output with --list-different + unformatted differs when piped (stderr) 2`] = `""`; -exports[`output with --list-different + unformatted differs when piped (stdout) 1`] = `"unformatted.jsunformatted.js"`; +exports[`output with --list-different + unformatted differs when piped (stdout) 1`] = ` +"unformatted.js +[[called readline.clearLine(process.stdout)]] +unformatted.js" +`; exports[`output with --list-different + unformatted differs when piped (stdout) 2`] = `"unformatted.js"`; diff --git a/tests/integration/__tests__/__snapshots__/print-code.js.snap b/tests/integration/__tests__/__snapshots__/print-code.js.snap new file mode 100644 index 000000000..2740292b5 --- /dev/null +++ b/tests/integration/__tests__/__snapshots__/print-code.js.snap @@ -0,0 +1,14 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Line breaking after filepath with errors (stdout) 1`] = ` +"ignored.js +[[called readline.clearLine(process.stdout)]] +foo( +)" +`; + +exports[`Line breaking after filepath with errors (stdout) 2`] = ` +"not-ignored.js +[[called readline.clearLine(process.stdout)]] +foo();" +`; diff --git a/tests/integration/__tests__/print-code.js b/tests/integration/__tests__/print-code.js new file mode 100644 index 000000000..f29741a3a --- /dev/null +++ b/tests/integration/__tests__/print-code.js @@ -0,0 +1,8 @@ +describe("Line breaking after filepath with errors", () => { + runCli("cli/print-code", ["./ignored.js"], { + stdoutIsTTY: true, + }).test({ status: 0, write: [], stderr: "" }); + runCli("cli/print-code", ["./not-ignored.js"], { + stdoutIsTTY: true, + }).test({ status: 0, write: [], stderr: "" }); +}); diff --git a/tests/integration/cli-worker.js b/tests/integration/cli-worker.js index b31b01175..a01409215 100644 --- a/tests/integration/cli-worker.js +++ b/tests/integration/cli-worker.js @@ -2,6 +2,7 @@ import { workerData, parentPort } from "node:worker_threads"; import fs from "node:fs"; import path from "node:path"; import url from "node:url"; +import readline from "node:readline"; import { cosmiconfig } from "cosmiconfig"; import { prettierCli, mockable as mockableModuleFile } from "./env.js"; @@ -40,6 +41,18 @@ async function run() { : filename ); + readline.clearLine = (stream) => { + stream.write( + `\n[[called readline.clearLine(${ + stream === process.stdout + ? "process.stdout" + : stream === process.stderr + ? "process.stderr" + : "unknown stream" + })]]\n` + ); + }; + process.stdin.isTTY = Boolean(options.isTTY); process.stdout.isTTY = Boolean(options.stdoutIsTTY); diff --git a/tests/integration/cli/print-code/.prettierignore b/tests/integration/cli/print-code/.prettierignore new file mode 100644 index 000000000..1615b0c68 --- /dev/null +++ b/tests/integration/cli/print-code/.prettierignore @@ -0,0 +1 @@ +ignored.js diff --git a/tests/integration/cli/print-code/ignored.js b/tests/integration/cli/print-code/ignored.js new file mode 100644 index 000000000..d495fd9e7 --- /dev/null +++ b/tests/integration/cli/print-code/ignored.js @@ -0,0 +1,2 @@ +foo( +) diff --git a/tests/integration/cli/print-code/not-ignored.js b/tests/integration/cli/print-code/not-ignored.js new file mode 100644 index 000000000..d495fd9e7 --- /dev/null +++ b/tests/integration/cli/print-code/not-ignored.js @@ -0,0 +1,2 @@ +foo( +)