Skip to content

Commit a0dea8e

Browse files
authored
fix: allow name in global ignores, fix --no-ignore for non-global (#18875)
* fix: allow `name` in global ignores, fix `--no-ignore` for non-global * enable CI * pin dev dep to fix CI
1 parent 3836bb4 commit a0dea8e

File tree

4 files changed

+76
-5
lines changed

4 files changed

+76
-5
lines changed

lib/config/flat-config-array.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ const jsPlugin = require("@eslint/js");
1919
// Helpers
2020
//-----------------------------------------------------------------------------
2121

22+
/**
23+
* Fields that are considered metadata and not part of the config object.
24+
*/
25+
const META_FIELDS = new Set(["name"]);
26+
2227
const ruleValidator = new RuleValidator();
2328

2429
/**
@@ -155,15 +160,15 @@ class FlatConfigArray extends ConfigArray {
155160
}
156161

157162
/*
158-
* If `shouldIgnore` is false, we remove any ignore patterns specified
159-
* in the config so long as it's not a default config and it doesn't
160-
* have a `files` entry.
163+
* If a config object has `ignores` and no other non-meta fields, then it's an object
164+
* for global ignores. If `shouldIgnore` is false, that object shouldn't apply,
165+
* so we'll remove its `ignores`.
161166
*/
162167
if (
163168
!this.shouldIgnore &&
164169
!this[originalBaseConfig].includes(config) &&
165170
config.ignores &&
166-
!config.files
171+
Object.keys(config).filter(key => !META_FIELDS.has(key)).length === 1
167172
) {
168173
/* eslint-disable-next-line no-unused-vars -- need to strip off other keys */
169174
const { ignores, ...otherKeys } = config;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
"@eslint-community/regexpp": "^4.6.1",
6767
"@eslint/eslintrc": "^2.1.4",
6868
"@eslint/js": "8.57.0",
69-
"@humanwhocodes/config-array": "^0.11.14",
69+
"@humanwhocodes/config-array": "^0.12.3",
7070
"@humanwhocodes/module-importer": "^1.0.1",
7171
"@nodelib/fs.walk": "^1.2.8",
7272
"@ungap/structured-clone": "^1.2.0",
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const eslintConfig = require("./eslint.config.js");
2+
3+
module.exports = [
4+
eslintConfig,
5+
{
6+
name: "Global ignores",
7+
ignores: ["**/*.json", "**/*.js"]
8+
}
9+
];

tests/lib/eslint/flat-eslint.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1549,6 +1549,16 @@ describe("FlatESLint", () => {
15491549
}, /All files matched by 'tests\/fixtures\/cli-engine\/' are ignored\./u);
15501550
});
15511551

1552+
it("should throw an error when all given files are ignored by a config object that has `name`", async () => {
1553+
eslint = new FlatESLint({
1554+
overrideConfigFile: getFixturePath("eslint.config-with-ignores3.js")
1555+
});
1556+
1557+
await assert.rejects(async () => {
1558+
await eslint.lintFiles(["tests/fixtures/cli-engine/"]);
1559+
}, /All files matched by 'tests\/fixtures\/cli-engine\/' are ignored\./u);
1560+
});
1561+
15521562
it("should throw an error when all given files are ignored even with a `./` prefix", async () => {
15531563
eslint = new FlatESLint({
15541564
overrideConfigFile: getFixturePath("eslint.config_with_ignores.js")
@@ -1682,6 +1692,29 @@ describe("FlatESLint", () => {
16821692
assert.strictEqual(results[0].suppressedMessages.length, 0);
16831693
});
16841694

1695+
it("should return two messages when given a file in excluded files list by a config object that has `name` while ignore is off", async () => {
1696+
eslint = new FlatESLint({
1697+
cwd: getFixturePath(),
1698+
ignore: false,
1699+
overrideConfigFile: getFixturePath("eslint.config-with-ignores3.js"),
1700+
overrideConfig: {
1701+
rules: {
1702+
"no-undef": 2
1703+
}
1704+
}
1705+
});
1706+
const filePath = fs.realpathSync(getFixturePath("undef.js"));
1707+
const results = await eslint.lintFiles([filePath]);
1708+
1709+
assert.strictEqual(results.length, 1);
1710+
assert.strictEqual(results[0].filePath, filePath);
1711+
assert.strictEqual(results[0].messages[0].ruleId, "no-undef");
1712+
assert.strictEqual(results[0].messages[0].severity, 2);
1713+
assert.strictEqual(results[0].messages[1].ruleId, "no-undef");
1714+
assert.strictEqual(results[0].messages[1].severity, 2);
1715+
assert.strictEqual(results[0].suppressedMessages.length, 0);
1716+
});
1717+
16851718
// https://github.com/eslint/eslint/issues/16300
16861719
it("should process ignore patterns relative to basePath not cwd", async () => {
16871720
eslint = new FlatESLint({
@@ -5724,6 +5757,30 @@ describe("FlatESLint", () => {
57245757
assert.strictEqual(warnResult.messages[0].ruleId, "no-unused-vars");
57255758
assert.strictEqual(warnResult.messages[0].severity, 1);
57265759
});
5760+
5761+
// https://github.com/eslint/eslint/issues/18261
5762+
it("should apply to all files except for 'error.js' even with `ignore: false` option", async () => {
5763+
const engine = new FlatESLint({
5764+
cwd,
5765+
ignore: false
5766+
});
5767+
5768+
const results = await engine.lintFiles("{error,warn}.js");
5769+
5770+
assert.strictEqual(results.length, 2);
5771+
5772+
const [errorResult, warnResult] = results;
5773+
5774+
assert.strictEqual(errorResult.filePath, path.join(getPath(), "error.js"));
5775+
assert.strictEqual(errorResult.messages.length, 1);
5776+
assert.strictEqual(errorResult.messages[0].ruleId, "no-unused-vars");
5777+
assert.strictEqual(errorResult.messages[0].severity, 2);
5778+
5779+
assert.strictEqual(warnResult.filePath, path.join(getPath(), "warn.js"));
5780+
assert.strictEqual(warnResult.messages.length, 1);
5781+
assert.strictEqual(warnResult.messages[0].ruleId, "no-unused-vars");
5782+
assert.strictEqual(warnResult.messages[0].severity, 1);
5783+
});
57275784
});
57285785

57295786
describe("config with ignores: ['**/*.json']", () => {

0 commit comments

Comments
 (0)