-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
152 lines (131 loc) · 4.18 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
const { createPlugin } = require('stylelint')
const isStandardSyntaxRule = require('stylelint/lib/utils/isStandardSyntaxRule')
const parseSelector = require('stylelint/lib/utils/parseSelector')
const report = require('stylelint/lib/utils/report')
const ruleMessages = require('stylelint/lib/utils/ruleMessages')
const validateOptions = require('stylelint/lib/utils/validateOptions')
const ruleName = 'stylistic/selector-pseudo-class-parentheses-space-inside'
const messages = ruleMessages(ruleName, {
expectedOpening: 'Expected single space after "("',
rejectedOpening: 'Unexpected whitespace after "("',
expectedClosing: 'Expected single space before ")"',
rejectedClosing: 'Unexpected whitespace before ")"',
})
const meta = {
url: 'https://github.com/elirasza/stylelint-stylistic/tree/main/lib/rules/selector-pseudo-class-parentheses-space-inside',
fixable: true,
}
/** @type {import('stylelint').Rule} */
const rule = (primary, _secondaryOptions, context) => (root, result) => {
const validOptions = validateOptions(result, ruleName, {
actual: primary,
possible: ['always', 'never'],
})
if (!validOptions) {
return
}
root.walkRules((ruleNode) => {
if (!isStandardSyntaxRule(ruleNode)) {
return
}
if (!ruleNode.selector.includes('(')) {
return
}
let hasFixed = false
const selector = ruleNode.raws.selector ? ruleNode.raws.selector.raw : ruleNode.selector
const fixedSelector = parseSelector(selector, result, ruleNode, (selectorTree) => {
selectorTree.walkPseudos((pseudoNode) => {
if (!pseudoNode.length) {
return
}
const paramString = pseudoNode.map((node) => String(node)).join(',')
const nextCharIsSpace = paramString.startsWith(' ')
const openIndex = pseudoNode.sourceIndex + pseudoNode.value.length + 1
if (nextCharIsSpace && primary === 'never') {
if (context.fix) {
hasFixed = true
setFirstNodeSpaceBefore(pseudoNode, '')
} else {
complain(messages.rejectedOpening, openIndex)
}
}
if (!nextCharIsSpace && primary === 'always') {
if (context.fix) {
hasFixed = true
setFirstNodeSpaceBefore(pseudoNode, ' ')
} else {
complain(messages.expectedOpening, openIndex)
}
}
const prevCharIsSpace = paramString.endsWith(' ')
const closeIndex = openIndex + paramString.length - 1
if (prevCharIsSpace && primary === 'never') {
if (context.fix) {
hasFixed = true
setLastNodeSpaceAfter(pseudoNode, '')
} else {
complain(messages.rejectedClosing, closeIndex)
}
}
if (!prevCharIsSpace && primary === 'always') {
if (context.fix) {
hasFixed = true
setLastNodeSpaceAfter(pseudoNode, ' ')
} else {
complain(messages.expectedClosing, closeIndex)
}
}
})
})
if (hasFixed && fixedSelector) {
if (!ruleNode.raws.selector) {
ruleNode.selector = fixedSelector
} else {
ruleNode.raws.selector.raw = fixedSelector
}
}
/**
* @param {string} message
* @param {number} index
*/
function complain(message, index) {
report({
message,
index,
result,
ruleName,
node: ruleNode,
})
}
})
}
/**
* @param {import('postcss-selector-parser').Container} node
* @param {string} value
* @returns {void}
*/
function setFirstNodeSpaceBefore(node, value) {
const target = node.first
if (target.type === 'selector') {
setFirstNodeSpaceBefore(target, value)
} else {
target.spaces.before = value
}
}
/**
* @param {import('postcss-selector-parser').Container} node
* @param {string} value
* @returns {void}
*/
function setLastNodeSpaceAfter(node, value) {
const target = node.last
if (target.type === 'selector') {
setLastNodeSpaceAfter(target, value)
} else {
target.spaces.after = value
}
}
rule.ruleName = ruleName
rule.messages = messages
rule.meta = meta
module.exports = { messages, meta, plugin: createPlugin(ruleName, rule), rule, ruleName }