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: escaping for square brackets in ignore patterns #15666

Merged
merged 5 commits into from Mar 11, 2022
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
2 changes: 1 addition & 1 deletion package.json
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
@@ -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
@@ -0,0 +1 @@
/* content is not necessary */
1 change: 1 addition & 0 deletions tests/fixtures/ignored-paths/brackets/[index].js
@@ -0,0 +1 @@
/* content is not necessary */
1 change: 1 addition & 0 deletions tests/fixtures/ignored-paths/brackets/index.js
@@ -0,0 +1 @@
/* content is not necessary */
1 change: 1 addition & 0 deletions tests/fixtures/ignored-paths/brackets/index].js
@@ -0,0 +1 @@
/* content is not necessary */
37 changes: 37 additions & 0 deletions tests/lib/eslint/eslint.js
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