index.cjs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // NOTICE: This file is generated by Rollup. To modify it,
  2. // please instead edit the ESM counterpart and rebuild with Rollup (npm run build).
  3. 'use strict';
  4. const selectorParser = require('postcss-selector-parser');
  5. const flattenNestedSelectorsForRule = require('../../utils/flattenNestedSelectorsForRule.cjs');
  6. const getStrippedSelectorSource = require('../../utils/getStrippedSelectorSource.cjs');
  7. const isContextFunctionalPseudoClass = require('../../utils/isContextFunctionalPseudoClass.cjs');
  8. const isNonNegativeInteger = require('../../utils/isNonNegativeInteger.cjs');
  9. const isStandardSyntaxRule = require('../../utils/isStandardSyntaxRule.cjs');
  10. const report = require('../../utils/report.cjs');
  11. const ruleMessages = require('../../utils/ruleMessages.cjs');
  12. const validateOptions = require('../../utils/validateOptions.cjs');
  13. const { isRoot, isSelector } = selectorParser;
  14. const ruleName = 'selector-max-class';
  15. const messages = ruleMessages(ruleName, {
  16. expected: (selector, max) =>
  17. `Expected "${selector}" to have no more than ${max} ${max === 1 ? 'class' : 'classes'}`,
  18. });
  19. const meta = {
  20. url: 'https://stylelint.io/user-guide/rules/selector-max-class',
  21. };
  22. /** @type {import('stylelint').CoreRules[ruleName]} */
  23. const rule = (primary) => {
  24. return (root, result) => {
  25. const validOptions = validateOptions(result, ruleName, {
  26. actual: primary,
  27. possible: isNonNegativeInteger,
  28. });
  29. if (!validOptions) {
  30. return;
  31. }
  32. /**
  33. * @param {import('postcss-selector-parser').Selector} resolvedSelectorNode
  34. * @param {import('postcss-selector-parser').Selector} selectorNode
  35. * @param {import('postcss').Rule} ruleNode
  36. */
  37. function checkSelector(resolvedSelectorNode, selectorNode, ruleNode) {
  38. const count = resolvedSelectorNode.reduce((total, childNode) => {
  39. if (childNode.type === 'class') total += 1;
  40. return total;
  41. }, 0);
  42. if (count > primary) {
  43. const { index, endIndex, selector: selectorStr } = getStrippedSelectorSource(selectorNode);
  44. report({
  45. ruleName,
  46. result,
  47. node: ruleNode,
  48. message: messages.expected,
  49. messageArgs: [selectorStr, primary],
  50. index,
  51. endIndex,
  52. });
  53. }
  54. }
  55. root.walkRules((ruleNode) => {
  56. if (!isStandardSyntaxRule(ruleNode)) return;
  57. flattenNestedSelectorsForRule(ruleNode, result).forEach(({ selector, resolvedSelectors }) => {
  58. resolvedSelectors.walk((childSelector) => {
  59. if (!isSelector(childSelector)) return;
  60. if (
  61. isRoot(childSelector.parent) ||
  62. isContextFunctionalPseudoClass(childSelector.parent)
  63. ) {
  64. checkSelector(childSelector, selector, ruleNode);
  65. }
  66. });
  67. });
  68. });
  69. };
  70. };
  71. rule.ruleName = ruleName;
  72. rule.messages = messages;
  73. rule.meta = meta;
  74. module.exports = rule;