analyze-css
Advanced tools
Comparing version 0.7.0 to 0.8.0
@@ -27,2 +27,3 @@ #!/usr/bin/env node | ||
.describe('pretty', 'Causes JSON with the results to be pretty-printed').boolean('pretty').alias('pretty', 'p') | ||
.describe('no-offenders', 'Show only the metrics without the offenders part').boolean('no-offenders').alias('no-offenders', 'N') | ||
@@ -70,2 +71,3 @@ // version / help | ||
runnerOpts.ignoreSslErrors = argv['ignore-ssl-errors']; | ||
runnerOpts.noOffenders = argv['no-offenders'] || (argv.offenders === false); | ||
@@ -72,0 +74,0 @@ debug('opts: %j', runnerOpts); |
@@ -58,5 +58,9 @@ /** | ||
metrics: this.metrics, | ||
offenders: this.offenders | ||
}; | ||
// disable offenders output if requested (issue #64) | ||
if (options.noOffenders !== true) { | ||
res.offenders = this.offenders; | ||
} | ||
callback(null, res); | ||
@@ -63,0 +67,0 @@ } |
@@ -53,3 +53,4 @@ /** | ||
var analyzerOpts = { | ||
preprocessor: false | ||
'noOffenders': options.noOffenders, | ||
'preprocessor': false, | ||
}; | ||
@@ -56,0 +57,0 @@ |
{ | ||
"name": "analyze-css", | ||
"version": "0.7.0", | ||
"version": "0.8.0", | ||
"author": "Maciej Brencz <maciej.brencz@gmail.com> (https://github.com/macbre)", | ||
@@ -5,0 +5,0 @@ "description": "CSS selectors complexity and performance analyzer", |
@@ -46,3 +46,14 @@ analyze-css | ||
}); | ||
``` | ||
```js | ||
// options can be provided | ||
var opts = { | ||
'noOffenders': true | ||
}; | ||
new analyzer('.foo {margin: 0 !important}', opts, function(err, results) { | ||
console.error(err); | ||
console.log(results); // example? see below | ||
});``` | ||
``` | ||
@@ -74,2 +85,3 @@ | ||
"duplicatedSelectors": 7, | ||
"duplicatedProperties": 66, | ||
"emptyRules": 0, | ||
@@ -118,2 +130,3 @@ "expressions": 0, | ||
* **duplicatedSelectors**: number of CSS selectors defined more than once in CSS source | ||
* **duplicatedProperties**: number of CSS property definitions duplicated within a selector | ||
* **emptyRules**: number of rules with no properties (e.g. ``.foo { }``) | ||
@@ -120,0 +133,0 @@ * **expressions**: number of rules with CSS expressions (e.g. ``expression( document.body.clientWidth > 600 ? "600px" : "auto" )``) |
@@ -9,5 +9,7 @@ 'use strict'; | ||
var selectors = new collection(), | ||
mediaQueryStack = []; | ||
mediaQueryStack = [], | ||
browserPrefixRegEx = /^-(moz|o|webkit|ms)-/; | ||
analyzer.setMetric('duplicatedSelectors'); | ||
analyzer.setMetric('duplicatedProperties'); | ||
@@ -35,2 +37,37 @@ // handle nested media queries | ||
// find duplicated properties (issue #60) | ||
analyzer.on('rule', function(rule) { | ||
var propertiesHash = {}; | ||
if (rule.declarations) { | ||
rule.declarations.forEach(function(declaration) { | ||
var propertyName; | ||
if (declaration.type === 'declaration') { | ||
propertyName = declaration.property; | ||
// skip properties that require browser prefixes | ||
// background-image:-moz-linear-gradient(...) | ||
// background-image:-webkit-gradient(...) | ||
if (browserPrefixRegEx.test(declaration.value) === true) { | ||
return; | ||
} | ||
// property was already used in the current selector - report it | ||
if (propertiesHash[propertyName] === true) { | ||
// report the position of the offending property | ||
analyzer.setCurrentPosition(declaration.position); | ||
analyzer.incrMetric('duplicatedProperties'); | ||
analyzer.addOffender('duplicatedProperties', format('%s {%s: %s}', rule.selectors.join(', '), declaration.property, declaration.value)); | ||
} | ||
else { | ||
// mark given property as defined in the context of the current selector | ||
propertiesHash[propertyName] = true; | ||
} | ||
} | ||
}); | ||
} | ||
}); | ||
// special handling for @font-face (#52) | ||
@@ -61,3 +98,3 @@ // include URL when detecting duplicates | ||
rule.description = 'Reports duplicated CSS selectors'; | ||
rule.description = 'Reports duplicated CSS selectors and properties'; | ||
module.exports = rule; |
@@ -76,3 +76,34 @@ exports.tests = [ | ||
} | ||
} | ||
}, | ||
// duplicated properties | ||
{ | ||
css: '#foo { background: none; background-color: red;}', | ||
metrics: { | ||
duplicatedProperties: 0 | ||
} | ||
}, | ||
{ | ||
css: '#foo { background: none; background-color: red; background: transparent}', | ||
metrics: { | ||
duplicatedProperties: 1 | ||
} | ||
}, | ||
{ | ||
css: '#foo { color: #000; background: none; background-color: red; color: red; color: blue; background: none}', | ||
metrics: { | ||
duplicatedProperties: 3 // color x3, background x2 | ||
} | ||
}, | ||
{ | ||
css: 'button{background-color:#006cb0; background-image:-moz-linear-gradient(top,#008be3 35%,#006cb0 65%);background-image:-webkit-gradient(linear,0% 0%,0% 100%,color-stop(35%,#008be3),color-stop(65%,#006cb0));background-image:-o-linear-gradient(top,#008be3 35%,#006cb0 65%);background-image:-ms-linear-gradient(top,#008be3 35%,#006cb0 65%); border:1px solid #006cb0;border-radius:4px;}', | ||
metrics: { | ||
duplicatedProperties: 0 // browser prefixes should not be included | ||
} | ||
}, | ||
{ | ||
css: 'button{background-color:#006cb0; background:-moz-linear-gradient(top,#008be3 35%,#006cb0 65%);background:-webkit-gradient(linear,0% 0%,0% 100%,color-stop(35%,#008be3),color-stop(65%,#006cb0));background:-o-linear-gradient(top,#008be3 35%,#006cb0 65%);background:-ms-linear-gradient(top,#008be3 35%,#006cb0 65%); border:1px solid #006cb0;border-radius:4px;}', | ||
metrics: { | ||
duplicatedProperties: 0 // browser prefixes should not be included | ||
} | ||
}, | ||
]; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
155928
59
3600
177