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 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // @ts-nocheck
  2. 'use strict';
  3. const beforeBlockString = require('../../utils/beforeBlockString');
  4. const blockString = require('../../utils/blockString');
  5. const hasBlock = require('../../utils/hasBlock');
  6. const hasEmptyBlock = require('../../utils/hasEmptyBlock');
  7. const rawNodeString = require('../../utils/rawNodeString');
  8. const report = require('../../utils/report');
  9. const ruleMessages = require('../../utils/ruleMessages');
  10. const validateOptions = require('../../utils/validateOptions');
  11. const whitespaceChecker = require('../../utils/whitespaceChecker');
  12. const ruleName = 'block-opening-brace-newline-after';
  13. const messages = ruleMessages(ruleName, {
  14. expectedAfter: () => 'Expected newline after "{"',
  15. expectedAfterMultiLine: () => 'Expected newline after "{" of a multi-line block',
  16. rejectedAfterMultiLine: () => 'Unexpected whitespace after "{" of a multi-line block',
  17. });
  18. function rule(expectation, options, context) {
  19. const checker = whitespaceChecker('newline', expectation, messages);
  20. return (root, result) => {
  21. const validOptions = validateOptions(result, ruleName, {
  22. actual: expectation,
  23. possible: ['always', 'always-multi-line', 'never-multi-line'],
  24. });
  25. if (!validOptions) {
  26. return;
  27. }
  28. // Check both kinds of statement: rules and at-rules
  29. root.walkRules(check);
  30. root.walkAtRules(check);
  31. function check(statement) {
  32. // Return early if blockless or has an empty block
  33. if (!hasBlock(statement) || hasEmptyBlock(statement)) {
  34. return;
  35. }
  36. const backupCommentNextBefores = new Map();
  37. // next node with checking newlines after comment
  38. function nextNode(startNode) {
  39. if (!startNode || !startNode.next) return null;
  40. if (startNode.type === 'comment') {
  41. const reNewLine = /\r?\n/;
  42. const newLineMatch = reNewLine.test(startNode.raws.before);
  43. const next = startNode.next();
  44. if (next && newLineMatch && !reNewLine.test(next.raws.before)) {
  45. backupCommentNextBefores.set(next, next.raws.before);
  46. next.raws.before = startNode.raws.before;
  47. }
  48. return nextNode(next);
  49. }
  50. return startNode;
  51. }
  52. // Allow an end-of-line comment
  53. const nodeToCheck = nextNode(statement.first);
  54. if (!nodeToCheck) {
  55. return;
  56. }
  57. checker.afterOneOnly({
  58. source: rawNodeString(nodeToCheck),
  59. index: -1,
  60. lineCheckStr: blockString(statement),
  61. err: (m) => {
  62. if (context.fix) {
  63. if (expectation.startsWith('always')) {
  64. const index = nodeToCheck.raws.before.search(/\r?\n/);
  65. if (index >= 0) {
  66. nodeToCheck.raws.before = nodeToCheck.raws.before.slice(index);
  67. } else {
  68. nodeToCheck.raws.before = context.newline + nodeToCheck.raws.before;
  69. }
  70. backupCommentNextBefores.delete(nodeToCheck);
  71. return;
  72. }
  73. if (expectation === 'never-multi-line') {
  74. // Restore the `before` of the node next to the comment node.
  75. backupCommentNextBefores.forEach((before, node) => {
  76. node.raws.before = before;
  77. });
  78. backupCommentNextBefores.clear();
  79. // Fix
  80. const reNewLine = /\r?\n/;
  81. let fixTarget = statement.first;
  82. while (fixTarget) {
  83. if (reNewLine.test(fixTarget.raws.before)) {
  84. fixTarget.raws.before = fixTarget.raws.before.replace(/\r?\n/g, '');
  85. }
  86. if (fixTarget.type !== 'comment') {
  87. break;
  88. }
  89. fixTarget = fixTarget.next();
  90. }
  91. nodeToCheck.raws.before = '';
  92. return;
  93. }
  94. }
  95. report({
  96. message: m,
  97. node: statement,
  98. index: beforeBlockString(statement, { noRawBefore: true }).length + 1,
  99. result,
  100. ruleName,
  101. });
  102. },
  103. });
  104. // Restore the `before` of the node next to the comment node.
  105. backupCommentNextBefores.forEach((before, node) => {
  106. node.raws.before = before;
  107. });
  108. }
  109. };
  110. }
  111. rule.ruleName = ruleName;
  112. rule.messages = messages;
  113. module.exports = rule;