<commit_before>import { NODE, NODE_TYPE } from '../types';

export function transform(ast: NODE): NODE {
  if (ast.type === NODE_TYPE.STYLESHEET) {
    return transformStyleSheet(ast);
  }
  if (ast.type === NODE_TYPE.RULE) {
    const stylesheetNode = makeNode(NODE_TYPE.STYLESHEET);

    stylesheetNode.children = transformRule(ast);
    return stylesheetNode;
  }
  throw new Error('Invalid abstract syntax tree.');
}

export function transformStyleSheet(node: NODE): NODE {
  const stylesheetNode = makeNode(NODE_TYPE.STYLESHEET);

  if (node.children.length) {
    node.children.forEach(node => transformRule(node).forEach(subNode => stylesheetNode.children.push(subNode)));
  }
  return stylesheetNode;
}

export function transformRule(node: NODE): NODE[] {
  const rtn: NODE[] = [];
  const ruleNode: NODE = makeNode(NODE_TYPE.RULE);

  ruleNode.declarations = node.declarations;
  node.selectors.forEach((s) => {
    expandSelector(s).forEach(s => ruleNode.selectors.push(s));
  });
  sPush(ruleNode);
  rtn.push(ruleNode);
  node.children.forEach(node => transformRule(node).forEach(subNode => rtn.push(subNode)));
  sPop();
  return rtn;
}

// utils
// ====

export function expandSelector(selector: string): string[] {
  if (sIsEmpty()) {
    return [ selector ];
  } else if (selector.indexOf('&') > -1) {
    return sTop().selectors.map((parentSelector) => {
      return selector.replace(/&/g, parentSelector);
    });
  } else if (selector.indexOf('^') > -1) {
    return sBottom().selectors.map((rootSelector) => {
      return selector.replace(/\^/g, rootSelector);
    });
  } else {
    return sTop().selectors.map((parentSelector) => {
      return `${parentSelector} ${selector}`;
    });
  }
}

export function makeNode(nodeType: NODE_TYPE): NODE {
  const node: NODE = {
    type: nodeType<commit_msg>fix: extend caret lose parent selector<commit_after>import { NODE, NODE_TYPE } from '../types';

export function transform(ast: NODE): NODE {
  if (ast.type === NODE_TYPE.STYLESHEET) {
    return transformStyleSheet(ast);
  }
  if (ast.type === NODE_TYPE.RULE) {
    const stylesheetNode = makeNode(NODE_TYPE.STYLESHEET);

    stylesheetNode.children = transformRule(ast);
    return stylesheetNode;
  }
  throw new Error('Invalid abstract syntax tree.');
}

export function transformStyleSheet(node: NODE): NODE {
  const stylesheetNode = makeNode(NODE_TYPE.STYLESHEET);

  if (node.children.length) {
    node.children.forEach(node => transformRule(node).forEach(subNode => stylesheetNode.children.push(subNode)));
  }
  return stylesheetNode;
}

export function transformRule(node: NODE): NODE[] {
  const rtn: NODE[] = [];
  const ruleNode: NODE = makeNode(NODE_TYPE.RULE);

  ruleNode.declarations = node.declarations;
  node.selectors.forEach((s) => {
    expandSelector(s).forEach(s => ruleNode.selectors.push(s));
  });
  sPush(ruleNode);
  rtn.push(ruleNode);
  node.children.forEach(node => transformRule(node).forEach(subNode => rtn.push(subNode)));
  sPop();
  return rtn;
}

// utils
// ====

export function expandSelector(selector: string): string[] {
  if (sIsEmpty()) {
    return [ selector ];
  } else if (selector.indexOf('&') > -1) {
    return sTop().selectors.map((parentSelector) => {
      return selector.replace(/&/g, parentSelector);
    });
  } else if (selector.indexOf('^') > -1) {
    const rtn: string[] = [];

    sBottom().selectors.map((rootSelector) => {
      sTop().selectors.map((parentSelector) => {
        rtn.push(`${parentSelector} ${selector.replace(/\^/g, rootSelector)}`);
      });
    });
    return rtn;
  } else {
    return sTop().selectors.map((parentSelector) => {
      return `${parentSelector} ${selector}`;
    });
  }
}

export function makeNode(nodeType: NODE_TYPE): NODE {
  const node: NODE = {
    type: nodeType
{'fraction_non_alphanumeric': 0.16289473684210526, 'fraction_numerical': 0.0010526315789473684, 'mean_word_length': 4.001315789473685, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '324020', 'n_tokens_mistral': 1256, 'n_tokens_neox': 1252, 'n_words': 239}