findAtRuleContext.mjs 504 B

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