index.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. 'use strict';
  2. const declarationValueIndex = require('../../utils/declarationValueIndex');
  3. const findFontFamily = require('../../utils/findFontFamily');
  4. const isStandardSyntaxValue = require('../../utils/isStandardSyntaxValue');
  5. const isVariable = require('../../utils/isVariable');
  6. const keywordSets = require('../../reference/keywordSets');
  7. const optionsMatches = require('../../utils/optionsMatches');
  8. const postcss = require('postcss');
  9. const report = require('../../utils/report');
  10. const ruleMessages = require('../../utils/ruleMessages');
  11. const validateOptions = require('../../utils/validateOptions');
  12. const { isAtRule } = require('../../utils/typeGuards');
  13. const { isRegExp, isString } = require('../../utils/validateTypes');
  14. const ruleName = 'font-family-no-missing-generic-family-keyword';
  15. const messages = ruleMessages(ruleName, {
  16. rejected: 'Unexpected missing generic font family',
  17. });
  18. const meta = {
  19. url: 'https://stylelint.io/user-guide/rules/list/font-family-no-missing-generic-family-keyword',
  20. };
  21. /**
  22. * @param {import('postcss-value-parser').Node} node
  23. * @returns {boolean}
  24. */
  25. const isFamilyNameKeyword = (node) =>
  26. !('quote' in node) && keywordSets.fontFamilyKeywords.has(node.value.toLowerCase());
  27. /**
  28. * @param {string} value
  29. * @returns {boolean}
  30. */
  31. const isLastFontFamilyVariable = (value) => {
  32. const lastValue = postcss.list.comma(value).pop();
  33. return lastValue != null && (isVariable(lastValue) || !isStandardSyntaxValue(lastValue));
  34. };
  35. /** @type {import('stylelint').Rule} */
  36. const rule = (primary, secondaryOptions) => {
  37. return (root, result) => {
  38. const validOptions = validateOptions(
  39. result,
  40. ruleName,
  41. { actual: primary },
  42. {
  43. actual: secondaryOptions,
  44. possible: {
  45. ignoreFontFamilies: [isString, isRegExp],
  46. },
  47. optional: true,
  48. },
  49. );
  50. if (!validOptions) {
  51. return;
  52. }
  53. root.walkDecls(/^font(-family)?$/i, (decl) => {
  54. // Ignore @font-face
  55. const parent = decl.parent;
  56. if (parent && isAtRule(parent) && parent.name.toLowerCase() === 'font-face') {
  57. return;
  58. }
  59. if (decl.prop === 'font' && keywordSets.systemFontValues.has(decl.value.toLowerCase())) {
  60. return;
  61. }
  62. if (isLastFontFamilyVariable(decl.value)) {
  63. return;
  64. }
  65. const fontFamilies = findFontFamily(decl.value);
  66. if (fontFamilies.length === 0) {
  67. return;
  68. }
  69. if (fontFamilies.some((node) => isFamilyNameKeyword(node))) {
  70. return;
  71. }
  72. if (
  73. fontFamilies.some((node) =>
  74. optionsMatches(secondaryOptions, 'ignoreFontFamilies', node.value),
  75. )
  76. ) {
  77. return;
  78. }
  79. report({
  80. result,
  81. ruleName,
  82. message: messages.rejected,
  83. node: decl,
  84. index: declarationValueIndex(decl) + fontFamilies[fontFamilies.length - 1].sourceIndex,
  85. });
  86. });
  87. };
  88. };
  89. rule.ruleName = ruleName;
  90. rule.messages = messages;
  91. rule.meta = meta;
  92. module.exports = rule;