Skip to content

Commit 7f80c30

Browse files
committed
precompile minimatch matchers
1 parent ca01f07 commit 7f80c30

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

index.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
108119
NYC.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) {
119130
NYC.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

126147
NYC.prototype.addAllFiles = function () {

test/src/nyc-test.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ describe('nyc', function () {
8282
'test/**',
8383
'test{,-*}.js'
8484
])
85+
nyc.excludeMatchers = nyc._compileGlobPatterns(nyc.exclude)
8586

8687
var shouldInstrumentFile = nyc.shouldInstrumentFile.bind(nyc)
8788

@@ -133,9 +134,10 @@ describe('nyc', function () {
133134

134135
// Root package contains config.exclude
135136
// Restore exclude to default patterns
136-
nyc.include = nyc._prepGlobPatterns([
137+
nyc.include = nyc._compileGlobPatterns([
137138
'test.js'
138139
])
140+
nyc.includeMatchers = nyc._compileGlobPatterns(nyc.include)
139141

140142
var shouldInstrumentFile = nyc.shouldInstrumentFile.bind(nyc)
141143
shouldInstrumentFile('test.js', 'test.js').should.equal(true)

0 commit comments

Comments
 (0)