index.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. 'use strict';
  2. const blockString = require('../../utils/blockString');
  3. const isSingleLineString = require('../../utils/isSingleLineString');
  4. const report = require('../../utils/report');
  5. const ruleMessages = require('../../utils/ruleMessages');
  6. const validateOptions = require('../../utils/validateOptions');
  7. const { isNumber } = require('../../utils/validateTypes');
  8. const ruleName = 'declaration-block-single-line-max-declarations';
  9. const messages = ruleMessages(ruleName, {
  10. expected: (max) => `Expected no more than ${max} ${max === 1 ? 'declaration' : 'declarations'}`,
  11. });
  12. const meta = {
  13. url: 'https://stylelint.io/user-guide/rules/declaration-block-single-line-max-declarations',
  14. };
  15. /** @type {import('stylelint').Rule} */
  16. const rule = (primary) => {
  17. return (root, result) => {
  18. const validOptions = validateOptions(result, ruleName, {
  19. actual: primary,
  20. possible: [isNumber],
  21. });
  22. if (!validOptions) {
  23. return;
  24. }
  25. root.walkRules((ruleNode) => {
  26. const block = blockString(ruleNode);
  27. if (!isSingleLineString(block)) {
  28. return;
  29. }
  30. if (!ruleNode.nodes) {
  31. return;
  32. }
  33. const decls = ruleNode.nodes.filter((node) => node.type === 'decl');
  34. if (decls.length <= primary) {
  35. return;
  36. }
  37. report({
  38. message: messages.expected(primary),
  39. node: ruleNode,
  40. word: block,
  41. result,
  42. ruleName,
  43. });
  44. });
  45. };
  46. };
  47. rule.ruleName = ruleName;
  48. rule.messages = messages;
  49. rule.meta = meta;
  50. module.exports = rule;