From 8f9759e2a94586357d85fac902e038fabdba79a7 Mon Sep 17 00:00:00 2001 From: Milos Djermanovic Date: Wed, 19 Oct 2022 19:24:27 +0200 Subject: [PATCH] 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 --- eslint.config.js | 2 +- lib/cli.js | 10 +++---- lib/eslint/flat-eslint.js | 11 -------- .../cli/ignore-pattern-relative/.eslintrc.js | 3 +++ .../ignore-pattern-relative/eslint.config.js | 1 + .../subdir/subsubdir/a.js | 0 tests/lib/cli.js | 26 +++++++++++++++++++ 7 files changed, 34 insertions(+), 19 deletions(-) create mode 100644 tests/fixtures/cli/ignore-pattern-relative/.eslintrc.js create mode 100644 tests/fixtures/cli/ignore-pattern-relative/eslint.config.js create mode 100644 tests/fixtures/cli/ignore-pattern-relative/subdir/subsubdir/a.js diff --git a/eslint.config.js b/eslint.config.js index 461171c5d01..cd4a5f1482b 100644 --- a/eslint.config.js +++ b/eslint.config.js @@ -92,7 +92,7 @@ module.exports = [ "tmp/**", "tools/internal-rules/node_modules/**", "**/test.js", - "!**/.eslintrc.js" + "!.eslintrc.js" ] }, { diff --git a/lib/cli.js b/lib/cli.js index 731a32620e2..afd1e65cbd1 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -143,12 +143,6 @@ async function translateOptions({ overrideConfig[0].plugins = plugins; } - if (ignorePattern) { - overrideConfig.push({ - ignores: ignorePattern - }); - } - } else { overrideConfigFile = config; @@ -187,7 +181,9 @@ async function translateOptions({ reportUnusedDisableDirectives: reportUnusedDisableDirectives ? "error" : void 0 }; - if (configType !== "flat") { + if (configType === "flat") { + options.ignorePatterns = ignorePattern; + } else { options.resolvePluginsRelativeTo = resolvePluginsRelativeTo; options.rulePaths = rulesdir; options.useEslintrc = eslintrc; diff --git a/lib/eslint/flat-eslint.js b/lib/eslint/flat-eslint.js index 3a79cef20a1..d5911853172 100644 --- a/lib/eslint/flat-eslint.js +++ b/lib/eslint/flat-eslint.js @@ -362,17 +362,6 @@ async function calculateConfigArray(eslint, { const negated = pattern.startsWith("!"); const basePattern = negated ? pattern.slice(1) : pattern; - /* - * Ignore patterns are considered relative to a directory - * when the pattern contains a slash in a position other - * than the last character. If that's the case, we need to - * add the relative ignore path to the current pattern to - * get the correct behavior. Otherwise, no change is needed. - */ - if (!basePattern.includes("/") || basePattern.endsWith("/")) { - return pattern; - } - return (negated ? "!" : "") + path.posix.join(relativeIgnorePath, basePattern); }); diff --git a/tests/fixtures/cli/ignore-pattern-relative/.eslintrc.js b/tests/fixtures/cli/ignore-pattern-relative/.eslintrc.js new file mode 100644 index 00000000000..cfa7c2db15b --- /dev/null +++ b/tests/fixtures/cli/ignore-pattern-relative/.eslintrc.js @@ -0,0 +1,3 @@ +module.exports = { + root: true +}; diff --git a/tests/fixtures/cli/ignore-pattern-relative/eslint.config.js b/tests/fixtures/cli/ignore-pattern-relative/eslint.config.js new file mode 100644 index 00000000000..e0a30c5dfa3 --- /dev/null +++ b/tests/fixtures/cli/ignore-pattern-relative/eslint.config.js @@ -0,0 +1 @@ +module.exports = []; diff --git a/tests/fixtures/cli/ignore-pattern-relative/subdir/subsubdir/a.js b/tests/fixtures/cli/ignore-pattern-relative/subdir/subsubdir/a.js new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/lib/cli.js b/tests/lib/cli.js index 75a42d0128b..735141758a9 100644 --- a/tests/lib/cli.js +++ b/tests/lib/cli.js @@ -798,6 +798,32 @@ describe("cli", () => { assert.strictEqual(exit, 0); }); + it(`should interpret pattern that contains a slash as relative to cwd with configType:${configType}`, async () => { + process.cwd = () => getFixturePath("cli/ignore-pattern-relative/subdir"); + + /* + * The config file is in `cli/ignore-pattern-relative`, so this would fail + * if `subdir/**` ignore pattern is interpreted as relative to the config base path. + */ + const exit = await cli.execute("**/*.js --ignore-pattern subdir/**", null, useFlatConfig); + + assert.strictEqual(exit, 0); + + await stdAssert.rejects( + async () => await cli.execute("**/*.js --ignore-pattern subsubdir/*.js", null, useFlatConfig), + /All files matched by '\*\*\/\*\.js' are ignored/u + ); + }); + + it(`should interpret pattern that doesn't contain a slash as relative to cwd with configType:${configType}`, async () => { + process.cwd = () => getFixturePath("cli/ignore-pattern-relative/subdir/subsubdir"); + + await stdAssert.rejects( + async () => await cli.execute("**/*.js --ignore-pattern *.js", null, useFlatConfig), + /All files matched by '\*\*\/\*\.js' are ignored/u + ); + }); + if (useFlatConfig) { it("should ignore files if the pattern is a path to a directory (with trailing slash)", async () => { const filePath = getFixturePath("cli/syntax-error.js");