1 | 'use strict';
|
2 |
|
3 | const _ = require('lodash');
|
4 |
|
5 | const COMMAND_PREFIX = 'stylelint-';
|
6 | const disableCommand = `${COMMAND_PREFIX}disable`;
|
7 | const enableCommand = `${COMMAND_PREFIX}enable`;
|
8 | const disableLineCommand = `${COMMAND_PREFIX}disable-line`;
|
9 | const disableNextLineCommand = `${COMMAND_PREFIX}disable-next-line`;
|
10 | const ALL_RULES = 'all';
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | function createDisableRange(comment, start, strictStart, description, end, strictEnd) {
|
28 | return {
|
29 | comment,
|
30 | start,
|
31 | end: end || undefined,
|
32 | strictStart,
|
33 | strictEnd: typeof strictEnd === 'boolean' ? strictEnd : undefined,
|
34 | description,
|
35 | };
|
36 | }
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 | module.exports = function (root, result) {
|
45 | result.stylelint = result.stylelint || {
|
46 | disabledRanges: {},
|
47 | ruleSeverities: {},
|
48 | customMessages: {},
|
49 | };
|
50 |
|
51 | |
52 |
|
53 |
|
54 |
|
55 | const disabledRanges = {
|
56 | all: [],
|
57 | };
|
58 |
|
59 | result.stylelint.disabledRanges = disabledRanges;
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 | let inlineEnd;
|
66 |
|
67 | root.walkComments((/** @type {PostcssComment} */ comment) => {
|
68 | if (inlineEnd) {
|
69 |
|
70 | if (inlineEnd === comment) inlineEnd = null;
|
71 |
|
72 | return;
|
73 | }
|
74 |
|
75 | const next = comment.next();
|
76 |
|
77 |
|
78 | if (
|
79 | !(
|
80 | isInlineComment(comment) &&
|
81 | isStylelintCommand(comment) &&
|
82 | next &&
|
83 | next.type === 'comment' &&
|
84 | (comment.text.includes('--') || next.text.startsWith('--'))
|
85 | )
|
86 | ) {
|
87 | checkComment(comment);
|
88 |
|
89 | return;
|
90 | }
|
91 |
|
92 | let lastLine = (comment.source && comment.source.end && comment.source.end.line) || 0;
|
93 | const fullComment = comment.clone();
|
94 |
|
95 |
|
96 | let current = next;
|
97 |
|
98 | while (isInlineComment(current) && !isStylelintCommand(current)) {
|
99 | const currentLine = (current.source && current.source.end && current.source.end.line) || 0;
|
100 |
|
101 | if (lastLine + 1 !== currentLine) break;
|
102 |
|
103 | fullComment.text += `\n${current.text}`;
|
104 |
|
105 | if (fullComment.source && current.source) {
|
106 | fullComment.source.end = current.source.end;
|
107 | }
|
108 |
|
109 | inlineEnd = current;
|
110 |
|
111 |
|
112 | const next = current.next();
|
113 |
|
114 | if (!next || next.type !== 'comment') break;
|
115 |
|
116 | current = next;
|
117 | lastLine = currentLine;
|
118 | }
|
119 | checkComment(fullComment);
|
120 | });
|
121 |
|
122 | return result;
|
123 |
|
124 | |
125 |
|
126 |
|
127 | function isInlineComment(comment) {
|
128 |
|
129 |
|
130 | return comment.inline || comment.raws.inline;
|
131 | }
|
132 |
|
133 | |
134 |
|
135 |
|
136 | function isStylelintCommand(comment) {
|
137 | return comment.text.startsWith(disableCommand) || comment.text.startsWith(enableCommand);
|
138 | }
|
139 |
|
140 | |
141 |
|
142 |
|
143 | function processDisableLineCommand(comment) {
|
144 | if (comment.source && comment.source.start) {
|
145 | const line = comment.source.start.line;
|
146 | const description = getDescription(comment.text);
|
147 |
|
148 | getCommandRules(disableLineCommand, comment.text).forEach((ruleName) => {
|
149 | disableLine(comment, line, ruleName, description);
|
150 | });
|
151 | }
|
152 | }
|
153 |
|
154 | |
155 |
|
156 |
|
157 | function processDisableNextLineCommand(comment) {
|
158 | if (comment.source && comment.source.end) {
|
159 | const line = comment.source.end.line;
|
160 | const description = getDescription(comment.text);
|
161 |
|
162 | getCommandRules(disableNextLineCommand, comment.text).forEach((ruleName) => {
|
163 | disableLine(comment, line + 1, ruleName, description);
|
164 | });
|
165 | }
|
166 | }
|
167 |
|
168 | |
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 | function disableLine(comment, line, ruleName, description) {
|
175 | if (ruleIsDisabled(ALL_RULES)) {
|
176 | throw comment.error('All rules have already been disabled', {
|
177 | plugin: 'stylelint',
|
178 | });
|
179 | }
|
180 |
|
181 | if (ruleName === ALL_RULES) {
|
182 | Object.keys(disabledRanges).forEach((disabledRuleName) => {
|
183 | if (ruleIsDisabled(disabledRuleName)) return;
|
184 |
|
185 | const strict = disabledRuleName === ALL_RULES;
|
186 |
|
187 | startDisabledRange(comment, line, disabledRuleName, strict, description);
|
188 | endDisabledRange(line, disabledRuleName, strict);
|
189 | });
|
190 | } else {
|
191 | if (ruleIsDisabled(ruleName)) {
|
192 | throw comment.error(`"${ruleName}" has already been disabled`, {
|
193 | plugin: 'stylelint',
|
194 | });
|
195 | }
|
196 |
|
197 | startDisabledRange(comment, line, ruleName, true, description);
|
198 | endDisabledRange(line, ruleName, true);
|
199 | }
|
200 | }
|
201 |
|
202 | |
203 |
|
204 |
|
205 | function processDisableCommand(comment) {
|
206 | const description = getDescription(comment.text);
|
207 |
|
208 | getCommandRules(disableCommand, comment.text).forEach((ruleToDisable) => {
|
209 | const isAllRules = ruleToDisable === ALL_RULES;
|
210 |
|
211 | if (ruleIsDisabled(ruleToDisable)) {
|
212 | throw comment.error(
|
213 | isAllRules
|
214 | ? 'All rules have already been disabled'
|
215 | : `"${ruleToDisable}" has already been disabled`,
|
216 | {
|
217 | plugin: 'stylelint',
|
218 | },
|
219 | );
|
220 | }
|
221 |
|
222 | if (comment.source && comment.source.start) {
|
223 | const line = comment.source.start.line;
|
224 |
|
225 | if (isAllRules) {
|
226 | Object.keys(disabledRanges).forEach((ruleName) => {
|
227 | startDisabledRange(comment, line, ruleName, ruleName === ALL_RULES, description);
|
228 | });
|
229 | } else {
|
230 | startDisabledRange(comment, line, ruleToDisable, true, description);
|
231 | }
|
232 | }
|
233 | });
|
234 | }
|
235 |
|
236 | |
237 |
|
238 |
|
239 | function processEnableCommand(comment) {
|
240 | getCommandRules(enableCommand, comment.text).forEach((ruleToEnable) => {
|
241 |
|
242 |
|
243 | const endLine = (comment.source &&
|
244 | comment.source.end &&
|
245 | comment.source.end.line);
|
246 |
|
247 | if (ruleToEnable === ALL_RULES) {
|
248 | if (
|
249 | Object.values(disabledRanges).every(
|
250 | (ranges) => ranges.length === 0 || typeof ranges[ranges.length - 1].end === 'number',
|
251 | )
|
252 | ) {
|
253 | throw comment.error('No rules have been disabled', {
|
254 | plugin: 'stylelint',
|
255 | });
|
256 | }
|
257 |
|
258 | Object.keys(disabledRanges).forEach((ruleName) => {
|
259 | if (!_.get(_.last(disabledRanges[ruleName]), 'end')) {
|
260 | endDisabledRange(endLine, ruleName, ruleName === ALL_RULES);
|
261 | }
|
262 | });
|
263 |
|
264 | return;
|
265 | }
|
266 |
|
267 | if (ruleIsDisabled(ALL_RULES) && disabledRanges[ruleToEnable] === undefined) {
|
268 |
|
269 | if (!disabledRanges[ruleToEnable]) {
|
270 | disabledRanges[ruleToEnable] = disabledRanges.all.map(({ start, end, description }) =>
|
271 | createDisableRange(comment, start, false, description, end, false),
|
272 | );
|
273 | } else {
|
274 | const range = _.last(disabledRanges[ALL_RULES]);
|
275 |
|
276 | if (range) {
|
277 | disabledRanges[ruleToEnable].push({ ...range });
|
278 | }
|
279 | }
|
280 |
|
281 | endDisabledRange(endLine, ruleToEnable, true);
|
282 |
|
283 | return;
|
284 | }
|
285 |
|
286 | if (ruleIsDisabled(ruleToEnable)) {
|
287 | endDisabledRange(endLine, ruleToEnable, true);
|
288 |
|
289 | return;
|
290 | }
|
291 |
|
292 | throw comment.error(`"${ruleToEnable}" has not been disabled`, {
|
293 | plugin: 'stylelint',
|
294 | });
|
295 | });
|
296 | }
|
297 |
|
298 | |
299 |
|
300 |
|
301 | function checkComment(comment) {
|
302 | const text = comment.text;
|
303 |
|
304 |
|
305 |
|
306 | if (text.indexOf(COMMAND_PREFIX) !== 0) {
|
307 | return result;
|
308 | }
|
309 |
|
310 | if (text.startsWith(disableLineCommand)) {
|
311 | processDisableLineCommand(comment);
|
312 | } else if (text.startsWith(disableNextLineCommand)) {
|
313 | processDisableNextLineCommand(comment);
|
314 | } else if (text.startsWith(disableCommand)) {
|
315 | processDisableCommand(comment);
|
316 | } else if (text.startsWith(enableCommand)) {
|
317 | processEnableCommand(comment);
|
318 | }
|
319 | }
|
320 |
|
321 | |
322 |
|
323 |
|
324 |
|
325 |
|
326 | function getCommandRules(command, fullText) {
|
327 | const rules = fullText
|
328 | .slice(command.length)
|
329 | .split(/\s-{2,}\s/u)[0]
|
330 | .trim()
|
331 | .split(',')
|
332 | .filter(Boolean)
|
333 | .map((r) => r.trim());
|
334 |
|
335 | if (_.isEmpty(rules)) {
|
336 | return [ALL_RULES];
|
337 | }
|
338 |
|
339 | return rules;
|
340 | }
|
341 |
|
342 | |
343 |
|
344 |
|
345 |
|
346 | function getDescription(fullText) {
|
347 | const descriptionStart = fullText.indexOf('--');
|
348 |
|
349 | if (descriptionStart === -1) return;
|
350 |
|
351 | return fullText.slice(descriptionStart + 2).trim();
|
352 | }
|
353 |
|
354 | |
355 |
|
356 |
|
357 |
|
358 |
|
359 |
|
360 |
|
361 | function startDisabledRange(comment, line, ruleName, strict, description) {
|
362 | const rangeObj = createDisableRange(comment, line, strict, description);
|
363 |
|
364 | ensureRuleRanges(ruleName);
|
365 | disabledRanges[ruleName].push(rangeObj);
|
366 | }
|
367 |
|
368 | |
369 |
|
370 |
|
371 |
|
372 |
|
373 | function endDisabledRange(line, ruleName, strict) {
|
374 | const lastRangeForRule = _.last(disabledRanges[ruleName]);
|
375 |
|
376 | if (!lastRangeForRule) {
|
377 | return;
|
378 | }
|
379 |
|
380 |
|
381 | lastRangeForRule.end = line;
|
382 | lastRangeForRule.strictEnd = strict;
|
383 | }
|
384 |
|
385 | |
386 |
|
387 |
|
388 | function ensureRuleRanges(ruleName) {
|
389 | if (!disabledRanges[ruleName]) {
|
390 | disabledRanges[ruleName] = disabledRanges.all.map(({ comment, start, end, description }) =>
|
391 | createDisableRange(comment, start, false, description, end, false),
|
392 | );
|
393 | }
|
394 | }
|
395 |
|
396 | |
397 |
|
398 |
|
399 |
|
400 | function ruleIsDisabled(ruleName) {
|
401 | if (disabledRanges[ruleName] === undefined) return false;
|
402 |
|
403 | if (_.last(disabledRanges[ruleName]) === undefined) return false;
|
404 |
|
405 | if (_.get(_.last(disabledRanges[ruleName]), 'end') === undefined) return true;
|
406 |
|
407 | return false;
|
408 | }
|
409 | };
|