3 const addEmptyLineBefore = require("../../utils/addEmptyLineBefore");
4 const hasEmptyLine = require("../../utils/hasEmptyLine");
5 const isAfterComment = require("../../utils/isAfterComment");
6 const isFirstNested = require("../../utils/isFirstNested");
7 const isFirstNodeOfRoot = require("../../utils/isFirstNodeOfRoot");
8 const isSharedLineComment = require("../../utils/isSharedLineComment");
9 const optionsMatches = require("../../utils/optionsMatches");
10 const removeEmptyLinesBefore = require("../../utils/removeEmptyLinesBefore");
11 const report = require("../../utils/report");
12 const ruleMessages = require("../../utils/ruleMessages");
13 const validateOptions = require("../../utils/validateOptions");
15 const ruleName = "comment-empty-line-before";
17 const messages = ruleMessages(ruleName, {
18 expected: "Expected empty line before comment",
19 rejected: "Unexpected empty line before comment"
22 const stylelintCommandPrefix = "stylelint-";
24 const rule = function(expectation, options, context) {
25 return (root, result) => {
26 const validOptions = validateOptions(
31 possible: ["always", "never"]
36 except: ["first-nested"],
37 ignore: ["stylelint-commands", "after-comment"]
46 root.walkComments(comment => {
47 // Ignore the first node
48 if (isFirstNodeOfRoot(comment)) {
52 // Optionally ignore stylelint commands
54 comment.text.indexOf(stylelintCommandPrefix) === 0 &&
55 optionsMatches(options, "ignore", "stylelint-commands")
60 // Optionally ignore newlines between comments
62 optionsMatches(options, "ignore", "after-comment") &&
63 isAfterComment(comment)
68 // Ignore shared-line comments
69 if (isSharedLineComment(comment)) {
73 // Ignore SCSS comments
74 if (comment.raws.inline || comment.inline) {
78 const expectEmptyLineBefore = (() => {
80 optionsMatches(options, "except", "first-nested") &&
81 isFirstNested(comment)
85 return expectation === "always";
88 const before = comment.raws.before || "";
89 const hasEmptyLineBefore = hasEmptyLine(before);
91 // Return if the expectation is met
92 if (expectEmptyLineBefore === hasEmptyLineBefore) {
98 if (expectEmptyLineBefore) {
99 addEmptyLineBefore(comment, context.newline);
101 removeEmptyLinesBefore(comment, context.newline);
107 const message = expectEmptyLineBefore
121 rule.ruleName = ruleName;
122 rule.messages = messages;
123 module.exports = rule;