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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // @ts-nocheck
  2. 'use strict';
  3. const atRuleParamIndex = require('../../utils/atRuleParamIndex');
  4. const declarationValueIndex = require('../../utils/declarationValueIndex');
  5. const report = require('../../utils/report');
  6. const ruleMessages = require('../../utils/ruleMessages');
  7. const validateOptions = require('../../utils/validateOptions');
  8. const valueParser = require('postcss-value-parser');
  9. const ruleName = 'number-leading-zero';
  10. const messages = ruleMessages(ruleName, {
  11. expected: 'Expected a leading zero',
  12. rejected: 'Unexpected leading zero',
  13. });
  14. function rule(expectation, secondary, context) {
  15. return (root, result) => {
  16. const validOptions = validateOptions(result, ruleName, {
  17. actual: expectation,
  18. possible: ['always', 'never'],
  19. });
  20. if (!validOptions) {
  21. return;
  22. }
  23. root.walkAtRules((atRule) => {
  24. if (atRule.name.toLowerCase() === 'import') {
  25. return;
  26. }
  27. check(atRule, atRule.params, atRuleParamIndex);
  28. });
  29. root.walkDecls((decl) => check(decl, decl.value, declarationValueIndex));
  30. function check(node, value, getIndex) {
  31. const neverFixPositions = [];
  32. const alwaysFixPositions = [];
  33. // Get out quickly if there are no periods
  34. if (!value.includes('.')) {
  35. return;
  36. }
  37. valueParser(value).walk((valueNode) => {
  38. // Ignore `url` function
  39. if (valueNode.type === 'function' && valueNode.value.toLowerCase() === 'url') {
  40. return false;
  41. }
  42. // Ignore strings, comments, etc
  43. if (valueNode.type !== 'word') {
  44. return;
  45. }
  46. // Check leading zero
  47. if (expectation === 'always') {
  48. const match = /(?:\D|^)(\.\d+)/.exec(valueNode.value);
  49. if (match === null) {
  50. return;
  51. }
  52. // The regexp above consists of 2 capturing groups (or capturing parentheses).
  53. // We need the index of the second group. This makes sanse when we have "-.5" as an input
  54. // for regex. And we need the index of ".5".
  55. const capturingGroupIndex = match[0].length - match[1].length;
  56. const index = valueNode.sourceIndex + match.index + capturingGroupIndex;
  57. if (context.fix) {
  58. alwaysFixPositions.unshift({
  59. index,
  60. });
  61. return;
  62. }
  63. complain(messages.expected, node, getIndex(node) + index);
  64. }
  65. if (expectation === 'never') {
  66. const match = /(?:\D|^)(0+)(\.\d+)/.exec(valueNode.value);
  67. if (match === null) {
  68. return;
  69. }
  70. // The regexp above consists of 3 capturing groups (or capturing parentheses).
  71. // We need the index of the second group. This makes sanse when we have "-00.5"
  72. // as an input for regex. And we need the index of "00".
  73. const capturingGroupIndex = match[0].length - (match[1].length + match[2].length);
  74. const index = valueNode.sourceIndex + match.index + capturingGroupIndex;
  75. if (context.fix) {
  76. neverFixPositions.unshift({
  77. startIndex: index,
  78. // match[1].length is the length of our matched zero(s)
  79. endIndex: index + match[1].length,
  80. });
  81. return;
  82. }
  83. complain(messages.rejected, node, getIndex(node) + index);
  84. }
  85. });
  86. if (alwaysFixPositions.length) {
  87. alwaysFixPositions.forEach((fixPosition) => {
  88. const index = fixPosition.index;
  89. if (node.type === 'atrule') {
  90. node.params = addLeadingZero(node.params, index);
  91. } else {
  92. node.value = addLeadingZero(node.value, index);
  93. }
  94. });
  95. }
  96. if (neverFixPositions.length) {
  97. neverFixPositions.forEach((fixPosition) => {
  98. const startIndex = fixPosition.startIndex;
  99. const endIndex = fixPosition.endIndex;
  100. if (node.type === 'atrule') {
  101. node.params = removeLeadingZeros(node.params, startIndex, endIndex);
  102. } else {
  103. node.value = removeLeadingZeros(node.value, startIndex, endIndex);
  104. }
  105. });
  106. }
  107. }
  108. function complain(message, node, index) {
  109. report({
  110. result,
  111. ruleName,
  112. message,
  113. node,
  114. index,
  115. });
  116. }
  117. };
  118. }
  119. function addLeadingZero(input, index) {
  120. // eslint-disable-next-line prefer-template
  121. return input.slice(0, index) + '0' + input.slice(index);
  122. }
  123. function removeLeadingZeros(input, startIndex, endIndex) {
  124. return input.slice(0, startIndex) + input.slice(endIndex);
  125. }
  126. rule.ruleName = ruleName;
  127. rule.messages = messages;
  128. module.exports = rule;