.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / stylelint / lib / rules / block-opening-brace-newline-after / 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 nextNonCommentNode = require("../../utils/nextNonCommentNode");
8 const rawNodeString = require("../../utils/rawNodeString");
9 const report = require("../../utils/report");
10 const ruleMessages = require("../../utils/ruleMessages");
11 const validateOptions = require("../../utils/validateOptions");
12 const whitespaceChecker = require("../../utils/whitespaceChecker");
13
14 const ruleName = "block-opening-brace-newline-after";
15
16 const messages = ruleMessages(ruleName, {
17   expectedAfter: () => 'Expected newline after "{"',
18   expectedAfterMultiLine: () =>
19     'Expected newline after "{" of a multi-line block',
20   rejectedAfterMultiLine: () =>
21     'Unexpected whitespace after "{" of a multi-line block'
22 });
23
24 const rule = function(expectation) {
25   const checker = whitespaceChecker("newline", expectation, messages);
26
27   return (root, result) => {
28     const validOptions = validateOptions(result, ruleName, {
29       actual: expectation,
30       possible: ["always", "always-multi-line", "never-multi-line"]
31     });
32     if (!validOptions) {
33       return;
34     }
35
36     // Check both kinds of statement: rules and at-rules
37     root.walkRules(check);
38     root.walkAtRules(check);
39
40     function check(statement) {
41       // Return early if blockless or has an empty block
42       if (!hasBlock(statement) || hasEmptyBlock(statement)) {
43         return;
44       }
45
46       // Allow an end-of-line comment
47       const nodeToCheck = nextNonCommentNode(statement.first);
48       if (!nodeToCheck) {
49         return;
50       }
51
52       checker.afterOneOnly({
53         source: rawNodeString(nodeToCheck),
54         index: -1,
55         lineCheckStr: blockString(statement),
56         err: m => {
57           report({
58             message: m,
59             node: statement,
60             index:
61               beforeBlockString(statement, { noRawBefore: true }).length + 1,
62             result,
63             ruleName
64           });
65         }
66       });
67     }
68   };
69 };
70
71 rule.ruleName = ruleName;
72 rule.messages = messages;
73 module.exports = rule;