How to use the regexp-tree.traverse function in regexp-tree

To help you get started, we’ve selected a few regexp-tree examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github sverweij / dependency-cruiser / src / utl / safe-regex.js View on Github external
function isProbablySafe(pRegExAST, pOptions) {
  const lRepLimit = pOptions.limit || DEFAULT_REPETITION_LIMIT;
  let currentStarHeight = 0;
  let maxObservedStarHeight = 0;

  let repetitionCount = 0;

  regexpTree.traverse(pRegExAST, {
    Repetition: {
      pre() {
        repetitionCount += 1;

        currentStarHeight += 1;
        if (maxObservedStarHeight < currentStarHeight) {
          maxObservedStarHeight = currentStarHeight;
        }
      },

      post() {
        currentStarHeight -= 1;
      }
    }
  });