stylelint
Version:
A mighty, modern CSS linter.
100 lines (79 loc) • 2.93 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.messages = exports.ruleName = undefined;
exports.default = function (expectation) {
return function (root, result) {
var validOptions = (0, _utils.validateOptions)(result, ruleName, {
actual: expectation,
possible: ["always", "never"]
});
if (!validOptions) {
return;
}
root.walkAtRules(function (atRule) {
if (atRule.name.toLowerCase() === "import") {
return;
}
check(atRule, atRule.params, _utils.atRuleParamIndex);
});
root.walkDecls(function (decl) {
return check(decl, decl.value, _utils.declarationValueIndex);
});
function check(node, value, getIndex) {
// Get out quickly if there are no periods
if (value.indexOf(".") === -1) {
return;
}
(0, _postcssValueParser2.default)(value).walk(function (valueNode) {
// Ignore `url` function
if (valueNode.type === "function" && valueNode.value.toLowerCase() === "url") {
return false;
}
// Ignore strings, comments, etc
if (valueNode.type !== "word") {
return;
}
// Check leading zero
if (expectation === "always") {
var match = /(?:\D|^)(\.\d+)/.exec(valueNode.value);
if (match === null) {
return;
}
// The regexp above consists of 2 capturing groups (or capturing parentheses).
// We need the index of the second group. This makes sanse when we have "-.5" as an input
// for regex. And we need the index of ".5".
var capturingGroupIndex = match[0].length - match[1].length;
complain(messages.expected, node, getIndex(node) + valueNode.sourceIndex + match.index + capturingGroupIndex);
}
if (expectation === "never") {
var _match = /(?:\D|^)(0+\.\d+)/.exec(valueNode.value);
if (_match === null) {
return;
}
var _capturingGroupIndex = _match[0].length - _match[1].length;
complain(messages.rejected, node, getIndex(node) + valueNode.sourceIndex + _match.index + _capturingGroupIndex);
}
});
}
function complain(message, node, index) {
(0, _utils.report)({
result: result,
ruleName: ruleName,
message: message,
node: node,
index: index
});
}
};
};
var _utils = require("../../utils");
var _postcssValueParser = require("postcss-value-parser");
var _postcssValueParser2 = _interopRequireDefault(_postcssValueParser);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var ruleName = exports.ruleName = "number-leading-zero";
var messages = exports.messages = (0, _utils.ruleMessages)(ruleName, {
expected: "Expected a leading zero",
rejected: "Unexpected leading zero"
});