From af4fd69dd05799f4cb6701ba91ef32d028373d87 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Fri, 15 May 2015 02:33:22 +0100 Subject: [PATCH] make path patterns used by only and ignore **very** liberal, this will ease a lot of pain in dealing with them --- CHANGELOG.md | 5 +++++ src/babel/util.js | 51 +++++++++++++++++++---------------------------- test/core/util.js | 9 ++++++++- 3 files changed, 33 insertions(+), 32 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 902fffc915..720a94858a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,11 @@ _Note: Gaps between patch versions are faulty/broken releases._ See [CHANGELOG - 6to5](CHANGELOG-6to5.md) for the pre-4.0.0 version changelog. +## 5.4.2 + + * **Polish** + * Make `ignore` and `only` patterns even less strict. The pattern can now exist anywhere in the path. + ## 5.4.1 * **Bug Fix** diff --git a/src/babel/util.js b/src/babel/util.js index ba0bc1ad3d..6ee855709d 100644 --- a/src/babel/util.js +++ b/src/babel/util.js @@ -1,7 +1,7 @@ import "./patch"; import escapeRegExp from "lodash/string/escapeRegExp"; - +import startsWith from "lodash/string/startsWith"; import cloneDeep from "lodash/lang/cloneDeep"; import isBoolean from "lodash/lang/isBoolean"; import * as messages from "./messages"; @@ -69,9 +69,22 @@ export function list(val: string): Array { export function regexify(val: any): RegExp { if (!val) return new RegExp(/.^/); + if (Array.isArray(val)) val = new RegExp(val.map(escapeRegExp).join("|"), "i"); - if (isString(val)) return minimatch.makeRe(val, { nocase: true }); + + if (isString(val)) { + // normalise path separators + val = slash(val); + + // remove relative separator if present + if (startsWith(val, "./")) val = val.slice(2); + + var regex = minimatch.makeRe(val, { nocase: true }); + return new RegExp(regex.source.slice(1, -1), "i"); + } + if (isRegExp(val)) return val; + throw new TypeError("illegal type for regexify"); } @@ -95,40 +108,16 @@ export function booleanify(val: any): boolean { } export function shouldIgnore(filename, ignore, only) { - if (!ignore.length && !only.length) return false; - filename = slash(filename); - var filenames = []; - - // try and match each section of the path - var parts = filename.split("/"); - for (var i = 0; i < parts.length; i++) { - var part = parts[i]; - if (!part) continue; - - filenames.push(part); - filenames.push(parts.slice(0, i + 1).join("/")); - } - if (only.length) { - var matches = false; - - for (let filename of (filenames: Array)) { - for (var pattern of (only: Array)) { - if (pattern.test(filename)) { - matches = true; - break; - } - } + for (var pattern of (only: Array)) { + if (pattern.test(filename)) return false; } - - return !matches; + return true; } else if (ignore.length) { - for (let filename of (filenames: Array)) { - for (var pattern of (ignore: Array)) { - if (pattern.test(filename)) return true; - } + for (var pattern of (ignore: Array)) { + if (pattern.test(filename)) return true; } } diff --git a/test/core/util.js b/test/core/util.js index 5c1ec25459..4588f728fe 100644 --- a/test/core/util.js +++ b/test/core/util.js @@ -80,9 +80,16 @@ suite("util", function () { assert.deepEqual(util.regexify(null), /.^/); assert.deepEqual(util.regexify(""), /.^/); assert.deepEqual(util.regexify(["foo", "bar"]), /foo|bar/i); - assert.deepEqual(util.regexify("foobar"), /^(?:(?=.)foobar)$/i); + assert.deepEqual(util.regexify("foobar"), /(?:(?=.)foobar)/i); assert.deepEqual(util.regexify(/foobar/), /foobar/); + assert.ok(util.regexify("foo/bar").test("bar/foo/bar")); + assert.ok(util.regexify("foo/*").test("foo/bar.js")); + assert.ok(util.regexify("*.js").test("bar.js")); + assert.ok(util.regexify("./foo").test("foo")); + assert.ok(util.regexify("./foo/bar.js").test("foo/bar.js")); + assert.ok(util.regexify("foobar").test("foobar")); + assert.throws(function () { util.regexify({}); }, /illegal type for regexify/);