index.js 5.5 KB

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