1 |
|
2 |
|
3 | 'use strict';
|
4 |
|
5 | const declarationValueIndex = require('../../utils/declarationValueIndex');
|
6 | const ruleMessages = require('../../utils/ruleMessages');
|
7 | const validateOptions = require('../../utils/validateOptions');
|
8 | const valueListCommaWhitespaceChecker = require('../valueListCommaWhitespaceChecker');
|
9 | const whitespaceChecker = require('../../utils/whitespaceChecker');
|
10 |
|
11 | const ruleName = 'value-list-comma-newline-after';
|
12 |
|
13 | const messages = ruleMessages(ruleName, {
|
14 | expectedAfter: () => 'Expected newline after ","',
|
15 | expectedAfterMultiLine: () => 'Expected newline after "," in a multi-line list',
|
16 | rejectedAfterMultiLine: () => 'Unexpected whitespace after "," in a multi-line list',
|
17 | });
|
18 |
|
19 | function rule(expectation, options, context) {
|
20 | const checker = whitespaceChecker('newline', expectation, messages);
|
21 |
|
22 | return (root, result) => {
|
23 | const validOptions = validateOptions(result, ruleName, {
|
24 | actual: expectation,
|
25 | possible: ['always', 'always-multi-line', 'never-multi-line'],
|
26 | });
|
27 |
|
28 | if (!validOptions) {
|
29 | return;
|
30 | }
|
31 |
|
32 | let fixData;
|
33 |
|
34 | valueListCommaWhitespaceChecker({
|
35 | root,
|
36 | result,
|
37 | locationChecker: checker.afterOneOnly,
|
38 | checkedRuleName: ruleName,
|
39 | fix: context.fix
|
40 | ? (declNode, index) => {
|
41 | const valueIndex = declarationValueIndex(declNode);
|
42 |
|
43 | if (index <= valueIndex) {
|
44 | return false;
|
45 | }
|
46 |
|
47 | fixData = fixData || new Map();
|
48 | const commaIndices = fixData.get(declNode) || [];
|
49 |
|
50 | commaIndices.push(index);
|
51 | fixData.set(declNode, commaIndices);
|
52 |
|
53 | return true;
|
54 | }
|
55 | : null,
|
56 | determineIndex: (declString, match) => {
|
57 | const nextChars = declString.substr(match.endIndex, declString.length - match.endIndex);
|
58 |
|
59 |
|
60 |
|
61 | if (/^[ \t]*\/\//.test(nextChars)) {
|
62 | return false;
|
63 | }
|
64 |
|
65 |
|
66 | return /^[ \t]*\/\*/.test(nextChars)
|
67 | ? declString.indexOf('*/', match.endIndex) + 1
|
68 | : match.startIndex;
|
69 | },
|
70 | });
|
71 |
|
72 | if (fixData) {
|
73 | fixData.forEach((commaIndices, decl) => {
|
74 | commaIndices
|
75 | .sort((a, b) => a - b)
|
76 | .reverse()
|
77 | .forEach((index) => {
|
78 | const value = decl.raws.value ? decl.raws.value.raw : decl.value;
|
79 | const valueIndex = index - declarationValueIndex(decl);
|
80 | const beforeValue = value.slice(0, valueIndex + 1);
|
81 | let afterValue = value.slice(valueIndex + 1);
|
82 |
|
83 | if (expectation.startsWith('always')) {
|
84 | afterValue = context.newline + afterValue;
|
85 | } else if (expectation.startsWith('never-multi-line')) {
|
86 | afterValue = afterValue.replace(/^\s*/, '');
|
87 | }
|
88 |
|
89 | if (decl.raws.value) {
|
90 | decl.raws.value.raw = beforeValue + afterValue;
|
91 | } else {
|
92 | decl.value = beforeValue + afterValue;
|
93 | }
|
94 | });
|
95 | });
|
96 | }
|
97 | };
|
98 | }
|
99 |
|
100 | rule.ruleName = ruleName;
|
101 | rule.messages = messages;
|
102 | module.exports = rule;
|