1 | 'use strict';
|
2 |
|
3 | var debug = require('debug')('analyze-css:propertyResets'),
|
4 | format = require('util').format,
|
5 | shorthandProperties = require('css-shorthand-properties');
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | function rule(analyzer) {
|
13 | var debug = require('debug');
|
14 |
|
15 | analyzer.setMetric('propertyResets');
|
16 |
|
17 | analyzer.on('selector', function(rule, selector) {
|
18 | var declarations = rule.declarations || [],
|
19 | properties;
|
20 |
|
21 |
|
22 | properties = declarations.
|
23 | map(function(declaration) {
|
24 | return (declaration.type === 'declaration') ? declaration.property : false;
|
25 | }).
|
26 | filter(function(item) {
|
27 | return item !== false;
|
28 | });
|
29 |
|
30 | debug('%s: %j', selector, properties);
|
31 |
|
32 |
|
33 |
|
34 | properties.forEach(function(property, idx) {
|
35 | var expanded;
|
36 |
|
37 |
|
38 | if (typeof shorthandProperties.shorthandProperties[property] === 'undefined') {
|
39 | return;
|
40 | }
|
41 |
|
42 |
|
43 |
|
44 | expanded = shorthandProperties.expand(property);
|
45 | debug('%s: %s', property, expanded.join(', '));
|
46 |
|
47 | expanded.forEach(function(expandedProperty) {
|
48 | var propertyPos = properties.indexOf(expandedProperty);
|
49 |
|
50 | if (propertyPos > -1 && propertyPos < idx) {
|
51 | analyzer.incrMetric('propertyResets');
|
52 | analyzer.addOffender('propertyResets', format('%s: "%s" resets "%s" property set earlier', selector, property, expandedProperty));
|
53 | }
|
54 | });
|
55 | });
|
56 | });
|
57 | }
|
58 |
|
59 | rule.description = 'Reports accidental property resets';
|
60 | module.exports = rule;
|