Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 4.9KB

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