Skip to content

Commit

Permalink
fix: --ignore-pattern in flat config mode should be relative to `cw…
Browse files Browse the repository at this point in the history
…d` (#16425)

* fix: `--ignore-pattern` in flat config mode should be relative to `cwd`

* fix .eslintrc.js ignore pattern

* flip conditionals
  • Loading branch information
mdjermanovic committed Oct 19, 2022
1 parent 325ad37 commit 8f9759e
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 19 deletions.
2 changes: 1 addition & 1 deletion eslint.config.js
Expand Up @@ -92,7 +92,7 @@ module.exports = [
"tmp/**",
"tools/internal-rules/node_modules/**",
"**/test.js",
"!**/.eslintrc.js"
"!.eslintrc.js"
]
},
{
Expand Down
10 changes: 3 additions & 7 deletions lib/cli.js
Expand Up @@ -143,12 +143,6 @@ async function translateOptions({
overrideConfig[0].plugins = plugins;
}

if (ignorePattern) {
overrideConfig.push({
ignores: ignorePattern
});
}

} else {
overrideConfigFile = config;

Expand Down Expand Up @@ -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;
Expand Down
11 changes: 0 additions & 11 deletions lib/eslint/flat-eslint.js
Expand Up @@ -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);
});
Expand Down
3 changes: 3 additions & 0 deletions tests/fixtures/cli/ignore-pattern-relative/.eslintrc.js
@@ -0,0 +1,3 @@
module.exports = {
root: true
};
@@ -0,0 +1 @@
module.exports = [];
Empty file.
26 changes: 26 additions & 0 deletions tests/lib/cli.js
Expand Up @@ -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");
Expand Down

0 comments on commit 8f9759e

Please sign in to comment.