-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8327 from loganfsmyth/drop-pattern-matching
Treat string ignore/only/test/include/exclude values as paths with only basic pattern matching
- Loading branch information
Showing
12 changed files
with
125 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// @flow | ||
import path from "path"; | ||
import escapeRegExp from "lodash/escapeRegExp"; | ||
|
||
const sep = `\\${path.sep}`; | ||
const endSep = `(?:${sep}|$)`; | ||
|
||
const substitution = `[^${sep}]+`; | ||
|
||
const starPat = `(?:${substitution}${sep})`; | ||
const starPatLast = `(?:${substitution}${endSep})`; | ||
|
||
const starStarPat = `${starPat}*?`; | ||
const starStarPatLast = `${starPat}*?${starPatLast}?`; | ||
|
||
/** | ||
* Implement basic pattern matching that will allow users to do the simple | ||
* tests with * and **. If users want full complex pattern matching, then can | ||
* always use regex matching, or function validation. | ||
*/ | ||
export default function pathToPattern( | ||
pattern: string, | ||
dirname: string, | ||
): RegExp { | ||
const parts = path.resolve(dirname, pattern).split(path.sep); | ||
|
||
return new RegExp( | ||
[ | ||
"^", | ||
...parts.map((part, i) => { | ||
const last = i === parts.length - 1; | ||
|
||
// ** matches 0 or more path parts. | ||
if (part === "**") return last ? starStarPatLast : starStarPat; | ||
|
||
// * matches 1 path part. | ||
if (part === "*") return last ? starPatLast : starPat; | ||
|
||
// *.ext matches a wildcard with an extension. | ||
if (part.indexOf("*.") === 0) { | ||
return ( | ||
substitution + escapeRegExp(part.slice(1)) + (last ? endSep : sep) | ||
); | ||
} | ||
|
||
// Otherwise match the pattern text. | ||
return escapeRegExp(part) + (last ? endSep : sep); | ||
}), | ||
].join(""), | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.