index.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. 'use strict';
  2. const isContextFunctionalPseudoClass = require('../../utils/isContextFunctionalPseudoClass');
  3. const isNonNegativeInteger = require('../../utils/isNonNegativeInteger');
  4. const isStandardSyntaxRule = require('../../utils/isStandardSyntaxRule');
  5. const keywordSets = require('../../reference/keywordSets');
  6. const parseSelector = require('../../utils/parseSelector');
  7. const report = require('../../utils/report');
  8. const resolvedNestedSelector = require('postcss-resolve-nested-selector');
  9. const ruleMessages = require('../../utils/ruleMessages');
  10. const validateOptions = require('../../utils/validateOptions');
  11. const ruleName = 'selector-max-pseudo-class';
  12. const messages = ruleMessages(ruleName, {
  13. expected: (selector, max) =>
  14. `Expected "${selector}" to have no more than ${max} pseudo-${max === 1 ? 'class' : 'classes'}`,
  15. });
  16. const meta = {
  17. url: 'https://stylelint.io/user-guide/rules/list/selector-max-pseudo-class',
  18. };
  19. /** @type {import('stylelint').Rule} */
  20. const rule = (primary) => {
  21. return (root, result) => {
  22. const validOptions = validateOptions(result, ruleName, {
  23. actual: primary,
  24. possible: isNonNegativeInteger,
  25. });
  26. if (!validOptions) {
  27. return;
  28. }
  29. /**
  30. * @param {import('postcss-selector-parser').Container<unknown>} selectorNode
  31. * @param {import('postcss').Rule} ruleNode
  32. */
  33. function checkSelector(selectorNode, ruleNode) {
  34. const count = selectorNode.reduce((total, childNode) => {
  35. // Only traverse inside actual selectors and context functional pseudo-classes
  36. if (childNode.type === 'selector' || isContextFunctionalPseudoClass(childNode)) {
  37. checkSelector(childNode, ruleNode);
  38. }
  39. // Exclude pseudo elements from the count
  40. if (
  41. childNode.type === 'pseudo' &&
  42. (childNode.value.includes('::') ||
  43. keywordSets.levelOneAndTwoPseudoElements.has(childNode.value.toLowerCase().slice(1)))
  44. ) {
  45. return total;
  46. }
  47. if (childNode.type === 'pseudo') {
  48. total += 1;
  49. }
  50. return total;
  51. }, 0);
  52. if (count > primary) {
  53. const selector = selectorNode.toString();
  54. report({
  55. ruleName,
  56. result,
  57. node: ruleNode,
  58. message: messages.expected(selector, primary),
  59. word: selector,
  60. });
  61. }
  62. }
  63. root.walkRules((ruleNode) => {
  64. if (!isStandardSyntaxRule(ruleNode)) {
  65. return;
  66. }
  67. for (const selector of ruleNode.selectors) {
  68. for (const resolvedSelector of resolvedNestedSelector(selector, ruleNode)) {
  69. parseSelector(resolvedSelector, result, ruleNode, (selectorTree) => {
  70. checkSelector(selectorTree, ruleNode);
  71. });
  72. }
  73. }
  74. });
  75. };
  76. };
  77. rule.ruleName = ruleName;
  78. rule.messages = messages;
  79. rule.meta = meta;
  80. module.exports = rule;