From e3d1f0dee0389aed9a0db247eff7e5f39d3fdd5b Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Fri, 21 Aug 2020 09:11:59 +0800 Subject: [PATCH] CLI: Add `--ignore-unknown` (#8829) Co-authored-by: Georgii Dolzhykov Co-authored-by: sosukesuzuki --- .pre-commit-hooks.yaml | 14 +--- changelog_unreleased/cli/pr-8829.md | 14 ++++ docs/cli.md | 8 ++ docs/install.md | 4 +- docs/precommit.md | 2 +- docs/watching-files.md | 4 +- src/cli/constant.js | 5 ++ src/cli/util.js | 5 +- .../__snapshots__/early-exit.js.snap | 2 + .../__snapshots__/help-options.js.snap | 11 +++ .../__snapshots__/ignore-unknown.js.snap | 69 +++++++++++++++++ tests_integration/__tests__/ignore-unknown.js | 77 +++++++++++++++++++ .../cli/ignore-unknown/.prettierignore | 1 + .../cli/ignore-unknown/.prettierrc | 10 +++ .../cli/ignore-unknown/ignored.js | 0 .../cli/ignore-unknown/javascript.js | 1 + .../cli/ignore-unknown/override.as-js-file | 1 + .../unkown-file-extension.unknown | 1 + .../cli/ignore-unknown/unkown-filename | 1 + 19 files changed, 212 insertions(+), 18 deletions(-) create mode 100644 changelog_unreleased/cli/pr-8829.md create mode 100644 tests_integration/__tests__/__snapshots__/ignore-unknown.js.snap create mode 100644 tests_integration/__tests__/ignore-unknown.js create mode 100644 tests_integration/cli/ignore-unknown/.prettierignore create mode 100644 tests_integration/cli/ignore-unknown/.prettierrc create mode 100644 tests_integration/cli/ignore-unknown/ignored.js create mode 100644 tests_integration/cli/ignore-unknown/javascript.js create mode 100644 tests_integration/cli/ignore-unknown/override.as-js-file create mode 100644 tests_integration/cli/ignore-unknown/unkown-file-extension.unknown create mode 100644 tests_integration/cli/ignore-unknown/unkown-filename diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml index 83519e34a..ac74494a2 100644 --- a/.pre-commit-hooks.yaml +++ b/.pre-commit-hooks.yaml @@ -1,16 +1,4 @@ - id: prettier name: prettier - entry: prettier --write --list-different + entry: prettier --write --list-different --ignore-unknown language: node - files: "\\.(\ - css|less|scss\ - |graphql|gql\ - |html\ - |js|jsx\ - |json\ - |md|markdown|mdown|mkdn\ - |mdx\ - |ts|tsx\ - |vue\ - |yaml|yml\ - )$" diff --git a/changelog_unreleased/cli/pr-8829.md b/changelog_unreleased/cli/pr-8829.md new file mode 100644 index 000000000..1d81c3806 --- /dev/null +++ b/changelog_unreleased/cli/pr-8829.md @@ -0,0 +1,14 @@ +#### Added `--ignore-unknown`(alias `-u`) flag ([#8829](https://github.com/prettier/prettier/pull/8829) by [@fisker](https://github.com/fisker)) + +```console +# Prettier stable +npx prettier * --check +Checking formatting... +foo.unknown[error] No parser could be inferred for file: foo.unknown +All matched files use Prettier code style! + +# Prettier master +npx prettier * --check --ignore-unknown +Checking formatting... +All matched files use Prettier code style! +``` diff --git a/docs/cli.md b/docs/cli.md index 896b0dfcf..2e88f0fb9 100644 --- a/docs/cli.md +++ b/docs/cli.md @@ -204,3 +204,11 @@ $ cat abc.css | prettier --stdin-filepath abc.css display: none; } ``` + +## `--ignore-unknown` + +With `--ignore-unknown` (or `-u`), prettier will ignore unknown files matched by patterns. + +```console +$ prettier "**/*" --write --ignore-unknown +``` diff --git a/docs/install.md b/docs/install.md index 377662987..7305e0819 100644 --- a/docs/install.md +++ b/docs/install.md @@ -103,11 +103,13 @@ For example, you can add the following to your `package.json` to have ESLint and } }, "lint-staged": { - "**/*": ["eslint --fix", "prettier --write"] + "**/*": "prettier --write --ignore-unknown" } } ``` +> Note: If you use ESLint, make sure lint-staged runs it before Prettier, not after. + See [Pre-commit Hook](precommit.md) for more information. ## Summary diff --git a/docs/precommit.md b/docs/precommit.md index 123d7279f..24627270f 100644 --- a/docs/precommit.md +++ b/docs/precommit.md @@ -103,7 +103,7 @@ and add this config to your `package.json`: { "husky": { "hooks": { - "pre-commit": "git-format-staged -f 'prettier --stdin --stdin-filepath \"{}\"' ." + "pre-commit": "git-format-staged -f 'prettier --ignore-unknown --stdin --stdin-filepath \"{}\"' ." } } } diff --git a/docs/watching-files.md b/docs/watching-files.md index fa11f5153..b4cb93d70 100644 --- a/docs/watching-files.md +++ b/docs/watching-files.md @@ -6,7 +6,7 @@ title: Watching For Changes You can have Prettier watch for changes from the command line by using [onchange](https://www.npmjs.com/package/onchange). For example: ```bash -npx onchange '**/*.js' -- npx prettier --write {{changed}} +npx onchange "**/*" -- npx prettier --write --ignore-unknown {{changed}} ``` Or add the following to your `package.json`: @@ -14,7 +14,7 @@ Or add the following to your `package.json`: ```json { "scripts": { - "prettier-watch": "onchange '**/*.js' -- prettier --write {{changed}}" + "prettier-watch": "onchange \"**/*\" -- prettier --write --ignore-unknown {{changed}}" } } ``` diff --git a/src/cli/constant.js b/src/cli/constant.js index 79c4d871f..ff4321d9b 100644 --- a/src/cli/constant.js +++ b/src/cli/constant.js @@ -174,6 +174,11 @@ const options = { default: ".prettierignore", description: "Path to a file with patterns describing files to ignore.", }, + "ignore-unknown": { + type: "boolean", + alias: "u", + description: "Ignore unknown files.", + }, "list-different": { type: "boolean", category: coreOptions.CATEGORY_OUTPUT, diff --git a/src/cli/util.js b/src/cli/util.js index 9d42588cc..3065e69e0 100644 --- a/src/cli/util.js +++ b/src/cli/util.js @@ -61,10 +61,13 @@ function diff(a, b) { function handleError(context, filename, error) { if (error instanceof errors.UndefinedParserError) { - if (context.argv.write && isTTY()) { + if ((context.argv.write || context.argv["ignore-unknown"]) && isTTY()) { readline.clearLine(process.stdout, 0); readline.cursorTo(process.stdout, 0, null); } + if (context.argv["ignore-unknown"]) { + return; + } if (!context.argv.check && !context.argv["list-different"]) { process.exitCode = 2; } diff --git a/tests_integration/__tests__/__snapshots__/early-exit.js.snap b/tests_integration/__tests__/__snapshots__/early-exit.js.snap index babcd5b54..9036ee217 100644 --- a/tests_integration/__tests__/__snapshots__/early-exit.js.snap +++ b/tests_integration/__tests__/__snapshots__/early-exit.js.snap @@ -151,6 +151,7 @@ Other options: * inferredParser (string | null) - name of parser inferred from file path -h, --help Show CLI usage, or details about the given flag. Example: --help write + -u, --ignore-unknown Ignore unknown files. --insert-pragma Insert @format pragma into file's first docblock comment. Defaults to false. --loglevel @@ -310,6 +311,7 @@ Other options: * inferredParser (string | null) - name of parser inferred from file path -h, --help Show CLI usage, or details about the given flag. Example: --help write + -u, --ignore-unknown Ignore unknown files. --insert-pragma Insert @format pragma into file's first docblock comment. Defaults to false. --loglevel diff --git a/tests_integration/__tests__/__snapshots__/help-options.js.snap b/tests_integration/__tests__/__snapshots__/help-options.js.snap index b58a598ec..a7db8cca2 100644 --- a/tests_integration/__tests__/__snapshots__/help-options.js.snap +++ b/tests_integration/__tests__/__snapshots__/help-options.js.snap @@ -221,6 +221,17 @@ Default: .prettierignore exports[`show detailed usage with --help ignore-path (write) 1`] = `Array []`; +exports[`show detailed usage with --help ignore-unknown (stderr) 1`] = `""`; + +exports[`show detailed usage with --help ignore-unknown (stdout) 1`] = ` +"-u, --ignore-unknown + + Ignore unknown files. +" +`; + +exports[`show detailed usage with --help ignore-unknown (write) 1`] = `Array []`; + exports[`show detailed usage with --help insert-pragma (stderr) 1`] = `""`; exports[`show detailed usage with --help insert-pragma (stdout) 1`] = ` diff --git a/tests_integration/__tests__/__snapshots__/ignore-unknown.js.snap b/tests_integration/__tests__/__snapshots__/ignore-unknown.js.snap new file mode 100644 index 000000000..3d563676b --- /dev/null +++ b/tests_integration/__tests__/__snapshots__/ignore-unknown.js.snap @@ -0,0 +1,69 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Ignored file (stderr) 1`] = `""`; + +exports[`Ignored file (stdout) 1`] = `""`; + +exports[`Ignored file (write) 1`] = `Array []`; + +exports[`None exist file (stderr) 1`] = ` +"[error] No files matching the pattern were found: \\"non-exist-file\\". +" +`; + +exports[`None exist file (stdout) 1`] = `""`; + +exports[`None exist file (write) 1`] = `Array []`; + +exports[`Not matching pattern (stderr) 1`] = ` +"[error] No files matching the pattern were found: \\"*.non-exist-pattern\\". +" +`; + +exports[`Not matching pattern (stdout) 1`] = `""`; + +exports[`Not matching pattern (write) 1`] = `Array []`; + +exports[`ignore-unknown alias (stdout) 1`] = ` +"javascript.js +" +`; + +exports[`ignore-unknown check (stderr) 1`] = ` +"[warn] javascript.js +[warn] Code style issues found in the above file(s). Forgot to run Prettier? +" +`; + +exports[`ignore-unknown check (stdout) 1`] = ` +"Checking formatting... +" +`; + +exports[`ignore-unknown check (write) 1`] = `Array []`; + +exports[`ignore-unknown dir (stdout) 1`] = ` +"javascript.js +" +`; + +exports[`ignore-unknown pattern (stdout) 1`] = ` +"javascript.js +override.as-js-file +" +`; + +exports[`ignore-unknown write (stdout) 1`] = ` +"javascript.js +" +`; + +exports[`ignore-unknown write (write) 1`] = ` +Array [ + Object { + "content": "const foo = \\"bar\\"; +", + "filename": "javascript.js", + }, +] +`; diff --git a/tests_integration/__tests__/ignore-unknown.js b/tests_integration/__tests__/ignore-unknown.js new file mode 100644 index 000000000..01b5581e2 --- /dev/null +++ b/tests_integration/__tests__/ignore-unknown.js @@ -0,0 +1,77 @@ +"use strict"; + +const runPrettier = require("../runPrettier"); + +describe("ignore-unknown dir", () => { + runPrettier("cli/ignore-unknown", [ + ".", + "--ignore-unknown", + "--list-different", + ]).test({ + status: "non-zero", + stderr: "", + write: [], + }); +}); + +describe("ignore-unknown alias", () => { + runPrettier("cli/ignore-unknown", [".", "-u", "--list-different"]).test({ + status: "non-zero", + stderr: "", + write: [], + }); +}); + +describe("ignore-unknown pattern", () => { + runPrettier("cli/ignore-unknown", [ + "*", + "--ignore-unknown", + "--list-different", + ]).test({ + status: "non-zero", + stderr: "", + write: [], + }); +}); + +describe("ignore-unknown write", () => { + runPrettier("cli/ignore-unknown", [ + ".", + "--ignore-unknown", + "--write", + "--list-different", + ]).test({ + status: 0, + stderr: "", + }); +}); + +describe("ignore-unknown check", () => { + runPrettier("cli/ignore-unknown", [".", "--ignore-unknown", "--check"]).test({ + status: 1, + }); +}); + +describe("None exist file", () => { + runPrettier("cli/ignore-unknown", [ + "non-exist-file", + "--ignore-unknown", + ]).test({ + status: 2, + }); +}); + +describe("Not matching pattern", () => { + runPrettier("cli/ignore-unknown", [ + "*.non-exist-pattern", + "--ignore-unknown", + ]).test({ + status: 2, + }); +}); + +describe("Ignored file", () => { + runPrettier("cli/ignore-unknown", ["ignored.js", "--ignore-unknown"]).test({ + status: 0, + }); +}); diff --git a/tests_integration/cli/ignore-unknown/.prettierignore b/tests_integration/cli/ignore-unknown/.prettierignore new file mode 100644 index 000000000..1615b0c68 --- /dev/null +++ b/tests_integration/cli/ignore-unknown/.prettierignore @@ -0,0 +1 @@ +ignored.js diff --git a/tests_integration/cli/ignore-unknown/.prettierrc b/tests_integration/cli/ignore-unknown/.prettierrc new file mode 100644 index 000000000..df113efb1 --- /dev/null +++ b/tests_integration/cli/ignore-unknown/.prettierrc @@ -0,0 +1,10 @@ +{ + "overrides": [ + { + "files": "*.as-js-file", + "options": { + "parser": "babel" + } + } + ] +} diff --git a/tests_integration/cli/ignore-unknown/ignored.js b/tests_integration/cli/ignore-unknown/ignored.js new file mode 100644 index 000000000..e69de29bb diff --git a/tests_integration/cli/ignore-unknown/javascript.js b/tests_integration/cli/ignore-unknown/javascript.js new file mode 100644 index 000000000..b02987937 --- /dev/null +++ b/tests_integration/cli/ignore-unknown/javascript.js @@ -0,0 +1 @@ +const foo= "bar"; diff --git a/tests_integration/cli/ignore-unknown/override.as-js-file b/tests_integration/cli/ignore-unknown/override.as-js-file new file mode 100644 index 000000000..b02987937 --- /dev/null +++ b/tests_integration/cli/ignore-unknown/override.as-js-file @@ -0,0 +1 @@ +const foo= "bar"; diff --git a/tests_integration/cli/ignore-unknown/unkown-file-extension.unknown b/tests_integration/cli/ignore-unknown/unkown-file-extension.unknown new file mode 100644 index 000000000..e2358dcb5 --- /dev/null +++ b/tests_integration/cli/ignore-unknown/unkown-file-extension.unknown @@ -0,0 +1 @@ +PRETTIER diff --git a/tests_integration/cli/ignore-unknown/unkown-filename b/tests_integration/cli/ignore-unknown/unkown-filename new file mode 100644 index 000000000..e2358dcb5 --- /dev/null +++ b/tests_integration/cli/ignore-unknown/unkown-filename @@ -0,0 +1 @@ +PRETTIER