This repository has been archived by the owner on Jun 16, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
semicolon.js
123 lines (107 loc) · 3.07 KB
/
semicolon.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
"use strict"
const {
utils: { ruleMessages, validateOptions, report },
} = require("stylelint")
const hasBlock = require("stylelint/lib/utils/hasBlock")
const { inCssLiteral, isObjectProperty } = require("../utils/nodes")
const ruleName = "stylus/semicolon"
const messages = ruleMessages(ruleName, {
rejected: "Unexpected semicolon",
expected: "Expected semicolon",
})
module.exports = {
ruleName,
rule,
meta: {
docs: {
description: "require or disallow semicolon.",
category: "standard",
},
fixable: true,
},
}
function rule(expectation, _secondary, context) {
return (root, result) => {
if (root.source.lang !== "stylus") {
return
}
const validOptions = validateOptions(result, ruleName, {
actual: expectation,
possible: ["always", "never"],
})
if (!validOptions) {
return
}
root.walkAtRules((atRule) => {
if (hasBlock(atRule)) {
return
}
verifyNode(atRule)
})
root.walkDecls(verifyNode)
function verifyNode(node) {
if (inCssLiteral(node)) {
return
}
if (isObjectProperty(node)) {
return
}
const lastNode = findLastNode(node.parent.nodes)
const isLast = node === lastNode
const hasSemicolon = isLast
? node.parent.raws.semicolon
: !node.omittedSemi
if (expectation === "always") {
if (hasSemicolon) {
return
}
// auto-fix
if (context.fix) {
if (isLast) {
node.parent.raws.semicolon = true
} else {
node.omittedSemi = false
}
return
}
report({
message: messages.expected,
node,
index: node.toString().trim().length,
result,
ruleName,
})
} else {
if (!hasSemicolon) {
return
}
// auto-fix
if (context.fix) {
if (isLast) {
node.parent.raws.semicolon = false
} else {
node.omittedSemi = true
}
return
}
report({
message: messages.rejected,
node,
index: node.toString().trim().length,
result,
ruleName,
})
}
}
}
}
function findLastNode(nodes) {
for (let index = nodes.length - 1; index >= 0; index--) {
const node = nodes[index]
if (node.type === "comment") {
continue
}
return node
}
return null
}