/
assignDisabledRanges.js
404 lines (333 loc) · 10.1 KB
/
assignDisabledRanges.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
'use strict';
const _ = require('lodash');
const COMMAND_PREFIX = 'stylelint-';
const disableCommand = `${COMMAND_PREFIX}disable`;
const enableCommand = `${COMMAND_PREFIX}enable`;
const disableLineCommand = `${COMMAND_PREFIX}disable-line`;
const disableNextLineCommand = `${COMMAND_PREFIX}disable-next-line`;
const ALL_RULES = 'all';
/** @typedef {import('postcss/lib/comment')} PostcssComment */
/** @typedef {import('postcss').Root} PostcssRoot */
/** @typedef {import('stylelint').PostcssResult} PostcssResult */
/** @typedef {import('stylelint').DisabledRangeObject} DisabledRangeObject */
/** @typedef {import('stylelint').DisabledRange} DisabledRange */
/**
* @param {number} start
* @param {boolean} strictStart
* @param {string|undefined} description
* @param {number} [end]
* @param {boolean} [strictEnd]
* @returns {DisabledRange}
*/
function createDisableRange(start, strictStart, description, end, strictEnd) {
return {
start,
end: end || undefined,
strictStart,
strictEnd: typeof strictEnd === 'boolean' ? strictEnd : undefined,
description,
};
}
/**
* Run it like a plugin ...
* @param {PostcssRoot} root
* @param {PostcssResult} result
* @returns {PostcssResult}
*/
module.exports = function (root, result) {
result.stylelint = result.stylelint || {
disabledRanges: {},
ruleSeverities: {},
customMessages: {},
};
/**
* Most of the functions below work via side effects mutating this object
* @type {DisabledRangeObject}
*/
const disabledRanges = {
all: [],
};
result.stylelint.disabledRanges = disabledRanges;
// Work around postcss/postcss-scss#109 by merging adjacent `//` comments
// into a single node before passing to `checkComment`.
/** @type {PostcssComment?} */
let inlineEnd;
root.walkComments((/** @type {PostcssComment} */ comment) => {
if (inlineEnd) {
// Ignore comments already processed by grouping with a previous one.
if (inlineEnd === comment) inlineEnd = null;
return;
}
const next = comment.next();
// If any of these conditions are not met, do not merge comments.
if (
!(
isInlineComment(comment) &&
isStylelintCommand(comment) &&
next &&
next.type === 'comment' &&
(comment.text.includes('--') || next.text.startsWith('--'))
)
) {
checkComment(comment);
return;
}
let lastLine = (comment.source && comment.source.end && comment.source.end.line) || 0;
const fullComment = comment.clone();
/** @type {PostcssComment} */
let current = next;
while (isInlineComment(current) && !isStylelintCommand(current)) {
const currentLine = (current.source && current.source.end && current.source.end.line) || 0;
if (lastLine + 1 !== currentLine) break;
fullComment.text += `\n${current.text}`;
if (fullComment.source && current.source) {
fullComment.source.end = current.source.end;
}
inlineEnd = current;
const next = current.next();
if (!next || next.type !== 'comment') break;
current = next;
lastLine = currentLine;
}
checkComment(fullComment);
});
return result;
/**
* @param {PostcssComment} comment
*/
function isInlineComment(comment) {
// We check both here because the Sass parser uses `raws.inline` to indicate
// inline comments, while the Less parser uses `inline`.
return comment.inline || comment.raws.inline;
}
/**
* @param {PostcssComment} comment
*/
function isStylelintCommand(comment) {
return /^stylelint-(disable|enable)/.test(comment.text);
}
/**
* @param {PostcssComment} comment
*/
function processDisableLineCommand(comment) {
if (comment.source && comment.source.start) {
const line = comment.source.start.line;
const description = getDescription(comment.text);
getCommandRules(disableLineCommand, comment.text).forEach((ruleName) => {
disableLine(line, ruleName, comment, description);
});
}
}
/**
* @param {PostcssComment} comment
*/
function processDisableNextLineCommand(comment) {
if (comment.source && comment.source.end) {
const line = comment.source.end.line;
const description = getDescription(comment.text);
getCommandRules(disableNextLineCommand, comment.text).forEach((ruleName) => {
disableLine(line + 1, ruleName, comment, description);
});
}
}
/**
* @param {number} line
* @param {string} ruleName
* @param {PostcssComment} comment
* @param {string|undefined} description
*/
function disableLine(line, ruleName, comment, description) {
if (ruleIsDisabled(ALL_RULES)) {
throw comment.error('All rules have already been disabled', {
plugin: 'stylelint',
});
}
if (ruleName === ALL_RULES) {
Object.keys(disabledRanges).forEach((disabledRuleName) => {
if (ruleIsDisabled(disabledRuleName)) return;
const strict = disabledRuleName === ALL_RULES;
startDisabledRange(line, disabledRuleName, strict, description);
endDisabledRange(line, disabledRuleName, strict);
});
} else {
if (ruleIsDisabled(ruleName)) {
throw comment.error(`"${ruleName}" has already been disabled`, {
plugin: 'stylelint',
});
}
startDisabledRange(line, ruleName, true, description);
endDisabledRange(line, ruleName, true);
}
}
/**
* @param {PostcssComment} comment
*/
function processDisableCommand(comment) {
const description = getDescription(comment.text);
getCommandRules(disableCommand, comment.text).forEach((ruleToDisable) => {
const isAllRules = ruleToDisable === ALL_RULES;
if (ruleIsDisabled(ruleToDisable)) {
throw comment.error(
isAllRules
? 'All rules have already been disabled'
: `"${ruleToDisable}" has already been disabled`,
{
plugin: 'stylelint',
},
);
}
if (comment.source && comment.source.start) {
const line = comment.source.start.line;
if (isAllRules) {
Object.keys(disabledRanges).forEach((ruleName) => {
startDisabledRange(line, ruleName, ruleName === ALL_RULES, description);
});
} else {
startDisabledRange(line, ruleToDisable, true, description);
}
}
});
}
/**
* @param {PostcssComment} comment
*/
function processEnableCommand(comment) {
getCommandRules(enableCommand, comment.text).forEach((ruleToEnable) => {
// TODO TYPES
// need fallback if endLine will be undefined
const endLine = /** @type {number} */ (comment.source &&
comment.source.end &&
comment.source.end.line);
if (ruleToEnable === ALL_RULES) {
if (
Object.values(disabledRanges).every(
(ranges) => ranges.length === 0 || typeof ranges[ranges.length - 1].end === 'number',
)
) {
throw comment.error('No rules have been disabled', {
plugin: 'stylelint',
});
}
Object.keys(disabledRanges).forEach((ruleName) => {
if (!_.get(_.last(disabledRanges[ruleName]), 'end')) {
endDisabledRange(endLine, ruleName, ruleName === ALL_RULES);
}
});
return;
}
if (ruleIsDisabled(ALL_RULES) && disabledRanges[ruleToEnable] === undefined) {
// Get a starting point from the where all rules were disabled
if (!disabledRanges[ruleToEnable]) {
disabledRanges[ruleToEnable] = disabledRanges.all.map(({ start, end, description }) =>
createDisableRange(start, false, description, end, false),
);
} else {
const range = _.last(disabledRanges[ALL_RULES]);
if (range) {
disabledRanges[ruleToEnable].push({ ...range });
}
}
endDisabledRange(endLine, ruleToEnable, true);
return;
}
if (ruleIsDisabled(ruleToEnable)) {
endDisabledRange(endLine, ruleToEnable, true);
return;
}
throw comment.error(`"${ruleToEnable}" has not been disabled`, {
plugin: 'stylelint',
});
});
}
/**
* @param {PostcssComment} comment
*/
function checkComment(comment) {
const text = comment.text;
// Ignore comments that are not relevant commands
if (text.indexOf(COMMAND_PREFIX) !== 0) {
return result;
}
if (text.startsWith(disableLineCommand)) {
processDisableLineCommand(comment);
} else if (text.startsWith(disableNextLineCommand)) {
processDisableNextLineCommand(comment);
} else if (text.startsWith(disableCommand)) {
processDisableCommand(comment);
} else if (text.startsWith(enableCommand)) {
processEnableCommand(comment);
}
}
/**
* @param {string} command
* @param {string} fullText
* @returns {string[]}
*/
function getCommandRules(command, fullText) {
const rules = fullText
.slice(command.length)
.split(/\s-{2,}\s/u)[0] // Allow for description (f.e. /* stylelint-disable a, b -- Description */).
.trim()
.split(',')
.filter(Boolean)
.map((r) => r.trim());
if (_.isEmpty(rules)) {
return [ALL_RULES];
}
return rules;
}
/**
* @param {string} fullText
* @returns {string|undefined}
*/
function getDescription(fullText) {
const descriptionStart = fullText.indexOf('--');
if (descriptionStart === -1) return;
return fullText.slice(descriptionStart + 2).trim();
}
/**
* @param {number} line
* @param {string} ruleName
* @param {boolean} strict
* @param {string|undefined} description
*/
function startDisabledRange(line, ruleName, strict, description) {
const rangeObj = createDisableRange(line, strict, description);
ensureRuleRanges(ruleName);
disabledRanges[ruleName].push(rangeObj);
}
/**
* @param {number} line
* @param {string} ruleName
* @param {boolean} strict
*/
function endDisabledRange(line, ruleName, strict) {
const lastRangeForRule = _.last(disabledRanges[ruleName]);
if (!lastRangeForRule) {
return;
}
// Add an `end` prop to the last range of that rule
lastRangeForRule.end = line;
lastRangeForRule.strictEnd = strict;
}
/**
* @param {string} ruleName
*/
function ensureRuleRanges(ruleName) {
if (!disabledRanges[ruleName]) {
disabledRanges[ruleName] = disabledRanges.all.map(({ start, end, description }) =>
createDisableRange(start, false, description, end, false),
);
}
}
/**
* @param {string} ruleName
* @returns {boolean}
*/
function ruleIsDisabled(ruleName) {
if (disabledRanges[ruleName] === undefined) return false;
if (_.last(disabledRanges[ruleName]) === undefined) return false;
if (_.get(_.last(disabledRanges[ruleName]), 'end') === undefined) return true;
return false;
}
};