stylelint
Version:
A mighty, modern CSS linter.
123 lines (103 loc) • 3.59 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.messages = exports.ruleName = undefined;
exports.default = function (on, options) {
return function (root, result) {
var validOptions = (0, _utils.validateOptions)(result, ruleName, { actual: on }, {
actual: options,
possible: {
ignore: ["consecutive-duplicates", "consecutive-duplicates-with-different-values"],
ignoreProperties: [_lodash.isString]
},
optional: true
});
if (!validOptions) {
return;
}
// In order to accommodate nested blocks (postcss-nested),
// we need to run a shallow loop (instead of eachDecl() or eachRule(),
// which loop recursively) and allow each nested block to accumulate
// its own list of properties -- so that a property in a nested rule
// does not conflict with the same property in the parent rule
root.each(function (node) {
if (node.type === "rule" || node.type === "atrule") {
checkRulesInNode(node);
}
});
function checkRulesInNode(node) {
var decls = [];
var values = [];
node.each(function (child) {
if (child.nodes && child.nodes.length) {
checkRulesInNode(child);
}
if (child.type !== "decl") {
return;
}
var prop = child.prop;
var value = child.value;
if (!(0, _utils.isStandardSyntaxProperty)(prop)) {
return;
}
if ((0, _utils.isCustomProperty)(prop)) {
return;
}
// Return early if the property is to be ignored
if ((0, _utils.optionsMatches)(options, "ignoreProperties", prop)) {
return;
}
// Ignore the src property as commonly duplicated in at-fontface
if (prop.toLowerCase() === "src") {
return;
}
var indexDuplicate = decls.indexOf(prop.toLowerCase());
if (indexDuplicate !== -1) {
if ((0, _utils.optionsMatches)(options, "ignore", "consecutive-duplicates-with-different-values")) {
// if duplicates are not consecutive
if (indexDuplicate !== decls.length - 1) {
(0, _utils.report)({
message: messages.rejected(prop),
node: child,
result: result,
ruleName: ruleName
});
return;
}
// if values of consecutive duplicates are equal
if (value === values[indexDuplicate]) {
(0, _utils.report)({
message: messages.rejected(value),
node: child,
result: result,
ruleName: ruleName
});
return;
}
return;
}
if ((0, _utils.optionsMatches)(options, "ignore", "consecutive-duplicates") && indexDuplicate === decls.length - 1) {
return;
}
(0, _utils.report)({
message: messages.rejected(prop),
node: child,
result: result,
ruleName: ruleName
});
}
decls.push(prop.toLowerCase());
values.push(value.toLowerCase());
});
}
};
};
var _utils = require("../../utils");
var _lodash = require("lodash");
var ruleName = exports.ruleName = "declaration-block-no-duplicate-properties";
var messages = exports.messages = (0, _utils.ruleMessages)(ruleName, {
rejected: function rejected(property) {
return "Unexpected duplicate \"" + property + "\"";
}
});