-
Notifications
You must be signed in to change notification settings - Fork 129
/
Copy pathno-unused-styles.js
65 lines (54 loc) · 1.69 KB
/
no-unused-styles.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
/**
* @fileoverview Detects unused styles
* @author Tom Hastjarjanto
*/
'use strict';
const Components = require('../util/Components');
const styleSheet = require('../util/stylesheet');
const StyleSheets = styleSheet.StyleSheets;
const astHelpers = styleSheet.astHelpers;
module.exports = Components.detect((context, components) => {
const styleSheets = new StyleSheets();
const styleReferences = new Set();
function reportUnusedStyles(unusedStyles) {
Object.keys(unusedStyles).forEach((key) => {
if ({}.hasOwnProperty.call(unusedStyles, key)) {
const styles = unusedStyles[key];
styles.forEach((node) => {
const message = [
'Unused style detected: ',
key,
'.',
node.key.name,
].join('');
context.report(node, message);
});
}
});
}
return {
MemberExpression: function (node) {
const styleRef = astHelpers.getPotentialStyleReferenceFromMemberExpression(node);
if (styleRef) {
styleReferences.add(styleRef);
}
},
VariableDeclarator: function (node) {
if (astHelpers.isStyleSheetDeclaration(node, context.settings)) {
const styleSheetName = astHelpers.getStyleSheetName(node);
const styles = astHelpers.getStyleDeclarations(node);
styleSheets.add(styleSheetName, styles);
}
},
'Program:exit': function () {
const list = components.all();
if (Object.keys(list).length > 0) {
styleReferences.forEach((reference) => {
styleSheets.markAsUsed(reference);
});
reportUnusedStyles(styleSheets.getUnusedReferences());
}
},
};
});
module.exports.schema = [];