-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
74 lines (61 loc) · 1.91 KB
/
index.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
68
69
70
71
72
73
74
import stylelint from 'stylelint'
import getCustomPropertiesFromRoot from './lib/get-custom-properties-from-root'
import getCustomPropertiesFromImports from './lib/get-custom-properties-from-imports'
const ruleName = 'plugin/value-no-exposed-custom-properties'
function isCustomPropertyDecl(prop) {
return prop.includes('--')
}
function prepareValue(value) {
if (typeof value === 'string') {
value = value.toUpperCase()
}
return value
}
function foundCustomPropertyInValue(properies, value) {
value = prepareValue(value)
for (let i in properies) {
const property = prepareValue(properies[i])
if (value.includes(property)) return i
}
}
async function getCustomProperties(root, importFrom) {
// all custom properties from the file and imports
return Object.assign(
{},
await getCustomPropertiesFromImports(importFrom),
getCustomPropertiesFromRoot(root)
)
}
function checkDeclOnCustomProperty(decl, customProperties, result) {
const foundVariable = foundCustomPropertyInValue(customProperties, decl.value)
if (foundVariable) {
stylelint.utils.report({
ruleName,
result,
node: decl,
message:
'The value (or a part of it) should be presented as a custom property: "' +
decl.value +
'" is "' +
foundVariable +
'" (' +
ruleName +
')',
})
}
}
module.exports = stylelint.createPlugin(ruleName, function (method, options) {
return async function (root, result) {
if (!method) return
const importFrom = options && options.importFrom ? options.importFrom : []
const customProperties = await getCustomProperties(root, importFrom)
root.walkRules(function (rule) {
rule.walkDecls(function (decl) {
// Skip custom properties declarations
if (!isCustomPropertyDecl(decl.prop)) {
checkDeclOnCustomProperty(decl, customProperties, result)
}
})
})
}
})