3 const addEmptyLineBefore = require("../../utils/addEmptyLineBefore");
4 const blockString = require("../../utils/blockString");
5 const hasEmptyLine = require("../../utils/hasEmptyLine");
6 const isAfterComment = require("../../utils/isAfterComment");
7 const isAfterStandardPropertyDeclaration = require("../../utils/isAfterStandardPropertyDeclaration");
8 const isCustomProperty = require("../../utils/isCustomProperty");
9 const isFirstNested = require("../../utils/isFirstNested");
10 const isSingleLineString = require("../../utils/isSingleLineString");
11 const isStandardSyntaxDeclaration = require("../../utils/isStandardSyntaxDeclaration");
12 const optionsMatches = require("../../utils/optionsMatches");
13 const removeEmptyLinesBefore = require("../../utils/removeEmptyLinesBefore");
14 const report = require("../../utils/report");
15 const ruleMessages = require("../../utils/ruleMessages");
16 const validateOptions = require("../../utils/validateOptions");
18 const ruleName = "declaration-empty-line-before";
20 const messages = ruleMessages(ruleName, {
21 expected: "Expected empty line before declaration",
22 rejected: "Unexpected empty line before declaration"
25 const rule = function(expectation, options, context) {
26 return (root, result) => {
27 const validOptions = validateOptions(
32 possible: ["always", "never"]
37 except: ["first-nested", "after-comment", "after-declaration"],
41 "inside-single-line-block"
51 root.walkDecls(decl => {
52 const prop = decl.prop,
55 if (!isStandardSyntaxDeclaration(decl)) {
58 if (isCustomProperty(prop)) {
62 // Optionally ignore the node if a comment precedes it
64 optionsMatches(options, "ignore", "after-comment") &&
70 // Optionally ignore the node if a declaration precedes it
72 optionsMatches(options, "ignore", "after-declaration") &&
73 isAfterStandardPropertyDeclaration(decl)
78 // Optionally ignore nodes inside single-line blocks
80 optionsMatches(options, "ignore", "inside-single-line-block") &&
81 isSingleLineString(blockString(parent))
86 let expectEmptyLineBefore = expectation === "always" ? true : false;
88 // Optionally reverse the expectation if any exceptions apply
90 (optionsMatches(options, "except", "first-nested") &&
91 isFirstNested(decl)) ||
92 (optionsMatches(options, "except", "after-comment") &&
93 isAfterComment(decl)) ||
94 (optionsMatches(options, "except", "after-declaration") &&
95 isAfterStandardPropertyDeclaration(decl))
97 expectEmptyLineBefore = !expectEmptyLineBefore;
100 // Check for at least one empty line
101 const hasEmptyLineBefore = hasEmptyLine(decl.raws.before);
103 // Return if the expectation is met
104 if (expectEmptyLineBefore === hasEmptyLineBefore) {
110 if (expectEmptyLineBefore) {
111 addEmptyLineBefore(decl, context.newline);
113 removeEmptyLinesBefore(decl, context.newline);
119 const message = expectEmptyLineBefore
122 report({ message, node: decl, result, ruleName });
127 rule.ruleName = ruleName;
128 rule.messages = messages;
129 module.exports = rule;