Skip to content

Commit

Permalink
Update: Improve perf not traversing default ignored dirs (fixes #5679) (
Browse files Browse the repository at this point in the history
  • Loading branch information
alberto authored and nzakas committed May 31, 2016
1 parent 320e8b0 commit fca0679
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 5 deletions.
42 changes: 38 additions & 4 deletions lib/ignored-paths.js
Expand Up @@ -24,9 +24,9 @@ debug = debug("eslint:ignored-paths");
//------------------------------------------------------------------------------

var ESLINT_IGNORE_FILENAME = ".eslintignore";
var DEFAULT_IGNORE_PATTERNS = [
"/node_modules/*",
"/bower_components/*"
var DEFAULT_IGNORE_DIRS = [
"node_modules/",
"bower_components/"
];
var DEFAULT_OPTIONS = {
dotfiles: false,
Expand Down Expand Up @@ -97,7 +97,9 @@ function IgnoredPaths(options) {
return ig.add(fs.readFileSync(filepath, "utf8"));
}

this.defaultPatterns = DEFAULT_IGNORE_PATTERNS.concat(options.patterns || []);
this.defaultPatterns = DEFAULT_IGNORE_DIRS.map(function(dir) {
return "/" + dir + "*";
}).concat(options.patterns || []);
this.baseDir = options.cwd;

this.ig = {
Expand Down Expand Up @@ -188,4 +190,36 @@ IgnoredPaths.prototype.contains = function(filepath, category) {

};

/**
* Returns a list of dir patterns for glob to ignore
* @returns {string[]} list of glob ignore patterns
*/
IgnoredPaths.prototype.getIgnoredFoldersGlobPatterns = function() {
var dirs = DEFAULT_IGNORE_DIRS;

if (this.options.ignore) {

/* eslint-disable no-underscore-dangle */

var patterns = this.ig.custom._rules.filter(function(rule) {
return rule.negative;
}).map(function(rule) {
return rule.origin;
});

/* eslint-enable no-underscore-dangle */

dirs = dirs.filter(function(dir) {
return patterns.every(function(p) {
return (p.indexOf("!" + dir) !== 0 && p.indexOf("!/" + dir) !== 0);
});
});
}


return dirs.map(function(dir) {
return dir + "**";
});
};

module.exports = IgnoredPaths;
3 changes: 2 additions & 1 deletion lib/util/glob-util.js
Expand Up @@ -154,7 +154,8 @@ function listFilesToProcess(globPatterns, options) {
ignoredPaths = new IgnoredPaths(options);
globOptions = {
nodir: true,
cwd: cwd
cwd: cwd,
ignore: ignoredPaths.getIgnoredFoldersGlobPatterns()
};

debug("Creating list of files to process.");
Expand Down
47 changes: 47 additions & 0 deletions tests/lib/ignored-paths.js
Expand Up @@ -529,4 +529,51 @@ describe("IgnoredPaths", function() {

});

describe("getIgnoredFoldersGlobPatterns", function() {
it("should return default ignores glob dir patterns when there is no eslintignore file", function() {
var ignoredPaths = new IgnoredPaths({ ignore: true, cwd: getFixturePath("no-ignore-file") });

var expected = ["node_modules/**", "bower_components/**"];
var actual = ignoredPaths.getIgnoredFoldersGlobPatterns();

assert.sameMembers(actual, expected);
});

it("should return default glob dir patterns when there is an ignore file without unignored defaults", function() {
var ignoredPaths = new IgnoredPaths({ ignore: true, ignorePath: getFixturePath(".eslintignore"), cwd: getFixturePath() });

var expected = ["node_modules/**", "bower_components/**"];
var actual = ignoredPaths.getIgnoredFoldersGlobPatterns();

assert.sameMembers(actual, expected);
});

it("should not return dirs with unignored defaults in ignore file", function() {
var ignoredPaths = new IgnoredPaths({ ignore: true, ignorePath: getFixturePath(".eslintignoreWithUnignoredDefaults"), cwd: getFixturePath() });

var actual = ignoredPaths.getIgnoredFoldersGlobPatterns();

assert.notInclude(actual, "node_modules/**");
assert.notInclude(actual, "bower_components/**");
});

it("should return dirs with unignored defaults in ignore file when ignore option is disabled", function() {
var ignoredPaths = new IgnoredPaths({ ignore: false, ignorePath: getFixturePath(".eslintignoreWithUnignoredDefaults"), cwd: getFixturePath() });

var expected = ["node_modules/**", "bower_components/**"];
var actual = ignoredPaths.getIgnoredFoldersGlobPatterns();

assert.includeMembers(actual, expected);
});

it("should not return dirs unignored by ignorePattern", function() {
var ignoredPaths = new IgnoredPaths({ ignore: true, cwd: getFixturePath("no-ignore-file"), ignorePattern: "!/node_modules/package" });

var actual = ignoredPaths.getIgnoredFoldersGlobPatterns();

assert.notInclude(actual, "node_modules/**");
assert.include(actual, "bower_components/**");
});
});

});

0 comments on commit fca0679

Please sign in to comment.