-
Notifications
You must be signed in to change notification settings - Fork 129
/
Copy pathno-inline-styles.js
57 lines (49 loc) · 1.42 KB
/
no-inline-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
/**
* @fileoverview Detects inline styles
* @author Aaron Greenwald
*/
'use strict';
const util = require('util');
const Components = require('../util/Components');
const styleSheet = require('../util/stylesheet');
const { StyleSheets } = styleSheet;
const { astHelpers } = styleSheet;
module.exports = Components.detect((context) => {
const options = context.options[0] || {};
const styleSheets = new StyleSheets();
function reportInlineStyles(inlineStyles) {
if (inlineStyles) {
inlineStyles.forEach((style) => {
if (style) {
const lengthLimit = options.allowStylePropertiesLessThan;
if (lengthLimit && Object.keys(style.expression).length < lengthLimit) return;
const expression = util.inspect(style.expression);
context.report({
node: style.node,
message: 'Inline style: {{expression}}',
data: { expression },
});
}
});
}
}
return {
JSXAttribute: (node) => {
if (astHelpers.isStyleAttribute(node)) {
const styles = astHelpers.collectStyleObjectExpressions(node.value, context);
styleSheets.addObjectExpressions(styles);
}
},
'Program:exit': () => reportInlineStyles(styleSheets.getObjectExpressions()),
};
});
module.exports.schema = [
{
type: 'object',
properties: {
allowStylePropertiesLessThan: {
type: 'number',
},
},
},
];