-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
272 lines (223 loc) · 6.7 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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
const { createPlugin } = require('stylelint')
const declarationValueIndex = require('stylelint/lib/utils/declarationValueIndex')
const getDeclarationValue = require('stylelint/lib/utils/getDeclarationValue')
const isSingleLineString = require('stylelint/lib/utils/isSingleLineString')
const isStandardSyntaxFunction = require('stylelint/lib/utils/isStandardSyntaxFunction')
const report = require('stylelint/lib/utils/report')
const ruleMessages = require('stylelint/lib/utils/ruleMessages')
const setDeclarationValue = require('stylelint/lib/utils/setDeclarationValue')
const validateOptions = require('stylelint/lib/utils/validateOptions')
const valueParser = require('postcss-value-parser')
const ruleName = 'stylistic/function-parentheses-newline-inside'
const messages = ruleMessages(ruleName, {
expectedOpening: 'Expected newline after "("',
expectedClosing: 'Expected newline before ")"',
expectedOpeningMultiLine: 'Expected newline after "(" in a multi-line function',
rejectedOpeningMultiLine: 'Unexpected whitespace after "(" in a multi-line function',
expectedClosingMultiLine: 'Expected newline before ")" in a multi-line function',
rejectedClosingMultiLine: 'Unexpected whitespace before ")" in a multi-line function',
})
const meta = {
url: 'https://github.com/elirasza/stylelint-stylistic/tree/main/lib/rules/function-parentheses-newline-inside',
fixable: true,
}
/** @type {import('stylelint').Rule} */
const rule = (primary, _secondaryOptions, context) => (root, result) => {
const validOptions = validateOptions(result, ruleName, {
actual: primary,
possible: ['always', 'always-multi-line', 'never-multi-line'],
})
if (!validOptions) {
return
}
root.walkDecls((decl) => {
if (!decl.value.includes('(')) {
return
}
let hasFixed = false
const declValue = getDeclarationValue(decl)
const parsedValue = valueParser(declValue)
parsedValue.walk((valueNode) => {
if (valueNode.type !== 'function') {
return
}
if (!isStandardSyntaxFunction(valueNode)) {
return
}
const functionString = valueParser.stringify(valueNode)
const isMultiLine = !isSingleLineString(functionString)
const containsNewline = (/** @type {string} */ str) => str.includes('\n')
// Check opening ...
const openingIndex = valueNode.sourceIndex + valueNode.value.length + 1
const checkBefore = getCheckBefore(valueNode)
if (primary === 'always' && !containsNewline(checkBefore)) {
if (context.fix) {
hasFixed = true
fixBeforeForAlways(valueNode, context.newline || '')
} else {
complain(messages.expectedOpening, openingIndex)
}
}
if (isMultiLine && primary === 'always-multi-line' && !containsNewline(checkBefore)) {
if (context.fix) {
hasFixed = true
fixBeforeForAlways(valueNode, context.newline || '')
} else {
complain(messages.expectedOpeningMultiLine, openingIndex)
}
}
if (isMultiLine && primary === 'never-multi-line' && checkBefore !== '') {
if (context.fix) {
hasFixed = true
fixBeforeForNever(valueNode)
} else {
complain(messages.rejectedOpeningMultiLine, openingIndex)
}
}
// Check closing ...
const closingIndex = valueNode.sourceIndex + functionString.length - 2
const checkAfter = getCheckAfter(valueNode)
if (primary === 'always' && !containsNewline(checkAfter)) {
if (context.fix) {
hasFixed = true
fixAfterForAlways(valueNode, context.newline || '')
} else {
complain(messages.expectedClosing, closingIndex)
}
}
if (isMultiLine && primary === 'always-multi-line' && !containsNewline(checkAfter)) {
if (context.fix) {
hasFixed = true
fixAfterForAlways(valueNode, context.newline || '')
} else {
complain(messages.expectedClosingMultiLine, closingIndex)
}
}
if (isMultiLine && primary === 'never-multi-line' && checkAfter !== '') {
if (context.fix) {
hasFixed = true
fixAfterForNever(valueNode)
} else {
complain(messages.rejectedClosingMultiLine, closingIndex)
}
}
})
if (hasFixed) {
setDeclarationValue(decl, parsedValue.toString())
}
/**
* @param {string} message
* @param {number} offset
*/
function complain(message, offset) {
report({
ruleName,
result,
message,
node: decl,
index: declarationValueIndex(decl) + offset,
})
}
})
}
/** @typedef {import('postcss-value-parser').FunctionNode} FunctionNode */
/**
* @param {FunctionNode} valueNode
*/
function getCheckBefore(valueNode) {
let { before } = valueNode
for (const node of valueNode.nodes) {
if (node.type === 'comment') {
continue
}
if (node.type === 'space') {
before += node.value
continue
}
break
}
return before
}
/**
* @param {FunctionNode} valueNode
*/
function getCheckAfter(valueNode) {
let after = ''
for (const node of [...valueNode.nodes].reverse()) {
if (node.type === 'comment') {
continue
}
if (node.type === 'space') {
after = node.value + after
continue
}
break
}
after += valueNode.after
return after
}
/**
* @param {FunctionNode} valueNode
* @param {string} newline
*/
function fixBeforeForAlways(valueNode, newline) {
let target
for (const node of valueNode.nodes) {
if (node.type === 'comment') {
continue
}
if (node.type === 'space') {
target = node
continue
}
break
}
if (target) {
target.value = newline + target.value
} else {
valueNode.before = newline + valueNode.before
}
}
/**
* @param {FunctionNode} valueNode
*/
function fixBeforeForNever(valueNode) {
valueNode.before = ''
for (const node of valueNode.nodes) {
if (node.type === 'comment') {
continue
}
if (node.type === 'space') {
node.value = ''
continue
}
break
}
}
/**
* @param {FunctionNode} valueNode
* @param {string} newline
*/
function fixAfterForAlways(valueNode, newline) {
valueNode.after = newline + valueNode.after
}
/**
* @param {FunctionNode} valueNode
*/
function fixAfterForNever(valueNode) {
valueNode.after = ''
for (const node of [...valueNode.nodes].reverse()) {
if (node.type === 'comment') {
continue
}
if (node.type === 'space') {
node.value = ''
continue
}
break
}
}
rule.ruleName = ruleName
rule.messages = messages
rule.meta = meta
module.exports = { messages, meta, plugin: createPlugin(ruleName, rule), rule, ruleName }