index.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. /**
  20. * @fileoverview Rule to warn about literal colors
  21. * @author Apache
  22. */
  23. const COLOR_KEYWORDS = require('./colors');
  24. function hasHexColor(quasi) {
  25. if (typeof quasi === 'string') {
  26. const regex = /#([a-f0-9]{3}|[a-f0-9]{4}(?:[a-f0-9]{2}){0,2})\b/gi;
  27. return !!quasi.match(regex);
  28. }
  29. return false;
  30. }
  31. function hasRgbColor(quasi) {
  32. if (typeof quasi === 'string') {
  33. const regex = /rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*(\d+(?:\.\d+)?))?\)/i;
  34. return !!quasi.match(regex);
  35. }
  36. return false;
  37. }
  38. function hasLiteralColor(quasi, strict = false) {
  39. if (typeof quasi === 'string') {
  40. // matches literal colors at the start or end of a CSS prop
  41. return COLOR_KEYWORDS.some(color => {
  42. const regexColon = new RegExp(`: ${color}`);
  43. const regexSemicolon = new RegExp(` ${color};`);
  44. return (
  45. !!quasi.match(regexColon) ||
  46. !!quasi.match(regexSemicolon) ||
  47. (strict && quasi === color)
  48. );
  49. });
  50. }
  51. return false;
  52. }
  53. const WARNING_MESSAGE =
  54. 'Theme color variables are preferred over rgb(a)/hex/literal colors';
  55. //------------------------------------------------------------------------------
  56. // Rule Definition
  57. //------------------------------------------------------------------------------
  58. /** @type {import('eslint').Rule.RuleModule} */
  59. module.exports = {
  60. rules: {
  61. 'no-literal-colors': {
  62. create(context) {
  63. const warned = [];
  64. return {
  65. TemplateElement(node) {
  66. const rawValue = node?.value?.raw;
  67. const isChildParentTagged =
  68. node?.parent?.parent?.type === 'TaggedTemplateExpression';
  69. const isChildParentArrow =
  70. node?.parent?.parent?.type === 'ArrowFunctionExpression';
  71. const isParentTemplateLiteral =
  72. node?.parent?.type === 'TemplateLiteral';
  73. const loc = node?.parent?.parent?.loc;
  74. const locId = loc && JSON.stringify(loc);
  75. const hasWarned = warned.includes(locId);
  76. if (
  77. !hasWarned &&
  78. (isChildParentTagged ||
  79. (isChildParentArrow && isParentTemplateLiteral)) &&
  80. rawValue &&
  81. (hasLiteralColor(rawValue) ||
  82. hasHexColor(rawValue) ||
  83. hasRgbColor(rawValue))
  84. ) {
  85. context.report(node, loc, WARNING_MESSAGE);
  86. warned.push(locId);
  87. }
  88. },
  89. Literal(node) {
  90. const value = node?.value;
  91. const isParentProperty = node?.parent?.type === 'Property';
  92. const locId = JSON.stringify(node.loc);
  93. const hasWarned = warned.includes(locId);
  94. if (
  95. !hasWarned &&
  96. isParentProperty &&
  97. value &&
  98. (hasLiteralColor(value, true) ||
  99. hasHexColor(value) ||
  100. hasRgbColor(value))
  101. ) {
  102. context.report(node, node.loc, WARNING_MESSAGE);
  103. warned.push(locId);
  104. }
  105. },
  106. };
  107. },
  108. },
  109. },
  110. };