@@ -35,12 +35,15 @@ function NYC (opts) {
3535
3636 // load exclude stanza from config.
3737 this . include = false
38+ this . includeMatchers = false
3839 if ( config . include ) {
3940 this . include = this . _prepGlobPatterns ( arrify ( config . include ) )
41+ this . includeMatchers = this . _compileGlobPatterns ( this . include )
4042 }
4143
4244 this . exclude = [ '**/node_modules/**' ] . concat ( arrify ( config . exclude || [ 'test/**' , 'test{,-*}.js' ] ) )
4345 this . exclude = this . _prepGlobPatterns ( this . exclude )
46+ this . excludeMatchers = this . _compileGlobPatterns ( this . exclude )
4447
4548 // require extensions can be provided as config in package.json.
4649 this . require = arrify ( config . require || opts . require )
@@ -105,6 +108,14 @@ NYC.prototype._prepGlobPatterns = function (patterns) {
105108 return result
106109}
107110
111+ NYC . prototype . _compileGlobPatterns = function ( patterns ) {
112+ return patterns . map ( matcher )
113+ }
114+
115+ function matcher ( pattern ) {
116+ return micromatch . matcher ( pattern )
117+ }
118+
108119NYC . prototype . addFile = function ( filename ) {
109120 var relFile = path . relative ( this . cwd , filename )
110121 var source = stripBom ( fs . readFileSync ( filename , 'utf8' ) )
@@ -119,8 +130,18 @@ NYC.prototype.addFile = function (filename) {
119130NYC . prototype . shouldInstrumentFile = function ( filename , relFile ) {
120131 relFile = relFile . replace ( / ^ \. \/ / , '' ) // remove leading './'.
121132
122- return ( ! this . include || micromatch . any ( filename , this . include ) || micromatch . any ( relFile , this . include ) ) &&
123- ! ( micromatch . any ( filename , this . exclude ) || micromatch . any ( relFile , this . exclude ) )
133+ return ( ! this . includeMatchers || someMatch ( this . includeMatchers , filename , relFile ) ) && ! someMatch ( this . excludeMatchers , filename , relFile )
134+ }
135+
136+ function someMatch ( matcherList , filename , relFile ) {
137+ var len = matcherList . length
138+ for ( var i = 0 ; i < len ; i ++ ) {
139+ var matcher = matcherList [ i ]
140+ if ( matcher ( filename ) || matcher ( relFile ) ) {
141+ return true
142+ }
143+ }
144+ return false
124145}
125146
126147NYC . prototype . addAllFiles = function ( ) {
0 commit comments