<commit_before>package org.wololo.java2estree

import scala.collection.JavaConversions._
import scala.collection.mutable.Buffer
import org.wololo.estree._
import compilationunit._
import expression._
import org.eclipse.jdt.core.dom

object statement {
  def fromVariableDeclarationFragment(vd: dom.VariableDeclarationFragment)(implicit td: dom.TypeDeclaration) = 
    new VariableDeclarator(new Identifier(vd.getName.getIdentifier), toExpression(vd.getInitializer))
  
  def fromSwitchCases(x: Buffer[dom.Statement], accu: List[SwitchCase] = List())(implicit td: dom.TypeDeclaration): List[SwitchCase] = {
    val switchCase = x.head.asInstanceOf[dom.SwitchCase]
    val statements = x.tail.takeWhile { !_.isInstanceOf[dom.SwitchCase] } map { x => fromStatement(x) }
    val test = if (switchCase.getExpression == null) null else toExpression(switchCase.getExpression)
    val switchCases = accu :+ new SwitchCase(test, statements)
    if (x.length - 1 - statements.length>0)
      fromSwitchCases(x.drop(statements.length + 1), switchCases)
    else 
      switchCases
  }

  def fromCatchClauses(clauses: Iterable[dom.CatchClause], name: Identifier)(implicit td: dom.TypeDeclaration): Statement = {
    if (clauses.size > 0) {
      val test = toInstanceOf(name, clauses.head.getException.getType.resolveBinding().getName)
      val consequent = fromBlock(clauses.head.getBody)
      new IfStatement(test, consequent, fromCatchClauses(clauses.tail, name))
    } else {
      new ThrowStatement(name)
    }
  }
  
  def fromFragments(fragments: java.util.List[_])(implicit td: dom.TypeDeclaration) = 
    fragments collect { case x: dom.VariableDeclarationFragment => fromVariableDeclarationFragment(x) }
  
  def fromForStatement(x: dom.ForStatement)(implicit td: dom.TypeDeclaration) = {
    val init = if (x.initializers.size == 1 && x.initializers.get(0).isInstanceOf[dom.VariableDeclarationExpression]) {
      val vde = x.initializers.get(0).asInstanceOf[dom.VariableDeclarationExpression]
      new VariableDeclaration(fromFragments(vde.fragments))
    } 
    else
      new SequenceExpression(toExpressions(x.initializers))
    val update = new SequenceExpression(toExpressions(x.updaters))
    new ForStatement(init, toExpression(x.getExpression), update, fromStatement(x.getBody))
  }
  
