forked from karrot-dev/karrot-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstylelint.no-colons.js
70 lines (55 loc) · 1.51 KB
/
stylelint.no-colons.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
'use strict'
const stylelint = require('stylelint')
const hasBlock = require('stylelint/lib/utils/hasBlock')
const report = stylelint.utils.report
const ruleMessages = stylelint.utils.ruleMessages
const validateOptions = stylelint.utils.validateOptions
const ruleName = 'karrot/no-colons'
const messages = ruleMessages(ruleName, {
rejected: 'Unexpected colon',
})
const plugin = stylelint.createPlugin(ruleName, (expectation, secondary, context) => {
if (!expectation) return
return (root, result) => {
if (root.source.lang !== 'stylus') return
const validOptions = validateOptions(result, ruleName, {
actual: expectation,
possible: [true],
})
if (!validOptions) {
return
}
root.walkAtRules((atRule) => {
if (hasBlock(atRule)) {
return
}
checkNode(atRule)
})
root.walkDecls((decl) => {
checkNode(decl)
})
function checkNode (node) {
const hasColon = !node.raws.stylusBetween && /:/.test(node.raws.between)
if (!hasColon) return
// auto-fix
if (context.fix) {
node.raws.stylusBetween = node.raws.between.replace(/:/g, '')
if (node.raws.stylusBetween === '') {
node.raws.stylusBetween = ' '
}
return
}
const message = messages.rejected
report({
message,
node,
index: node.toString().trim().length,
result,
ruleName,
})
}
}
})
plugin.ruleName = ruleName
plugin.messages = messages
module.exports = plugin