Skip to content

Commit 8f9759e

Browse files
authored
fix: --ignore-pattern in flat config mode should be relative to cwd (#16425)
* fix: `--ignore-pattern` in flat config mode should be relative to `cwd` * fix .eslintrc.js ignore pattern * flip conditionals
1 parent 325ad37 commit 8f9759e

7 files changed

Lines changed: 34 additions & 19 deletions

File tree

eslint.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ module.exports = [
9292
"tmp/**",
9393
"tools/internal-rules/node_modules/**",
9494
"**/test.js",
95-
"!**/.eslintrc.js"
95+
"!.eslintrc.js"
9696
]
9797
},
9898
{

lib/cli.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,6 @@ async function translateOptions({
143143
overrideConfig[0].plugins = plugins;
144144
}
145145

146-
if (ignorePattern) {
147-
overrideConfig.push({
148-
ignores: ignorePattern
149-
});
150-
}
151-
152146
} else {
153147
overrideConfigFile = config;
154148

@@ -187,7 +181,9 @@ async function translateOptions({
187181
reportUnusedDisableDirectives: reportUnusedDisableDirectives ? "error" : void 0
188182
};
189183

190-
if (configType !== "flat") {
184+
if (configType === "flat") {
185+
options.ignorePatterns = ignorePattern;
186+
} else {
191187
options.resolvePluginsRelativeTo = resolvePluginsRelativeTo;
192188
options.rulePaths = rulesdir;
193189
options.useEslintrc = eslintrc;

lib/eslint/flat-eslint.js

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -362,17 +362,6 @@ async function calculateConfigArray(eslint, {
362362
const negated = pattern.startsWith("!");
363363
const basePattern = negated ? pattern.slice(1) : pattern;
364364

365-
/*
366-
* Ignore patterns are considered relative to a directory
367-
* when the pattern contains a slash in a position other
368-
* than the last character. If that's the case, we need to
369-
* add the relative ignore path to the current pattern to
370-
* get the correct behavior. Otherwise, no change is needed.
371-
*/
372-
if (!basePattern.includes("/") || basePattern.endsWith("/")) {
373-
return pattern;
374-
}
375-
376365
return (negated ? "!" : "") +
377366
path.posix.join(relativeIgnorePath, basePattern);
378367
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
root: true
3+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = [];

tests/fixtures/cli/ignore-pattern-relative/subdir/subsubdir/a.js

Whitespace-only changes.

tests/lib/cli.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,32 @@ describe("cli", () => {
798798
assert.strictEqual(exit, 0);
799799
});
800800

801+
it(`should interpret pattern that contains a slash as relative to cwd with configType:${configType}`, async () => {
802+
process.cwd = () => getFixturePath("cli/ignore-pattern-relative/subdir");
803+
804+
/*
805+
* The config file is in `cli/ignore-pattern-relative`, so this would fail
806+
* if `subdir/**` ignore pattern is interpreted as relative to the config base path.
807+
*/
808+
const exit = await cli.execute("**/*.js --ignore-pattern subdir/**", null, useFlatConfig);
809+
810+
assert.strictEqual(exit, 0);
811+
812+
await stdAssert.rejects(
813+
async () => await cli.execute("**/*.js --ignore-pattern subsubdir/*.js", null, useFlatConfig),
814+
/All files matched by '\*\*\/\*\.js' are ignored/u
815+
);
816+
});
817+
818+
it(`should interpret pattern that doesn't contain a slash as relative to cwd with configType:${configType}`, async () => {
819+
process.cwd = () => getFixturePath("cli/ignore-pattern-relative/subdir/subsubdir");
820+
821+
await stdAssert.rejects(
822+
async () => await cli.execute("**/*.js --ignore-pattern *.js", null, useFlatConfig),
823+
/All files matched by '\*\*\/\*\.js' are ignored/u
824+
);
825+
});
826+
801827
if (useFlatConfig) {
802828
it("should ignore files if the pattern is a path to a directory (with trailing slash)", async () => {
803829
const filePath = getFixturePath("cli/syntax-error.js");

0 commit comments

Comments
 (0)