CLI: Add --ignore-unknown (#8829)

Co-authored-by: Georgii Dolzhykov <thorn.mailbox@gmail.com>
Co-authored-by: sosukesuzuki <aosukeke@gmail.com>
This commit is contained in:
authored and GitHub committed 2020-08-21 09:11:59 +08:00
1 parent 7bf1803a11
commit e3d1f0dee0
19 files changed
+212 -18

No files matched your search

+1 -13
View File
@@ -1,16 +1,4 @@
- id: prettier - id: prettier
name: prettier name: prettier
entry: prettier --write --list-different entry: prettier --write --list-different --ignore-unknown
language: node language: node
files: "\\.(\
css|less|scss\
|graphql|gql\
|html\
|js|jsx\
|json\
|md|markdown|mdown|mkdn\
|mdx\
|ts|tsx\
|vue\
|yaml|yml\
)$"
+14
View File
@@ -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!
```
+8
View File
@@ -204,3 +204,11 @@ $ cat abc.css | prettier --stdin-filepath abc.css
display: none; display: none;
} }
``` ```
## `--ignore-unknown`
With `--ignore-unknown` (or `-u`), prettier will ignore unknown files matched by patterns.
```console
$ prettier "**/*" --write --ignore-unknown
```
+3 -1
View File
@@ -103,11 +103,13 @@ For example, you can add the following to your `package.json` to have ESLint and
} }
}, },
"lint-staged": { "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. See [Pre-commit Hook](precommit.md) for more information.
## Summary ## Summary
+1 -1
View File
@@ -103,7 +103,7 @@ and add this config to your `package.json`:
{ {
"husky": { "husky": {
"hooks": { "hooks": {
"pre-commit": "git-format-staged -f 'prettier --stdin --stdin-filepath \"{}\"' ." "pre-commit": "git-format-staged -f 'prettier --ignore-unknown --stdin --stdin-filepath \"{}\"' ."
} }
} }
} }
+2 -2
View File
@@ -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: You can have Prettier watch for changes from the command line by using [onchange](https://www.npmjs.com/package/onchange). For example:
```bash ```bash
npx onchange '**/*.js' -- npx prettier --write {{changed}} npx onchange "**/*" -- npx prettier --write --ignore-unknown {{changed}}
``` ```
Or add the following to your `package.json`: Or add the following to your `package.json`:
@@ -14,7 +14,7 @@ Or add the following to your `package.json`:
```json ```json
{ {
"scripts": { "scripts": {
"prettier-watch": "onchange '**/*.js' -- prettier --write {{changed}}" "prettier-watch": "onchange \"**/*\" -- prettier --write --ignore-unknown {{changed}}"
} }
} }
``` ```
+5
View File
@@ -174,6 +174,11 @@ const options = {
default: ".prettierignore", default: ".prettierignore",
description: "Path to a file with patterns describing files to ignore.", description: "Path to a file with patterns describing files to ignore.",
}, },
"ignore-unknown": {
type: "boolean",
alias: "u",
description: "Ignore unknown files.",
},
"list-different": { "list-different": {
type: "boolean", type: "boolean",
category: coreOptions.CATEGORY_OUTPUT, category: coreOptions.CATEGORY_OUTPUT,
+4 -1
View File
@@ -61,10 +61,13 @@ function diff(a, b) {
function handleError(context, filename, error) { function handleError(context, filename, error) {
if (error instanceof errors.UndefinedParserError) { 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.clearLine(process.stdout, 0);
readline.cursorTo(process.stdout, 0, null); readline.cursorTo(process.stdout, 0, null);
} }
if (context.argv["ignore-unknown"]) {
return;
}
if (!context.argv.check && !context.argv["list-different"]) { if (!context.argv.check && !context.argv["list-different"]) {
process.exitCode = 2; process.exitCode = 2;
} }
@@ -151,6 +151,7 @@ Other options:
* inferredParser (string | null) - name of parser inferred from file path * inferredParser (string | null) - name of parser inferred from file path
-h, --help <flag> Show CLI usage, or details about the given flag. -h, --help <flag> Show CLI usage, or details about the given flag.
Example: --help write Example: --help write
-u, --ignore-unknown Ignore unknown files.
--insert-pragma Insert @format pragma into file's first docblock comment. --insert-pragma Insert @format pragma into file's first docblock comment.
Defaults to false. Defaults to false.
--loglevel <silent|error|warn|log|debug> --loglevel <silent|error|warn|log|debug>
@@ -310,6 +311,7 @@ Other options:
* inferredParser (string | null) - name of parser inferred from file path * inferredParser (string | null) - name of parser inferred from file path
-h, --help <flag> Show CLI usage, or details about the given flag. -h, --help <flag> Show CLI usage, or details about the given flag.
Example: --help write Example: --help write
-u, --ignore-unknown Ignore unknown files.
--insert-pragma Insert @format pragma into file's first docblock comment. --insert-pragma Insert @format pragma into file's first docblock comment.
Defaults to false. Defaults to false.
--loglevel <silent|error|warn|log|debug> --loglevel <silent|error|warn|log|debug>
@@ -221,6 +221,17 @@ Default: .prettierignore
exports[`show detailed usage with --help ignore-path (write) 1`] = `Array []`; 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 (stderr) 1`] = `""`;
exports[`show detailed usage with --help insert-pragma (stdout) 1`] = ` exports[`show detailed usage with --help insert-pragma (stdout) 1`] = `
@@ -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",
},
]
`;
@@ -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,
});
});
@@ -0,0 +1 @@
ignored.js
@@ -0,0 +1,10 @@
{
"overrides": [
{
"files": "*.as-js-file",
"options": {
"parser": "babel"
}
}
]
}
Whitespace-only changes.
@@ -0,0 +1 @@
const foo= "bar";
@@ -0,0 +1 @@
const foo= "bar";
@@ -0,0 +1 @@
PRETTIER
@@ -0,0 +1 @@
PRETTIER