From 114ecbd0f0585753472e4daf744bfd0cab22157b Mon Sep 17 00:00:00 2001 From: Toru Nagashima Date: Mon, 3 Jun 2019 21:02:17 +0900 Subject: [PATCH] Fix: `overrides` handle relative paths as expected (fixes #11577) --- .../config-array/override-tester.js | 25 ++++++++++++++++--- .../config-array/override-tester.js | 12 ++++----- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/lib/cli-engine/config-array/override-tester.js b/lib/cli-engine/config-array/override-tester.js index 2aaefac7d1c2..38dc7642de8b 100644 --- a/lib/cli-engine/config-array/override-tester.js +++ b/lib/cli-engine/config-array/override-tester.js @@ -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} 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)) )); } diff --git a/tests/lib/cli-engine/config-array/override-tester.js b/tests/lib/cli-engine/config-array/override-tester.js index f351cfaa197d..554ebb421d73 100644 --- a/tests/lib/cli-engine/config-array/override-tester.js +++ b/tests/lib/cli-engine/config-array/override-tester.js @@ -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"]); @@ -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/*"], []);