Skip to content
This repository has been archived by the owner on Mar 23, 2024. It is now read-only.

Better configuration, plugin support #731

Closed
wants to merge 11 commits into from
67 changes: 24 additions & 43 deletions lib/checker.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ var StringChecker = require('./string-checker');
var utils = require('util');
var path = require('path');

var additionalRules = require('./options/additional-rules');
var excludeFiles = require('./options/exclude-files');
var fileExtensions = require('./options/file-extensions');
var esnext = require('./options/esnext');
var NodeConfiguration = require('./config/node-configuration');

/**
* Starts Code Style checking process.
Expand All @@ -26,14 +23,9 @@ utils.inherits(Checker, StringChecker);
* @param {Object} config
*/
Checker.prototype.configure = function(config) {
var cwd = config.configPath ? path.dirname(config.configPath) : process.cwd();

fileExtensions(config, this);
excludeFiles(config, this, cwd);
additionalRules(config, this, cwd);
esnext(config);

StringChecker.prototype.configure.apply(this, arguments);

this._fileExtensions = this._configuration.getFileExtensions();
};

/**
Expand All @@ -43,18 +35,13 @@ Checker.prototype.configure = function(config) {
* @returns {Promise * Errors}
*/
Checker.prototype.checkFile = function(path) {
var _this = this;

if (!_this._isExcluded(path)) {
if (!this._configuration.isFileExcluded(path)) {
return vowFs.read(path, 'utf8').then(function(data) {
return _this.checkString(data, path);
});
return this.checkString(data, path);
}, this);
}

var defer = Vow.defer();
defer.resolve(null);

return defer.promise();
return Vow.resolve(null);
};

/**
Expand All @@ -64,38 +51,35 @@ Checker.prototype.checkFile = function(path) {
* @returns {Promise * Error[]}
*/
Checker.prototype.checkDirectory = function(path) {
var _this = this;

return vowFs.listDir(path).then(function(filenames) {
return Vow.all(filenames.map(function(filename) {
var fullname = path + '/' + filename;

// check for exclude path
if (_this._isExcluded(fullname)) {
if (this._configuration.isFileExcluded(fullname)) {
return [];
}

return vowFs.stat(fullname).then(function(stat) {
if (stat.isDirectory()) {
return _this.checkDirectory(fullname);
return this.checkDirectory(fullname);
}

if (!_this._hasCorrectExtension(fullname)) {
if (!this._hasCorrectExtension(fullname)) {
return [];
}

return Vow.when(_this.checkFile(fullname)).then(function(errors) {
return Vow.when(this.checkFile(fullname)).then(function(errors) {
if (errors) {
return errors;
}

return [];
});
});
})).then(function(results) {
}, this);
}, this)).then(function(results) {
return [].concat.apply([], results);
});
});
}, this);
};

/**
Expand Down Expand Up @@ -154,19 +138,6 @@ Checker.prototype.checkStdin = function() {
return deferred.promise();
};

/**
* Returns true if specified path is in excluded list.
*
* @returns {Boolean}
*/
Checker.prototype._isExcluded = function(testPath) {
testPath = path.resolve(testPath);

return !this._excludes.every(function(exclude) {
return !exclude.match(testPath);
});
};

/**
* Returns true if the file extension matches a file extension to process.
*
Expand All @@ -183,4 +154,14 @@ Checker.prototype._hasCorrectExtension = function(testPath) {
);
};

/**
* Returns new configuration instance.
*
* @protected
* @returns {Configuration}
*/
Checker.prototype._createConfiguration = function() {
return new NodeConfiguration();
};

module.exports = Checker;
11 changes: 5 additions & 6 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
*/
var Checker = require('./checker');
var configFile = require('./cli-config');
var preset = require('./options/preset');

var Vow = require('vow');
var supportsColor = require('supports-color');
Expand Down Expand Up @@ -63,8 +62,8 @@ module.exports = function(program) {
return returnArgs;
}

if (program.preset && !preset.exists(program.preset)) {
console.error(preset.getDoesNotExistError(program.preset));
if (program.preset && !checker.getConfiguration().hasPreset(program.preset)) {
console.error('Preset "%s" does not exist', program.preset);
defer.reject(1);

return returnArgs;
Expand All @@ -82,11 +81,11 @@ module.exports = function(program) {
}

if (program.preset) {
config.preset = program.preset;
checker.getConfiguration().override({preset: program.preset});
}

if (program.maxErrors) {
config.maxErrors = Number(program.maxErrors);
checker.getConfiguration().override({maxErrors: Number(program.maxErrors)});
}

if (program.reporter) {
Expand All @@ -113,7 +112,7 @@ module.exports = function(program) {
return returnArgs;
}

checker.registerDefaultRules();
checker.getConfiguration().registerDefaultRules();
checker.configure(config);

// Handle usage like 'cat myfile.js | jscs' or 'jscs -''
Expand Down
Loading