UNPKG

stylelint

Version:
95 lines (77 loc) 3.27 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.messages = exports.ruleName = undefined; exports.default = function (expectation, options) { return function (root, result) { var validOptions = (0, _utils.validateOptions)(result, ruleName, { actual: expectation, possible: ["always", "never"] }, { actual: options, possible: { except: ["first-nested", "after-comment", "after-declaration"], ignore: ["after-comment", "after-declaration", "inside-single-line-block"] }, optional: true }); if (!validOptions) { return; } root.walkDecls(function (decl) { var prop = decl.prop; var parent = decl.parent; if (!(0, _utils.isStandardSyntaxDeclaration)(decl)) { return; } if ((0, _utils.isCustomProperty)(prop)) { return; } // Optionally ignore the node if a comment precedes it if ((0, _utils.optionsMatches)(options, "ignore", "after-comment") && decl.prev() && decl.prev().type === "comment") { return; } // Optionally ignore the node if a declaration precedes it if ((0, _utils.optionsMatches)(options, "ignore", "after-declaration") && decl.prev() && decl.prev().type === "decl") { return; } // Optionally ignore nodes inside single-line blocks if ((0, _utils.optionsMatches)(options, "ignore", "inside-single-line-block") && (0, _utils.isSingleLineString)((0, _utils.blockString)(parent))) { return; } var expectEmptyLineBefore = expectation === "always" ? true : false; // Optionally reverse the expectation for the first nested node if ((0, _utils.optionsMatches)(options, "except", "first-nested") && decl === parent.first) { expectEmptyLineBefore = !expectEmptyLineBefore; } // Optionally reverse the expectation if a comment precedes this node if ((0, _utils.optionsMatches)(options, "except", "after-comment") && decl.prev() && decl.prev().type === "comment") { expectEmptyLineBefore = !expectEmptyLineBefore; } // Optionally reverse the expectation if a declaration precedes this node if ((0, _utils.optionsMatches)(options, "except", "after-declaration") && decl.prev() && decl.prev().prop && (0, _utils.isStandardSyntaxDeclaration)(decl.prev()) && !(0, _utils.isCustomProperty)(decl.prev().prop)) { expectEmptyLineBefore = !expectEmptyLineBefore; } // Check for at least one empty line var hasEmptyLineBefore = (0, _utils.hasEmptyLine)(decl.raws["before"]); // Return if the expectation is met if (expectEmptyLineBefore === hasEmptyLineBefore) { return; } var message = expectEmptyLineBefore ? messages.expected : messages.rejected; (0, _utils.report)({ message: message, node: decl, result: result, ruleName: ruleName }); }); }; }; var _utils = require("../../utils"); var ruleName = exports.ruleName = "declaration-empty-line-before"; var messages = exports.messages = (0, _utils.ruleMessages)(ruleName, { expected: "Expected empty line before declaration", rejected: "Unexpected empty line before declaration" });