Skip to content

Commit

Permalink
Fix: Change error message for implicit file ignore (fixes eslint#12348)
Browse files Browse the repository at this point in the history
  • Loading branch information
snhardin committed Nov 25, 2019
1 parent af95154 commit 9f44018
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 5 deletions.
36 changes: 32 additions & 4 deletions docs/user-guide/configuring.md
Expand Up @@ -1032,12 +1032,25 @@ Of particular note is that like `.gitignore` files, all paths used as patterns f

Please see `.gitignore`'s specification for further examples of valid syntax.

In addition to any patterns in a `.eslintignore` file, ESLint always ignores files in `/node_modules/*` and `/bower_components/*`.
In addition to any patterns in the `.eslintignore` file, ESLint always follows a couple implicit ignore rules even if the `--no-ignore` flag is passed. The implicit rules are as follows:

For example, placing the following `.eslintignore` file in the current working directory will ignore all of `node_modules`, `bower_components` in the project root and anything in the `build/` directory except `build/index.js`:
* `node_modules/` as well as `bower_components/` directories are ignored.
* Dotfiles as well as Dotfolders and their contents are ignored.

There are also some exceptions to these rules:

* If the path to lint is a glob pattern or directory path and contains a Dotfolder, all Dotfiles and Dotfolders will be linted. This includes subDotfiles and subDotfolders that are buried deeper in the directory structure.

For example, `eslint .config/` will lint all Dotfolders and Dotfiles in the `.config` directory, including immediate children as well as children that are deeper in the directory structure.

* If the path to lint is a specific file path and the `--no-ignore` flag has been passed, ESLint will lint the file regardless of the implicit ignore rules.

For example, `eslint .config/my-config-file.js --no-ignore` will cause `my-config-file.js` to be linted. It should be noted that the same command without the `--no-ignore` line will not lint the `my-config-file.js` file.

Another example: placing the following `.eslintignore` file in the current working directory will ignore the `.git`, `node_modules`, and `bower_components` directories in the project root and anything in the `build/` directory, except `build/index.js`:

```text
# /node_modules/* and /bower_components/* in the project root are ignored by default
# All .git/* Dotfiles as well as /node_modules/* and /bower_components/* in the project root are ignored by default
# Ignore built files except build/index.js
build/*
Expand Down Expand Up @@ -1090,13 +1103,28 @@ You'll see this warning:

```text
foo.js
0:0 warning File ignored because of your .eslintignore file. Use --no-ignore to override.
0:0 warning File ignored because of a matching ignore pattern. Use "--no-ignore" to override.
✖ 1 problem (0 errors, 1 warning)
```

This message occurs because ESLint is unsure if you wanted to actually lint the file or not. As the message indicates, you can use `--no-ignore` to omit using the ignore rules.

Consider another scenario where you may want to run ESLint on a specific Dotfile or Dotfolder, but have forgotten to specifically allow those files in your `.eslintignore` file. You would run something like this:

eslint .config/foo.js

You would see this warning:

```text
.config/foo.js
0:0 warning File ignored by default. Use a negated ignore pattern (like "--ignore-pattern '!<relative/path/to/filename>'") to override
✖ 1 problem (0 errors, 1 warning)
```

This messages occurs because normally, this file would be ignored by ESLint's implicit ignore rules (as mentioned above). A negated ignore rule in your `.eslintignore` file would override the implicit rule and reinclude this file for linting. Additionally, in this specific case, `--no-ignore` could be used to lint the file as well.

## Personal Configuration File (deprecated)

⚠️ **This feature has been deprecated**. ESLint will print deprecation warnings beginning with the 7.0.0 release. This feature will then be removed in the 8.0.0 release. If you want to continue to use personal configuration files, please use the [`--config` CLI option](https://eslint.org/docs/user-guide/command-line-interface#-c---config). For more information regarding this decision, please see [RFC 28](https://github.com/eslint/rfcs/pull/28) and [RFC 32](https://github.com/eslint/rfcs/pull/32).
Expand Down
3 changes: 2 additions & 1 deletion lib/cli-engine/cli-engine.js
Expand Up @@ -278,7 +278,8 @@ function verifyText({
*/
function createIgnoreResult(filePath, baseDir) {
let message;
const isHidden = /^\./u.test(path.basename(filePath));
const isHidden = filePath.split(path.sep)
.find(directory => /^\./u.test(directory));
const isInNodeModules = baseDir && path.relative(baseDir, filePath).startsWith("node_modules");
const isInBowerComponents = baseDir && path.relative(baseDir, filePath).startsWith("bower_components");

Expand Down
21 changes: 21 additions & 0 deletions tests/lib/cli-engine/cli-engine.js
Expand Up @@ -1005,6 +1005,27 @@ describe("CLIEngine", () => {
assert.strictEqual(report.results[0].messages[0].message, expectedMsg);
});

// https://github.com/eslint/eslint/issues/12348
it("should not check files within a .hidden folder if they are passed explicitly without the --no-ignore flag", () => {
engine = new CLIEngine({
cwd: getFixturePath("cli-engine"),
useEslintrc: false,
rules: {
quotes: [2, "single"]
}
});

const report = engine.executeOnFiles(["hidden/.hiddenfolder/double-quotes.js"]);
const expectedMsg = "File ignored by default. Use a negated ignore pattern (like \"--ignore-pattern '!<relative/path/to/filename>'\") to override.";

assert.strictEqual(report.results.length, 1);
assert.strictEqual(report.results[0].errorCount, 0);
assert.strictEqual(report.results[0].warningCount, 1);
assert.strictEqual(report.results[0].fixableErrorCount, 0);
assert.strictEqual(report.results[0].fixableWarningCount, 0);
assert.strictEqual(report.results[0].messages[0].message, expectedMsg);
});

it("should check .hidden files if they are passed explicitly with --no-ignore flag", () => {

engine = new CLIEngine({
Expand Down

0 comments on commit 9f44018

Please sign in to comment.