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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // @ts-nocheck
  2. 'use strict';
  3. const report = require('../../utils/report');
  4. const ruleMessages = require('../../utils/ruleMessages');
  5. const validateOptions = require('../../utils/validateOptions');
  6. const ruleName = 'no-irregular-whitespace';
  7. const messages = ruleMessages(ruleName, {
  8. unexpected: 'Unexpected irregular whitespace',
  9. });
  10. const IRREGULAR_WHITESPACES = [
  11. '\u000B', // Line Tabulation (\v) - <VT>
  12. '\u000C', // Form Feed (\f) - <FF>
  13. '\u00A0', // No-Break Space - <NBSP>
  14. '\u0085', // Next Line
  15. '\u1680', // Ogham Space Mark
  16. '\u180E', // Mongolian Vowel Separator - <MVS>
  17. '\uFEFF', // Zero Width No-Break Space - <BOM>
  18. '\u2000', // En Quad
  19. '\u2001', // Em Quad
  20. '\u2002', // En Space - <ENSP>
  21. '\u2003', // Em Space - <EMSP>
  22. '\u2004', // Tree-Per-Em
  23. '\u2005', // Four-Per-Em
  24. '\u2006', // Six-Per-Em
  25. '\u2007', // Figure Space
  26. '\u2008', // Punctuation Space - <PUNCSP>
  27. '\u2009', // Thin Space
  28. '\u200A', // Hair Space
  29. '\u200B', // Zero Width Space - <ZWSP>
  30. '\u2028', // Line Separator
  31. '\u2029', // Paragraph Separator
  32. '\u202F', // Narrow No-Break Space
  33. '\u205F', // Medium Mathematical Space
  34. '\u3000', // Ideographic Space
  35. ];
  36. const IRREGULAR_WHITESPACES_PATTERN = new RegExp(`([${IRREGULAR_WHITESPACES.join('')}])`);
  37. const generateInvalidWhitespaceValidator = () => {
  38. return (str) => typeof str === 'string' && IRREGULAR_WHITESPACES_PATTERN.exec(str);
  39. };
  40. const declarationSchema = {
  41. prop: 'string',
  42. value: 'string',
  43. raws: {
  44. before: 'string',
  45. between: 'string',
  46. },
  47. };
  48. const atRuleSchema = {
  49. name: 'string',
  50. params: 'string',
  51. raws: {
  52. before: 'string',
  53. between: 'string',
  54. afterName: 'string',
  55. after: 'string',
  56. },
  57. };
  58. const ruleSchema = {
  59. selector: 'string',
  60. raws: {
  61. before: 'string',
  62. between: 'string',
  63. after: 'string',
  64. },
  65. };
  66. const generateNodeValidator = (nodeSchema, validator) => {
  67. const allKeys = Object.keys(nodeSchema);
  68. const validatorForKey = {};
  69. allKeys.forEach((key) => {
  70. if (typeof nodeSchema[key] === 'string') validatorForKey[key] = validator;
  71. if (typeof nodeSchema[key] === 'object')
  72. validatorForKey[key] = generateNodeValidator(nodeSchema[key], validator);
  73. });
  74. // This will be called many times, so it's optimized for performance and not readibility.
  75. // Surprisingly, this seem to be slightly faster then concatenating the params and running the validator once.
  76. return (node) => {
  77. for (const currentKey of allKeys) {
  78. if (validatorForKey[currentKey](node[currentKey])) {
  79. return validatorForKey[currentKey](node[currentKey]);
  80. }
  81. }
  82. };
  83. };
  84. function rule(on) {
  85. return (root, result) => {
  86. const validOptions = validateOptions(result, ruleName, { actual: on });
  87. if (!validOptions) {
  88. return;
  89. }
  90. const genericValidator = generateInvalidWhitespaceValidator();
  91. const validate = (node, validator) => {
  92. const issue = validator(node);
  93. if (issue) {
  94. report({
  95. ruleName,
  96. result,
  97. message: messages.unexpected,
  98. node,
  99. word: issue[1],
  100. });
  101. }
  102. };
  103. const atRuleValidator = generateNodeValidator(atRuleSchema, genericValidator);
  104. const ruleValidator = generateNodeValidator(ruleSchema, genericValidator);
  105. const declValidator = generateNodeValidator(declarationSchema, genericValidator);
  106. root.walkAtRules((atRule) => validate(atRule, atRuleValidator));
  107. root.walkRules((selector) => validate(selector, ruleValidator));
  108. root.walkDecls((declaration) => validate(declaration, declValidator));
  109. };
  110. }
  111. rule.ruleName = ruleName;
  112. rule.messages = messages;
  113. module.exports = rule;