.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / stylelint / lib / rules / function-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 = "function-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       if (decl.value.indexOf("(") === -1) {
30         return;
31       }
32
33       const declString = decl.toString();
34       const repeatLFNewLines = _.repeat("\n", maxAdjacentNewlines);
35       const repeatCRLFNewLines = _.repeat("\r\n", maxAdjacentNewlines);
36
37       styleSearch(
38         {
39           source: declString,
40           target: "\n",
41           functionArguments: "only"
42         },
43         match => {
44           if (
45             declString.substr(match.startIndex + 1, maxAdjacentNewlines) ===
46               repeatLFNewLines ||
47             declString.substr(match.startIndex + 1, maxAdjacentNewlines * 2) ===
48               repeatCRLFNewLines
49           ) {
50             // Put index at `\r` if it's CRLF, otherwise leave it at `\n`
51             let index = match.startIndex;
52             if (declString[index - 1] === "\r") {
53               index -= 1;
54             }
55
56             report({
57               message: messages.expected(max),
58               node: decl,
59               index,
60               result,
61               ruleName
62             });
63           }
64         }
65       );
66     });
67   };
68 };
69
70 rule.ruleName = ruleName;
71 rule.messages = messages;
72 module.exports = rule;