  def fromStatement(s: dom.Statement)(implicit td: dom.TypeDeclaration): Statement = s match {
    case x: dom.EmptyStatement =>
      new EmptyStatement()  
    case x: dom.ReturnStatement =>
      new ReturnStatement(toExpression(x.getExpression))
    case x: dom.IfStatement =>
      new IfStatement(toExpression(x.getExpression),          fromStatement(x.getThenStatement),          fromStatement(x.getElseStatement))
    case x: dom.SwitchStatement =>
      val cases = fromSwitchCases(x.statements collect { case x: dom.Statement => x })
      new SwitchStatement(toExpression(x.getExpression), cases)
    case x: dom.ContinueStatement =>
      new ContinueStatement()
    case x: dom.BreakStatement =>
      new BreakStatement()
    case x: dom.ForStatement =>
      fromForStatement(x)
    case x: dom.EnhancedForStatement =>
      val left = toExpression(x.getParameter.getInitializer)
      val right = toExpression(x.getExpression) 
      val body = fromStatement(x.getBody)
      new ForInStatement(left, right, body)
    case x: dom.WhileStatement =>
      new WhileStatement(toExpression(x.getExpression), fromStatement(x.getBody))<commit_msg>Fix issues with empty for statement init and update expressions<commit_after>package org.wololo.java2estree

import scala.collection.JavaConversions._
import scala.collection.mutable.Buffer
import org.wololo.estree._
import compilationunit._
import expression._
import org.eclipse.jdt.core.dom

object statement {
  def fromVariableDeclarationFragment(vd: dom.VariableDeclarationFragment)(implicit td: dom.TypeDeclaration) = 
    new VariableDeclarator(new Identifier(vd.getName.getIdentifier), toExpression(vd.getInitializer))
  
  def fromSwitchCases(x: Buffer[dom.Statement], accu: List[SwitchCase] = List())(implicit td: dom.TypeDeclaration): List[SwitchCase] = {
    val switchCase = x.head.asInstanceOf[dom.SwitchCase]
    val statements = x.tail.takeWhile { !_.isInstanceOf[dom.SwitchCase] } map { x => fromStatement(x) }
    val test = if (switchCase.getExpression == null) null else toExpression(switchCase.getExpression)
    val switchCases = accu :+ new SwitchCase(test, statements)
    if (x.length - 1 - statements.length>0)
      fromSwitchCases(x.drop(statements.length + 1), switchCases)
    else 
      switchCases
  }

  def fromCatchClauses(clauses: Iterable[dom.CatchClause], name: Identifier)(implicit td: dom.TypeDeclaration): Statement = {
    if (clauses.size > 0) {
      val test = toInstanceOf(name, clauses.head.getException.getType.resolveBinding().getName)
      val consequent = fromBlock(clauses.head.getBody)
      new IfStatement(test, consequent, fromCatchClauses(clauses.tail, name))
    } else {
      new ThrowStatement(name)
    }
  }
  
  def fromFragments(fragments: java.util.List[_])(implicit td: dom.TypeDeclaration) = 
    fragments collect { case x: dom.VariableDeclarationFragment => fromVariableDeclarationFragment(x) }
  
  def fromForStatement(x: dom.ForStatement)(implicit td: dom.TypeDeclaration) = {
    val init = if (x.initializers.size == 0) null 
    else if (x.initializers.size == 1 && x.initializers.get(0).isInstanceOf[dom.VariableDeclarationExpression]) {
      val vde = x.initializers.get(0).asInstanceOf[dom.VariableDeclarationExpression]
      new VariableDeclaration(fromFragments(vde.fragments))
    } 
    else
      new SequenceExpression(toExpressions(x.initializers))
    val update: Expression = if (x.updaters.size() == 0) null
    else if (x.updaters.size() == 1) toExpressions(x.updaters).head
    else new SequenceExpression(toExpressions(x.updaters))
    new ForStatement(init, toExpression(x.getExpression), update, fromStatement(x.getBody))
  }
  
  def fromStatement(s: dom.Statement)(implicit td: dom.TypeDeclaration): Statement = s match {
    case x: dom.EmptyStatement =>
      new EmptyStatement()  
    case x: dom.ReturnStatement =>
      new ReturnStatement(toExpression(x.getExpression))
    case x: dom.IfStatement =>
      val consequent = fromStatement(x.getThenStatement)
      val alternate = fromStatement(x.getElseStatement)
      new IfStatement(toExpression(x.getExpression), consequent, alternate)
    case x: dom.SwitchStatement =>
      val cases = fromSwitchCases(x.statements collect { case x: dom.Statement => x })
      new SwitchStatement(toExpression(x.getExpression), cases)
    case x: dom.ContinueStatement =>
      new ContinueStatement()
    case x: dom.BreakStatement =>
      new BreakStatement()
    case x: dom.ForStatement =>
      fromForStatement(x)
    case x: dom.EnhancedForStatement =>
      val left = toExpression(x.getParameter.getInitializer)
      val right = toExpression(x.getExpression) 
      val body = fromStatement(x.getBody)
      new ForInStatement(left, right, body)
    case x: dom.WhileStatement =>
      new WhileStatement(toExpression(x.getExpression), fromStatement(x.getBody))
{'fraction_non_alphanumeric': 0.10603316148808695, 'fraction_numerical': 0.0026473456876132087, 'mean_word_length': 4.285714285714286, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 31, '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': '2137444', 'n_tokens_mistral': 2129, 'n_tokens_neox': 2086, 'n_words': 444}