Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: --no-ignore should not apply to non-global ignores #18334

Merged
merged 1 commit into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 9 additions & 4 deletions lib/config/flat-config-array.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ const { defaultConfig } = require("./default-config");
// Helpers
//-----------------------------------------------------------------------------

/**
* Fields that are considered metadata and not part of the config object.
*/
const META_FIELDS = new Set(["name"]);

const ruleValidator = new RuleValidator();

/**
Expand Down Expand Up @@ -135,15 +140,15 @@ class FlatConfigArray extends ConfigArray {
[ConfigArraySymbol.preprocessConfig](config) {

/*
* If `shouldIgnore` is false, we remove any ignore patterns specified
* in the config so long as it's not a default config and it doesn't
* have a `files` entry.
* If a config object has `ignores` and no other non-meta fields, then it's an object
* for global ignores. If `shouldIgnore` is false, that object shouldn't apply,
* so we'll remove its `ignores`.
*/
if (
!this.shouldIgnore &&
!this[originalBaseConfig].includes(config) &&
config.ignores &&
!config.files
Object.keys(config).filter(key => !META_FIELDS.has(key)).length === 1
) {
/* eslint-disable-next-line no-unused-vars -- need to strip off other keys */
const { ignores, ...otherKeys } = config;
Expand Down
9 changes: 9 additions & 0 deletions tests/fixtures/eslint.config-with-ignores3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const eslintConfig = require("./eslint.config.js");

module.exports = [
eslintConfig,
{
name: "Global ignores",
ignores: ["**/*.json", "**/*.js"]
}
];
57 changes: 57 additions & 0 deletions tests/lib/eslint/eslint.js
Original file line number Diff line number Diff line change
Expand Up @@ -1630,6 +1630,16 @@ describe("ESLint", () => {
}, /All files matched by 'tests\/fixtures\/cli-engine\/' are ignored\./u);
});

it("should throw an error when all given files are ignored by a config object that has `name`", async () => {
eslint = new ESLint({
overrideConfigFile: getFixturePath("eslint.config-with-ignores3.js")
});

await assert.rejects(async () => {
await eslint.lintFiles(["tests/fixtures/cli-engine/"]);
}, /All files matched by 'tests\/fixtures\/cli-engine\/' are ignored\./u);
});

it("should throw an error when all given files are ignored even with a `./` prefix", async () => {
eslint = new ESLint({
overrideConfigFile: getFixturePath("eslint.config-with-ignores.js")
Expand Down Expand Up @@ -1775,6 +1785,29 @@ describe("ESLint", () => {
assert.strictEqual(results[0].suppressedMessages.length, 0);
});

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 () => {
eslint = new ESLint({
cwd: getFixturePath(),
ignore: false,
overrideConfigFile: getFixturePath("eslint.config-with-ignores3.js"),
overrideConfig: {
rules: {
"no-undef": 2
}
}
});
const filePath = fs.realpathSync(getFixturePath("undef.js"));
const results = await eslint.lintFiles([filePath]);

assert.strictEqual(results.length, 1);
assert.strictEqual(results[0].filePath, filePath);
assert.strictEqual(results[0].messages[0].ruleId, "no-undef");
assert.strictEqual(results[0].messages[0].severity, 2);
assert.strictEqual(results[0].messages[1].ruleId, "no-undef");
assert.strictEqual(results[0].messages[1].severity, 2);
assert.strictEqual(results[0].suppressedMessages.length, 0);
});

// https://github.com/eslint/eslint/issues/16300
it("should process ignore patterns relative to basePath not cwd", async () => {
eslint = new ESLint({
Expand Down Expand Up @@ -5698,6 +5731,30 @@ describe("ESLint", () => {
assert.strictEqual(warnResult.messages[0].ruleId, "no-unused-vars");
assert.strictEqual(warnResult.messages[0].severity, 1);
});

// https://github.com/eslint/eslint/issues/18261
it("should apply to all files except for 'error.js' even with `ignore: false` option", async () => {
const engine = new ESLint({
cwd,
ignore: false
});

const results = await engine.lintFiles("{error,warn}.js");

assert.strictEqual(results.length, 2);

const [errorResult, warnResult] = results;

assert.strictEqual(errorResult.filePath, path.join(getPath(), "error.js"));
assert.strictEqual(errorResult.messages.length, 1);
assert.strictEqual(errorResult.messages[0].ruleId, "no-unused-vars");
assert.strictEqual(errorResult.messages[0].severity, 2);

assert.strictEqual(warnResult.filePath, path.join(getPath(), "warn.js"));
assert.strictEqual(warnResult.messages.length, 1);
assert.strictEqual(warnResult.messages[0].ruleId, "no-unused-vars");
assert.strictEqual(warnResult.messages[0].severity, 1);
});
});

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