<commit_before>var traverse = require("../traverse");
var util     = require("../util");
var b        = require("acorn-ast-types").builders;
var _        = require("lodash");

var buildVariableAssign = function (kind, id, init) {
  if (kind === false) {
    return b.expressionStatement(b.assignmentExpression("=", id, init));
  } else {
    return b.variableDeclaration(kind, [
      b.variableDeclarator(id, init)
    ]);
  }
};

var push = function (kind, nodes, pattern, parentId) {
  if (pattern.type === "ObjectPattern") {
    pushObjectPattern(kind, nodes, pattern, parentId);
  } else if (pattern.type === "ArrayPattern") {
    pushArrayPattern(kind, nodes, pattern, parentId);
  }
};

var pushObjectPattern = function (kind, nodes, pattern, parentId) {
  _.each(pattern.properties, function (prop) {
    var pattern2   = prop.value;
    var patternId2 = b.memberExpression(parentId, prop.key, false);

    if (traverse.isPattern(pattern2)) {
      push(kind, nodes, pattern2, patternId2);
    } else {
      nodes.push(buildVariableAssign(kind, pattern2, patternId2));
    }
  });
};

var pushArrayPattern = function (kind, nodes, pattern, parentId) {
  _.each(pattern.elements, function (elem, i) {
    if (!elem) return;

    var newPatternId = b.memberExpression(parentId, b.literal(i), true);

    if (elem.type === "Identifier") {
      nodes.push(buildVariableAssign(kind, elem, newPatternId));
    } else {
      push(kind, nodes, elem, newPatternId);
    }
  });
};

var pushPattern = function (kind, nodes, pattern, parentId, file) {
  if (parentId.type !== "MemberExpression" && parentId.type !== "Identifier") {
    var key = b.identifier(file.generateUid("ref"));

    nodes.push(b.variableDeclaration("var", [
      b.variableDeclarator(key, parentId)
    ]));

    parentId = key;
  }

  push(kind, nodes, pattern, parentId);
};

exports.ForInStatement =
exports.ForOfStatement = function (node, parent, file) {
  var declar = node.left;
  if (declar.type !== "VariableDeclaration") return;

  var pattern = declar.declarations[0].id;
  if (!traverse.isPattern(pattern)) return;<commit_msg>support MemberExpressions in destructuring - fixes #100<commit_after>var traverse = require("../traverse");
var util     = require("../util");
var b        = require("acorn-ast-types").builders;
var _        = require("lodash");

var buildVariableAssign = function (kind, id, init) {
  if (kind === false) {
    return b.expressionStatement(b.assignmentExpression("=", id, init));
  } else {
    return b.variableDeclaration(kind, [
      b.variableDeclarator(id, init)
    ]);
  }
};

var push = function (kind, nodes, pattern, parentId) {
  if (pattern.type === "ObjectPattern") {
    pushObjectPattern(kind, nodes, pattern, parentId);
  } else if (pattern.type === "ArrayPattern") {
    pushArrayPattern(kind, nodes, pattern, parentId);
  }
};

var pushObjectPattern = function (kind, nodes, pattern, parentId) {
  _.each(pattern.properties, function (prop) {
    var pattern2   = prop.value;
    var patternId2 = b.memberExpression(parentId, prop.key, false);

    if (traverse.isPattern(pattern2)) {
      push(kind, nodes, pattern2, patternId2);
    } else {
      nodes.push(buildVariableAssign(kind, pattern2, patternId2));
    }
  });
};

var pushArrayPattern = function (kind, nodes, pattern, parentId) {
  _.each(pattern.elements, function (elem, i) {
    if (!elem) return;

    var newPatternId = b.memberExpression(parentId, b.literal(i), true);

    if (elem.type === "Identifier") {
      nodes.push(buildVariableAssign(kind, elem, newPatternId));
    } else if (elem.type === "MemberExpression") {
      nodes.push(buildVariableAssign(false, elem, newPatternId));
    } else {
      push(kind, nodes, elem, newPatternId);
    }
  });
};

var pushPattern = function (kind, nodes, pattern, parentId, file) {
  if (parentId.type !== "MemberExpression" && parentId.type !== "Identifier") {
    var key = b.identifier(file.generateUid("ref"));

    nodes.push(b.variableDeclaration("var", [
      b.variableDeclarator(key, parentId)
    ]));

    parentId = key;
  }

  push(kind, nodes, pattern, parentId);
};

exports.ForInStatement =
exports.ForOfStatement = function (node, parent, file) {
  var declar = node.left;
  if (declar.type !== "VariableDeclaration") return;

  var pattern = declar.declarations[0].id;
  if (!traverse.isPattern(pattern)) return;
{'fraction_non_alphanumeric': 0.1532497149372862, 'fraction_numerical': 0.004332953249714938, 'mean_word_length': 3.851769911504425, 'pattern_counts': {'":': 0, '<': 3, '<?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': '5201712', 'n_tokens_mistral': 1379, 'n_tokens_neox': 1358, 'n_words': 317}