-
-
Notifications
You must be signed in to change notification settings - Fork 928
/
index.js
236 lines (185 loc) · 5.31 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
'use strict';
const isStandardSyntaxAtRule = require('../../utils/isStandardSyntaxAtRule');
const isStandardSyntaxRule = require('../../utils/isStandardSyntaxRule');
const report = require('../../utils/report');
const ruleMessages = require('../../utils/ruleMessages');
const styleSearch = require('style-search');
const validateOptions = require('../../utils/validateOptions');
const ruleName = 'no-extra-semicolons';
const messages = ruleMessages(ruleName, {
rejected: 'Unexpected extra semicolon',
});
/**
* @param {import('postcss').Node} node
* @returns {number}
*/
function getOffsetByNode(node) {
// @ts-expect-error -- TS2339: Property 'document' does not exist on type 'Document | Container<ChildNode>'
if (node.parent && node.parent.document) {
return 0;
}
const root = node.root();
if (!root.source) throw new Error('The root node must have a source');
if (!node.source) throw new Error('The node must have a source');
if (!node.source.start) throw new Error('The source must have a start position');
const string = root.source.input.css;
const nodeColumn = node.source.start.column;
const nodeLine = node.source.start.line;
let line = 1;
let column = 1;
let index = 0;
for (let i = 0; i < string.length; i++) {
if (column === nodeColumn && nodeLine === line) {
index = i;
break;
}
if (string[i] === '\n') {
column = 1;
line += 1;
} else {
column += 1;
}
}
return index;
}
/** @type {import('stylelint').Rule} */
const rule = (primary, _secondaryOptions, context) => {
return (root, result) => {
const validOptions = validateOptions(result, ruleName, { actual: primary });
if (!validOptions) {
return;
}
if (root.raws.after && root.raws.after.trim().length !== 0) {
const rawAfterRoot = root.raws.after;
/** @type {number[]} */
const fixSemiIndices = [];
styleSearch({ source: rawAfterRoot, target: ';' }, (match) => {
if (context.fix) {
fixSemiIndices.push(match.startIndex);
return;
}
if (!root.source) throw new Error('The root node must have a source');
complain(root.source.input.css.length - rawAfterRoot.length + match.startIndex);
});
// fix
if (fixSemiIndices.length) {
root.raws.after = removeIndices(rawAfterRoot, fixSemiIndices);
}
}
root.walk((node) => {
if (node.type === 'atrule' && !isStandardSyntaxAtRule(node)) {
return;
}
if (node.type === 'rule' && !isStandardSyntaxRule(node)) {
return;
}
if (node.raws.before && node.raws.before.trim().length !== 0) {
const rawBeforeNode = node.raws.before;
const allowedSemi = 0;
const rawBeforeIndexStart = 0;
/** @type {number[]} */
const fixSemiIndices = [];
styleSearch({ source: rawBeforeNode, target: ';' }, (match, count) => {
if (count === allowedSemi) {
return;
}
if (context.fix) {
fixSemiIndices.push(match.startIndex - rawBeforeIndexStart);
return;
}
complain(getOffsetByNode(node) - rawBeforeNode.length + match.startIndex);
});
// fix
if (fixSemiIndices.length) {
node.raws.before = removeIndices(rawBeforeNode, fixSemiIndices);
}
}
if ('after' in node.raws && node.raws.after && node.raws.after.trim().length !== 0) {
const rawAfterNode = node.raws.after;
/**
* If the last child is a Less mixin followed by more than one semicolon,
* node.raws.after will be populated with that semicolon.
* Since we ignore Less mixins, exit here
*/
if (
'last' in node &&
node.last &&
node.last.type === 'atrule' &&
!isStandardSyntaxAtRule(node.last)
) {
return;
}
/** @type {number[]} */
const fixSemiIndices = [];
styleSearch({ source: rawAfterNode, target: ';' }, (match) => {
if (context.fix) {
fixSemiIndices.push(match.startIndex);
return;
}
const index =
getOffsetByNode(node) +
node.toString().length -
1 -
rawAfterNode.length +
match.startIndex;
complain(index);
});
// fix
if (fixSemiIndices.length) {
node.raws.after = removeIndices(rawAfterNode, fixSemiIndices);
}
}
if ('ownSemicolon' in node.raws && node.raws.ownSemicolon) {
const rawOwnSemicolon = node.raws.ownSemicolon;
const allowedSemi = 0;
/** @type {number[]} */
const fixSemiIndices = [];
styleSearch({ source: rawOwnSemicolon, target: ';' }, (match, count) => {
if (count === allowedSemi) {
return;
}
if (context.fix) {
fixSemiIndices.push(match.startIndex);
return;
}
const index =
getOffsetByNode(node) +
node.toString().length -
rawOwnSemicolon.length +
match.startIndex;
complain(index);
});
// fix
if (fixSemiIndices.length) {
node.raws.ownSemicolon = removeIndices(rawOwnSemicolon, fixSemiIndices);
}
}
});
/**
* @param {number} index
*/
function complain(index) {
report({
message: messages.rejected,
node: root,
index,
result,
ruleName,
});
}
/**
* @param {string} str
* @param {number[]} indices
* @returns {string}
*/
function removeIndices(str, indices) {
for (const index of indices.reverse()) {
str = str.slice(0, index) + str.slice(index + 1);
}
return str;
}
};
};
rule.ruleName = ruleName;
rule.messages = messages;
module.exports = rule;