-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathselectors.js
67 lines (61 loc) · 1.63 KB
/
selectors.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**
* External dependencies
*/
const { parse } = require( 'postcss' );
const { getSpecificityArray } = require( '../utils/get-specificity' );
module.exports = function ( files = [] ) {
// let longest = 0;
const selectors = [];
files.forEach( ( { name, content } ) => {
const root = parse( content, { from: name } );
root.walkRules( function ( { selector } ) {
const selectorList = selector.split( ',' );
selectorList.forEach( ( selectorName ) => {
// Remove excess whitespace from selectors.
selectorName = selectorName.replace( /\s+/g, ' ' ).trim();
const [ a, b, c ] = getSpecificityArray( selectorName );
const sum = 100 * a + 10 * b + c; // eslint-disable-line no-mixed-operators
selectors.push( {
file: name,
selector: selectorName,
a,
b,
c,
sum,
} );
} );
} );
} );
// Reverse sort to be highest -> lowest.
selectors.sort( ( a, b ) => b.sum - a.sum );
const selectorsByLength = [ ...selectors ].sort(
( a, b ) => b.selector.length - a.selector.length
);
const selectorsWithIds = selectors.filter( ( { a } ) => a > 0 );
return {
audit: 'selectors',
name: 'Selectors',
results: [
{
id: 'count',
label: 'Total number of selectors',
value: selectors.length,
},
{
id: 'count-with-ids',
label: 'Number of selectors with IDs',
value: selectorsWithIds.length,
},
{
id: 'top-10-selectors',
label: 'Top 10 selectors with the highest specificity',
value: selectors.slice( 0, 10 ),
},
{
id: 'bottom-10-selectors',
label: 'Top 10 selectors by length',
value: selectorsByLength.slice( 0, 10 ),
},
],
};
};