Skip to content

Commit

Permalink
precompile minimatch matchers
Browse files Browse the repository at this point in the history
  • Loading branch information
jamestalmage committed Dec 18, 2015
1 parent ca01f07 commit 7f80c30
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
25 changes: 23 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,15 @@ function NYC (opts) {

// load exclude stanza from config.
this.include = false
this.includeMatchers = false
if (config.include) {
this.include = this._prepGlobPatterns(arrify(config.include))
this.includeMatchers = this._compileGlobPatterns(this.include)
}

this.exclude = ['**/node_modules/**'].concat(arrify(config.exclude || ['test/**', 'test{,-*}.js']))
this.exclude = this._prepGlobPatterns(this.exclude)
this.excludeMatchers = this._compileGlobPatterns(this.exclude)

// require extensions can be provided as config in package.json.
this.require = arrify(config.require || opts.require)
Expand Down Expand Up @@ -105,6 +108,14 @@ NYC.prototype._prepGlobPatterns = function (patterns) {
return result
}

NYC.prototype._compileGlobPatterns = function (patterns) {
return patterns.map(matcher)
}

function matcher (pattern) {
return micromatch.matcher(pattern)
}

NYC.prototype.addFile = function (filename) {
var relFile = path.relative(this.cwd, filename)
var source = stripBom(fs.readFileSync(filename, 'utf8'))
Expand All @@ -119,8 +130,18 @@ NYC.prototype.addFile = function (filename) {
NYC.prototype.shouldInstrumentFile = function (filename, relFile) {
relFile = relFile.replace(/^\.\//, '') // remove leading './'.

return (!this.include || micromatch.any(filename, this.include) || micromatch.any(relFile, this.include)) &&
!(micromatch.any(filename, this.exclude) || micromatch.any(relFile, this.exclude))
return (!this.includeMatchers || someMatch(this.includeMatchers, filename, relFile)) && !someMatch(this.excludeMatchers, filename, relFile)
}

function someMatch (matcherList, filename, relFile) {
var len = matcherList.length
for (var i = 0; i < len; i++) {
var matcher = matcherList[i]
if (matcher(filename) || matcher(relFile)) {
return true
}
}
return false
}

NYC.prototype.addAllFiles = function () {
Expand Down
4 changes: 3 additions & 1 deletion test/src/nyc-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ describe('nyc', function () {
'test/**',
'test{,-*}.js'
])
nyc.excludeMatchers = nyc._compileGlobPatterns(nyc.exclude)

var shouldInstrumentFile = nyc.shouldInstrumentFile.bind(nyc)

Expand Down Expand Up @@ -133,9 +134,10 @@ describe('nyc', function () {

// Root package contains config.exclude
// Restore exclude to default patterns
nyc.include = nyc._prepGlobPatterns([
nyc.include = nyc._compileGlobPatterns([
'test.js'
])
nyc.includeMatchers = nyc._compileGlobPatterns(nyc.include)

var shouldInstrumentFile = nyc.shouldInstrumentFile.bind(nyc)
shouldInstrumentFile('test.js', 'test.js').should.equal(true)
Expand Down

0 comments on commit 7f80c30

Please sign in to comment.