/
hash-object-property-comma.js
178 lines (158 loc) · 4.61 KB
/
hash-object-property-comma.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
"use strict"
const {
utils: { ruleMessages, validateOptions, report },
} = require("stylelint")
const { inCssLiteral, isObjectProperty } = require("../utils/nodes")
const ruleName = "stylus/hash-object-property-comma"
const messages = ruleMessages(ruleName, {
rejected: "Unexpected comma",
expected: "Expected comma",
})
const possibleOptions = ["always", "never"]
function getNodeOption(node, expectation, secondary) {
if (secondary) {
if (isLast(node) && secondary.trailing) {
return secondary.trailing
}
}
return expectation
}
function canOmitComma(node) {
const parent = node.parent
const index = parent.nodes.indexOf(node)
const nextNodes = parent.nodes.slice(index + 1)
if (
nextNodes.length &&
nextNodes.every(
(next) => next.raws.before && !next.raws.before.includes("\n"), // no line feed
)
) {
return false
}
return true
}
module.exports = {
ruleName,
rule,
meta: {
docs: {
description:
"require or disallow commas in hash object properties.",
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: possibleOptions,
},
{
actual: secondary,
possible: {
trailing: possibleOptions,
},
optional: true,
},
)
if (!validOptions) {
return
}
root.walkAtRules(verifyNode)
root.walkDecls(verifyNode)
function verifyNode(node) {
if (inCssLiteral(node)) {
return
}
if (!isObjectProperty(node)) {
return
}
if (!canOmitComma(node)) {
return
}
const hasSemicolon =
node.type === "decl"
? isLast(node)
? node.parent.raws.semicolon
: !node.omittedSemi
: node.raws.ownSemicolon &&
node.raws.ownSemicolon.includes(",")
const always =
getNodeOption(node, expectation, secondary) === "always"
if (always) {
if (hasSemicolon) {
return
}
// auto-fix
if (context.fix) {
if (node.type === "decl") {
if (isLast(node)) {
node.parent.raws.semicolon = true
} else {
node.omittedSemi = false
}
} else {
node.raws.ownSemicolon = ","
}
return
}
report({
message: messages.expected,
node,
index: node.toString().trim().length,
result,
ruleName,
})
} else {
if (!hasSemicolon) {
return
}
// auto-fix
if (context.fix) {
if (node.type === "decl") {
if (isLast(node)) {
node.parent.raws.semicolon = false
} else {
node.omittedSemi = true
}
} else {
node.raws.ownSemicolon = node.raws.ownSemicolon.replace(
/,/gu,
"",
)
}
return
}
report({
message: messages.rejected,
node,
index: node.toString().trim().length,
result,
ruleName,
})
}
}
}
}
function isLast(node) {
const lastNode = findLastNode(node.parent.nodes)
return node === lastNode
}
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
}