findAtRuleContext.js 523 B

1234567891011121314151617181920212223242526272829
  1. 'use strict';
  2. const { isAtRule, isRule } = require('./typeGuards');
  3. /**
  4. * Find the at-rule in which a rule is nested.
  5. *
  6. * Returns `null` if the rule is not nested within an at-rule.
  7. *
  8. * @param {import('postcss').Rule} rule
  9. * @returns {null | import('postcss').AtRule}
  10. */
  11. module.exports = function findAtRuleContext(rule) {
  12. const parent = rule.parent;
  13. if (!parent) {
  14. return null;
  15. }
  16. if (isAtRule(parent)) {
  17. return parent;
  18. }
  19. if (isRule(parent)) {
  20. return findAtRuleContext(parent);
  21. }
  22. return null;
  23. };