Skip to content

Commit

Permalink
fix: escaping for square brackets in ignore patterns (#15666)
Browse files Browse the repository at this point in the history
* fix: escaping for square brackets in ignore patterns

Fixes #15642

* remove file to fix its filename

* re-add file to fix its filename

* use github:eslint/eslintrc main branch

* use @eslint/eslintrc@1.2.1
  • Loading branch information
mdjermanovic authored Mar 11, 2022
1 parent c481cec commit 6814922
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"homepage": "https://eslint.org",
"bugs": "https://github.com/eslint/eslint/issues/",
"dependencies": {
"@eslint/eslintrc": "^1.2.0",
"@eslint/eslintrc": "^1.2.1",
"@humanwhocodes/config-array": "^0.9.2",
"ajv": "^6.10.0",
"chalk": "^4.0.0",
Expand Down
3 changes: 3 additions & 0 deletions tests/fixtures/ignored-paths/.eslintignoreWithEscapedBrackets
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
brackets/\[index.js
brackets/index\].js
brackets/\[index\].js
1 change: 1 addition & 0 deletions tests/fixtures/ignored-paths/brackets/[index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* content is not necessary */
1 change: 1 addition & 0 deletions tests/fixtures/ignored-paths/brackets/[index].js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* content is not necessary */
1 change: 1 addition & 0 deletions tests/fixtures/ignored-paths/brackets/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* content is not necessary */
1 change: 1 addition & 0 deletions tests/fixtures/ignored-paths/brackets/index].js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* content is not necessary */
37 changes: 37 additions & 0 deletions tests/lib/eslint/eslint.js
Original file line number Diff line number Diff line change
Expand Up @@ -1446,6 +1446,21 @@ describe("ESLint", () => {
}, /All files matched by '\.\/tests\/fixtures\/cli-engine\/' are ignored\./u);
});

// https://github.com/eslint/eslint/issues/15642
it("should ignore files that are ignored by patterns with escaped brackets", async () => {
eslint = new ESLint({
ignorePath: getFixturePath("ignored-paths", ".eslintignoreWithEscapedBrackets"),
useEslintrc: false,
cwd: getFixturePath("ignored-paths")
});

// Only `brackets/index.js` should be linted. Other files in `brackets/` should be ignored.
const results = await eslint.lintFiles(["brackets/*.js"]);

assert.strictEqual(results.length, 1);
assert.strictEqual(results[0].filePath, getFixturePath("ignored-paths", "brackets", "index.js"));
});

it("should throw an error when all given files are ignored via ignore-pattern", async () => {
eslint = new ESLint({
overrideConfig: {
Expand Down Expand Up @@ -4677,6 +4692,28 @@ describe("ESLint", () => {

assert(!await engine.isPathIgnored(getFixturePath("ignored-paths", "negation", "unignore.js")));
});

// https://github.com/eslint/eslint/issues/15642
it("should correctly handle patterns with escaped brackets", async () => {
const cwd = getFixturePath("ignored-paths");
const ignorePath = getFixturePath("ignored-paths", ".eslintignoreWithEscapedBrackets");
const engine = new ESLint({ ignorePath, cwd });

const subdir = "brackets";

assert(
!await engine.isPathIgnored(getFixturePath("ignored-paths", subdir, "index.js")),
`'${subdir}/index.js' should not be ignored`
);

for (const filename of ["[index.js", "index].js", "[index].js"]) {
assert(
await engine.isPathIgnored(getFixturePath("ignored-paths", subdir, filename)),
`'${subdir}/${filename}' should be ignored`
);
}

});
});

describe("with --ignore-path option and --ignore-pattern option", () => {
Expand Down

0 comments on commit 6814922

Please sign in to comment.