{"size": 1277, "ext": "js", "max_stars_repo_path": "node_modules/postcss-bem-linter/lib/get-selectors.js", "max_stars_repo_name": "fillipegb/toksaude-prototipo", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "node_modules/postcss-bem-linter/lib/get-selectors.js", "max_issues_repo_name": "fillipegb/toksaude-prototipo", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "node_modules/postcss-bem-linter/lib/get-selectors.js", "max_forks_repo_name": "fillipegb/toksaude-prototipo", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 24.0943396226, "max_line_length": 74, "alphanum_fraction": 0.7008613939}
var resolveNestedSelector = require('postcss-resolve-nested-selector');

function isTopLevel(node) {
  return node.type === 'root';
}

function isNestedRule(node) {
  return /(?:at)?rule/.test(node.parent.type)
}

function hasNoDeclarations(node) {
  return node.nodes.length && node.every(function(child) {
    return child.type !== 'decl';
  });
}

function getComponentRootRule(node) {
  while (!isTopLevel(node.parent)) {
    node = node.parent;
  }
  return node;
}

function unWrapSelectors(parent, rule) {
  var selectors = [];
  parent.walkRules(function(node) {
    // Only unwrap as far as the current rule being linted
    if (node.selector !== rule.selector) {return;}
    node.selectors.forEach(function(selector) {
      selectors = selectors.concat(resolveNestedSelector(selector, node));
    });
  });
  return selectors;
}

function getSelectors(rule) {
  // Skip validation on rules with no declarations
  // as these don't exist after rules have been unwrapped
  if (hasNoDeclarations(rule)) {
    return [];
  }

  if (isNestedRule(rule)) {
    var componentRootRule = getComponentRootRule(rule);
    var nestedSelectors = unWrapSelectors(componentRootRule, rule);
    return nestedSelectors;
  }

  return rule.selectors;
}

module.exports = getSelectors;
