src/optimizer/transforms/combine-repeating-patterns-transform.js
function combineRepetitionWithPrevious(alternative, child, index) {
  const {node} = alternative;

  if (child.node.type === 'Repetition' && child.node.quantifier.greedy) {
    let right = child.getChild();
    let left;

    if (right.node.type === 'Group' && !right.node.capturing) {
      right = right.getChild();
    }

    let rightLength;
    if (right.node.type === 'Alternative') {
      rightLength = right.node.expressions.length;
      left = NodePath.getForNode({
        type: 'Alternative',
        expressions: [...node.expressions.slice(index - rightLength, index)]
      });
    } else {
      rightLength = 1;
      left = alternative.getChild(index - 1);
      if (left.node.type === 'Group' && !left.node.capturing) {
        left = left.getChild();
      }
    }

    if (left.hasEqualSource(right)) {
      for (let j = index - rightLength; j < index; j++) {
        alternative.getChild(index - rightLength).remove();
      }

      increaseQuantifierByOne(child.node.quantifier);

      return index - rightLength;
    }
  }
  return index;
}
function combineRepetitionWithPrevious(alternative, child, index) {
  const {node} = alternative;

  if (child.node.type === 'Repetition' && child.node.quantifier.greedy) {
    let right = child.getChild();
    let left;

    if (right.node.type === 'Group' && !right.node.capturing) {
      right = right.getChild();
    }

    let rightLength;
    if (right.node.type === 'Alternative') {
      rightLength = right.node.expressions.length;
      left = NodePath.getForNode({
        type: 'Alternative',
        expressions: [...node.expressions.slice(index - rightLength, index)]
      });
    } else {
      rightLength = 1;
      left = alternative.getChild(index - 1);
      if (left.node.type === 'Group' && !left.node.capturing) {
        left = left.getChild();
      }
    }

    if (left.hasEqualSource(right)) {
      for (let j = index - rightLength; j < index; j++) {
        alternative.getChild(index - rightLength).remove();
      }

      increaseQuantifierByOne(child.node.quantifier);

      return index - rightLength;
    }
  }
  return index;
}
function combineRepetitionWithPrevious ( alternative , child , index ) { const { node } = alternative ; if ( child . node . type === 'Repetition' && child . node . quantifier . greedy ) { let right = child . getChild ( ) ; let left ; if ( right . node . type === 'Group' && ! right . node . capturing ) { right = right . getChild ( ) ; } let rightLength ; if ( right . node . type === 'Alternative' ) { rightLength = right . node . expressions . length ; left = NodePath . getForNode ( { type : 'Alternative' , expressions : [ ... node . expressions . slice ( index - rightLength , index ) ] } ) ; } else { rightLength = 1 ; left = alternative . getChild ( index - 1 ) ; if ( left . node . type === 'Group' && ! left . node . capturing ) { left = left . getChild ( ) ; } } if ( left . hasEqualSource ( right ) ) { for ( let j = index - rightLength ; j < index ; j ++ ) { alternative . getChild ( index - rightLength ) . remove ( ) ; } increaseQuantifierByOne ( child . node . quantifier ) ; return index - rightLength ; } } return index ; }
abc(?:abc){2} -> (?:abc){3}
abc ( ? : abc ) { 2 } - > ( ? : abc ) { 3 }