-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathmatcher.ts
81 lines (66 loc) · 2.13 KB
/
matcher.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import { Pattern, MicromatchOptions, PatternRe } from '../../types';
import * as utils from '../../utils';
import Settings from '../../settings';
export type PatternSegment = StaticPatternSegment | DynamicPatternSegment;
type StaticPatternSegment = {
dynamic: false;
pattern: Pattern;
};
type DynamicPatternSegment = {
dynamic: true;
pattern: Pattern;
patternRe: PatternRe;
};
export type PatternSection = PatternSegment[];
export type PatternInfo = {
/**
* Indicates that the pattern has a globstar (more than a single section).
*/
complete: boolean;
pattern: Pattern;
segments: PatternSegment[];
sections: PatternSection[];
};
export default abstract class Matcher {
protected readonly _storage: PatternInfo[] = [];
constructor(private readonly _patterns: Pattern[], private readonly _settings: Settings, private readonly _micromatchOptions: MicromatchOptions) {
this._fillStorage();
}
private _fillStorage(): void {
/**
* The original pattern may include `{,*,**,a/*}`, which will lead to problems with matching (unresolved level).
* So, before expand patterns with brace expansion into separated patterns.
*/
const patterns = utils.pattern.expandPatternsWithBraceExpansion(this._patterns);
for (const pattern of patterns) {
const segments = this._getPatternSegments(pattern);
const sections = this._splitSegmentsIntoSections(segments);
this._storage.push({
complete: sections.length <= 1,
pattern,
segments,
sections
});
}
}
private _getPatternSegments(pattern: Pattern): PatternSegment[] {
const parts = utils.pattern.getPatternParts(pattern, this._micromatchOptions);
return parts.map((part) => {
const dynamic = utils.pattern.isDynamicPattern(part, this._settings);
if (!dynamic) {
return {
dynamic: false,
pattern: part
};
}
return {
dynamic: true,
pattern: part,
patternRe: utils.pattern.makeRe(part, this._micromatchOptions)
};
});
}
private _splitSegmentsIntoSections(segments: PatternSegment[]): PatternSection[] {
return utils.array.splitWhen(segments, (segment) => segment.dynamic && utils.pattern.hasGlobStar(segment.pattern));
}
}