You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
117 lines
4.1 KiB
117 lines
4.1 KiB
"use strict";
|
|
|
|
Object.defineProperty(exports, "__esModule", {
|
|
value: true
|
|
});
|
|
exports["default"] = rule;
|
|
exports.ruleName = exports.meta = exports.messages = void 0;
|
|
var _stylelint = require("stylelint");
|
|
var _utils = require("../../utils");
|
|
var ruleName = (0, _utils.namespace)("dollar-variable-empty-line-before");
|
|
exports.ruleName = ruleName;
|
|
var messages = _stylelint.utils.ruleMessages(ruleName, {
|
|
expected: "Expected an empty line before $-variable",
|
|
rejected: "Unexpected empty line before $-variable"
|
|
});
|
|
exports.messages = messages;
|
|
var meta = {
|
|
url: (0, _utils.ruleUrl)(ruleName)
|
|
};
|
|
exports.meta = meta;
|
|
function rule(expectation, options, context) {
|
|
return function (root, result) {
|
|
var validOptions = _stylelint.utils.validateOptions(result, ruleName, {
|
|
actual: expectation,
|
|
possible: ["always", "never"]
|
|
}, {
|
|
actual: options,
|
|
possible: {
|
|
except: ["first-nested", "after-comment", "after-dollar-variable"],
|
|
ignore: ["after-comment", "inside-single-line-block", "after-dollar-variable"],
|
|
disableFix: _utils.isBoolean
|
|
},
|
|
optional: true
|
|
});
|
|
if (!validOptions) {
|
|
return;
|
|
}
|
|
var fix = function fix(decl, match, replace) {
|
|
decl.raws.before = decl.raws.before.replace(new RegExp("^".concat(match)), replace);
|
|
};
|
|
var hasNewline = function hasNewline(str) {
|
|
return str.includes(context.newline);
|
|
};
|
|
root.walkDecls(function (decl) {
|
|
if (!isDollarVar(decl)) {
|
|
return;
|
|
}
|
|
|
|
// Always ignore the first $var in a stylesheet
|
|
if (decl === root.first) {
|
|
return;
|
|
}
|
|
|
|
// If ignoring vars after comments is set
|
|
if ((0, _utils.optionsHaveIgnored)(options, "after-comment") && decl.prev() && decl.prev().type === "comment") {
|
|
return;
|
|
}
|
|
|
|
// If ignoring single-line blocks
|
|
if ((0, _utils.optionsHaveIgnored)(options, "inside-single-line-block") && decl.parent.type !== "root" && (0, _utils.isSingleLineString)((0, _utils.blockString)(decl.parent))) {
|
|
return;
|
|
}
|
|
|
|
// if ignoring after another $-variable
|
|
if ((0, _utils.optionsHaveIgnored)(options, "after-dollar-variable") && decl.prev() && isDollarVar(decl.prev())) {
|
|
return;
|
|
}
|
|
var expectHasEmptyLineBefore = expectation === "always";
|
|
|
|
// Reverse for a variable that is a first child of its parent
|
|
if ((0, _utils.optionsHaveException)(options, "first-nested") && decl === decl.parent.first) {
|
|
expectHasEmptyLineBefore = !expectHasEmptyLineBefore;
|
|
}
|
|
|
|
// Reverse if after a comment
|
|
if ((0, _utils.optionsHaveException)(options, "after-comment") && decl.prev() && decl.prev().type === "comment") {
|
|
expectHasEmptyLineBefore = !expectHasEmptyLineBefore;
|
|
}
|
|
|
|
// Reverse if after another $-variable
|
|
if ((0, _utils.optionsHaveException)(options, "after-dollar-variable") && decl.prev() && isDollarVar(decl.prev())) {
|
|
expectHasEmptyLineBefore = !expectHasEmptyLineBefore;
|
|
}
|
|
var before = decl.raws.before;
|
|
if (expectHasEmptyLineBefore === (0, _utils.hasEmptyLine)(before)) {
|
|
return;
|
|
}
|
|
var isFixDisabled = options && options.disableFix === true;
|
|
if (context.fix && !isFixDisabled) {
|
|
if (expectHasEmptyLineBefore && !(0, _utils.hasEmptyLine)(before)) {
|
|
fix(decl, context.newline, context.newline + context.newline);
|
|
if ((0, _utils.optionsHaveException)(options, "first-nested") && !hasNewline(before)) {
|
|
fix(decl, "\\s+", context.newline + context.newline);
|
|
}
|
|
return;
|
|
}
|
|
if (!expectHasEmptyLineBefore && (0, _utils.hasEmptyLine)(before)) {
|
|
fix(decl, "\\n\\r\\n", "\r\n");
|
|
fix(decl, context.newline + context.newline, context.newline);
|
|
return;
|
|
}
|
|
}
|
|
_stylelint.utils.report({
|
|
message: expectHasEmptyLineBefore ? messages.expected : messages.rejected,
|
|
node: decl,
|
|
result: result,
|
|
ruleName: ruleName
|
|
});
|
|
});
|
|
};
|
|
}
|
|
rule.ruleName = ruleName;
|
|
rule.messages = messages;
|
|
rule.meta = meta;
|
|
function isDollarVar(node) {
|
|
return node.prop && node.prop[0] === "$";
|
|
}
|