index.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. 'use strict';
  2. const isStandardSyntaxAtRule = require('../../utils/isStandardSyntaxAtRule');
  3. const isStandardSyntaxRule = require('../../utils/isStandardSyntaxRule');
  4. const report = require('../../utils/report');
  5. const ruleMessages = require('../../utils/ruleMessages');
  6. const styleSearch = require('style-search');
  7. const validateOptions = require('../../utils/validateOptions');
  8. const ruleName = 'no-extra-semicolons';
  9. const messages = ruleMessages(ruleName, {
  10. rejected: 'Unexpected extra semicolon',
  11. });
  12. const meta = {
  13. url: 'https://stylelint.io/user-guide/rules/list/no-extra-semicolons',
  14. };
  15. /**
  16. * @param {import('postcss').Node} node
  17. * @returns {number}
  18. */
  19. function getOffsetByNode(node) {
  20. // @ts-expect-error -- TS2339: Property 'document' does not exist on type 'Document | Container<ChildNode>'
  21. if (node.parent && node.parent.document) {
  22. return 0;
  23. }
  24. const root = node.root();
  25. if (!root.source) throw new Error('The root node must have a source');
  26. if (!node.source) throw new Error('The node must have a source');
  27. if (!node.source.start) throw new Error('The source must have a start position');
  28. const string = root.source.input.css;
  29. const nodeColumn = node.source.start.column;
  30. const nodeLine = node.source.start.line;
  31. let line = 1;
  32. let column = 1;
  33. let index = 0;
  34. for (let i = 0; i < string.length; i++) {
  35. if (column === nodeColumn && nodeLine === line) {
  36. index = i;
  37. break;
  38. }
  39. if (string[i] === '\n') {
  40. column = 1;
  41. line += 1;
  42. } else {
  43. column += 1;
  44. }
  45. }
  46. return index;
  47. }
  48. /** @type {import('stylelint').Rule} */
  49. const rule = (primary, _secondaryOptions, context) => {
  50. return (root, result) => {
  51. const validOptions = validateOptions(result, ruleName, { actual: primary });
  52. if (!validOptions) {
  53. return;
  54. }
  55. if (root.raws.after && root.raws.after.trim().length !== 0) {
  56. const rawAfterRoot = root.raws.after;
  57. /** @type {number[]} */
  58. const fixSemiIndices = [];
  59. styleSearch({ source: rawAfterRoot, target: ';' }, (match) => {
  60. if (context.fix) {
  61. fixSemiIndices.push(match.startIndex);
  62. return;
  63. }
  64. if (!root.source) throw new Error('The root node must have a source');
  65. complain(root.source.input.css.length - rawAfterRoot.length + match.startIndex);
  66. });
  67. // fix
  68. if (fixSemiIndices.length) {
  69. root.raws.after = removeIndices(rawAfterRoot, fixSemiIndices);
  70. }
  71. }
  72. root.walk((node) => {
  73. if (node.type === 'atrule' && !isStandardSyntaxAtRule(node)) {
  74. return;
  75. }
  76. if (node.type === 'rule' && !isStandardSyntaxRule(node)) {
  77. return;
  78. }
  79. if (node.raws.before && node.raws.before.trim().length !== 0) {
  80. const rawBeforeNode = node.raws.before;
  81. const allowedSemi = 0;
  82. const rawBeforeIndexStart = 0;
  83. /** @type {number[]} */
  84. const fixSemiIndices = [];
  85. styleSearch({ source: rawBeforeNode, target: ';' }, (match, count) => {
  86. if (count === allowedSemi) {
  87. return;
  88. }
  89. if (context.fix) {
  90. fixSemiIndices.push(match.startIndex - rawBeforeIndexStart);
  91. return;
  92. }
  93. complain(getOffsetByNode(node) - rawBeforeNode.length + match.startIndex);
  94. });
  95. // fix
  96. if (fixSemiIndices.length) {
  97. node.raws.before = removeIndices(rawBeforeNode, fixSemiIndices);
  98. }
  99. }
  100. if (typeof node.raws.after === 'string' && node.raws.after.trim().length !== 0) {
  101. const rawAfterNode = node.raws.after;
  102. /**
  103. * If the last child is a Less mixin followed by more than one semicolon,
  104. * node.raws.after will be populated with that semicolon.
  105. * Since we ignore Less mixins, exit here
  106. */
  107. if (
  108. 'last' in node &&
  109. node.last &&
  110. node.last.type === 'atrule' &&
  111. !isStandardSyntaxAtRule(node.last)
  112. ) {
  113. return;
  114. }
  115. /** @type {number[]} */
  116. const fixSemiIndices = [];
  117. styleSearch({ source: rawAfterNode, target: ';' }, (match) => {
  118. if (context.fix) {
  119. fixSemiIndices.push(match.startIndex);
  120. return;
  121. }
  122. const index =
  123. getOffsetByNode(node) +
  124. node.toString().length -
  125. 1 -
  126. rawAfterNode.length +
  127. match.startIndex;
  128. complain(index);
  129. });
  130. // fix
  131. if (fixSemiIndices.length) {
  132. node.raws.after = removeIndices(rawAfterNode, fixSemiIndices);
  133. }
  134. }
  135. if (typeof node.raws.ownSemicolon === 'string') {
  136. const rawOwnSemicolon = node.raws.ownSemicolon;
  137. const allowedSemi = 0;
  138. /** @type {number[]} */
  139. const fixSemiIndices = [];
  140. styleSearch({ source: rawOwnSemicolon, target: ';' }, (match, count) => {
  141. if (count === allowedSemi) {
  142. return;
  143. }
  144. if (context.fix) {
  145. fixSemiIndices.push(match.startIndex);
  146. return;
  147. }
  148. const index =
  149. getOffsetByNode(node) +
  150. node.toString().length -
  151. rawOwnSemicolon.length +
  152. match.startIndex;
  153. complain(index);
  154. });
  155. // fix
  156. if (fixSemiIndices.length) {
  157. node.raws.ownSemicolon = removeIndices(rawOwnSemicolon, fixSemiIndices);
  158. }
  159. }
  160. });
  161. /**
  162. * @param {number} index
  163. */
  164. function complain(index) {
  165. report({
  166. message: messages.rejected,
  167. node: root,
  168. index,
  169. result,
  170. ruleName,
  171. });
  172. }
  173. /**
  174. * @param {string} str
  175. * @param {number[]} indices
  176. * @returns {string}
  177. */
  178. function removeIndices(str, indices) {
  179. for (const index of indices.reverse()) {
  180. str = str.slice(0, index) + str.slice(index + 1);
  181. }
  182. return str;
  183. }
  184. };
  185. };
  186. rule.ruleName = ruleName;
  187. rule.messages = messages;
  188. rule.meta = meta;
  189. module.exports = rule;