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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // @ts-nocheck
  2. 'use strict';
  3. const _ = require('lodash');
  4. const declarationValueIndex = require('../../utils/declarationValueIndex');
  5. const isStandardSyntaxFunction = require('../../utils/isStandardSyntaxFunction');
  6. const isStandardSyntaxValue = require('../../utils/isStandardSyntaxValue');
  7. const keywordSets = require('../../reference/keywordSets');
  8. const namedColorDataHex = require('../../reference/namedColorData');
  9. const optionsMatches = require('../../utils/optionsMatches');
  10. const propertySets = require('../../reference/propertySets');
  11. const report = require('../../utils/report');
  12. const ruleMessages = require('../../utils/ruleMessages');
  13. const validateOptions = require('../../utils/validateOptions');
  14. const valueParser = require('postcss-value-parser');
  15. const generateColorFuncs = require('./generateColorFuncs');
  16. const ruleName = 'color-named';
  17. const messages = ruleMessages(ruleName, {
  18. expected: (named, original) => `Expected "${original}" to be "${named}"`,
  19. rejected: (named) => `Unexpected named color "${named}"`,
  20. });
  21. // Todo tested on case insensitivity
  22. const NODE_TYPES = new Set(['word', 'function']);
  23. function rule(expectation, options) {
  24. return (root, result) => {
  25. const validOptions = validateOptions(
  26. result,
  27. ruleName,
  28. {
  29. actual: expectation,
  30. possible: ['never', 'always-where-possible'],
  31. },
  32. {
  33. actual: options,
  34. possible: {
  35. ignoreProperties: [_.isString, _.isRegExp],
  36. ignore: ['inside-function'],
  37. },
  38. optional: true,
  39. },
  40. );
  41. if (!validOptions) {
  42. return;
  43. }
  44. const namedColors = Object.keys(namedColorDataHex);
  45. const namedColorData = {};
  46. namedColors.forEach((name) => {
  47. const hex = namedColorDataHex[name];
  48. namedColorData[name] = {
  49. hex,
  50. func: generateColorFuncs(hex[0]),
  51. };
  52. });
  53. root.walkDecls((decl) => {
  54. if (propertySets.acceptCustomIdents.has(decl.prop)) {
  55. return;
  56. }
  57. // Return early if the property is to be ignored
  58. if (optionsMatches(options, 'ignoreProperties', decl.prop)) {
  59. return;
  60. }
  61. valueParser(decl.value).walk((node) => {
  62. const value = node.value;
  63. const type = node.type;
  64. const sourceIndex = node.sourceIndex;
  65. if (optionsMatches(options, 'ignore', 'inside-function') && type === 'function') {
  66. return false;
  67. }
  68. if (!isStandardSyntaxFunction(node)) {
  69. return false;
  70. }
  71. if (!isStandardSyntaxValue(value)) {
  72. return;
  73. }
  74. // Return early if neither a word nor a function
  75. if (!NODE_TYPES.has(type)) {
  76. return;
  77. }
  78. // Check for named colors for "never" option
  79. if (
  80. expectation === 'never' &&
  81. type === 'word' &&
  82. namedColors.includes(value.toLowerCase())
  83. ) {
  84. complain(messages.rejected(value), decl, declarationValueIndex(decl) + sourceIndex);
  85. return;
  86. }
  87. // Check "always-where-possible" option ...
  88. if (expectation !== 'always-where-possible') {
  89. return;
  90. }
  91. // First by checking for alternative color function representations ...
  92. if (type === 'function' && keywordSets.colorFunctionNames.has(value.toLowerCase())) {
  93. // Remove all spaces to match what's in `representations`
  94. const normalizedFunctionString = valueParser.stringify(node).replace(/\s+/g, '');
  95. let namedColor;
  96. for (let i = 0, l = namedColors.length; i < l; i++) {
  97. namedColor = namedColors[i];
  98. if (namedColorData[namedColor].func.includes(normalizedFunctionString.toLowerCase())) {
  99. complain(
  100. messages.expected(namedColor, normalizedFunctionString),
  101. decl,
  102. declarationValueIndex(decl) + sourceIndex,
  103. );
  104. return; // Exit as soon as a problem is found
  105. }
  106. }
  107. return;
  108. }
  109. // Then by checking for alternative hex representations
  110. let namedColor;
  111. for (let i = 0, l = namedColors.length; i < l; i++) {
  112. namedColor = namedColors[i];
  113. if (namedColorData[namedColor].hex.includes(value.toLowerCase())) {
  114. complain(
  115. messages.expected(namedColor, value),
  116. decl,
  117. declarationValueIndex(decl) + sourceIndex,
  118. );
  119. return; // Exit as soon as a problem is found
  120. }
  121. }
  122. });
  123. });
  124. function complain(message, node, index) {
  125. report({
  126. result,
  127. ruleName,
  128. message,
  129. node,
  130. index,
  131. });
  132. }
  133. };
  134. }
  135. rule.ruleName = ruleName;
  136. rule.messages = messages;
  137. module.exports = rule;