1 |
|
2 |
|
3 | 'use strict';
|
4 |
|
5 | const atRuleParamIndex = require('../../utils/atRuleParamIndex');
|
6 | const mediaFeatureColonSpaceChecker = require('../mediaFeatureColonSpaceChecker');
|
7 | const ruleMessages = require('../../utils/ruleMessages');
|
8 | const validateOptions = require('../../utils/validateOptions');
|
9 | const whitespaceChecker = require('../../utils/whitespaceChecker');
|
10 |
|
11 | const ruleName = 'media-feature-colon-space-before';
|
12 |
|
13 | const messages = ruleMessages(ruleName, {
|
14 | expectedBefore: () => 'Expected single space before ":"',
|
15 | rejectedBefore: () => 'Unexpected whitespace before ":"',
|
16 | });
|
17 |
|
18 | function rule(expectation, options, context) {
|
19 | const checker = whitespaceChecker('space', expectation, messages);
|
20 |
|
21 | return (root, result) => {
|
22 | const validOptions = validateOptions(result, ruleName, {
|
23 | actual: expectation,
|
24 | possible: ['always', 'never'],
|
25 | });
|
26 |
|
27 | if (!validOptions) {
|
28 | return;
|
29 | }
|
30 |
|
31 | let fixData;
|
32 |
|
33 | mediaFeatureColonSpaceChecker({
|
34 | root,
|
35 | result,
|
36 | locationChecker: checker.before,
|
37 | checkedRuleName: ruleName,
|
38 | fix: context.fix
|
39 | ? (atRule, index) => {
|
40 | const paramColonIndex = index - atRuleParamIndex(atRule);
|
41 |
|
42 | fixData = fixData || new Map();
|
43 | const colonIndices = fixData.get(atRule) || [];
|
44 |
|
45 | colonIndices.push(paramColonIndex);
|
46 | fixData.set(atRule, colonIndices);
|
47 |
|
48 | return true;
|
49 | }
|
50 | : null,
|
51 | });
|
52 |
|
53 | if (fixData) {
|
54 | fixData.forEach((colonIndices, atRule) => {
|
55 | let params = atRule.raws.params ? atRule.raws.params.raw : atRule.params;
|
56 |
|
57 | colonIndices
|
58 | .sort((a, b) => b - a)
|
59 | .forEach((index) => {
|
60 | const beforeColon = params.slice(0, index);
|
61 | const afterColon = params.slice(index);
|
62 |
|
63 | if (expectation === 'always') {
|
64 | params = beforeColon.replace(/\s*$/, ' ') + afterColon;
|
65 | } else if (expectation === 'never') {
|
66 | params = beforeColon.replace(/\s*$/, '') + afterColon;
|
67 | }
|
68 | });
|
69 |
|
70 | if (atRule.raws.params) {
|
71 | atRule.raws.params.raw = params;
|
72 | } else {
|
73 | atRule.params = params;
|
74 | }
|
75 | });
|
76 | }
|
77 | };
|
78 | }
|
79 |
|
80 | rule.ruleName = ruleName;
|
81 | rule.messages = messages;
|
82 | module.exports = rule;
|