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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // @ts-nocheck
  2. 'use strict';
  3. const _ = require('lodash');
  4. const atRuleParamIndex = require('../../utils/atRuleParamIndex');
  5. const declarationValueIndex = require('../../utils/declarationValueIndex');
  6. const getDeclarationValue = require('../../utils/getDeclarationValue');
  7. const isWhitespace = require('../../utils/isWhitespace');
  8. const report = require('../../utils/report');
  9. const ruleMessages = require('../../utils/ruleMessages');
  10. const setDeclarationValue = require('../../utils/setDeclarationValue');
  11. const styleSearch = require('style-search');
  12. const validateOptions = require('../../utils/validateOptions');
  13. const ruleName = 'function-whitespace-after';
  14. const messages = ruleMessages(ruleName, {
  15. expected: 'Expected whitespace after ")"',
  16. rejected: 'Unexpected whitespace after ")"',
  17. });
  18. const ACCEPTABLE_AFTER_CLOSING_PAREN = new Set([')', ',', '}', ':', '/', undefined]);
  19. function rule(expectation, options, context) {
  20. return (root, result) => {
  21. const validOptions = validateOptions(result, ruleName, {
  22. actual: expectation,
  23. possible: ['always', 'never'],
  24. });
  25. if (!validOptions) {
  26. return;
  27. }
  28. function check(node, value, getIndex, fix) {
  29. styleSearch(
  30. {
  31. source: value,
  32. target: ')',
  33. functionArguments: 'only',
  34. },
  35. (match) => {
  36. checkClosingParen(value, match.startIndex + 1, node, getIndex, fix);
  37. },
  38. );
  39. }
  40. function checkClosingParen(source, index, node, getIndex, fix) {
  41. const nextChar = source[index];
  42. if (expectation === 'always') {
  43. // Allow for the next character to be a single empty space,
  44. // another closing parenthesis, a comma, or the end of the value
  45. if (nextChar === ' ') {
  46. return;
  47. }
  48. if (nextChar === '\n') {
  49. return;
  50. }
  51. if (source.substr(index, 2) === '\r\n') {
  52. return;
  53. }
  54. if (ACCEPTABLE_AFTER_CLOSING_PAREN.has(nextChar)) {
  55. return;
  56. }
  57. if (fix) {
  58. fix(index);
  59. return;
  60. }
  61. report({
  62. message: messages.expected,
  63. node,
  64. index: getIndex(node) + index,
  65. result,
  66. ruleName,
  67. });
  68. } else if (expectation === 'never') {
  69. if (isWhitespace(nextChar)) {
  70. if (fix) {
  71. fix(index);
  72. return;
  73. }
  74. report({
  75. message: messages.rejected,
  76. node,
  77. index: getIndex(node) + index,
  78. result,
  79. ruleName,
  80. });
  81. }
  82. }
  83. }
  84. function createFixer(value) {
  85. let fixed = '';
  86. let lastIndex = 0;
  87. let applyFix;
  88. if (expectation === 'always') {
  89. applyFix = (index) => {
  90. // eslint-disable-next-line prefer-template
  91. fixed += value.slice(lastIndex, index) + ' ';
  92. lastIndex = index;
  93. };
  94. } else if (expectation === 'never') {
  95. applyFix = (index) => {
  96. let whitespaceEndIndex = index + 1;
  97. while (whitespaceEndIndex < value.length && isWhitespace(value[whitespaceEndIndex])) {
  98. whitespaceEndIndex++;
  99. }
  100. fixed += value.slice(lastIndex, index);
  101. lastIndex = whitespaceEndIndex;
  102. };
  103. }
  104. return {
  105. applyFix,
  106. get hasFixed() {
  107. return Boolean(lastIndex);
  108. },
  109. get fixed() {
  110. return fixed + value.slice(lastIndex);
  111. },
  112. };
  113. }
  114. root.walkAtRules(/^import$/i, (atRule) => {
  115. const param = _.get(atRule, 'raws.params.raw', atRule.params);
  116. const fixer = context.fix && createFixer(param);
  117. check(atRule, param, atRuleParamIndex, fixer && fixer.applyFix);
  118. if (fixer && fixer.hasFixed) {
  119. if (atRule.raws.params) {
  120. atRule.raws.params.raw = fixer.fixed;
  121. } else {
  122. atRule.params = fixer.fixed;
  123. }
  124. }
  125. });
  126. root.walkDecls((decl) => {
  127. const value = getDeclarationValue(decl);
  128. const fixer = context.fix && createFixer(value);
  129. check(decl, value, declarationValueIndex, fixer && fixer.applyFix);
  130. if (fixer && fixer.hasFixed) {
  131. setDeclarationValue(decl, fixer.fixed);
  132. }
  133. });
  134. };
  135. }
  136. rule.ruleName = ruleName;
  137. rule.messages = messages;
  138. module.exports = rule;