index.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. const stylelint = require('stylelint');
  2. const { getContainingNode, isRuleWithNodes } = require('../../utils');
  3. const checkNode = require('./checkNode');
  4. const createOrderInfo = require('./createOrderInfo');
  5. const validatePrimaryOption = require('./validatePrimaryOption');
  6. const ruleName = require('./ruleName');
  7. const messages = require('./messages');
  8. function rule(primaryOption, options = {}, context = {}) {
  9. return function ruleBody(root, result) {
  10. let validOptions = stylelint.utils.validateOptions(
  11. result,
  12. ruleName,
  13. {
  14. actual: primaryOption,
  15. possible: validatePrimaryOption,
  16. },
  17. {
  18. actual: options,
  19. possible: {
  20. unspecified: ['top', 'bottom', 'ignore'],
  21. },
  22. optional: true,
  23. }
  24. );
  25. if (!validOptions) {
  26. return;
  27. }
  28. let unspecified = options.unspecified || 'ignore';
  29. let orderInfo = createOrderInfo(primaryOption);
  30. let processedParents = [];
  31. // Check all rules and at-rules recursively
  32. root.walk(function processRulesAndAtrules(originalNode) {
  33. let node = getContainingNode(originalNode);
  34. // Avoid warnings duplication, caused by interfering in `root.walk()` algorigthm with `getContainingNode()`
  35. if (processedParents.includes(node)) {
  36. return;
  37. }
  38. processedParents.push(node);
  39. if (isRuleWithNodes(node)) {
  40. checkNode({
  41. node,
  42. isFixEnabled: context.fix,
  43. orderInfo,
  44. primaryOption,
  45. result,
  46. unspecified,
  47. });
  48. }
  49. });
  50. };
  51. }
  52. rule.ruleName = ruleName;
  53. rule.messages = messages;
  54. rule.primaryOptionArray = true;
  55. module.exports = rule;