index.js 1.7 KB

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