Skip to content

Commit

Permalink
CLI: Add --ignore-unknown (#8829)
Browse files Browse the repository at this point in the history
Co-authored-by: Georgii Dolzhykov <thorn.mailbox@gmail.com>
Co-authored-by: sosukesuzuki <aosukeke@gmail.com>
  • Loading branch information
3 people committed Aug 21, 2020
1 parent 7bf1803 commit e3d1f0d
Show file tree
Hide file tree
Showing 19 changed files with 212 additions and 18 deletions.
14 changes: 1 addition & 13 deletions .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\
)$"
14 changes: 14 additions & 0 deletions 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!
```
8 changes: 8 additions & 0 deletions docs/cli.md
Expand Up @@ -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
```
4 changes: 3 additions & 1 deletion docs/install.md
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion docs/precommit.md
Expand Up @@ -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 \"{}\"' ."
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions docs/watching-files.md
Expand Up @@ -6,15 +6,15 @@ 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`:

```json
{
"scripts": {
"prettier-watch": "onchange '**/*.js' -- prettier --write {{changed}}"
"prettier-watch": "onchange \"**/*\" -- prettier --write --ignore-unknown {{changed}}"
}
}
```
5 changes: 5 additions & 0 deletions src/cli/constant.js
Expand Up @@ -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,
Expand Down
5 changes: 4 additions & 1 deletion src/cli/util.js
Expand Up @@ -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;
}
Expand Down
2 changes: 2 additions & 0 deletions tests_integration/__tests__/__snapshots__/early-exit.js.snap
Expand Up @@ -151,6 +151,7 @@ Other options:
* inferredParser (string | null) - name of parser inferred from file path
-h, --help <flag> 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 <silent|error|warn|log|debug>
Expand Down Expand Up @@ -310,6 +311,7 @@ Other options:
* inferredParser (string | null) - name of parser inferred from file path
-h, --help <flag> 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 <silent|error|warn|log|debug>
Expand Down
11 changes: 11 additions & 0 deletions tests_integration/__tests__/__snapshots__/help-options.js.snap
Expand Up @@ -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`] = `
Expand Down
69 changes: 69 additions & 0 deletions 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",
},
]
`;
77 changes: 77 additions & 0 deletions 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,
});
});
1 change: 1 addition & 0 deletions tests_integration/cli/ignore-unknown/.prettierignore
@@ -0,0 +1 @@
ignored.js
10 changes: 10 additions & 0 deletions tests_integration/cli/ignore-unknown/.prettierrc
@@ -0,0 +1,10 @@
{
"overrides": [
{
"files": "*.as-js-file",
"options": {
"parser": "babel"
}
}
]
}
Empty file.
1 change: 1 addition & 0 deletions tests_integration/cli/ignore-unknown/javascript.js
@@ -0,0 +1 @@
const foo= "bar";
1 change: 1 addition & 0 deletions tests_integration/cli/ignore-unknown/override.as-js-file
@@ -0,0 +1 @@
const foo= "bar";
@@ -0,0 +1 @@
PRETTIER
1 change: 1 addition & 0 deletions tests_integration/cli/ignore-unknown/unkown-filename
@@ -0,0 +1 @@
PRETTIER

0 comments on commit e3d1f0d

Please sign in to comment.