Skip to content
Permalink
Browse files

New: Add no-error-on-unmatched-pattern flag (fixes #10587) (#12377)

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 1713d0758b083f3840d724505f997a7cb20ff384
@@ -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
@@ -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.
@@ -576,6 +576,7 @@ class CLIEngine {
cwd: options.cwd,
extensions: options.extensions,
globInputPaths: options.globInputPaths,
errorOnUnmatchedPattern: options.errorOnUnmatchedPattern,
ignore: options.ignore
});
const lintResultCache =
@@ -190,6 +190,7 @@ class FileEnumerator {
configArrayFactory = new CascadingConfigArrayFactory({ cwd }),
extensions = [".js"],
globInputPaths = true,
errorOnUnmatchedPattern = true,
ignore = true
} = {}) {
internalSlotsMap.set(this, {
@@ -208,6 +209,7 @@ class FileEnumerator {
"u"
),
globInputPaths,
errorOnUnmatchedPattern,
ignoreFlag: ignore
});
}
@@ -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];
@@ -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);
}
}
}

@@ -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
};
}

@@ -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",
@@ -0,0 +1,3 @@
var foo = "bar";
if (foo) {
foo = "bar2";
@@ -0,0 +1,5 @@
let foo = "bar";

if (foo) {
foo = "bar2";
}
@@ -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);

0 comments on commit 1713d07

Please sign in to comment.
You can’t perform that action at this time.