3 const isCustomPropertySet = require("../../utils/isCustomPropertySet");
4 const isStandardSyntaxRule = require("../../utils/isStandardSyntaxRule");
5 const report = require("../../utils/report");
6 const ruleMessages = require("../../utils/ruleMessages");
7 const styleSearch = require("style-search");
8 const validateOptions = require("../../utils/validateOptions");
10 const ruleName = "no-extra-semicolons";
12 const messages = ruleMessages(ruleName, {
13 rejected: "Unexpected extra semicolon"
16 function getOffsetByNode(node) {
17 const string = node.root().source.input.css;
18 const nodeColumn = node.source.start.column;
19 const nodeLine = node.source.start.line;
24 for (let i = 0; i < string.length; i++) {
25 if (column === nodeColumn && nodeLine === line) {
30 if (string[i] === "\n") {
41 const rule = function(actual) {
42 return (root, result) => {
43 const validOptions = validateOptions(result, ruleName, { actual });
48 const rawAfterRoot = root.raws.after;
50 if (rawAfterRoot && rawAfterRoot.trim().length !== 0) {
51 styleSearch({ source: rawAfterRoot, target: ";" }, match => {
53 root.toString().length - rawAfterRoot.length + match.startIndex
60 node.type === "rule" &&
61 !isCustomPropertySet(node) &&
62 !isStandardSyntaxRule(node)
67 let rawBeforeNode = node.raws.before;
69 if (rawBeforeNode && rawBeforeNode.trim().length !== 0) {
72 const next = node.next();
74 // Ignore semicolon before comment if next node is custom properties sets or comment
76 node.type === "comment" &&
78 isCustomPropertySet(next) &&
79 node.parent.index(next) > 0
84 const prev = node.prev();
86 // Adding previous node string to custom properties set if previous node is comment
88 isCustomPropertySet(node) &&
89 node.parent.index(node) > 0 &&
91 prev.type === "comment"
93 rawBeforeNode = prev.toString() + rawBeforeNode;
97 styleSearch({ source: rawBeforeNode, target: ";" }, (match, count) => {
98 if (count === allowedSemi) {
103 getOffsetByNode(node) - rawBeforeNode.length + match.startIndex
108 const rawAfterNode = node.raws.after;
110 if (rawAfterNode && rawAfterNode.trim().length !== 0) {
112 * If the last child is a Less mixin followed by more than one semicolon,
113 * node.raws.after will be populated with that semicolon.
114 * Since we ignore Less mixins, exit here
118 node.last.type === "rule" &&
119 !isCustomPropertySet(node.last) &&
120 !isStandardSyntaxRule(node.last)
125 styleSearch({ source: rawAfterNode, target: ";" }, match => {
127 getOffsetByNode(node) +
128 node.toString().length -
130 rawAfterNode.length +
137 const rawOwnSemicolon = node.raws.ownSemicolon;
139 if (rawOwnSemicolon) {
142 if (isCustomPropertySet(node)) {
147 { source: rawOwnSemicolon, target: ";" },
149 if (count === allowedSemi) {
154 getOffsetByNode(node) +
155 node.toString().length -
156 rawOwnSemicolon.length +
164 function complain(index) {
166 message: messages.rejected,
176 rule.ruleName = ruleName;
177 rule.messages = messages;
178 module.exports = rule;