MCPcopy
hub / github.com/jestjs/jest / globsToMatcher

Function globsToMatcher

packages/jest-util/src/globsToMatcher.ts:35–97  ·  view source on GitHub ↗
(
  globs: Array<string>,
  picomatchOptions?: picomatch.PicomatchOptions,
)

Source from the content-addressed store, hash-verified

33 * isMatch('pizza.test.js'); // false
34 */
35export default function globsToMatcher(
36 globs: Array<string>,
37 picomatchOptions?: picomatch.PicomatchOptions,
38): Matcher {
39 const dot = picomatchOptions?.dot ?? true;
40 if (globs.length === 0) {
41 // Since there were no globs given, we can simply have a fast path here and
42 // return with a very simple function.
43 return () => false;
44 }
45
46 const matchers = globs.map(glob => {
47 if (!globsToMatchersMap.has(glob)) {
48 const isMatch = picomatch(glob, {dot, ...picomatchOptions}, true);
49
50 const matcher = {
51 isMatch,
52 // Matchers that are negated have different behavior than matchers that
53 // are not negated, so we need to store this information ahead of time.
54 negated: isMatch.state.negated || !!isMatch.state.negatedExtglob,
55 };
56
57 globsToMatchersMap.set(glob, matcher);
58 }
59
60 return globsToMatchersMap.get(glob)!;
61 });
62
63 return path => {
64 const replacedPath = replacePathSepForGlob(path);
65 let kept = undefined;
66 let negatives = 0;
67
68 for (const matcher of matchers) {
69 const {isMatch, negated} = matcher;
70
71 if (negated) {
72 negatives++;
73 }
74
75 const matched = isMatch(replacedPath);
76
77 if (!matched && negated) {
78 // The path was not matched, and the matcher is a negated matcher, so we
79 // want to omit the path. This means that the negative matcher is
80 // filtering the path out.
81 kept = false;
82 } else if (matched && !negated) {
83 // The path was matched, and the matcher is not a negated matcher, so we
84 // want to keep the path.
85 kept = true;
86 }
87 }
88
89 // If all of the globs were negative globs, then we want to include the path
90 // as long as it was not explicitly not kept. Otherwise only include
91 // the path if it was kept. This allows sets of globs that are all negated
92 // to allow some paths to be matched, while sets of globs that are mixed

Callers 7

normalizeFunction · 0.90
constructorMethod · 0.90
filterPathsWin32Method · 0.90
matchFilesWithGlobMethod · 0.90
isFileIncludedMethod · 0.90
shouldInstrumentFunction · 0.90

Calls 3

replacePathSepForGlobFunction · 0.85
setMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected