1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- 'use strict';
- const beforeBlockString = require('../../utils/beforeBlockString');
- const blockString = require('../../utils/blockString');
- const isSingleLineString = require('../../utils/isSingleLineString');
- const report = require('../../utils/report');
- const ruleMessages = require('../../utils/ruleMessages');
- const validateOptions = require('../../utils/validateOptions');
- const { isNumber } = require('../../utils/validateTypes');
- const ruleName = 'declaration-block-single-line-max-declarations';
- const messages = ruleMessages(ruleName, {
- expected: (max) => `Expected no more than ${max} ${max === 1 ? 'declaration' : 'declarations'}`,
- });
- const meta = {
- url: 'https://stylelint.io/user-guide/rules/list/declaration-block-single-line-max-declarations',
- };
- /** @type {import('stylelint').Rule} */
- const rule = (primary) => {
- return (root, result) => {
- const validOptions = validateOptions(result, ruleName, {
- actual: primary,
- possible: [isNumber],
- });
- if (!validOptions) {
- return;
- }
- root.walkRules((ruleNode) => {
- if (!isSingleLineString(blockString(ruleNode))) {
- return;
- }
- if (!ruleNode.nodes) {
- return;
- }
- const decls = ruleNode.nodes.filter((node) => node.type === 'decl');
- if (decls.length <= primary) {
- return;
- }
- report({
- message: messages.expected(primary),
- node: ruleNode,
- index: beforeBlockString(ruleNode, { noRawBefore: true }).length,
- result,
- ruleName,
- });
- });
- };
- };
- rule.ruleName = ruleName;
- rule.messages = messages;
- rule.meta = meta;
- module.exports = rule;
|