.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / stylelint / lib / rules / value-list-max-empty-lines / index.js
1 "use strict";
2
3 const _ = require("lodash");
4 const report = require("../../utils/report");
5 const ruleMessages = require("../../utils/ruleMessages");
6 const styleSearch = require("style-search");
7 const validateOptions = require("../../utils/validateOptions");
8
9 const ruleName = "value-list-max-empty-lines";
10
11 const messages = ruleMessages(ruleName, {
12   expected: max =>
13     `Expected no more than ${max} empty ${max === 1 ? "line" : "lines"}`
14 });
15
16 const rule = function(max) {
17   const maxAdjacentNewlines = max + 1;
18
19   return (root, result) => {
20     const validOptions = validateOptions(result, ruleName, {
21       actual: max,
22       possible: _.isNumber
23     });
24     if (!validOptions) {
25       return;
26     }
27
28     root.walkDecls(decl => {
29       const value = decl.value;
30       const repeatLFNewLines = _.repeat("\n", maxAdjacentNewlines);
31       const repeatCRLFNewLines = _.repeat("\r\n", maxAdjacentNewlines);
32
33       styleSearch({ source: value, target: "\n" }, match => {
34         if (
35           value.substr(match.startIndex + 1, maxAdjacentNewlines) ===
36             repeatLFNewLines ||
37           value.substr(match.startIndex + 1, maxAdjacentNewlines * 2) ===
38             repeatCRLFNewLines
39         ) {
40           // Put index at `\r` if it's CRLF, otherwise leave it at `\n`
41           let index = match.startIndex;
42           if (value[index - 1] === "\r") {
43             index -= 1;
44           }
45
46           report({
47             message: messages.expected(max),
48             node: decl,
49             index,
50             result,
51             ruleName
52           });
53         }
54       });
55     });
56   };
57 };
58
59 rule.ruleName = ruleName;
60 rule.messages = messages;
61 module.exports = rule;