Skip to content

Commit 03fb0bc

Browse files
authored
feat: normalize patterns to handle "./" prefix in files and ignores (#19568)
* feat: normalize patterns to handle "./" prefix in files and ignores * add tests * format * fix tests flakiness
1 parent b9a5efa commit 03fb0bc

2 files changed

Lines changed: 227 additions & 1 deletion

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@
106106
"dependencies": {
107107
"@eslint-community/eslint-utils": "^4.2.0",
108108
"@eslint-community/regexpp": "^4.12.1",
109-
"@eslint/config-array": "^0.19.2",
109+
"@eslint/config-array": "^0.20.0",
110110
"@eslint/config-helpers": "^0.2.0",
111111
"@eslint/core": "^0.12.0",
112112
"@eslint/eslintrc": "^3.3.1",

tests/lib/eslint/eslint.js

Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7717,6 +7717,232 @@ describe("ESLint", () => {
77177717
await assert.rejects(eslint.lintFiles("*.js"));
77187718
});
77197719
});
7720+
7721+
describe("patterns with './' prefix", () => {
7722+
const root = getFixturePath(
7723+
"cli-engine/patterns-with-dot-prefix",
7724+
);
7725+
7726+
let cleanup;
7727+
let i = 0;
7728+
7729+
beforeEach(() => {
7730+
cleanup = () => {};
7731+
i++;
7732+
});
7733+
7734+
afterEach(() => cleanup());
7735+
7736+
it("should match patterns with './' prefix in `files` patterns", async () => {
7737+
const teardown = createCustomTeardown({
7738+
cwd: `${root}${i}`,
7739+
files: {
7740+
"src/foo.js": "undefinedVariable;",
7741+
"eslint.config.js": `module.exports = [{
7742+
files: ["./src/*.js"],
7743+
rules: { "no-undef": "error" }
7744+
}];`,
7745+
},
7746+
});
7747+
7748+
await teardown.prepare();
7749+
cleanup = teardown.cleanup;
7750+
7751+
eslint = new ESLint({ flags, cwd: teardown.getPath() });
7752+
const results = await eslint.lintFiles("src/**/*.js");
7753+
7754+
assert.strictEqual(results.length, 1);
7755+
assert.strictEqual(
7756+
results[0].filePath,
7757+
path.join(teardown.getPath(), "src/foo.js"),
7758+
);
7759+
assert.strictEqual(results[0].messages.length, 1);
7760+
assert.strictEqual(
7761+
results[0].messages[0].ruleId,
7762+
"no-undef",
7763+
);
7764+
assert.strictEqual(results[0].messages[0].severity, 2);
7765+
});
7766+
7767+
it("should match patterns with './' prefix in `ignores` patterns", async () => {
7768+
const teardown = createCustomTeardown({
7769+
cwd: `${root}${i}`,
7770+
files: {
7771+
"src/foo.js": "undefinedVariable;",
7772+
"eslint.config.js": `module.exports = [{
7773+
files: ["**/*.js"],
7774+
ignores: ["./src/*.js"],
7775+
rules: { "no-undef": "error" }
7776+
}];`,
7777+
},
7778+
});
7779+
7780+
await teardown.prepare();
7781+
cleanup = teardown.cleanup;
7782+
7783+
eslint = new ESLint({ flags, cwd: teardown.getPath() });
7784+
const results = await eslint.lintFiles("src/**/*.js");
7785+
7786+
assert.strictEqual(results.length, 1);
7787+
assert.strictEqual(
7788+
results[0].filePath,
7789+
path.join(teardown.getPath(), "src/foo.js"),
7790+
);
7791+
assert.strictEqual(results[0].messages.length, 0);
7792+
});
7793+
7794+
it("should match patterns with './' prefix in global `ignores` patterns", async () => {
7795+
const teardown = createCustomTeardown({
7796+
cwd: `${root}${i}`,
7797+
files: {
7798+
"src/foo.js": "undefinedVariable;",
7799+
"eslint.config.js": `module.exports = [
7800+
{
7801+
files: ["**/*.js"],
7802+
rules: { "no-undef": "error" }
7803+
},
7804+
{
7805+
ignores: ["./src/*.js"]
7806+
}
7807+
];`,
7808+
},
7809+
});
7810+
7811+
await teardown.prepare();
7812+
cleanup = teardown.cleanup;
7813+
7814+
eslint = new ESLint({ flags, cwd: teardown.getPath() });
7815+
7816+
await assert.rejects(async () => {
7817+
await eslint.lintFiles("src/**/*.js");
7818+
}, /All files matched by 'src\/\*\*\/\*\.js' are ignored\./u);
7819+
});
7820+
7821+
it("should match negated `files` patterns with './' prefix", async () => {
7822+
const teardown = createCustomTeardown({
7823+
cwd: `${root}${i}`,
7824+
files: {
7825+
"src/foo.js": "undefinedVariable;",
7826+
"eslint.config.js": `module.exports = [{
7827+
files: ["!./src/*.js"],
7828+
rules: { "no-undef": "error" }
7829+
}];`,
7830+
},
7831+
});
7832+
7833+
await teardown.prepare();
7834+
cleanup = teardown.cleanup;
7835+
7836+
eslint = new ESLint({ flags, cwd: teardown.getPath() });
7837+
const results = await eslint.lintFiles("src/**/*.js");
7838+
7839+
assert.strictEqual(results.length, 1);
7840+
assert.strictEqual(
7841+
results[0].filePath,
7842+
path.join(teardown.getPath(), "src/foo.js"),
7843+
);
7844+
assert.strictEqual(results[0].messages.length, 0);
7845+
});
7846+
7847+
it("should match negated `ignores` patterns with './' prefix", async () => {
7848+
const teardown = createCustomTeardown({
7849+
cwd: `${root}${i}`,
7850+
files: {
7851+
"src/foo.js": "undefinedVariable;",
7852+
"eslint.config.js": `module.exports = [{
7853+
files: ["**/*.js"],
7854+
ignores: ["**/*.js", "!./src/foo.js"],
7855+
rules: { "no-undef": "error" }
7856+
}];`,
7857+
},
7858+
});
7859+
7860+
await teardown.prepare();
7861+
cleanup = teardown.cleanup;
7862+
7863+
eslint = new ESLint({ flags, cwd: teardown.getPath() });
7864+
const results = await eslint.lintFiles("src/**/*.js");
7865+
7866+
assert.strictEqual(results.length, 1);
7867+
assert.strictEqual(
7868+
results[0].filePath,
7869+
path.join(teardown.getPath(), "src/foo.js"),
7870+
);
7871+
assert.strictEqual(results[0].messages.length, 1);
7872+
assert.strictEqual(
7873+
results[0].messages[0].ruleId,
7874+
"no-undef",
7875+
);
7876+
assert.strictEqual(results[0].messages[0].severity, 2);
7877+
});
7878+
7879+
it("should match negated global `ignores` patterns with './' prefix", async () => {
7880+
const teardown = createCustomTeardown({
7881+
cwd: `${root}${i}`,
7882+
files: {
7883+
"src/foo.js": "undefinedVariable;",
7884+
"eslint.config.js": `module.exports = [
7885+
{
7886+
files: ["**/*.js"],
7887+
rules: { "no-undef": "error" }
7888+
},
7889+
{
7890+
ignores: ["**/*.js", "!./src/*.js"]
7891+
}
7892+
];`,
7893+
},
7894+
});
7895+
7896+
await teardown.prepare();
7897+
cleanup = teardown.cleanup;
7898+
7899+
eslint = new ESLint({ flags, cwd: teardown.getPath() });
7900+
const results = await eslint.lintFiles("src/**/*.js");
7901+
7902+
assert.strictEqual(results.length, 1);
7903+
assert.strictEqual(
7904+
results[0].filePath,
7905+
path.join(teardown.getPath(), "src/foo.js"),
7906+
);
7907+
assert.strictEqual(results[0].messages.length, 1);
7908+
assert.strictEqual(
7909+
results[0].messages[0].ruleId,
7910+
"no-undef",
7911+
);
7912+
assert.strictEqual(results[0].messages[0].severity, 2);
7913+
});
7914+
7915+
it("should match nested `files` patterns with './' prefix", async () => {
7916+
const teardown = createCustomTeardown({
7917+
cwd: `${root}${i}`,
7918+
files: {
7919+
"src/foo.js": "undefinedVariable;",
7920+
"eslint.config.js": `module.exports = [{
7921+
files: [["./src/*.js"]],
7922+
rules: { "no-undef": "error" }
7923+
}];`,
7924+
},
7925+
});
7926+
7927+
await teardown.prepare();
7928+
cleanup = teardown.cleanup;
7929+
7930+
eslint = new ESLint({ flags, cwd: teardown.getPath() });
7931+
const results = await eslint.lintFiles("src/**/*.js");
7932+
7933+
assert.strictEqual(results.length, 1);
7934+
assert.strictEqual(
7935+
results[0].filePath,
7936+
path.join(teardown.getPath(), "src/foo.js"),
7937+
);
7938+
assert.strictEqual(results[0].messages.length, 1);
7939+
assert.strictEqual(
7940+
results[0].messages[0].ruleId,
7941+
"no-undef",
7942+
);
7943+
assert.strictEqual(results[0].messages[0].severity, 2);
7944+
});
7945+
});
77207946
});
77217947

77227948
describe("Fix Types", () => {

0 commit comments

Comments
 (0)