Add CLI option to prevent errors on unmatched patterns (#10058)

This commit is contained in:
Daron McIntosh authored and GitHub committed 2021-01-28 00:30:16 +02:00
1 parent 53b59d2d39
commit 3d9f102301
12 files changed
+134 -2

No files matched your search

+1
View File
@@ -23,3 +23,4 @@ package-lock.json
.pnp.*
.nyc_output
/scripts/tools/eslint-plugin-prettier-internal-rules/node_modules
.devcontainer
+16
View File
@@ -0,0 +1,16 @@
#### Add CLI option to prevent errors on unmatched pattern (#8233 by @daronmcintosh)
The following command will not throw an error when it doesn't find a YAML file.
```sh
// Prettier stable
$ npx prettier --check "prettier/docs/*.yaml"
Checking formatting...
[error] No files matching the pattern were found: "prettier/docs/*.yaml".
All matched files use Prettier code style!
// Prettier master
$ npx prettier --check --no-error-on-unmatched-pattern "prettier/docs/*.yaml"
Checking formatting...
All matched files use Prettier code style!
```
+4
View File
@@ -212,3 +212,7 @@ With `--ignore-unknown` (or `-u`), prettier will ignore unknown files matched by
```console
$ prettier "**/*" --write --ignore-unknown
```
## `--no-error-on-unmatched-pattern`
Prevent errors when pattern is unmatched.
+4
View File
@@ -148,6 +148,10 @@ const options = {
"Don't take .editorconfig into account when parsing configuration.",
default: true,
},
"error-on-unmatched-pattern": {
type: "boolean",
oppositeDescription: "Prevent errors when pattern is unmatched.",
},
"find-config-path": {
type: "path",
category: coreOptions.CATEGORY_CONFIG,
+4 -2
View File
@@ -33,7 +33,7 @@ function* expandPatterns(context) {
yield relativePath;
}
if (noResults) {
if (noResults && context.argv["error-on-unmatched-pattern"] !== false) {
// If there was no files and no other errors, let's yield a general error.
yield {
error: `No matching files. Patterns: ${context.filePatterns.join(" ")}`,
@@ -116,7 +116,9 @@ function* expandPatternsInternal(context) {
}
if (result.length === 0) {
yield { error: `${errorMessages.emptyResults[type]}: "${input}".` };
if (context.argv["error-on-unmatched-pattern"] !== false) {
yield { error: `${errorMessages.emptyResults[type]}: "${input}".` };
}
} else {
yield* sortPaths(result);
}
@@ -146,6 +146,8 @@ Editor options:
Other options:
--no-color Do not colorize error messages.
--no-error-on-unmatched-pattern
Prevent errors when pattern is unmatched.
--file-info <path> Extract the following info (as JSON) for a given file path. Reported fields:
* ignored (boolean) - true if file path is filtered by --ignore-path
* inferredParser (string | null) - name of parser inferred from file path
@@ -308,6 +310,8 @@ Editor options:
Other options:
--no-color Do not colorize error messages.
--no-error-on-unmatched-pattern
Prevent errors when pattern is unmatched.
--file-info <path> Extract the following info (as JSON) for a given file path. Reported fields:
* ignored (boolean) - true if file path is filtered by --ignore-path
* inferredParser (string | null) - name of parser inferred from file path
@@ -0,0 +1,48 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`error on unmatched pattern (stderr) 1`] = `
"[error] No files matching the pattern were found: \\"**/*.toml\\".
"
`;
exports[`error on unmatched pattern (stdout) 1`] = `""`;
exports[`error on unmatched pattern (write) 1`] = `Array []`;
exports[`error on unmatched pattern when 2nd glob has no match (stderr) 1`] = `
"[error] No files matching the pattern were found: \\"**/*.toml\\".
"
`;
exports[`error on unmatched pattern when 2nd glob has no match (stdout) 1`] = `
"const hello = \\"there\\";
{
\\"hello\\": \\"there\\"
}
hello: there
"
`;
exports[`error on unmatched pattern when 2nd glob has no match (write) 1`] = `Array []`;
exports[`no error on unmatched pattern (stderr) 1`] = `""`;
exports[`no error on unmatched pattern (stdout) 1`] = `
"const hello = \\"there\\";
"
`;
exports[`no error on unmatched pattern (write) 1`] = `Array []`;
exports[`no error on unmatched pattern when 2nd glob has no match (stderr) 1`] = `""`;
exports[`no error on unmatched pattern when 2nd glob has no match (stdout) 1`] = `
"const hello = \\"there\\";
{
\\"hello\\": \\"there\\"
}
hello: there
"
`;
exports[`no error on unmatched pattern when 2nd glob has no match (write) 1`] = `Array []`;
@@ -347,6 +347,17 @@ exports[`show detailed usage with --help no-editorconfig (stdout) 1`] = `
exports[`show detailed usage with --help no-editorconfig (write) 1`] = `Array []`;
exports[`show detailed usage with --help no-error-on-unmatched-pattern (stderr) 1`] = `""`;
exports[`show detailed usage with --help no-error-on-unmatched-pattern (stdout) 1`] = `
"--no-error-on-unmatched-pattern
Prevent errors when pattern is unmatched.
"
`;
exports[`show detailed usage with --help no-error-on-unmatched-pattern (write) 1`] = `Array []`;
exports[`show detailed usage with --help no-semi (stderr) 1`] = `""`;
exports[`show detailed usage with --help no-semi (stdout) 1`] = `
@@ -0,0 +1,37 @@
"use strict";
const runPrettier = require("../runPrettier");
describe("no error on unmatched pattern", () => {
runPrettier("cli/error-on-unmatched-pattern", [
"--no-error-on-unmatched-pattern",
"**/*.js",
]).test({
status: 0,
});
});
describe("error on unmatched pattern", () => {
runPrettier("cli/error-on-unmatched-pattern", ["**/*.toml"]).test({
status: 2,
});
});
describe("no error on unmatched pattern when 2nd glob has no match", () => {
runPrettier("cli/error-on-unmatched-pattern", [
"--no-error-on-unmatched-pattern",
"**/*.{json,js,yml}",
"**/*.toml",
]).test({
status: 0,
});
});
describe("error on unmatched pattern when 2nd glob has no match", () => {
runPrettier("cli/error-on-unmatched-pattern", [
"**/*.{json,js,yml}",
"**/*.toml",
]).test({
status: 2,
});
});
@@ -0,0 +1 @@
const hello = "there";
@@ -0,0 +1,3 @@
{
"hello": "there"
}
@@ -0,0 +1 @@
hello: there