Skip to content

Commit

Permalink
Added CLI option for ignoring rules (fixes #231)
Browse files Browse the repository at this point in the history
  • Loading branch information
nzakas committed Jul 23, 2012
1 parent e025e5a commit d215467
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
27 changes: 23 additions & 4 deletions src/cli/common.js
Expand Up @@ -26,9 +26,8 @@ function cli(api){
* @param options {Object} The CLI options. * @param options {Object} The CLI options.
* @return {Object} A ruleset object. * @return {Object} A ruleset object.
*/ */
function gatherRules(options){ function gatherRules(options, ruleset){
var ruleset, var warnings = options.rules || options.warnings,
warnings = options.rules || options.warnings,
errors = options.errors; errors = options.errors;


if (warnings){ if (warnings){
Expand All @@ -44,6 +43,24 @@ function cli(api){
ruleset[value] = 2; ruleset[value] = 2;
}); });
} }

return ruleset;
}

/**
* Filters out rules using the ignore command line option.
* @param options {Object} the CLI options
* @return {Object} A ruleset object.
*/
function filterRules(options) {
var ignore = options.ignore,
ruleset = CSSLint.getRuleset();

if (ignore) {
ignore.split(",").forEach(function(value){
delete ruleset[value];
});
}


return ruleset; return ruleset;
} }
Expand All @@ -68,7 +85,8 @@ function cli(api){
*/ */
function processFile(relativeFilePath, options) { function processFile(relativeFilePath, options) {
var input = api.readFile(relativeFilePath), var input = api.readFile(relativeFilePath),
result = CSSLint.verify(input, gatherRules(options)), ruleset = filterRules(options),
result = CSSLint.verify(input, gatherRules(options, ruleset)),
formatter = CSSLint.getFormatter(options.format || "text"), formatter = CSSLint.getFormatter(options.format || "text"),
messages = result.messages || [], messages = result.messages || [],
output, output,
Expand Down Expand Up @@ -113,6 +131,7 @@ function cli(api){
" --quiet Only output when errors are present.", " --quiet Only output when errors are present.",
" --errors=<rule[,rule]+> Indicate which rules to include as errors.", " --errors=<rule[,rule]+> Indicate which rules to include as errors.",
" --warnings=<rule[,rule]+> Indicate which rules to include as warnings.", " --warnings=<rule[,rule]+> Indicate which rules to include as warnings.",
" --ignore=<rule,[,rule]+> Indicate which rules to ignore completely.",
" --version Outputs the current version number." " --version Outputs the current version number."
].join("\n") + "\n"); ].join("\n") + "\n");
} }
Expand Down
23 changes: 19 additions & 4 deletions src/core/CSSLint.js
Expand Up @@ -45,6 +45,23 @@ var CSSLint = (function(){
return a.id > b.id ? 1 : 0; return a.id > b.id ? 1 : 0;
}); });
}; };

/**
* Returns a ruleset configuration object with all current rules.
* @return A ruleset object.
* @method getRuleset
*/
api.getRuleset = function() {
var ruleset = {},
i = 0,
len = rules.length;

while (i < len){
ruleset[rules[i++].id] = 1; //by default, everything is a warning
}

return ruleset;
};


//------------------------------------------------------------------------- //-------------------------------------------------------------------------
// Formatters // Formatters
Expand Down Expand Up @@ -125,13 +142,11 @@ var CSSLint = (function(){
parser = new parserlib.css.Parser({ starHack: true, ieFilters: true, parser = new parserlib.css.Parser({ starHack: true, ieFilters: true,
underscoreHack: true, strict: false }); underscoreHack: true, strict: false });


// normalize line endings
lines = text.replace(/\n\r?/g, "$split$").split('$split$'); lines = text.replace(/\n\r?/g, "$split$").split('$split$');


if (!ruleset){ if (!ruleset){
ruleset = {}; ruleset = this.getRuleset();
while (i < len){
ruleset[rules[i++].id] = 1; //by default, everything is a warning
}
} }


reporter = new Reporter(lines, ruleset); reporter = new Reporter(lines, ruleset);
Expand Down

0 comments on commit d215467

Please sign in to comment.