|
| 1 | +/** |
| 2 | + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +import micromatch = require('micromatch'); |
| 9 | +import type {Config} from '@jest/types'; |
| 10 | +import replacePathSepForGlob from './replacePathSepForGlob'; |
| 11 | + |
| 12 | +const globsToMatchersMap = new Map< |
| 13 | + string, |
| 14 | + { |
| 15 | + isMatch: (str: string) => boolean; |
| 16 | + negated: boolean; |
| 17 | + } |
| 18 | +>(); |
| 19 | + |
| 20 | +const micromatchOptions = {dot: true}; |
| 21 | + |
| 22 | +/** |
| 23 | + * Converts a list of globs into a function that matches a path against the |
| 24 | + * globs. |
| 25 | + * |
| 26 | + * Every time micromatch is called, it will parse the glob strings and turn |
| 27 | + * them into regexp instances. Instead of calling micromatch repeatedly with |
| 28 | + * the same globs, we can use this function which will build the micromatch |
| 29 | + * matchers ahead of time and then have an optimized path for determining |
| 30 | + * whether an individual path matches. |
| 31 | + * |
| 32 | + * This function is intended to match the behavior of `micromatch()`. |
| 33 | + * |
| 34 | + * @example |
| 35 | + * const isMatch = globsToMatcher(['*.js', '!*.test.js']); |
| 36 | + * isMatch('pizza.js'); // true |
| 37 | + * isMatch('pizza.test.js'); // false |
| 38 | + */ |
| 39 | +export default function globsToMatcher( |
| 40 | + globs: Array<Config.Glob>, |
| 41 | +): (path: Config.Path) => boolean { |
| 42 | + if (globs.length === 0) { |
| 43 | + // Since there were no globs given, we can simply have a fast path here and |
| 44 | + // return with a very simple function. |
| 45 | + return (_: Config.Path): boolean => false; |
| 46 | + } |
| 47 | + |
| 48 | + const matchers = globs.map(glob => { |
| 49 | + if (!globsToMatchersMap.has(glob)) { |
| 50 | + // Matchers that are negated have different behavior than matchers that |
| 51 | + // are not negated, so we need to store this information ahead of time. |
| 52 | + const {negated} = micromatch.scan(glob, micromatchOptions); |
| 53 | + |
| 54 | + const matcher = { |
| 55 | + isMatch: micromatch.matcher(glob, micromatchOptions), |
| 56 | + negated, |
| 57 | + }; |
| 58 | + |
| 59 | + globsToMatchersMap.set(glob, matcher); |
| 60 | + } |
| 61 | + |
| 62 | + return globsToMatchersMap.get(glob)!; |
| 63 | + }); |
| 64 | + |
| 65 | + return (path: Config.Path): boolean => { |
| 66 | + const replacedPath = replacePathSepForGlob(path); |
| 67 | + let kept = undefined; |
| 68 | + let negatives = 0; |
| 69 | + |
| 70 | + for (let i = 0; i < matchers.length; i++) { |
| 71 | + const {isMatch, negated} = matchers[i]; |
| 72 | + |
| 73 | + if (negated) { |
| 74 | + negatives++; |
| 75 | + } |
| 76 | + |
| 77 | + const matched = isMatch(replacedPath); |
| 78 | + |
| 79 | + if (!matched && negated) { |
| 80 | + // The path was not matched, and the matcher is a negated matcher, so we |
| 81 | + // want to omit the path. This means that the negative matcher is |
| 82 | + // filtering the path out. |
| 83 | + kept = false; |
| 84 | + } else if (matched && !negated) { |
| 85 | + // The path was matched, and the matcher is not a negated matcher, so we |
| 86 | + // want to keep the path. |
| 87 | + kept = true; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + // If all of the globs were negative globs, then we want to include the path |
| 92 | + // as long as it was not explicitly not kept. Otherwise only include |
| 93 | + // the path if it was kept. This allows sets of globs that are all negated |
| 94 | + // to allow some paths to be matched, while sets of globs that are mixed |
| 95 | + // negated and non-negated to cause the negated matchers to only omit paths |
| 96 | + // and not keep them. |
| 97 | + return negatives === matchers.length ? kept !== false : !!kept; |
| 98 | + }; |
| 99 | +} |
0 commit comments