lib/hacks/grid-utils.js
function inheritGridGap (decl, gap) {
  let rule = decl.parent
  let mediaRule = getParentMedia(rule)
  let root = rule.root()

  // get an array of selector split in 3-deep array
  let splitSelectorArr = splitSelector(rule.selector)

  // abort if the rule already has gaps
  if (Object.keys(gap).length > 0) {
    return false
  }

  // e.g ['min-width']
  let [prop] = parseMediaParams(mediaRule.params)

  let lastBySpace = splitSelectorArr[0]

  // get escaped value from the selector
  // if we have '.grid-2.foo.bar' selector, will be '\.grid\-2'
  let escaped = escapeRegexp(lastBySpace[lastBySpace.length - 1][0])

  let regexp = new RegExp(`(${ escaped }$)|(${ escaped }[,.])`)

  // find the closest rule with the same selector
  let closestRuleGap
  root.walkRules(regexp, r => {
    let gridGap

    // abort if are checking the same rule
    if (rule.toString() === r.toString()) {
      return false
    }

    // find grid-gap values
    r.walkDecls('grid-gap', d => (gridGap = getGridGap(d)))

    // skip rule without gaps
    if (!gridGap || Object.keys(gridGap).length === 0) {
      return true
    }

    // skip rules that should not be inherited from
    if (!shouldInheritGap(rule.selector, r.selector)) {
      return true
    }

    let media = getParentMedia(r)
    if (media) {
      // if we are inside media, we need to check that media props match
      // e.g ('min-width' === 'min-width')
      let propToCompare = parseMediaParams(media.params)[0]
      if (propToCompare === prop) {
        closestRuleGap = gridGap
        return true
      }
    } else {
      closestRuleGap = gridGap
      return true
    }

    return undefined
  })

  // if we find the closest gap object
  if (closestRuleGap && Object.keys(closestRuleGap).length > 0) {
    return closestRuleGap
  }
  return false
}
function inheritGridGap (decl, gap) {
  let rule = decl.parent
  let mediaRule = getParentMedia(rule)
  let root = rule.root()

  // get an array of selector split in 3-deep array
  let splitSelectorArr = splitSelector(rule.selector)

  // abort if the rule already has gaps
  if (Object.keys(gap).length > 0) {
    return false
  }

  // e.g ['min-width']
  let [prop] = parseMediaParams(mediaRule.params)

  let lastBySpace = splitSelectorArr[0]

  // get escaped value from the selector
  // if we have '.grid-2.foo.bar' selector, will be '\.grid\-2'
  let escaped = escapeRegexp(lastBySpace[lastBySpace.length - 1][0])

  let regexp = new RegExp(`(${ escaped }$)|(${ escaped }[,.])`)

  // find the closest rule with the same selector
  let closestRuleGap
  root.walkRules(regexp, r => {
    let gridGap

    // abort if are checking the same rule
    if (rule.toString() === r.toString()) {
      return false
    }

    // find grid-gap values
    r.walkDecls('grid-gap', d => (gridGap = getGridGap(d)))

    // skip rule without gaps
    if (!gridGap || Object.keys(gridGap).length === 0) {
      return true
    }

    // skip rules that should not be inherited from
    if (!shouldInheritGap(rule.selector, r.selector)) {
      return true
    }

    let media = getParentMedia(r)
    if (media) {
      // if we are inside media, we need to check that media props match
      // e.g ('min-width' === 'min-width')
      let propToCompare = parseMediaParams(media.params)[0]
      if (propToCompare === prop) {
        closestRuleGap = gridGap
        return true
      }
    } else {
      closestRuleGap = gridGap
      return true
    }

    return undefined
  })

  // if we find the closest gap object
  if (closestRuleGap && Object.keys(closestRuleGap).length > 0) {
    return closestRuleGap
  }
  return false
}
function inheritGridGap ( decl , gap ) { let rule = decl . parent let mediaRule = getParentMedia ( rule ) let root = rule . root ( ) // get an array of selector split in 3-deep array let splitSelectorArr = splitSelector ( rule . selector ) // abort if the rule already has gaps if ( Object . keys ( gap ) . length > 0 ) { return false } // e.g ['min-width'] let [ prop ] = parseMediaParams ( mediaRule . params ) let lastBySpace = splitSelectorArr [ 0 ] // get escaped value from the selector // if we have '.grid-2.foo.bar' selector, will be '\.grid\-2' let escaped = escapeRegexp ( lastBySpace [ lastBySpace . length - 1 ] [ 0 ] ) let regexp = new RegExp ( ` ${ escaped } ${ escaped } ` ) // find the closest rule with the same selector let closestRuleGap root . walkRules ( regexp , r => { let gridGap // abort if are checking the same rule if ( rule . toString ( ) === r . toString ( ) ) { return false } // find grid-gap values r . walkDecls ( 'grid-gap' , d => ( gridGap = getGridGap ( d ) ) ) // skip rule without gaps if ( ! gridGap || Object . keys ( gridGap ) . length === 0 ) { return true } // skip rules that should not be inherited from if ( ! shouldInheritGap ( rule . selector , r . selector ) ) { return true } let media = getParentMedia ( r ) if ( media ) { // if we are inside media, we need to check that media props match // e.g ('min-width' === 'min-width') let propToCompare = parseMediaParams ( media . params ) [ 0 ] if ( propToCompare === prop ) { closestRuleGap = gridGap return true } } else { closestRuleGap = gridGap return true } return undefined } ) // if we find the closest gap object if ( closestRuleGap && Object . keys ( closestRuleGap ) . length > 0 ) { return closestRuleGap } return false }
inherit grid gap values from the closest rule above
with the same selector
@param  {Declaration} decl
@param  {Object} gap gap values
@return {Object | Boolean} return gap values or false (if not found)
inherit grid gap values from the closest rule above with the same selector