-
-
Notifications
You must be signed in to change notification settings - Fork 929
/
index.js
88 lines (71 loc) · 2.51 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
// @ts-nocheck
'use strict';
const atRuleParamIndex = require('../../utils/atRuleParamIndex');
const mediaQueryListCommaWhitespaceChecker = require('../mediaQueryListCommaWhitespaceChecker');
const ruleMessages = require('../../utils/ruleMessages');
const validateOptions = require('../../utils/validateOptions');
const whitespaceChecker = require('../../utils/whitespaceChecker');
const ruleName = 'media-query-list-comma-newline-after';
const messages = ruleMessages(ruleName, {
expectedAfter: () => 'Expected newline after ","',
expectedAfterMultiLine: () => 'Expected newline after "," in a multi-line list',
rejectedAfterMultiLine: () => 'Unexpected whitespace after "," in a multi-line list',
});
function rule(expectation, options, context) {
const checker = whitespaceChecker('newline', expectation, messages);
return (root, result) => {
const validOptions = validateOptions(result, ruleName, {
actual: expectation,
possible: ['always', 'always-multi-line', 'never-multi-line'],
});
if (!validOptions) {
return;
}
// Only check for the newline after the comma, while allowing
// arbitrary indentation after the newline
let fixData;
mediaQueryListCommaWhitespaceChecker({
root,
result,
locationChecker: checker.afterOneOnly,
checkedRuleName: ruleName,
allowTrailingComments: expectation.startsWith('always'),
fix: context.fix
? (atRule, index) => {
const paramCommaIndex = index - atRuleParamIndex(atRule);
fixData = fixData || new Map();
const commaIndices = fixData.get(atRule) || [];
commaIndices.push(paramCommaIndex);
fixData.set(atRule, commaIndices);
return true;
}
: null,
});
if (fixData) {
fixData.forEach((commaIndices, atRule) => {
let params = atRule.raws.params ? atRule.raws.params.raw : atRule.params;
commaIndices
.sort((a, b) => b - a)
.forEach((index) => {
const beforeComma = params.slice(0, index + 1);
const afterComma = params.slice(index + 1);
if (expectation.startsWith('always')) {
params = /^\s*\r?\n/.test(afterComma)
? beforeComma + afterComma.replace(/^[^\S\r\n]*/, '')
: beforeComma + context.newline + afterComma;
} else if (expectation.startsWith('never')) {
params = beforeComma + afterComma.replace(/^\s*/, '');
}
});
if (atRule.raws.params) {
atRule.raws.params.raw = params;
} else {
atRule.params = params;
}
});
}
};
}
rule.ruleName = ruleName;
rule.messages = messages;
module.exports = rule;