stylelint
Version:
A mighty, modern CSS linter.
116 lines (91 loc) • 3.12 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.messages = exports.ruleName = undefined;
exports.default = function (actual) {
return function (root, result) {
var validOptions = (0, _utils.validateOptions)(result, ruleName, { actual: actual });
if (!validOptions) {
return;
}
var rawAfterRoot = root.raws.after;
if (rawAfterRoot && rawAfterRoot.trim().length !== 0) {
(0, _styleSearch2.default)({ source: rawAfterRoot, target: ";" }, function (match) {
complain(root.toString().length - rawAfterRoot.length + match.startIndex);
});
}
root.walk(function (node) {
var rawBeforeNode = node.raws.before;
if (rawBeforeNode && rawBeforeNode.trim().length !== 0) {
(function () {
var allowedSemi = 0;
// Forbid semicolon before first custom properties sets
if ((0, _utils.isCustomPropertySet)(node) && node.parent.index(node) > 0) {
allowedSemi = 1;
}
(0, _styleSearch2.default)({ source: rawBeforeNode, target: ";" }, function (match, count) {
if (count === allowedSemi) {
return;
}
complain(getOffsetByNode(node) - rawBeforeNode.length + match.startIndex);
});
})();
}
var rawAfterNode = node.raws.after;
if (rawAfterNode && rawAfterNode.trim().length !== 0) {
(function () {
var allowedSemi = 0;
if (!(0, _utils.hasEmptyBlock)(node) && (0, _utils.isCustomPropertySet)(node.nodes[node.nodes.length - 1])) {
allowedSemi = 1;
}
(0, _styleSearch2.default)({ source: rawAfterNode, target: ";" }, function (match, count) {
if (count === allowedSemi) {
return;
}
var index = getOffsetByNode(node) + node.toString().length - 1 - rawAfterNode.length + match.startIndex;
complain(index);
});
})();
}
});
function complain(index) {
(0, _utils.report)({
message: messages.rejected,
node: root,
index: index,
result: result,
ruleName: ruleName
});
}
};
};
var _utils = require("../../utils");
var _styleSearch = require("style-search");
var _styleSearch2 = _interopRequireDefault(_styleSearch);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var ruleName = exports.ruleName = "no-extra-semicolons";
var messages = exports.messages = (0, _utils.ruleMessages)(ruleName, {
rejected: "Unexpected extra semicolon"
});
function getOffsetByNode(node) {
var string = node.root().toString();
var nodeColumn = node.source.start.column;
var nodeLine = node.source.start.line;
var line = 1;
var column = 1;
var index = 0;
for (var i = 0; i < string.length; i++) {
if (column === nodeColumn && nodeLine === line) {
index = i;
break;
}
if (string[i] === "\n") {
column = 1;
line += 1;
} else {
column += 1;
}
}
return index;
}