Skip to content

Commit

Permalink
New: Add no-error-on-unmatched-pattern flag (fixes #10587) (#12377)
Browse files Browse the repository at this point in the history
Add flag to prevent errors when a pattern or --ext is not matched
  • Loading branch information
ncraley authored and btmills committed Dec 20, 2019
1 parent 5c25a26 commit 1713d07
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 10 deletions.
5 changes: 5 additions & 0 deletions docs/user-guide/command-line-interface.md
Expand Up @@ -79,6 +79,7 @@ Caching:
Miscellaneous:
--init Run config initialization wizard - default: false
--env-info Output execution environment information - default: false
--no-error-on-unmatched-pattern Prevent errors when pattern is unmatched - default: false
--debug Output debugging information
-h, --help Show help
-v, --version Output the version number
Expand Down Expand Up @@ -451,6 +452,10 @@ The resulting configuration file will be created in the current directory.

This option outputs information about the execution environment, including the version of Node, npm, and local and global installations of ESLint. The ESLint team may ask for this information to help solve bugs.

#### `--no-error-on-unmatched-pattern`

This option prevents errors when a quoted glob pattern or `--ext` is unmatched. This will not prevent errors when your shell can't match a glob.

#### `--debug`

This option outputs debugging information to the console. This information is useful when you're seeing a problem and having a hard time pinpointing it. The ESLint team may ask for this debugging information to help solve bugs.
Expand Down
1 change: 1 addition & 0 deletions lib/cli-engine/cli-engine.js
Expand Up @@ -576,6 +576,7 @@ class CLIEngine {
cwd: options.cwd,
extensions: options.extensions,
globInputPaths: options.globInputPaths,
errorOnUnmatchedPattern: options.errorOnUnmatchedPattern,
ignore: options.ignore
});
const lintResultCache =
Expand Down
22 changes: 13 additions & 9 deletions lib/cli-engine/file-enumerator.js
Expand Up @@ -190,6 +190,7 @@ class FileEnumerator {
configArrayFactory = new CascadingConfigArrayFactory({ cwd }),
extensions = [".js"],
globInputPaths = true,
errorOnUnmatchedPattern = true,
ignore = true
} = {}) {
internalSlotsMap.set(this, {
Expand All @@ -208,6 +209,7 @@ class FileEnumerator {
"u"
),
globInputPaths,
errorOnUnmatchedPattern,
ignoreFlag: ignore
});
}
Expand All @@ -226,7 +228,7 @@ class FileEnumerator {
* @returns {IterableIterator<FileAndConfig>} The found files.
*/
*iterateFiles(patternOrPatterns) {
const { globInputPaths } = internalSlotsMap.get(this);
const { globInputPaths, errorOnUnmatchedPattern } = internalSlotsMap.get(this);
const patterns = Array.isArray(patternOrPatterns)
? patternOrPatterns
: [patternOrPatterns];
Expand Down Expand Up @@ -265,14 +267,16 @@ class FileEnumerator {
}

// Raise an error if any files were not found.
if (!foundRegardlessOfIgnored) {
throw new NoFilesFoundError(
pattern,
!globInputPaths && isGlob(pattern)
);
}
if (!found) {
throw new AllFilesIgnoredError(pattern);
if (errorOnUnmatchedPattern) {
if (!foundRegardlessOfIgnored) {
throw new NoFilesFoundError(
pattern,
!globInputPaths && isGlob(pattern)
);
}
if (!found) {
throw new AllFilesIgnoredError(pattern);
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion lib/cli.js
Expand Up @@ -68,7 +68,8 @@ function translateOptions(cliOptions) {
fixTypes: cliOptions.fixType,
allowInlineConfig: cliOptions.inlineConfig,
reportUnusedDisableDirectives: cliOptions.reportUnusedDisableDirectives,
resolvePluginsRelativeTo: cliOptions.resolvePluginsRelativeTo
resolvePluginsRelativeTo: cliOptions.resolvePluginsRelativeTo,
errorOnUnmatchedPattern: cliOptions.errorOnUnmatchedPattern
};
}

Expand Down
6 changes: 6 additions & 0 deletions lib/options.js
Expand Up @@ -230,6 +230,12 @@ module.exports = optionator({
default: "false",
description: "Output execution environment information"
},
{
option: "error-on-unmatched-pattern",
type: "Boolean",
default: "true",
description: "Prevent errors when pattern is unmatched"
},
{
option: "debug",
type: "Boolean",
Expand Down
3 changes: 3 additions & 0 deletions tests/fixtures/unmatched-patterns/failing.js
@@ -0,0 +1,3 @@
var foo = "bar";
if (foo) {
foo = "bar2";
5 changes: 5 additions & 0 deletions tests/fixtures/unmatched-patterns/passing.js2
@@ -0,0 +1,5 @@
let foo = "bar";

if (foo) {
foo = "bar2";
}
68 changes: 68 additions & 0 deletions tests/lib/cli.js
Expand Up @@ -347,6 +347,74 @@ describe("cli", () => {
});
});

describe("when executing without no-error-on-unmatched-pattern flag", () => {
it("should throw an error on unmatched glob pattern", () => {
const filePath = getFixturePath("unmatched-patterns");
const globPattern = "*.js3";

assert.throws(() => {
cli.execute(`"${filePath}/${globPattern}"`);
}, `No files matching '${filePath}/${globPattern}' were found.`);
});

it("should throw an error on unmatched --ext", () => {
const filePath = getFixturePath("unmatched-patterns");
const extension = ".js3";

assert.throws(() => {
cli.execute(`--ext ${extension} ${filePath}`);
}, `No files matching '${filePath}' were found`);
});
});

describe("when executing with no-error-on-unmatched-pattern flag", () => {
it("should not throw an error on unmatched node glob syntax patterns", () => {
const filePath = getFixturePath("unmatched-patterns");
const exit = cli.execute(`--no-error-on-unmatched-pattern "${filePath}/*.js3"`);

assert.strictEqual(exit, 0);
});

it("should not throw an error on unmatched --ext", () => {
const filePath = getFixturePath("unmatched-patterns");
const exit = cli.execute(`--no-error-on-unmatched-pattern --ext .js3 ${filePath}`);

assert.strictEqual(exit, 0);
});
});

describe("when executing with no-error-on-unmatched-pattern flag and multiple patterns", () => {
it("should not throw an error on multiple unmatched node glob syntax patterns", () => {
const filePath = getFixturePath("unmatched-patterns");
const exit = cli.execute(`--no-error-on-unmatched-pattern ${filePath}/*.js3 ${filePath}/*.js4`);

assert.strictEqual(exit, 0);
});

it("should still throw an error on when a matched pattern has lint errors", () => {
const filePath = getFixturePath("unmatched-patterns");
const exit = cli.execute(`--no-error-on-unmatched-pattern ${filePath}/*.js3 ${filePath}/*.js`);

assert.strictEqual(exit, 1);
});
});

describe("when executing with no-error-on-unmatched-pattern flag and multiple --ext arguments", () => {
it("should not throw an error on multiple unmatched --ext arguments", () => {
const filePath = getFixturePath("unmatched-patterns");
const exit = cli.execute(`--no-error-on-unmatched-pattern --ext .js3 --ext .js4 ${filePath}`);

assert.strictEqual(exit, 0);
});

it("should still throw an error on when a matched pattern has lint errors", () => {
const filePath = getFixturePath("unmatched-patterns");
const exit = cli.execute(`--no-error-on-unmatched-pattern --ext .js3 --ext .js ${filePath}`);

assert.strictEqual(exit, 1);
});
});

describe("when executing with help flag", () => {
it("should print out help", () => {
assert.strictEqual(cli.execute("-h"), 0);
Expand Down

0 comments on commit 1713d07

Please sign in to comment.