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

  let i = 0;
  while (i < index) {
    let previousChild = alternative.getChild(i);

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

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

      if (i + 1 === index) {
        right = child;
        if (right.node.type === 'Group' && !right.node.capturing) {
          right = right.getChild();
        }
      } else {
        right = NodePath.getForNode({
          type: 'Alternative',
          expressions: [...node.expressions.slice(i + 1, index + 1)]
        });
      }

      if (left.hasEqualSource(right)) {

        for (let j = i; j < index; j++) {
          alternative.getChild(i + 1).remove();
        }

        increaseQuantifierByOne(previousChild.node.quantifier);

        return i;
      }
    }

    i++;
  }
  return index;
}
function combineWithPreviousRepetition(alternative, child, index) {
  const {node} = alternative;

  let i = 0;
  while (i < index) {
    let previousChild = alternative.getChild(i);

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

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

      if (i + 1 === index) {
        right = child;
        if (right.node.type === 'Group' && !right.node.capturing) {
          right = right.getChild();
        }
      } else {
        right = NodePath.getForNode({
          type: 'Alternative',
          expressions: [...node.expressions.slice(i + 1, index + 1)]
        });
      }

      if (left.hasEqualSource(right)) {

        for (let j = i; j < index; j++) {
          alternative.getChild(i + 1).remove();
        }

        increaseQuantifierByOne(previousChild.node.quantifier);

        return i;
      }
    }

    i++;
  }
  return index;
}
(?:abc){2}abc -> (?:abc){3}