index.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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, isRegExp, isString, assert } = require('../../utils/validateTypes');
  8. const ruleName = 'max-line-length';
  9. const messages = ruleMessages(ruleName, {
  10. expected: (max) =>
  11. `Expected line length to be no more than ${max} ${max === 1 ? 'character' : 'characters'}`,
  12. });
  13. const meta = {
  14. url: 'https://stylelint.io/user-guide/rules/max-line-length',
  15. deprecated: true,
  16. };
  17. /** @type {import('stylelint').Rule} */
  18. const rule = (primary, secondaryOptions, context) => {
  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: ['non-comments', 'comments'],
  31. ignorePattern: [isString, isRegExp],
  32. },
  33. optional: true,
  34. },
  35. );
  36. if (!validOptions) {
  37. return;
  38. }
  39. if (root.source == null) {
  40. throw new Error('The root node must have a source');
  41. }
  42. const EXCLUDED_PATTERNS = [
  43. /url\(\s*(\S.*\S)\s*\)/gi, // allow tab, whitespace in url content
  44. /@import\s+(['"].*['"])/gi,
  45. ];
  46. const ignoreNonComments = optionsMatches(secondaryOptions, 'ignore', 'non-comments');
  47. const ignoreComments = optionsMatches(secondaryOptions, 'ignore', 'comments');
  48. const rootString = context.fix ? root.toString() : root.source.input.css;
  49. // Array of skipped sub strings, i.e `url(...)`, `@import "..."`
  50. /** @type {Array<[number, number]>} */
  51. let skippedSubStrings = [];
  52. let skippedSubStringsIndex = 0;
  53. for (const pattern of EXCLUDED_PATTERNS) {
  54. for (const match of rootString.matchAll(pattern)) {
  55. const subMatch = match[1] || '';
  56. const startOfSubString = (match.index || 0) + (match[0] || '').indexOf(subMatch);
  57. skippedSubStrings.push([startOfSubString, startOfSubString + subMatch.length]);
  58. }
  59. }
  60. skippedSubStrings = skippedSubStrings.sort((a, b) => a[0] - b[0]);
  61. // Check first line
  62. checkNewline({ endIndex: 0 });
  63. // Check subsequent lines
  64. styleSearch({ source: rootString, target: ['\n'], comments: 'check' }, (match) =>
  65. checkNewline(match),
  66. );
  67. /**
  68. * @param {number} index
  69. */
  70. function complain(index) {
  71. report({
  72. index,
  73. result,
  74. ruleName,
  75. message: messages.expected(primary),
  76. node: root,
  77. });
  78. }
  79. /**
  80. * @param {number} start
  81. * @param {number} end
  82. */
  83. function tryToPopSubString(start, end) {
  84. const skippedSubString = skippedSubStrings[skippedSubStringsIndex];
  85. assert(skippedSubString);
  86. const [startSubString, endSubString] = skippedSubString;
  87. // Excluded substring does not presented in current line
  88. if (end < startSubString) {
  89. return 0;
  90. }
  91. // Compute excluded substring size regarding to current line indexes
  92. const excluded = Math.min(end, endSubString) - Math.max(start, startSubString);
  93. // Current substring is out of range for next lines
  94. if (endSubString <= end) {
  95. skippedSubStringsIndex++;
  96. }
  97. return excluded;
  98. }
  99. /**
  100. * @param {import('style-search').StyleSearchMatch | { endIndex: number }} match
  101. */
  102. function checkNewline(match) {
  103. let nextNewlineIndex = rootString.indexOf('\n', match.endIndex);
  104. if (rootString[nextNewlineIndex - 1] === '\r') {
  105. nextNewlineIndex -= 1;
  106. }
  107. // Accommodate last line
  108. if (nextNewlineIndex === -1) {
  109. nextNewlineIndex = rootString.length;
  110. }
  111. const rawLineLength = nextNewlineIndex - match.endIndex;
  112. const excludedLength = skippedSubStrings[skippedSubStringsIndex]
  113. ? tryToPopSubString(match.endIndex, nextNewlineIndex)
  114. : 0;
  115. const lineText = rootString.slice(match.endIndex, nextNewlineIndex);
  116. // Case sensitive ignorePattern match
  117. if (optionsMatches(secondaryOptions, 'ignorePattern', lineText)) {
  118. return;
  119. }
  120. // If the line's length is less than or equal to the specified
  121. // max, ignore it ... So anything below is liable to be complained about.
  122. // **Note that the length of any url arguments or import urls
  123. // are excluded from the calculation.**
  124. if (rawLineLength - excludedLength <= primary) {
  125. return;
  126. }
  127. const complaintIndex = nextNewlineIndex - 1;
  128. if (ignoreComments) {
  129. if ('insideComment' in match && match.insideComment) {
  130. return;
  131. }
  132. // This trimming business is to notice when the line starts a
  133. // comment but that comment is indented, e.g.
  134. // /* something here */
  135. const nextTwoChars = rootString.slice(match.endIndex).trim().slice(0, 2);
  136. if (nextTwoChars === '/*' || nextTwoChars === '//') {
  137. return;
  138. }
  139. }
  140. if (ignoreNonComments) {
  141. if ('insideComment' in match && match.insideComment) {
  142. return complain(complaintIndex);
  143. }
  144. // This trimming business is to notice when the line starts a
  145. // comment but that comment is indented, e.g.
  146. // /* something here */
  147. const nextTwoChars = rootString.slice(match.endIndex).trim().slice(0, 2);
  148. if (nextTwoChars !== '/*' && nextTwoChars !== '//') {
  149. return;
  150. }
  151. return complain(complaintIndex);
  152. }
  153. // If there are no spaces besides initial (indent) spaces, ignore it
  154. const lineString = rootString.slice(match.endIndex, nextNewlineIndex);
  155. if (!lineString.replace(/^\s+/, '').includes(' ')) {
  156. return;
  157. }
  158. return complain(complaintIndex);
  159. }
  160. };
  161. };
  162. rule.ruleName = ruleName;
  163. rule.messages = messages;
  164. rule.meta = meta;
  165. module.exports = rule;