1
1
module . exports = minimatch
2
2
minimatch . Minimatch = Minimatch
3
3
4
- var path = { sep : '/' }
5
- try {
6
- path = require ( 'path' )
7
- } catch ( er ) { }
4
+ const path = ( ( ) => { try { return require ( 'path' ) } catch ( e ) { } } ) ( ) || {
5
+ sep : '/'
6
+ }
7
+ minimatch . sep = path . sep
8
8
9
- var GLOBSTAR = minimatch . GLOBSTAR = Minimatch . GLOBSTAR = { }
10
- var expand = require ( 'brace-expansion' )
9
+ const GLOBSTAR = minimatch . GLOBSTAR = Minimatch . GLOBSTAR = { }
10
+ const expand = require ( 'brace-expansion' )
11
11
12
- var plTypes = {
12
+ const plTypes = {
13
13
'!' : { open : '(?:(?!(?:' , close : '))[^/]*?)' } ,
14
14
'?' : { open : '(?:' , close : ')?' } ,
15
15
'+' : { open : '(?:' , close : ')+' } ,
@@ -19,22 +19,22 @@ var plTypes = {
19
19
20
20
// any single thing other than /
21
21
// don't need to escape / when using new RegExp()
22
- var qmark = '[^/]'
22
+ const qmark = '[^/]'
23
23
24
24
// * => any number of characters
25
- var star = qmark + '*?'
25
+ const star = qmark + '*?'
26
26
27
27
// ** when dots are allowed. Anything goes, except .. and .
28
28
// not (^ or / followed by one or two dots followed by $ or /),
29
29
// followed by anything, any number of times.
30
- var twoStarDot = '(?:(?!(?:\\\/|^)(?:\\.{1,2})($|\\\/)).)*?'
30
+ const twoStarDot = '(?:(?!(?:\\\/|^)(?:\\.{1,2})($|\\\/)).)*?'
31
31
32
32
// not a ^ or / followed by a dot,
33
33
// followed by anything, any number of times.
34
- var twoStarNoDot = '(?:(?!(?:\\\/|^)\\.).)*?'
34
+ const twoStarNoDot = '(?:(?!(?:\\\/|^)\\.).)*?'
35
35
36
36
// characters that need to be escaped in RegExp.
37
- var reSpecials = charSet ( '().*{}+?[]^$\\!' )
37
+ const reSpecials = charSet ( '().*{}+?[]^$\\!' )
38
38
39
39
// "abc" -> { a:true, b:true, c:true }
40
40
function charSet ( s ) {
@@ -45,7 +45,7 @@ function charSet (s) {
45
45
}
46
46
47
47
// normalizes slashes.
48
- var slashSplit = / \/ + /
48
+ const slashSplit = / \/ + /
49
49
50
50
minimatch . filter = filter
51
51
function filter ( pattern , options ) {
@@ -58,41 +58,63 @@ function filter (pattern, options) {
58
58
function ext ( a , b ) {
59
59
a = a || { }
60
60
b = b || { }
61
- var t = { }
62
- Object . keys ( b ) . forEach ( function ( k ) {
63
- t [ k ] = b [ k ]
64
- } )
61
+ const t = { }
65
62
Object . keys ( a ) . forEach ( function ( k ) {
66
63
t [ k ] = a [ k ]
67
64
} )
65
+ Object . keys ( b ) . forEach ( function ( k ) {
66
+ t [ k ] = b [ k ]
67
+ } )
68
68
return t
69
69
}
70
70
71
71
minimatch . defaults = function ( def ) {
72
- if ( ! def || ! Object . keys ( def ) . length ) return minimatch
72
+ if ( ! def || typeof def !== 'object' || ! Object . keys ( def ) . length ) {
73
+ return minimatch
74
+ }
73
75
74
- var orig = minimatch
76
+ const orig = minimatch
75
77
76
- var m = function minimatch ( p , pattern , options ) {
77
- return orig . minimatch ( p , pattern , ext ( def , options ) )
78
+ const m = function minimatch ( p , pattern , options ) {
79
+ return orig ( p , pattern , ext ( def , options ) )
78
80
}
79
81
80
82
m . Minimatch = function Minimatch ( pattern , options ) {
81
83
return new orig . Minimatch ( pattern , ext ( def , options ) )
82
84
}
85
+ m . Minimatch . defaults = options => {
86
+ return orig . defaults ( ext ( def , options ) ) . Minimatch
87
+ }
88
+
89
+ m . filter = function filter ( pattern , options ) {
90
+ return orig . filter ( pattern , ext ( def , options ) )
91
+ }
92
+
93
+ m . defaults = function defaults ( options ) {
94
+ return orig . defaults ( ext ( def , options ) )
95
+ }
96
+
97
+ m . makeRe = function makeRe ( pattern , options ) {
98
+ return orig . makeRe ( pattern , ext ( def , options ) )
99
+ }
100
+
101
+ m . braceExpand = function braceExpand ( pattern , options ) {
102
+ return orig . braceExpand ( pattern , ext ( def , options ) )
103
+ }
104
+
105
+ m . match = function ( list , pattern , options ) {
106
+ return orig . match ( list , pattern , ext ( def , options ) )
107
+ }
83
108
84
109
return m
85
110
}
86
111
87
112
Minimatch . defaults = function ( def ) {
88
- if ( ! def || ! Object . keys ( def ) . length ) return Minimatch
89
113
return minimatch . defaults ( def ) . Minimatch
90
114
}
91
115
92
116
function minimatch ( p , pattern , options ) {
93
- if ( typeof pattern !== 'string' ) {
94
- throw new TypeError ( 'glob pattern string required' )
95
- }
117
+ assertValidPattern ( pattern )
96
118
97
119
if ( ! options ) options = { }
98
120
@@ -112,9 +134,7 @@ function Minimatch (pattern, options) {
112
134
return new Minimatch ( pattern , options )
113
135
}
114
136
115
- if ( typeof pattern !== 'string' ) {
116
- throw new TypeError ( 'glob pattern string required' )
117
- }
137
+ assertValidPattern ( pattern )
118
138
119
139
if ( ! options ) options = { }
120
140
pattern = pattern . trim ( )
@@ -242,19 +262,27 @@ function braceExpand (pattern, options) {
242
262
pattern = typeof pattern === 'undefined'
243
263
? this . pattern : pattern
244
264
245
- if ( typeof pattern === 'undefined' ) {
246
- throw new TypeError ( 'undefined pattern' )
247
- }
265
+ assertValidPattern ( pattern )
248
266
249
- if ( options . nobrace ||
250
- ! pattern . match ( / \{ .* \} / ) ) {
267
+ if ( options . nobrace || ! / \{ (?: (? ! \{ ) .) * \} / . test ( pattern ) ) {
251
268
// shortcut. no need to expand.
252
269
return [ pattern ]
253
270
}
254
271
255
272
return expand ( pattern )
256
273
}
257
274
275
+ const MAX_PATTERN_LENGTH = 1024 * 64
276
+ const assertValidPattern = pattern => {
277
+ if ( typeof pattern !== 'string' ) {
278
+ throw new TypeError ( 'invalid pattern' )
279
+ }
280
+
281
+ if ( pattern . length > MAX_PATTERN_LENGTH ) {
282
+ throw new TypeError ( 'pattern is too long' )
283
+ }
284
+ }
285
+
258
286
// parse a component of the expanded set.
259
287
// At this point, no pattern may contain "/" in it
260
288
// so we're going to return a 2d array, where each entry is the full
@@ -267,11 +295,9 @@ function braceExpand (pattern, options) {
267
295
// of * is equivalent to a single *. Globstar behavior is enabled by
268
296
// default, and can be disabled by setting options.noglobstar.
269
297
Minimatch . prototype . parse = parse
270
- var SUBPARSE = { }
298
+ const SUBPARSE = { }
271
299
function parse ( pattern , isSub ) {
272
- if ( pattern . length > 1024 * 64 ) {
273
- throw new TypeError ( 'pattern is too long' )
274
- }
300
+ assertValidPattern ( pattern )
275
301
276
302
var options = this . options
277
303
@@ -280,7 +306,7 @@ function parse (pattern, isSub) {
280
306
if ( pattern === '' ) return ''
281
307
282
308
var re = ''
283
- var hasMagic = ! ! options . nocase
309
+ var hasMagic = false
284
310
var escaping = false
285
311
// ? => one single character
286
312
var patternListStack = [ ]
@@ -332,10 +358,11 @@ function parse (pattern, isSub) {
332
358
}
333
359
334
360
switch ( c ) {
335
- case '/' :
361
+ case '/' : /* istanbul ignore next */ {
336
362
// completely not allowed, even escaped.
337
363
// Should already be path-split by now.
338
364
return false
365
+ }
339
366
340
367
case '\\' :
341
368
clearStateChar ( )
@@ -620,7 +647,7 @@ function parse (pattern, isSub) {
620
647
var flags = options . nocase ? 'i' : ''
621
648
try {
622
649
var regExp = new RegExp ( '^' + re + '$' , flags )
623
- } catch ( er ) {
650
+ } catch ( er ) /* istanbul ignore next - should be impossible */ {
624
651
// If it was an invalid regular expression, then it can't match
625
652
// anything. This trick looks for a character after the end of
626
653
// the string, which is of course impossible, except in multi-line
@@ -678,15 +705,15 @@ function makeRe () {
678
705
679
706
try {
680
707
this . regexp = new RegExp ( re , flags )
681
- } catch ( ex ) {
708
+ } catch ( ex ) /* istanbul ignore next - should be impossible */ {
682
709
this . regexp = false
683
710
}
684
711
return this . regexp
685
712
}
686
713
687
714
minimatch . match = function ( list , pattern , options ) {
688
715
options = options || { }
689
- var mm = new Minimatch ( pattern , options )
716
+ const mm = new Minimatch ( pattern , options )
690
717
list = list . filter ( function ( f ) {
691
718
return mm . match ( f )
692
719
} )
@@ -779,6 +806,7 @@ Minimatch.prototype.matchOne = function (file, pattern, partial) {
779
806
780
807
// should be impossible.
781
808
// some invalid regexp stuff in the set.
809
+ /* istanbul ignore if */
782
810
if ( p === false ) return false
783
811
784
812
if ( p === GLOBSTAR ) {
@@ -852,6 +880,7 @@ Minimatch.prototype.matchOne = function (file, pattern, partial) {
852
880
// no match was found.
853
881
// However, in partial mode, we can't say this is necessarily over.
854
882
// If there's more *pattern* left, then
883
+ /* istanbul ignore if */
855
884
if ( partial ) {
856
885
// ran out of file
857
886
this . debug ( '\n>>> no match, partial?' , file , fr , pattern , pr )
@@ -900,16 +929,16 @@ Minimatch.prototype.matchOne = function (file, pattern, partial) {
900
929
// this is ok if we're doing the match as part of
901
930
// a glob fs traversal.
902
931
return partial
903
- } else if ( pi === pl ) {
932
+ } else /* istanbul ignore else */ if ( pi === pl ) {
904
933
// ran out of pattern, still have file left.
905
934
// this is only acceptable if we're on the very last
906
935
// empty segment of a file with a trailing slash.
907
936
// a/* should match a/b/
908
- var emptyFileEnd = ( fi === fl - 1 ) && ( file [ fi ] === '' )
909
- return emptyFileEnd
937
+ return ( fi === fl - 1 ) && ( file [ fi ] === '' )
910
938
}
911
939
912
940
// should be unreachable.
941
+ /* istanbul ignore next */
913
942
throw new Error ( 'wtf?' )
914
943
}
915
944
0 commit comments