123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- const stylelint = require('stylelint');
- const _ = require('lodash');
- const { getContainingNode, isRuleWithNodes } = require('../../utils');
- const checkNodeForOrder = require('./checkNodeForOrder');
- const checkNodeForEmptyLines = require('./checkNodeForEmptyLines');
- const createOrderInfo = require('./createOrderInfo');
- const validatePrimaryOption = require('./validatePrimaryOption');
- const ruleName = require('./ruleName');
- const messages = require('./messages');
- function rule(primaryOption, options = {}, context = {}) {
- return function ruleBody(root, result) {
- let validOptions = stylelint.utils.validateOptions(
- result,
- ruleName,
- {
- actual: primaryOption,
- possible: validatePrimaryOption,
- },
- {
- actual: options,
- possible: {
- unspecified: ['top', 'bottom', 'ignore', 'bottomAlphabetical'],
- emptyLineBeforeUnspecified: ['always', 'never', 'threshold'],
- disableFix: _.isBoolean,
- emptyLineMinimumPropertyThreshold: _.isNumber,
- },
- optional: true,
- }
- );
- if (!validOptions) {
- return;
- }
- let isFixEnabled = context.fix && !options.disableFix;
- let expectedOrder = createOrderInfo(primaryOption);
- let processedParents = [];
- // Check all rules and at-rules recursively
- root.walk(function processRulesAndAtrules(input) {
- let node = getContainingNode(input);
- // Avoid warnings duplication, caused by interfering in `root.walk()` algorigthm with `getContainingNode()`
- if (processedParents.includes(node)) {
- return;
- }
- processedParents.push(node);
- if (isRuleWithNodes(node)) {
- checkNodeForOrder({
- node,
- originalNode: input,
- isFixEnabled,
- primaryOption,
- unspecified: options.unspecified || 'ignore',
- result,
- expectedOrder,
- });
- checkNodeForEmptyLines({
- node,
- context,
- emptyLineBeforeUnspecified: options.emptyLineBeforeUnspecified,
- emptyLineMinimumPropertyThreshold:
- options.emptyLineMinimumPropertyThreshold || 0,
- expectedOrder,
- isFixEnabled,
- primaryOption,
- result,
- });
- }
- });
- };
- }
- rule.primaryOptionArray = true;
- rule.ruleName = ruleName;
- rule.messages = messages;
- module.exports = rule;
|