<commit_before>import TraversalContext from "./context";
import * as visitors from "./visitors";
import * as messages from "babel-messages";
import includes from "lodash/collection/includes";
import * as t from "babel-types";

export default function traverse(parent: Object, opts?: Object, scope?: Object, state: Object, parentPath: Object) {
  if (!parent) return;
  if (!opts) opts = {};

  if (!opts.noScope && !scope) {
    if (parent.type !== "Program" && parent.type !== "File") {
      throw new Error(messages.get("traverseNeedsParent", parent.type));
    }
  }

  visitors.explode(opts);

  // array of nodes
  if (Array.isArray(parent)) {
    for (var i = 0; i < parent.length; i++) {
      traverse.node(parent[i], opts, scope, state, parentPath);
    }
  } else {
    traverse.node(parent, opts, scope, state, parentPath);
  }
}

traverse.visitors = visitors;
traverse.verify = visitors.verify;
traverse.explode = visitors.explode;

traverse.NodePath = require("./path");
traverse.Scope    = require("./scope");
traverse.Hub      = require("./hub");

traverse.node = function (node: Object, opts: Object, scope: Object, state: Object, parentPath: Object, skipKeys?) {
  var keys = t.VISITOR_KEYS[node.type];
  if (!keys) return;

  var context = new TraversalContext(scope, opts, state, parentPath);
  for (var key of (keys: Array)) {
    if (skipKeys && skipKeys[key]) continue;
    if (context.visit(node, key)) return;
  }
};

const CLEAR_KEYS = t.COMMENT_KEYS.concat([
  "_scopeInfo", "_paths",
  "tokens", "comments",
  "start", "end", "loc",
  "raw", "rawValue"
]);

traverse.clearNode = function (node: Object) {
  for (var i = 0; i < CLEAR_KEYS.length; i++) {
    let key = CLEAR_KEYS[i];
    if (node[key] != null) node[key] = undefined;
  }
};

var clearVisitor = {
  noScope: true,
  exit: traverse.clearNode
};

traverse.removeProperties = function (tree: Object): Object {
  traverse(tree, clearVisitor);
  traverse.clearNode(tree);

  return tree;
};

function hasBlacklistedType(node: Object, parent: Object, scope: Object, state: Object) {
  if (node.type === state.type) {
    state.has = true;
    this.skip();<commit_msg>add traverse.cheap and upgrade to new traverse API<commit_after>import TraversalContext from "./context";
import * as visitors from "./visitors";
import * as messages from "babel-messages";
import includes from "lodash/collection/includes";
import * as t from "babel-types";

export default function traverse(parent: Object, opts?: Object, scope?: Object, state: Object, parentPath: Object) {
  if (!parent) return;
  if (!opts) opts = {};

  if (!opts.noScope && !scope) {
    if (parent.type !== "Program" && parent.type !== "File") {
      throw new Error(messages.get("traverseNeedsParent", parent.type));
    }
  }

  visitors.explode(opts);

  // array of nodes
  if (Array.isArray(parent)) {
    for (var i = 0; i < parent.length; i++) {
      traverse.node(parent[i], opts, scope, state, parentPath);
    }
  } else {
    traverse.node(parent, opts, scope, state, parentPath);
  }
}

traverse.visitors = visitors;
traverse.verify = visitors.verify;
traverse.explode = visitors.explode;

traverse.NodePath = require("./path");
traverse.Scope    = require("./scope");
traverse.Hub      = require("./hub");

traverse.cheap = function (node, enter) {
  var keys = t.VISITOR_KEYS[node.type];
  if (!keys) return;

  enter(node);

  for (var key of keys) {
    traverse.cheap(node[key], enter);
  }
};

traverse.node = function (node: Object, opts: Object, scope: Object, state: Object, parentPath: Object, skipKeys?) {
  var keys: Array = t.VISITOR_KEYS[node.type];
  if (!keys) return;

  var context = new TraversalContext(scope, opts, state, parentPath);
  for (var key of keys) {
    if (skipKeys && skipKeys[key]) continue;
    if (context.visit(node, key)) return;
  }
};

const CLEAR_KEYS: Array = t.COMMENT_KEYS.concat([
  "_scopeInfo", "_paths",
  "tokens", "comments",
  "start", "end", "loc",
  "raw", "rawValue"
]);

traverse.clearNode = function (node) {
  for (var key of CLEAR_KEYS) {
    if (node[key] != null) node[key] = undefined;
  }
};

traverse.removeProperties = function (tree) {
  traverse.cheap(tree, traverse.clearNode);
  return tree;
};

function hasBlacklistedType(path, state) {
  if (path.node.type === state.type) {
    state.has = true;
    path.skip();
{'fraction_non_alphanumeric': 0.16094865300483538, 'fraction_numerical': 0.000690766751093714, 'mean_word_length': 3.958904109589041, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 3, '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': '2215743', 'n_tokens_mistral': 1466, 'n_tokens_neox': 1434, 'n_words': 388}