index.js 4.8 KB

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