1 | 'use strict';
|
2 |
|
3 | const atRuleParamIndex = require('../../utils/atRuleParamIndex');
|
4 | const mediaQueryListCommaWhitespaceChecker = require('../mediaQueryListCommaWhitespaceChecker');
|
5 | const ruleMessages = require('../../utils/ruleMessages');
|
6 | const validateOptions = require('../../utils/validateOptions');
|
7 | const whitespaceChecker = require('../../utils/whitespaceChecker');
|
8 |
|
9 | const ruleName = 'media-query-list-comma-newline-after';
|
10 |
|
11 | const messages = ruleMessages(ruleName, {
|
12 | expectedAfter: () => 'Expected newline after ","',
|
13 | expectedAfterMultiLine: () => 'Expected newline after "," in a multi-line list',
|
14 | rejectedAfterMultiLine: () => 'Unexpected whitespace after "," in a multi-line list',
|
15 | });
|
16 |
|
17 | function rule(expectation, options, context) {
|
18 | const checker = whitespaceChecker('newline', expectation, messages);
|
19 |
|
20 | return (root, result) => {
|
21 | const validOptions = validateOptions(result, ruleName, {
|
22 | actual: expectation,
|
23 | possible: ['always', 'always-multi-line', 'never-multi-line'],
|
24 | });
|
25 |
|
26 | if (!validOptions) {
|
27 | return;
|
28 | }
|
29 |
|
30 |
|
31 |
|
32 | let fixData;
|
33 |
|
34 | mediaQueryListCommaWhitespaceChecker({
|
35 | root,
|
36 | result,
|
37 | locationChecker: checker.afterOneOnly,
|
38 | checkedRuleName: ruleName,
|
39 | allowTrailingComments: expectation.startsWith('always'),
|
40 | fix: context.fix
|
41 | ? (atRule, index) => {
|
42 | const paramCommaIndex = index - atRuleParamIndex(atRule);
|
43 |
|
44 | fixData = fixData || new Map();
|
45 | const commaIndices = fixData.get(atRule) || [];
|
46 |
|
47 | commaIndices.push(paramCommaIndex);
|
48 | fixData.set(atRule, commaIndices);
|
49 |
|
50 | return true;
|
51 | }
|
52 | : null,
|
53 | });
|
54 |
|
55 | if (fixData) {
|
56 | fixData.forEach((commaIndices, atRule) => {
|
57 | let params = atRule.raws.params ? atRule.raws.params.raw : atRule.params;
|
58 |
|
59 | commaIndices
|
60 | .sort((a, b) => b - a)
|
61 | .forEach((index) => {
|
62 | const beforeComma = params.slice(0, index + 1);
|
63 | const afterComma = params.slice(index + 1);
|
64 |
|
65 | if (expectation.startsWith('always')) {
|
66 | if (/^\s*\r?\n/.test(afterComma)) {
|
67 | params = beforeComma + afterComma.replace(/^[^\S\r\n]*/, '');
|
68 | } else {
|
69 | params = beforeComma + context.newline + afterComma;
|
70 | }
|
71 | } else if (expectation.startsWith('never')) {
|
72 | params = beforeComma + afterComma.replace(/^\s*/, '');
|
73 | }
|
74 | });
|
75 |
|
76 | if (atRule.raws.params) {
|
77 | atRule.raws.params.raw = params;
|
78 | } else {
|
79 | atRule.params = params;
|
80 | }
|
81 | });
|
82 | }
|
83 | };
|
84 | }
|
85 |
|
86 | rule.ruleName = ruleName;
|
87 | rule.messages = messages;
|
88 | module.exports = rule;
|