forked from stylelint/stylelint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
100 lines (82 loc) · 2.89 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
89
90
91
92
93
94
95
96
97
98
99
100
'use strict';
const declarationValueIndex = require('../../utils/declarationValueIndex');
const ruleMessages = require('../../utils/ruleMessages');
const validateOptions = require('../../utils/validateOptions');
const valueListCommaWhitespaceChecker = require('../valueListCommaWhitespaceChecker');
const whitespaceChecker = require('../../utils/whitespaceChecker');
const ruleName = 'value-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',
});
const rule = function(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;
}
let fixData;
valueListCommaWhitespaceChecker({
root,
result,
locationChecker: checker.afterOneOnly,
checkedRuleName: ruleName,
fix: context.fix
? (declNode, index) => {
const valueIndex = declarationValueIndex(declNode);
if (index <= valueIndex) {
return false;
}
fixData = fixData || new Map();
const commaIndices = fixData.get(declNode) || [];
commaIndices.push(index);
fixData.set(declNode, commaIndices);
return true;
}
: null,
determineIndex: (declString, match) => {
const nextChars = declString.substr(match.endIndex, declString.length - match.endIndex);
// If there's a // comment, that means there has to be a newline
// ending the comment so we're fine
if (nextChars.match(/^[ \t]*\/\//)) {
return false;
}
// If there are spaces and then a comment begins, look for the newline
return nextChars.match(/^[ \t]*\/\*/)
? declString.indexOf('*/', match.endIndex) + 1
: match.startIndex;
},
});
if (fixData) {
fixData.forEach((commaIndices, decl) => {
commaIndices
.sort((a, b) => a - b)
.reverse()
.forEach((index) => {
const value = decl.raws.value ? decl.raws.value.raw : decl.value;
const valueIndex = index - declarationValueIndex(decl);
const beforeValue = value.slice(0, valueIndex + 1);
let afterValue = value.slice(valueIndex + 1);
if (expectation.startsWith('always')) {
afterValue = context.newline + afterValue;
} else if (expectation.startsWith('never-multi-line')) {
afterValue = afterValue.replace(/^\s*/, '');
}
if (decl.raws.value) {
decl.raws.value.raw = beforeValue + afterValue;
} else {
decl.value = beforeValue + afterValue;
}
});
});
}
};
};
rule.ruleName = ruleName;
rule.messages = messages;
module.exports = rule;