Skip to content
Permalink
Browse files

Breaking: migrate from minimatch to node-ignore (fixes #2365)

  • Loading branch information...
gronke committed Dec 2, 2015
1 parent 2efd8fd commit d43f26c270f0070ca47000690d874d126d67fd89
@@ -558,18 +558,20 @@ You can tell ESLint to ignore specific files and directories by creating an `.es

When ESLint is run, it looks in the current working directory to find an `.eslintignore` file before determining which files to lint. If this file is found, then those preferences are applied when traversing directories. Only one `.eslintignore` file can be used at a time, so `.eslintignore` files other than the one in the current working directory will not be used.

Globs are matched using [minimatch](https://github.com/isaacs/minimatch), so a number of features are available:
Globs are matched using [node-ignore](https://github.com/kaelzhang/node-ignore), so a number of features are available:

* Lines beginning with `#` are treated as comments and do not affect ignore patterns.
* Paths are relative to `.eslintignore` location or the current working directory. This also influences paths passed via `--ignore-pattern`.
* Ignore patterns behave according to the `.gitignore` [specification](http://git-scm.com/docs/gitignore)
* Lines preceded by `!` are negated patterns that re-include a pattern that was ignored by an earlier pattern.
* Brace expansion can refer to multiple files in a pattern. For example, `file.{js,ts,coffee}` will ignore `file.js`, `file.ts`, and `file.coffee`.

In addition to any patterns in a `.eslintignore` file, ESLint always ignores files in `node_modules/**` and `bower_components/**`.
In addition to any patterns in a `.eslintignore` file, ESLint always ignores files in `/node_modules/**` and `/bower_components/**`.

For example, placing the following `.eslintignore` file in the current working directory will ignore all of `node_modules`, `bower_components`, any files with the extensions `.ts.js` or `.coffee.js` extension that might have been transpiled, and anything in the `build/` directory except `build/index.js`:

```text
# node_modules and bower_components ignored by default
# /node_modules and /bower_components ignored by default
# Ignore files compiled from TypeScript and CoffeeScript
**/*.{ts,coffee}.js
@@ -22,6 +22,7 @@ var fs = require("fs"),

assign = require("object-assign"),
debug = require("debug"),
glob = require("glob"),
shell = require("shelljs"),

rules = require("./rules"),
@@ -76,7 +77,6 @@ var DEFAULT_PARSER = require("../conf/eslint.json").parser;
// Private
//------------------------------------------------------------------------------


var defaultOptions = {
configFile: null,
baseConfig: false,
@@ -97,6 +97,7 @@ var defaultOptions = {
cacheFile: ".eslintcache",
fix: false,
allowInlineConfig: true,
warnIgnoredByDefault: true,
cwd: process.cwd()
},
loadedPlugins = Object.create(null);
@@ -287,7 +288,7 @@ function createIgnoreResult(filePath) {
{
fatal: false,
severity: 1,
message: "File ignored because of your .eslintignore file. Use --no-ignore to override."
message: "File ignored because of a matching ignore pattern. Use --no-ignore to override."
}
],
errorCount: 0,
@@ -391,12 +392,13 @@ function getCacheFile(cacheFile, cwd) {
*/
function CLIEngine(options) {

options = assign(Object.create(null), defaultOptions, options);

/**
* Stored options for this instance
* @type {Object}
*/
this.options = assign(Object.create(defaultOptions), options || {});

this.options = options;

var cacheFile = getCacheFile(this.options.cacheLocation || this.options.cacheFile, this.options.cwd);

@@ -537,13 +539,25 @@ CLIEngine.prototype = {
options = this.options,
fileCache = this._fileCache,
configHelper = new Config(options),
ignoredPaths = new IgnoredPaths(options),
globOptions,
stats,
startTime,
prevConfig; // the previous configuration used

startTime = Date.now();

patterns = this.resolveFileGlobPatterns(patterns);
globOptions = {
nodir: true
};

var cwd = options.cwd || process.cwd;
patterns = this.resolveFileGlobPatterns(patterns.map(function(pattern) {
if (pattern.indexOf("/") !== 0) {
return path.join(cwd, pattern);
}
return pattern;
}));

/**
* Calculates the hash of the config file used to validate a given file
@@ -575,11 +589,31 @@ CLIEngine.prototype = {
* Executes the linter on a file defined by the `filename`. Skips
* unsupported file extensions and any files that are already linted.
* @param {string} filename The resolved filename of the file to be linted
* @param {boolean} warnIgnored always warn when a file is ignored
* @returns {void}
*/
function executeOnFile(filename) {
function executeOnFile(filename, warnIgnored) {
var hashOfConfig;

if (options.ignore !== false) {

if (ignoredPaths.contains(filename, "custom")) {
if (warnIgnored) {
results.push(createIgnoreResult(filename));
}
return;
}

if (ignoredPaths.contains(filename, "default")) {
if (options.warnIgnoredByDefault || warnIgnored) {
results.push(createIgnoreResult(filename));
}
return;
}

}

filename = path.resolve(filename);
if (processed[filename]) {
return;
}
@@ -639,18 +673,19 @@ CLIEngine.prototype = {
results.push(res);
}

// Lint each desired file
globUtil.listFilesToProcess(patterns, options).forEach(executeOnFile);
patterns.forEach(function(pattern) {

// only warn for files explicitly passed on the command line
if (options.ignore) {
patterns.forEach(function(file) {
var fullPath = path.resolve(file);
if (shell.test("-f", fullPath) && !processed[fullPath]) {
results.push(createIgnoreResult(file));
}
});
}
var file = path.resolve(pattern);

if (shell.test("-f", file)) {
executeOnFile(fs.realpathSync(pattern), !shell.test("-d", file));
} else {
glob.sync(pattern, globOptions).forEach(function(globMatch) {
executeOnFile(globMatch, false);
});
}

});

stats = calculateStatsPerRun(results);

@@ -680,10 +715,9 @@ CLIEngine.prototype = {
stats,
options = this.options,
configHelper = new Config(options),
ignoredPaths = IgnoredPaths.load(options),
exclude = ignoredPaths.contains.bind(ignoredPaths);
ignoredPaths = new IgnoredPaths(options);

if (filename && options.ignore && exclude(filename)) {
if (filename && (options.ignore !== false) && ignoredPaths.contains(filename)) {
results.push(createIgnoreResult(filename));
} else {
results.push(processText(text, configHelper, filename, options.fix, options.allowInlineConfig));
@@ -719,7 +753,7 @@ CLIEngine.prototype = {
var ignoredPaths;

if (this.options.ignore) {
ignoredPaths = IgnoredPaths.load(this.options);
ignoredPaths = new IgnoredPaths(this.options);
return ignoredPaths.contains(filePath);
}

@@ -45,7 +45,7 @@ function getDirectoryEntries(directory) {
*/
function FileFinder(files, cwd) {
this.fileNames = Array.isArray(files) ? files : [files];
this.cwd = cwd;
this.cwd = cwd || process.cwd();
this.cache = {};
}

Oops, something went wrong.

0 comments on commit d43f26c

Please sign in to comment.
You can’t perform that action at this time.