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");
14 const ruleName = "block-opening-brace-newline-after";
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'
24 const rule = function(expectation) {
25 const checker = whitespaceChecker("newline", expectation, messages);
27 return (root, result) => {
28 const validOptions = validateOptions(result, ruleName, {
30 possible: ["always", "always-multi-line", "never-multi-line"]
36 // Check both kinds of statement: rules and at-rules
37 root.walkRules(check);
38 root.walkAtRules(check);
40 function check(statement) {
41 // Return early if blockless or has an empty block
42 if (!hasBlock(statement) || hasEmptyBlock(statement)) {
46 // Allow an end-of-line comment
47 const nodeToCheck = nextNonCommentNode(statement.first);
52 checker.afterOneOnly({
53 source: rawNodeString(nodeToCheck),
55 lineCheckStr: blockString(statement),
61 beforeBlockString(statement, { noRawBefore: true }).length + 1,
71 rule.ruleName = ruleName;
72 rule.messages = messages;
73 module.exports = rule;