diff --git a/changelog_unreleased/cli/12561.md b/changelog_unreleased/cli/12561.md new file mode 100644 index 000000000..625656768 --- /dev/null +++ b/changelog_unreleased/cli/12561.md @@ -0,0 +1,21 @@ +#### Print the number of files need change (#12561 by @Harry-Hopkinson) + +```console +# Prettier stable +$ prettier src --check +Checking formatting... +[warn] src/fileA.js +[warn] Code style issues found in the above file(s). Forgot to run Prettier? + +# Prettier main +$ prettier src --check +Checking formatting... +[warn] src/fileA.js +[warn] src/fileB.js +[warn] Code style issues found in 2 files. Forgot to run Prettier? + +$ prettier src --check +Checking formatting... +[warn] src/fileA.js +[warn] Code style issues found in the above file. Forgot to run Prettier? +``` diff --git a/docs/cli.md b/docs/cli.md index 49535d52a..f72895ebd 100644 --- a/docs/cli.md +++ b/docs/cli.md @@ -69,7 +69,7 @@ Console output if some of the files require re-formatting: Checking formatting... [warn] src/fileA.js [warn] src/fileB.js -[warn] Code style issues found in the above file(s). Forgot to run Prettier? +[warn] Code style issues found in 2 files. Forgot to run Prettier? ``` The command will return exit code `1` in the second case, which is helpful inside the CI pipelines. diff --git a/src/cli/format.js b/src/cli/format.js index bfff5e2cd..5e25bd4ca 100644 --- a/src/cli/format.js +++ b/src/cli/format.js @@ -417,11 +417,21 @@ async function formatFiles(context) { if (context.argv.check) { if (numberOfUnformattedFilesFound === 0) { context.logger.log("All matched files use Prettier code style!"); + } else if (numberOfUnformattedFilesFound === 1) { + context.logger.warn( + context.argv.write + ? "Code style issues fixed in the above file." + : "Code style issues found in the above file. Forgot to run Prettier?" + ); } else { context.logger.warn( context.argv.write - ? "Code style issues fixed in the above file(s)." - : "Code style issues found in the above file(s). Forgot to run Prettier?" + ? "Code style issues found in " + + numberOfUnformattedFilesFound + + " files." + : "Code style issues found in " + + numberOfUnformattedFilesFound + + " files. Forgot to run Prettier?" ); } } diff --git a/tests/integration/__tests__/__snapshots__/check.js.snap b/tests/integration/__tests__/__snapshots__/check.js.snap index f8d19510b..b5c69122a 100644 --- a/tests/integration/__tests__/__snapshots__/check.js.snap +++ b/tests/integration/__tests__/__snapshots__/check.js.snap @@ -1,14 +1,28 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`--checks should print the number of files that need formatting (stderr) 1`] = ` +"[warn] unformatted.js +[warn] unformatted2.js +[warn] Code style issues found in 2 files. Forgot to run Prettier? +" +`; + +exports[`--checks should print the number of files that need formatting (stdout) 1`] = ` +"Checking formatting... +" +`; + +exports[`--checks should print the number of files that need formatting (write) 1`] = `Array []`; + exports[`--checks works in CI just as in a non-TTY mode (stderr) 1`] = ` "[warn] unformatted.js -[warn] Code style issues found in the above file(s). Forgot to run Prettier? +[warn] Code style issues found in the above file. Forgot to run Prettier? " `; exports[`--checks works in CI just as in a non-TTY mode (stderr) 2`] = ` "[warn] unformatted.js -[warn] Code style issues found in the above file(s). Forgot to run Prettier? +[warn] Code style issues found in the above file. Forgot to run Prettier? " `; diff --git a/tests/integration/__tests__/__snapshots__/ignore-unknown.js.snap b/tests/integration/__tests__/__snapshots__/ignore-unknown.js.snap index 3d563676b..7fe86de9f 100644 --- a/tests/integration/__tests__/__snapshots__/ignore-unknown.js.snap +++ b/tests/integration/__tests__/__snapshots__/ignore-unknown.js.snap @@ -31,7 +31,7 @@ exports[`ignore-unknown alias (stdout) 1`] = ` exports[`ignore-unknown check (stderr) 1`] = ` "[warn] javascript.js -[warn] Code style issues found in the above file(s). Forgot to run Prettier? +[warn] Code style issues found in the above file. Forgot to run Prettier? " `; diff --git a/tests/integration/__tests__/__snapshots__/infer-parser.js.snap b/tests/integration/__tests__/__snapshots__/infer-parser.js.snap index 75e8fc1f6..21c86bbf1 100644 --- a/tests/integration/__tests__/__snapshots__/infer-parser.js.snap +++ b/tests/integration/__tests__/__snapshots__/infer-parser.js.snap @@ -3,7 +3,7 @@ exports[`--check with unknown path and no parser multiple files (stderr) 1`] = ` "[error] No parser could be inferred for file: FOO [warn] foo.js -[warn] Code style issues found in the above file(s). Forgot to run Prettier? +[warn] Code style issues found in the above file. Forgot to run Prettier? " `; @@ -36,7 +36,7 @@ exports[`--list-different with unknown path and no parser specific file (stderr) exports[`--write and --check with unknown path and no parser multiple files (stderr) 1`] = ` "[error] No parser could be inferred for file: FOO [warn] foo.js -[warn] Code style issues fixed in the above file(s). +[warn] Code style issues fixed in the above file. " `; diff --git a/tests/integration/__tests__/__snapshots__/piped-output.js.snap b/tests/integration/__tests__/__snapshots__/piped-output.js.snap index abeed4482..5ac545abd 100644 --- a/tests/integration/__tests__/__snapshots__/piped-output.js.snap +++ b/tests/integration/__tests__/__snapshots__/piped-output.js.snap @@ -34,13 +34,13 @@ exports[`no file diffs with --list-different + formatted file (write) 2`] = `Arr exports[`output with --check + unformatted differs when piped (stderr) 1`] = ` "[warn] unformatted.js -[warn] Code style issues fixed in the above file(s). +[warn] Code style issues fixed in the above file. " `; exports[`output with --check + unformatted differs when piped (stderr) 2`] = ` "[warn] unformatted.js -[warn] Code style issues fixed in the above file(s). +[warn] Code style issues fixed in the above file. " `; diff --git a/tests/integration/__tests__/check.js b/tests/integration/__tests__/check.js index 76b4acab5..183c48e8e 100644 --- a/tests/integration/__tests__/check.js +++ b/tests/integration/__tests__/check.js @@ -48,3 +48,11 @@ describe("--checks works in CI just as in a non-TTY mode", () => { expect(await result0.stdout).toEqual(await result1.stdout); }); }); + +describe("--checks should print the number of files that need formatting", () => { + runPrettier("cli/write", ["--check", "unformatted.js", "unformatted2.js"], { + input: "0", + }).test({ + status: 1, + }); +}); diff --git a/tests/integration/cli/write/unformatted2.js b/tests/integration/cli/write/unformatted2.js new file mode 100644 index 000000000..e0b797789 --- /dev/null +++ b/tests/integration/cli/write/unformatted2.js @@ -0,0 +1 @@ +var x = 1; diff --git a/website/versioned_docs/version-stable/cli.md b/website/versioned_docs/version-stable/cli.md index 9b92b425e..4c37ec8e3 100644 --- a/website/versioned_docs/version-stable/cli.md +++ b/website/versioned_docs/version-stable/cli.md @@ -70,7 +70,7 @@ Console output if some of the files require re-formatting: Checking formatting... [warn] src/fileA.js [warn] src/fileB.js -[warn] Code style issues found in the above file(s). Forgot to run Prettier? +[warn] Code style issues found in 2 files. Forgot to run Prettier? ``` The command will return exit code `1` in the second case, which is helpful inside the CI pipelines.