.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / stylelint / lib / rules / block-opening-brace-newline-before / index.js
1 "use strict";
2
3 const beforeBlockString = require("../../utils/beforeBlockString");
4 const blockString = require("../../utils/blockString");
5 const hasBlock = require("../../utils/hasBlock");
6 const hasEmptyBlock = require("../../utils/hasEmptyBlock");
7 const report = require("../../utils/report");
8 const ruleMessages = require("../../utils/ruleMessages");
9 const validateOptions = require("../../utils/validateOptions");
10 const whitespaceChecker = require("../../utils/whitespaceChecker");
11
12 const ruleName = "block-opening-brace-newline-before";
13
14 const messages = ruleMessages(ruleName, {
15   expectedBefore: () => 'Expected newline before "{"',
16   expectedBeforeSingleLine: () =>
17     'Expected newline before "{" of a single-line block',
18   rejectedBeforeSingleLine: () =>
19     'Unexpected whitespace before "{" of a single-line block',
20   expectedBeforeMultiLine: () =>
21     'Expected newline before "{" of a multi-line block',
22   rejectedBeforeMultiLine: () =>
23     'Unexpected whitespace before "{" of a multi-line block'
24 });
25
26 const rule = function(expectation) {
27   const checker = whitespaceChecker("newline", expectation, messages);
28
29   return (root, result) => {
30     const validOptions = validateOptions(result, ruleName, {
31       actual: expectation,
32       possible: [
33         "always",
34         "always-single-line",
35         "never-single-line",
36         "always-multi-line",
37         "never-multi-line"
38       ]
39     });
40     if (!validOptions) {
41       return;
42     }
43
44     // Check both kinds of statement: rules and at-rules
45     root.walkRules(check);
46     root.walkAtRules(check);
47
48     function check(statement) {
49       // Return early if blockless or has an empty block
50       if (!hasBlock(statement) || hasEmptyBlock(statement)) {
51         return;
52       }
53
54       const source = beforeBlockString(statement);
55       const beforeBraceNoRaw = beforeBlockString(statement, {
56         noRawBefore: true
57       });
58
59       let index = beforeBraceNoRaw.length - 1;
60       if (beforeBraceNoRaw[index - 1] === "\r") {
61         index -= 1;
62       }
63
64       checker.beforeAllowingIndentation({
65         lineCheckStr: blockString(statement),
66         source,
67         index: source.length,
68         err: m => {
69           report({
70             message: m,
71             node: statement,
72             index,
73             result,
74             ruleName
75           });
76         }
77       });
78     }
79   };
80 };
81
82 rule.ruleName = ruleName;
83 rule.messages = messages;
84 module.exports = rule;