.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / stylelint / lib / rules / selector-list-comma-newline-after / index.js
1 "use strict";
2
3 const isStandardSyntaxRule = require("../../utils/isStandardSyntaxRule");
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 const whitespaceChecker = require("../../utils/whitespaceChecker");
9
10 const ruleName = "selector-list-comma-newline-after";
11
12 const messages = ruleMessages(ruleName, {
13   expectedAfter: () => 'Expected newline after ","',
14   expectedAfterMultiLine: () =>
15     'Expected newline after "," in a multi-line list',
16   rejectedAfterMultiLine: () =>
17     'Unexpected whitespace after "," in a multi-line list'
18 });
19
20 const rule = function(expectation) {
21   const checker = whitespaceChecker("newline", expectation, messages);
22   return (root, result) => {
23     const validOptions = validateOptions(result, ruleName, {
24       actual: expectation,
25       possible: ["always", "always-multi-line", "never-multi-line"]
26     });
27     if (!validOptions) {
28       return;
29     }
30
31     root.walkRules(rule => {
32       if (!isStandardSyntaxRule(rule)) {
33         return;
34       }
35       // Get raw selector so we can allow end-of-line comments, e.g.
36       // a, /* comment */
37       // b {}
38       const selector = rule.raws.selector
39         ? rule.raws.selector.raw
40         : rule.selector;
41       styleSearch(
42         {
43           source: selector,
44           target: ",",
45           functionArguments: "skip"
46         },
47         match => {
48           const nextChars = selector.substr(
49             match.endIndex,
50             selector.length - match.endIndex
51           );
52
53           // If there's a // comment, that means there has to be a newline
54           // ending the comment so we're fine
55           if (nextChars.match(/^\s+\/\//)) {
56             return;
57           }
58
59           // If there are spaces and then a comment begins, look for the newline
60           const indextoCheckAfter = nextChars.match(/^\s+\/\*/)
61             ? selector.indexOf("*/", match.endIndex) + 1
62             : match.startIndex;
63           checker.afterOneOnly({
64             source: selector,
65             index: indextoCheckAfter,
66             err: m =>
67               report({
68                 message: m,
69                 node: rule,
70                 index: match.startIndex,
71                 result,
72                 ruleName
73               })
74           });
75         }
76       );
77     });
78   };
79 };
80
81 rule.ruleName = ruleName;
82 rule.messages = messages;
83 module.exports = rule;