index.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. 'use strict';
  2. const beforeBlockString = require('../../utils/beforeBlockString');
  3. const blockString = require('../../utils/blockString');
  4. const isSingleLineString = require('../../utils/isSingleLineString');
  5. const report = require('../../utils/report');
  6. const ruleMessages = require('../../utils/ruleMessages');
  7. const validateOptions = require('../../utils/validateOptions');
  8. const { isNumber } = require('../../utils/validateTypes');
  9. const ruleName = 'declaration-block-single-line-max-declarations';
  10. const messages = ruleMessages(ruleName, {
  11. expected: (max) => `Expected no more than ${max} ${max === 1 ? 'declaration' : 'declarations'}`,
  12. });
  13. const meta = {
  14. url: 'https://stylelint.io/user-guide/rules/list/declaration-block-single-line-max-declarations',
  15. };
  16. /** @type {import('stylelint').Rule} */
  17. const rule = (primary) => {
  18. return (root, result) => {
  19. const validOptions = validateOptions(result, ruleName, {
  20. actual: primary,
  21. possible: [isNumber],
  22. });
  23. if (!validOptions) {
  24. return;
  25. }
  26. root.walkRules((ruleNode) => {
  27. if (!isSingleLineString(blockString(ruleNode))) {
  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. index: beforeBlockString(ruleNode, { noRawBefore: true }).length,
  41. result,
  42. ruleName,
  43. });
  44. });
  45. };
  46. };
  47. rule.ruleName = ruleName;
  48. rule.messages = messages;
  49. rule.meta = meta;
  50. module.exports = rule;