Skip to content

Commit

Permalink
Fix: overrides handle relative paths as expected (fixes #11577)
Browse files Browse the repository at this point in the history
  • Loading branch information
mysticatea committed Jun 3, 2019
1 parent cb1922b commit 114ecbd
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
25 changes: 22 additions & 3 deletions lib/cli-engine/config-array/override-tester.js
Expand Up @@ -148,11 +148,30 @@ class OverrideTester {
if (typeof filePath !== "string" || !path.isAbsolute(filePath)) {
throw new Error(`'filePath' should be an absolute path, but got ${filePath}.`);
}
const relativePath = path.relative(this.basePath, filePath);

/*
* Check both with/without `./`.
* - Removing `./` at making patterns didn't work as expected because
* `./foo.js` will match with `subdir/foo.js`.
* - Adding `./` at making patterns didn't work as expected because
* `foo.js` will not match with `subdir/foo.js` even if `matchBase` is
* `true`.
*/
const relativePath1 = path.relative(this.basePath, filePath);
const relativePath2 = `.${path.sep}${relativePath1}`;

/**
* Test if a given matcher matches with either two relative paths.
* @param {InstanceType<Minimatch>} matcher The matcher to test.
* @returns {boolean} `true` if the matcher matched with the relative paths.
*/
function isMatched(matcher) {
return matcher.match(relativePath1) || matcher.match(relativePath2);
}

return this.patterns.every(({ includes, excludes }) => (
(!includes || includes.some(m => m.match(relativePath))) &&
(!excludes || !excludes.some(m => m.match(relativePath)))
(!includes || includes.some(isMatched)) &&
(!excludes || !excludes.some(isMatched))
));
}

Expand Down
12 changes: 6 additions & 6 deletions tests/lib/cli-engine/config-array/override-tester.js
Expand Up @@ -191,10 +191,10 @@ describe("OverrideTester", () => {
match("foo.js", ["*.js"], []);
match("foo.js", ["**/*.js"], []);
match("bar.js", ["*.js"], ["foo.js"]);
match("foo.js", ["./foo.js"], []);
match("foo.js", ["./*"], []);
match("foo.js", ["./**"], []);

noMatch("foo.js", ["./foo.js"], []);
noMatch("foo.js", ["./*"], []);
noMatch("foo.js", ["./**"], []);
noMatch("foo.js", ["*"], ["foo.js"]);
noMatch("foo.js", ["*.js"], ["foo.js"]);
noMatch("foo.js", ["**/*.js"], ["foo.js"]);
Expand All @@ -208,11 +208,11 @@ describe("OverrideTester", () => {
match("subdir/foo.js", ["subdir/foo.js"], []);
match("subdir/foo.js", ["subdir/*"], []);
match("subdir/second/foo.js", ["subdir/**"], []);
match("subdir/foo.js", ["./**"], []);
match("subdir/foo.js", ["./subdir/**"], []);
match("subdir/foo.js", ["./subdir/*"], []);

noMatch("subdir/foo.js", ["./foo.js"], []);
noMatch("subdir/foo.js", ["./**"], []);
noMatch("subdir/foo.js", ["./subdir/**"], []);
noMatch("subdir/foo.js", ["./subdir/*"], []);
noMatch("subdir/foo.js", ["*"], ["subdir/**"]);
noMatch("subdir/very/deep/foo.js", ["*.js"], ["subdir/**"]);
noMatch("subdir/second/foo.js", ["subdir/*"], []);
Expand Down

0 comments on commit 114ecbd

Please sign in to comment.