Skip to content

Commit

Permalink
Breaking: change default ignore pattern (refs eslint/rfcs#51) (#12888)
Browse files Browse the repository at this point in the history
  • Loading branch information
mysticatea authored Feb 14, 2020
1 parent bfe1dc4 commit cf38d0d
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 47 deletions.
8 changes: 3 additions & 5 deletions docs/user-guide/configuring.md
Original file line number Diff line number Diff line change
Expand Up @@ -1061,20 +1061,18 @@ 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 a `.eslintignore` file, ESLint always ignores files in `/**/node_modules/*`.

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`:
For example, placing the following `.eslintignore` file in the current working directory will ignore all of `node_modules/*` and anything in the `build/` directory except `build/index.js`:

```text
# /node_modules/* and /bower_components/* in the project root are ignored by default
# node_modules/* are ignored by default
# Ignore built files except build/index.js
build/*
!build/index.js
```

**Important**: Note that `node_modules` directories in, for example, a `packages` directory in a mono repo are *not* ignored by default and need to be added to `.eslintignore` explicitly.

### Using an Alternate File

If you'd prefer to use a different file than the `.eslintignore` in the current working directory, you can specify it on the command line using the `--ignore-path` option. For example, you can use `.jshintignore` file because it has the same format:
Expand Down
3 changes: 0 additions & 3 deletions lib/cli-engine/cli-engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,14 +278,11 @@ function createIgnoreResult(filePath, baseDir) {
let message;
const isHidden = /^\./u.test(path.basename(filePath));
const isInNodeModules = baseDir && path.relative(baseDir, filePath).startsWith("node_modules");
const isInBowerComponents = baseDir && path.relative(baseDir, filePath).startsWith("bower_components");

if (isHidden) {
message = "File ignored by default. Use a negated ignore pattern (like \"--ignore-pattern '!<relative/path/to/filename>'\") to override.";
} else if (isInNodeModules) {
message = "File ignored by default. Use \"--ignore-pattern '!node_modules/*'\" to override.";
} else if (isInBowerComponents) {
message = "File ignored by default. Use \"--ignore-pattern '!bower_components/*'\" to override.";
} else {
message = "File ignored because of a matching ignore pattern. Use \"--no-ignore\" to override.";
}
Expand Down
4 changes: 2 additions & 2 deletions lib/cli-engine/config-array/ignore-pattern.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ function dirSuffix(filePath) {
return isDir ? "/" : "";
}

const DefaultPatterns = Object.freeze(["/node_modules/*", "/bower_components/*"]);
const DotPatterns = Object.freeze([".*", "!../"]);
const DefaultPatterns = Object.freeze(["/**/node_modules/*"]);
const DotPatterns = Object.freeze([".*", "!.eslintrc.*", "!../"]);

//------------------------------------------------------------------------------
// Public
Expand Down
66 changes: 29 additions & 37 deletions tests/lib/cli-engine/cli-engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -3796,24 +3796,16 @@ describe("CLIEngine", () => {
const cwd = getFixturePath("ignored-paths");
const engine = new CLIEngine({ cwd });

assert(engine.isPathIgnored(getFixturePath("ignored-paths", "bower_components/package/file.js")));
assert(engine.isPathIgnored(getFixturePath("ignored-paths", "node_modules/package/file.js")));
assert(engine.isPathIgnored(getFixturePath("ignored-paths", "subdir/node_modules/package/file.js")));
});

it("should still apply defaultPatterns if ignore option is is false", () => {
const cwd = getFixturePath("ignored-paths");
const engine = new CLIEngine({ ignore: false, cwd });

assert(engine.isPathIgnored(getFixturePath("ignored-paths", "bower_components/package/file.js")));
assert(engine.isPathIgnored(getFixturePath("ignored-paths", "node_modules/package/file.js")));
});

it("should not ignore files in defaultPatterns within a subdirectory", () => {
const cwd = getFixturePath("ignored-paths");
const engine = new CLIEngine({ cwd });

assert(!engine.isPathIgnored(getFixturePath("ignored-paths", "subdir/bower_components/package/file.js")));
assert(!engine.isPathIgnored(getFixturePath("ignored-paths", "subdir/node_modules/package/file.js")));
assert(engine.isPathIgnored(getFixturePath("ignored-paths", "subdir/node_modules/package/file.js")));
});

it("should allow subfolders of defaultPatterns to be unignored by ignorePattern", () => {
Expand Down Expand Up @@ -3869,20 +3861,20 @@ describe("CLIEngine", () => {
assert(!engine.isPathIgnored(`${getFixturePath("ignored-paths", "foo")}/../unignored.js`));
});

it("should ignore /node_modules/ at top level relative to .eslintignore when loaded", () => {
it("should ignore /node_modules/ relative to .eslintignore when loaded", () => {
const cwd = getFixturePath("ignored-paths");
const engine = new CLIEngine({ ignorePath: getFixturePath("ignored-paths", ".eslintignore"), cwd });

assert(engine.isPathIgnored(getFixturePath("ignored-paths", "node_modules", "existing.js")));
assert(!engine.isPathIgnored(getFixturePath("ignored-paths", "foo", "node_modules", "existing.js")));
assert(engine.isPathIgnored(getFixturePath("ignored-paths", "foo", "node_modules", "existing.js")));
});

it("should ignore /node_modules/ at top level relative to cwd without an .eslintignore", () => {
it("should ignore /node_modules/ relative to cwd without an .eslintignore", () => {
const cwd = getFixturePath("ignored-paths", "no-ignore-file");
const engine = new CLIEngine({ cwd });

assert(engine.isPathIgnored(getFixturePath("ignored-paths", "no-ignore-file", "node_modules", "existing.js")));
assert(!engine.isPathIgnored(getFixturePath("ignored-paths", "no-ignore-file", "foo", "node_modules", "existing.js")));
assert(engine.isPathIgnored(getFixturePath("ignored-paths", "no-ignore-file", "foo", "node_modules", "existing.js")));
});
});

Expand Down Expand Up @@ -5052,12 +5044,12 @@ describe("CLIEngine", () => {
InMemoryCLIEngine = defineCLIEngineWithInMemoryFileSystem({
cwd: () => root,
files: {
".eslintrc.js": `module.exports = ${JSON.stringify({
".eslintrc.json": JSON.stringify({
ignorePatterns: "foo.js"
})}`,
"subdir/.eslintrc.js": `module.exports = ${JSON.stringify({
}),
"subdir/.eslintrc.json": JSON.stringify({
ignorePatterns: "bar.js"
})}`,
}),
"foo.js": "",
"bar.js": "",
"subdir/foo.js": "",
Expand Down Expand Up @@ -5107,12 +5099,12 @@ describe("CLIEngine", () => {
InMemoryCLIEngine = defineCLIEngineWithInMemoryFileSystem({
cwd: () => root,
files: {
".eslintrc.js": `module.exports = ${JSON.stringify({
".eslintrc.json": JSON.stringify({
ignorePatterns: "foo.js"
})}`,
"subdir/.eslintrc.js": `module.exports = ${JSON.stringify({
}),
"subdir/.eslintrc.json": JSON.stringify({
ignorePatterns: "!foo.js"
})}`,
}),
"foo.js": "",
"subdir/foo.js": ""
}
Expand Down Expand Up @@ -5193,13 +5185,13 @@ describe("CLIEngine", () => {
InMemoryCLIEngine = defineCLIEngineWithInMemoryFileSystem({
cwd: () => root,
files: {
".eslintrc.js": `module.exports = ${JSON.stringify({
".eslintrc.json": JSON.stringify({
ignorePatterns: "foo.js"
})}`,
"subdir/.eslintrc.js": `module.exports = ${JSON.stringify({
}),
"subdir/.eslintrc.json": JSON.stringify({
root: true,
ignorePatterns: "bar.js"
})}`,
}),
"foo.js": "",
"bar.js": "",
"subdir/foo.js": "",
Expand Down Expand Up @@ -5251,11 +5243,11 @@ describe("CLIEngine", () => {
InMemoryCLIEngine = defineCLIEngineWithInMemoryFileSystem({
cwd: () => root,
files: {
".eslintrc.js": `module.exports = ${JSON.stringify({})}`,
"subdir/.eslintrc.js": `module.exports = ${JSON.stringify({
".eslintrc.json": JSON.stringify({}),
"subdir/.eslintrc.json": JSON.stringify({
root: true,
ignorePatterns: "bar.js"
})}`,
}),
".eslintignore": "foo.js",
"foo.js": "",
"bar.js": "",
Expand Down Expand Up @@ -5305,9 +5297,9 @@ describe("CLIEngine", () => {
"node_modules/eslint-config-one/index.js": `module.exports = ${JSON.stringify({
ignorePatterns: "foo.js"
})}`,
".eslintrc.js": `module.exports = ${JSON.stringify({
".eslintrc.json": JSON.stringify({
extends: "one"
})}`,
}),
"foo.js": "",
"bar.js": ""
}
Expand Down Expand Up @@ -5347,9 +5339,9 @@ describe("CLIEngine", () => {
"node_modules/eslint-config-one/index.js": `module.exports = ${JSON.stringify({
ignorePatterns: "/foo.js"
})}`,
".eslintrc.js": `module.exports = ${JSON.stringify({
".eslintrc.json": JSON.stringify({
extends: "one"
})}`,
}),
"foo.js": "",
"subdir/foo.js": ""
}
Expand Down Expand Up @@ -5389,10 +5381,10 @@ describe("CLIEngine", () => {
"node_modules/eslint-config-one/index.js": `module.exports = ${JSON.stringify({
ignorePatterns: "*.js"
})}`,
".eslintrc.js": `module.exports = ${JSON.stringify({
".eslintrc.json": JSON.stringify({
extends: "one",
ignorePatterns: "!bar.js"
})}`,
}),
"foo.js": "",
"bar.js": ""
}
Expand Down Expand Up @@ -5429,9 +5421,9 @@ describe("CLIEngine", () => {
InMemoryCLIEngine = defineCLIEngineWithInMemoryFileSystem({
cwd: () => root,
files: {
".eslintrc.js": `module.exports = ${JSON.stringify({
".eslintrc.json": JSON.stringify({
ignorePatterns: "*.js"
})}`,
}),
"foo.js": ""
}
}).CLIEngine;
Expand Down

0 comments on commit cf38d0d

Please sign in to comment.