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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // @ts-nocheck
  2. 'use strict';
  3. const addEmptyLineBefore = require('../../utils/addEmptyLineBefore');
  4. const getPreviousNonSharedLineCommentNode = require('../../utils/getPreviousNonSharedLineCommentNode');
  5. const hasEmptyLine = require('../../utils/hasEmptyLine');
  6. const isAfterSingleLineComment = require('../../utils/isAfterSingleLineComment');
  7. const isFirstNested = require('../../utils/isFirstNested');
  8. const isFirstNodeOfRoot = require('../../utils/isFirstNodeOfRoot');
  9. const isSingleLineString = require('../../utils/isSingleLineString');
  10. const isStandardSyntaxRule = require('../../utils/isStandardSyntaxRule');
  11. const optionsMatches = require('../../utils/optionsMatches');
  12. const removeEmptyLinesBefore = require('../../utils/removeEmptyLinesBefore');
  13. const report = require('../../utils/report');
  14. const ruleMessages = require('../../utils/ruleMessages');
  15. const validateOptions = require('../../utils/validateOptions');
  16. const ruleName = 'rule-empty-line-before';
  17. const messages = ruleMessages(ruleName, {
  18. expected: 'Expected empty line before rule',
  19. rejected: 'Unexpected empty line before rule',
  20. });
  21. function rule(expectation, options, context) {
  22. return (root, result) => {
  23. const validOptions = validateOptions(
  24. result,
  25. ruleName,
  26. {
  27. actual: expectation,
  28. possible: ['always', 'never', 'always-multi-line', 'never-multi-line'],
  29. },
  30. {
  31. actual: options,
  32. possible: {
  33. ignore: ['after-comment', 'first-nested', 'inside-block'],
  34. except: [
  35. 'after-rule',
  36. 'after-single-line-comment',
  37. 'first-nested',
  38. 'inside-block-and-after-rule',
  39. 'inside-block',
  40. ],
  41. },
  42. optional: true,
  43. },
  44. );
  45. if (!validOptions) {
  46. return;
  47. }
  48. root.walkRules((ruleNode) => {
  49. if (!isStandardSyntaxRule(ruleNode)) {
  50. return;
  51. }
  52. // Ignore the first node
  53. if (isFirstNodeOfRoot(ruleNode)) {
  54. return;
  55. }
  56. // Optionally ignore the expectation if a comment precedes this node
  57. if (
  58. optionsMatches(options, 'ignore', 'after-comment') &&
  59. ruleNode.prev() &&
  60. ruleNode.prev().type === 'comment'
  61. ) {
  62. return;
  63. }
  64. // Optionally ignore the node if it is the first nested
  65. if (optionsMatches(options, 'ignore', 'first-nested') && isFirstNested(ruleNode)) {
  66. return;
  67. }
  68. const isNested = ruleNode.parent.type !== 'root';
  69. // Optionally ignore the expectation if inside a block
  70. if (optionsMatches(options, 'ignore', 'inside-block') && isNested) {
  71. return;
  72. }
  73. // Ignore if the expectation is for multiple and the rule is single-line
  74. if (expectation.includes('multi-line') && isSingleLineString(ruleNode.toString())) {
  75. return;
  76. }
  77. let expectEmptyLineBefore = Boolean(expectation.includes('always'));
  78. // Optionally reverse the expectation if any exceptions apply
  79. if (
  80. (optionsMatches(options, 'except', 'first-nested') && isFirstNested(ruleNode)) ||
  81. (optionsMatches(options, 'except', 'after-rule') && isAfterRule(ruleNode)) ||
  82. (optionsMatches(options, 'except', 'inside-block-and-after-rule') &&
  83. isNested &&
  84. isAfterRule(ruleNode)) ||
  85. (optionsMatches(options, 'except', 'after-single-line-comment') &&
  86. isAfterSingleLineComment(ruleNode)) ||
  87. (optionsMatches(options, 'except', 'inside-block') && isNested)
  88. ) {
  89. expectEmptyLineBefore = !expectEmptyLineBefore;
  90. }
  91. const hasEmptyLineBefore = hasEmptyLine(ruleNode.raws.before);
  92. // Return if the expectation is met
  93. if (expectEmptyLineBefore === hasEmptyLineBefore) {
  94. return;
  95. }
  96. // Fix
  97. if (context.fix) {
  98. if (expectEmptyLineBefore) {
  99. addEmptyLineBefore(ruleNode, context.newline);
  100. } else {
  101. removeEmptyLinesBefore(ruleNode, context.newline);
  102. }
  103. return;
  104. }
  105. const message = expectEmptyLineBefore ? messages.expected : messages.rejected;
  106. report({
  107. message,
  108. node: ruleNode,
  109. result,
  110. ruleName,
  111. });
  112. });
  113. };
  114. }
  115. function isAfterRule(ruleNode) {
  116. const prevNode = getPreviousNonSharedLineCommentNode(ruleNode);
  117. return prevNode && prevNode.type === 'rule';
  118. }
  119. rule.ruleName = ruleName;
  120. rule.messages = messages;
  121. module.exports = rule;