-
Notifications
You must be signed in to change notification settings - Fork 129
/
Copy pathno-color-literals.js
58 lines (48 loc) · 1.51 KB
/
no-color-literals.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
/**
* @fileoverview Detects color literals
* @author Aaron Greenwald
*/
'use strict';
const util = require('util');
const Components = require('../util/Components');
const styleSheet = require('../util/stylesheet');
const StyleSheets = styleSheet.StyleSheets;
const astHelpers = styleSheet.astHelpers;
module.exports = Components.detect((context) => {
const styleSheets = new StyleSheets();
function reportColorLiterals(colorLiterals) {
if (colorLiterals) {
colorLiterals.forEach((style) => {
if (style) {
const expression = util.inspect(style.expression);
context.report({
node: style.node,
message: 'Color literal: {{expression}}',
data: { expression },
});
}
});
}
}
return {
VariableDeclarator: (node) => {
if (astHelpers.isStyleSheetDeclaration(node, context.settings)) {
const styles = astHelpers.getStyleDeclarations(node);
if (styles) {
styles.forEach((style) => {
const literals = astHelpers.collectColorLiterals(style.value, context);
styleSheets.addColorLiterals(literals);
});
}
}
},
JSXAttribute: (node) => {
if (astHelpers.isStyleAttribute(node)) {
const literals = astHelpers.collectColorLiterals(node.value, context);
styleSheets.addColorLiterals(literals);
}
},
'Program:exit': () => reportColorLiterals(styleSheets.getColorLiterals()),
};
});
module.exports.schema = [];