Skip to content

Commit

Permalink
feat: change default ignore pattern to **/node_modules/ in flat con…
Browse files Browse the repository at this point in the history
…fig (#17184)

* feat: change default ignore pattern to `**/node_modules/` in flat config

Fixes #17113

* update warning message
  • Loading branch information
mdjermanovic committed May 17, 2023
1 parent 94da96c commit 880a431
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 15 deletions.
16 changes: 15 additions & 1 deletion docs/src/use/configure/configuration-files-new.md
Expand Up @@ -143,7 +143,21 @@ export default [
];
```

This configuration specifies that all of the files in the `.config` directory should be ignored. This pattern is added after the default patterns, which are `["**/node_modules/**", ".git/**"]`.
This configuration specifies that all of the files in the `.config` directory should be ignored. This pattern is added after the default patterns, which are `["**/node_modules/", ".git/"]`.

You can also unignore files and directories that are ignored by the default patterns. For example, this config unignores `node_modules/mylibrary`:

```js
export default [
{
ignores: [
"!node_modules/", // unignore `node_modules/` directory
"node_modules/*", // ignore its content
"!node_modules/mylibrary/" // unignore `node_modules/mylibrary` directory
]
}
];
```

#### Cascading configuration objects

Expand Down
2 changes: 1 addition & 1 deletion lib/config/default-config.js
Expand Up @@ -48,7 +48,7 @@ exports.defaultConfig = [
// default ignores are listed here
{
ignores: [
"**/node_modules/*",
"**/node_modules/",
".git/"
]
},
Expand Down
2 changes: 1 addition & 1 deletion lib/eslint/eslint-helpers.js
Expand Up @@ -598,7 +598,7 @@ function createIgnoreResult(filePath, baseDir) {
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.";
message = "File ignored by default because it is located under the node_modules directory. Use ignore pattern \"!**/node_modules/\" to override.";
} else {
message = "File ignored because of a matching ignore pattern. Use \"--no-ignore\" to override.";
}
Expand Down
24 changes: 12 additions & 12 deletions tests/lib/eslint/flat-eslint.js
Expand Up @@ -661,7 +661,7 @@ describe("FlatESLint", () => {
ignore: false
});
const results = await eslint.lintText("var bar = foo;", { filePath: "node_modules/passing.js", warnIgnored: true });
const expectedMsg = "File ignored by default. Use \"--ignore-pattern '!node_modules/*'\" to override.";
const expectedMsg = "File ignored by default because it is located under the node_modules directory. Use ignore pattern \"!**/node_modules/\" to override.";

assert.strictEqual(results.length, 1);
assert.strictEqual(results[0].filePath, getFixturePath("node_modules/passing.js"));
Expand Down Expand Up @@ -1196,7 +1196,7 @@ describe("FlatESLint", () => {
cwd: getFixturePath("cli-engine")
});
const results = await eslint.lintFiles(["node_modules/foo.js"]);
const expectedMsg = "File ignored by default. Use \"--ignore-pattern '!node_modules/*'\" to override.";
const expectedMsg = "File ignored by default because it is located under the node_modules directory. Use ignore pattern \"!**/node_modules/\" to override.";

assert.strictEqual(results.length, 1);
assert.strictEqual(results[0].errorCount, 0);
Expand Down Expand Up @@ -3578,26 +3578,26 @@ describe("FlatESLint", () => {
assert(await engine.isPathIgnored(getFixturePath("ignored-paths", "subdir/node_modules/package/file.js")));
});

it("should allow subfolders of defaultPatterns to be unignored by ignorePattern", async () => {
it("should allow subfolders of defaultPatterns to be unignored by ignorePattern constructor option", async () => {
const cwd = getFixturePath("ignored-paths");
const engine = new FlatESLint({
cwd,
overrideConfigFile: true,
ignorePatterns: ["!node_modules/package/**"]
ignorePatterns: ["!node_modules/", "node_modules/*", "!node_modules/package/"]
});

const result = await engine.isPathIgnored(getFixturePath("ignored-paths", "node_modules", "package", "file.js"));

assert(!result, "File should not be ignored");
});

it("should allow subfolders of defaultPatterns to be unignored by ignorePath", async () => {
it("should allow subfolders of defaultPatterns to be unignored by ignores in overrideConfig", async () => {
const cwd = getFixturePath("ignored-paths");
const engine = new FlatESLint({
cwd,
overrideConfigFile: true,
overrideConfig: {
ignores: ["!node_modules/package/**"]
ignores: ["!node_modules/", "node_modules/*", "!node_modules/package/"]
}
});

Expand Down Expand Up @@ -4608,13 +4608,13 @@ describe("FlatESLint", () => {
});


describe("ignores can unignore '/node_modules/foo'.", () => {
describe("ignores can unignore '/node_modules/foo' with patterns ['!node_modules/', 'node_modules/*', '!node_modules/foo/'].", () => {

const { prepare, cleanup, getPath } = createCustomTeardown({
cwd: `${root}-unignores`,
files: {
"eslint.config.js": `module.exports = {
ignores: ["!**/node_modules/foo"]
ignores: ["!node_modules/", "node_modules/*", "!node_modules/foo/"]
};`,
"node_modules/foo/index.js": "",
"node_modules/foo/.dot.js": "",
Expand Down Expand Up @@ -4659,13 +4659,13 @@ describe("FlatESLint", () => {
});
});

describe("ignores can unignore '/node_modules/foo/**'.", () => {
describe("ignores can unignore '/node_modules/foo' with patterns ['!node_modules/', 'node_modules/*', '!node_modules/foo/**'].", () => {

const { prepare, cleanup, getPath } = createCustomTeardown({
cwd: `${root}-unignores`,
files: {
"eslint.config.js": `module.exports = {
ignores: ["!**/node_modules/foo/**"]
ignores: ["!node_modules/", "node_modules/*", "!node_modules/foo/**"]
};`,
"node_modules/foo/index.js": "",
"node_modules/foo/.dot.js": "",
Expand Down Expand Up @@ -5093,7 +5093,7 @@ describe("FlatESLint", () => {
{
ruleId: null,
fatal: false,
message: "File ignored by default. Use \"--ignore-pattern '!node_modules/*'\" to override.",
message: "File ignored by default because it is located under the node_modules directory. Use ignore pattern \"!**/node_modules/\" to override.",
severity: 1,
nodeType: null
}
Expand Down Expand Up @@ -5194,7 +5194,7 @@ describe("FlatESLint", () => {
cwd: `${root}a3`,
files: {
"node_modules/myconf/eslint.config.js": `module.exports = [{
ignores: ["!node_modules/myconf", "foo/*.js"],
ignores: ["!node_modules", "node_modules/*", "!node_modules/myconf", "foo/*.js"],
}, {
rules: {
eqeqeq: "error"
Expand Down

0 comments on commit 880a431

Please sign in to comment.