3 const addEmptyLineBefore = require("../../utils/addEmptyLineBefore");
4 const blockString = require("../../utils/blockString");
5 const getPreviousNonSharedLineCommentNode = require("../../utils/getPreviousNonSharedLineCommentNode");
6 const hasEmptyLine = require("../../utils/hasEmptyLine");
7 const isAfterComment = require("../../utils/isAfterComment");
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 = "custom-property-empty-line-before";
20 const messages = ruleMessages(ruleName, {
21 expected: "Expected empty line before custom property",
22 rejected: "Unexpected empty line before custom property"
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-custom-property"],
38 ignore: ["after-comment", "inside-single-line-block"]
47 root.walkDecls(decl => {
48 const prop = decl.prop,
51 if (!isStandardSyntaxDeclaration(decl)) {
54 if (!isCustomProperty(prop)) {
58 // Optionally ignore the node if a comment precedes it
60 optionsMatches(options, "ignore", "after-comment") &&
66 // Optionally ignore nodes inside single-line blocks
68 optionsMatches(options, "ignore", "inside-single-line-block") &&
69 isSingleLineString(blockString(parent))
74 let expectEmptyLineBefore = expectation === "always" ? true : false;
76 // Optionally reverse the expectation if any exceptions apply
78 (optionsMatches(options, "except", "first-nested") &&
79 isFirstNested(decl)) ||
80 (optionsMatches(options, "except", "after-comment") &&
81 isAfterComment(decl)) ||
82 (optionsMatches(options, "except", "after-custom-property") &&
83 isAfterCustomProperty(decl))
85 expectEmptyLineBefore = !expectEmptyLineBefore;
88 const hasEmptyLineBefore = hasEmptyLine(decl.raws.before);
90 // Return if the expectation is met
91 if (expectEmptyLineBefore === hasEmptyLineBefore) {
97 if (expectEmptyLineBefore) {
98 addEmptyLineBefore(decl, context.newline);
100 removeEmptyLinesBefore(decl, context.newline);
106 const message = expectEmptyLineBefore
119 function isAfterCustomProperty(decl) {
120 const prevNode = getPreviousNonSharedLineCommentNode(decl);
121 return prevNode && prevNode.prop && isCustomProperty(prevNode.prop);
124 rule.ruleName = ruleName;
125 rule.messages = messages;
126 module.exports = rule;