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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // @ts-nocheck
  2. 'use strict';
  3. const _ = require('lodash');
  4. const isWhitespace = require('../../utils/isWhitespace');
  5. const report = require('../../utils/report');
  6. const ruleMessages = require('../../utils/ruleMessages');
  7. const validateOptions = require('../../utils/validateOptions');
  8. const ruleName = 'comment-whitespace-inside';
  9. const messages = ruleMessages(ruleName, {
  10. expectedOpening: 'Expected whitespace after "/*"',
  11. rejectedOpening: 'Unexpected whitespace after "/*"',
  12. expectedClosing: 'Expected whitespace before "*/"',
  13. rejectedClosing: 'Unexpected whitespace before "*/"',
  14. });
  15. function addWhitespaceBefore(comment) {
  16. if (comment.text.startsWith('*')) {
  17. comment.text = comment.text.replace(/^(\*+)/, `$1 `);
  18. } else {
  19. comment.raws.left = ' ';
  20. }
  21. }
  22. function addWhitespaceAfter(comment) {
  23. if (_.last(comment.text) === '*') {
  24. comment.text = comment.text.replace(/(\*+)$/, ` $1`);
  25. } else {
  26. comment.raws.right = ' ';
  27. }
  28. }
  29. function rule(expectation, options, context) {
  30. return function (root, result) {
  31. const validOptions = validateOptions(result, ruleName, {
  32. actual: expectation,
  33. possible: ['always', 'never'],
  34. });
  35. if (!validOptions) {
  36. return;
  37. }
  38. root.walkComments((comment) => {
  39. if (comment.raws.inline || comment.inline) {
  40. return;
  41. }
  42. const rawComment = comment.toString();
  43. const firstFourChars = rawComment.substr(0, 4);
  44. // Return early if sourcemap or copyright comment
  45. if (/^\/\*[#!]\s/.test(firstFourChars)) {
  46. return;
  47. }
  48. const leftMatches = rawComment.match(/(^\/\*+)(\s)?/);
  49. const rightMatches = rawComment.match(/(\s)?(\*+\/)$/);
  50. const opener = leftMatches[1];
  51. const leftSpace = leftMatches[2] || '';
  52. const rightSpace = rightMatches[1] || '';
  53. const closer = rightMatches[2];
  54. if (expectation === 'never' && leftSpace !== '') {
  55. complain(messages.rejectedOpening, opener.length);
  56. }
  57. if (expectation === 'always' && !isWhitespace(leftSpace)) {
  58. complain(messages.expectedOpening, opener.length);
  59. }
  60. if (expectation === 'never' && rightSpace !== '') {
  61. complain(messages.rejectedClosing, comment.toString().length - closer.length - 1);
  62. }
  63. if (expectation === 'always' && !isWhitespace(rightSpace)) {
  64. complain(messages.expectedClosing, comment.toString().length - closer.length - 1);
  65. }
  66. function complain(message, index) {
  67. if (context.fix) {
  68. if (expectation === 'never') {
  69. comment.raws.left = '';
  70. comment.raws.right = '';
  71. comment.text = comment.text.replace(/^(\*+)(\s+)?/, '$1').replace(/(\s+)?(\*+)$/, '$2');
  72. } else {
  73. if (!leftSpace) {
  74. addWhitespaceBefore(comment);
  75. }
  76. if (!rightSpace) {
  77. addWhitespaceAfter(comment);
  78. }
  79. }
  80. return;
  81. }
  82. report({
  83. message,
  84. index,
  85. result,
  86. ruleName,
  87. node: comment,
  88. });
  89. }
  90. });
  91. };
  92. }
  93. rule.ruleName = ruleName;
  94. rule.messages = messages;
  95. module.exports = rule;