index.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. 'use strict';
  2. const optionsMatches = require('../../utils/optionsMatches');
  3. const report = require('../../utils/report');
  4. const ruleMessages = require('../../utils/ruleMessages');
  5. const styleSearch = require('style-search');
  6. const validateOptions = require('../../utils/validateOptions');
  7. const { isNumber } = require('../../utils/validateTypes');
  8. const ruleName = 'max-empty-lines';
  9. const messages = ruleMessages(ruleName, {
  10. expected: (max) => `Expected no more than ${max} empty ${max === 1 ? 'line' : 'lines'}`,
  11. });
  12. const meta = {
  13. url: 'https://stylelint.io/user-guide/rules/list/max-empty-lines',
  14. };
  15. /** @type {import('stylelint').Rule} */
  16. const rule = (primary, secondaryOptions, context) => {
  17. let emptyLines = 0;
  18. let lastIndex = -1;
  19. return (root, result) => {
  20. const validOptions = validateOptions(
  21. result,
  22. ruleName,
  23. {
  24. actual: primary,
  25. possible: isNumber,
  26. },
  27. {
  28. actual: secondaryOptions,
  29. possible: {
  30. ignore: ['comments'],
  31. },
  32. optional: true,
  33. },
  34. );
  35. if (!validOptions) {
  36. return;
  37. }
  38. const ignoreComments = optionsMatches(secondaryOptions, 'ignore', 'comments');
  39. const getChars = replaceEmptyLines.bind(null, primary);
  40. /**
  41. * 1. walk nodes & replace enterchar
  42. * 2. deal with special case.
  43. */
  44. if (context.fix) {
  45. root.walk((node) => {
  46. if (node.type === 'comment' && !ignoreComments) {
  47. node.raws.left = getChars(node.raws.left);
  48. node.raws.right = getChars(node.raws.right);
  49. }
  50. if (node.raws.before) {
  51. node.raws.before = getChars(node.raws.before);
  52. }
  53. });
  54. // first node
  55. const firstNodeRawsBefore = root.first && root.first.raws.before;
  56. // root raws
  57. const rootRawsAfter = root.raws.after;
  58. // not document node
  59. // @ts-expect-error -- TS2339: Property 'document' does not exist on type 'Root'.
  60. if ((root.document && root.document.constructor.name) !== 'Document') {
  61. if (firstNodeRawsBefore) {
  62. root.first.raws.before = getChars(firstNodeRawsBefore, true);
  63. }
  64. if (rootRawsAfter) {
  65. // when max setted 0, should be treated as 1 in this situation.
  66. root.raws.after = replaceEmptyLines(primary === 0 ? 1 : primary, rootRawsAfter, true);
  67. }
  68. } else if (rootRawsAfter) {
  69. // `css in js` or `html`
  70. root.raws.after = replaceEmptyLines(primary === 0 ? 1 : primary, rootRawsAfter);
  71. }
  72. return;
  73. }
  74. emptyLines = 0;
  75. lastIndex = -1;
  76. const rootString = root.toString();
  77. styleSearch(
  78. {
  79. source: rootString,
  80. target: /\r\n/.test(rootString) ? '\r\n' : '\n',
  81. comments: ignoreComments ? 'skip' : 'check',
  82. },
  83. (match) => {
  84. checkMatch(rootString, match.startIndex, match.endIndex, root);
  85. },
  86. );
  87. /**
  88. * @param {string} source
  89. * @param {number} matchStartIndex
  90. * @param {number} matchEndIndex
  91. * @param {import('postcss').Root} node
  92. */
  93. function checkMatch(source, matchStartIndex, matchEndIndex, node) {
  94. const eof = matchEndIndex === source.length;
  95. let problem = false;
  96. // Additional check for beginning of file
  97. if (!matchStartIndex || lastIndex === matchStartIndex) {
  98. emptyLines++;
  99. } else {
  100. emptyLines = 0;
  101. }
  102. lastIndex = matchEndIndex;
  103. if (emptyLines > primary) problem = true;
  104. if (!eof && !problem) return;
  105. if (problem) {
  106. report({
  107. message: messages.expected(primary),
  108. node,
  109. index: matchStartIndex,
  110. result,
  111. ruleName,
  112. });
  113. }
  114. // Additional check for end of file
  115. if (eof && primary) {
  116. emptyLines++;
  117. if (emptyLines > primary && isEofNode(result.root, node)) {
  118. report({
  119. message: messages.expected(primary),
  120. node,
  121. index: matchEndIndex,
  122. result,
  123. ruleName,
  124. });
  125. }
  126. }
  127. }
  128. /**
  129. * @param {number} maxLines
  130. * @param {unknown} str
  131. * @param {boolean?} isSpecialCase
  132. */
  133. function replaceEmptyLines(maxLines, str, isSpecialCase = false) {
  134. const repeatTimes = isSpecialCase ? maxLines : maxLines + 1;
  135. if (repeatTimes === 0 || typeof str !== 'string') {
  136. return '';
  137. }
  138. const emptyLFLines = '\n'.repeat(repeatTimes);
  139. const emptyCRLFLines = '\r\n'.repeat(repeatTimes);
  140. return /(?:\r\n)+/.test(str)
  141. ? str.replace(/(\r\n)+/g, ($1) => {
  142. if ($1.length / 2 > repeatTimes) {
  143. return emptyCRLFLines;
  144. }
  145. return $1;
  146. })
  147. : str.replace(/(\n)+/g, ($1) => {
  148. if ($1.length > repeatTimes) {
  149. return emptyLFLines;
  150. }
  151. return $1;
  152. });
  153. }
  154. };
  155. };
  156. /**
  157. * Checks whether the given node is the last node of file.
  158. * @param {import('stylelint').PostcssResult['root']} document - the document node with `postcss-html` and `postcss-jsx`.
  159. * @param {import('postcss').Root} root - the root node of css
  160. */
  161. function isEofNode(document, root) {
  162. if (!document || document.constructor.name !== 'Document' || !('type' in document)) {
  163. return true;
  164. }
  165. // In the `postcss-html` and `postcss-jsx` syntax, checks that there is text after the given node.
  166. let after;
  167. if (root === document.last) {
  168. after = document.raws && document.raws.codeAfter;
  169. } else {
  170. // @ts-expect-error -- TS2345: Argument of type 'Root' is not assignable to parameter of type 'number | ChildNode'.
  171. const rootIndex = document.index(root);
  172. const nextNode = document.nodes[rootIndex + 1];
  173. after = nextNode && nextNode.raws && nextNode.raws.codeBefore;
  174. }
  175. return !String(after).trim();
  176. }
  177. rule.ruleName = ruleName;
  178. rule.messages = messages;
  179. rule.meta = meta;
  180. module.exports = rule;