<commit_before>package org.hyperscala.javascript.dsl2

import org.powerscala.reflect._
import org.hyperscala.javascript.JavaScriptContent
import scala.collection.immutable.ListMap


/**
 * @author <NAME> <<EMAIL>>
 */
abstract class JavaScriptContext extends Statement[JavaScriptContent] {
  implicit val jsContext = this

  private lazy val fields = getClass.fields.filterNot(f => f.name.contains("$") || f.name == "jsContext")
  private lazy val variables = ListMap(fields.map(f => f[Any](this) -> f): _*)
  private var statements = List.empty[Statement[_]]

  def variable(v: Any): Option[String] = {
    variables.get(v).map(f => f.name)
  }

  protected def hasReturn = false

  protected def before(b: StringBuilder, depth: Int) = {}
  protected def write(b: StringBuilder, depth: Int): Unit = {
    // Write variables out first
    variables.foreach {
      case (value, field) => if (!value.isInstanceOf[Statement[_]]) {
        val line = s"var ${field.name} = ${JavaScriptContent.toJS(value)}"
        writeLine(line, b, depth)
      }
    }

    // Write non-variables statements next
    val stmts = statements.reverse
    val last = if (stmts.nonEmpty) stmts.last else null
    stmts.foreach {
      case s => variables.get(s) match {
        case Some(field) => {
          val output = s match {     // Output statement as field
            case v: Variable[_] => {
              v.name = field.name
              v.content
            }
            case c: JavaScriptContext => c.toJS(depth + 1)
            case _ => s.content
          }
          val line = s"var ${field.name} = $output"
          writeLine(line, b, depth)
        }
        case None => {
          val isLast = s eq last
          if ((isLast && hasReturn) || s.sideEffects) {
            val line = if (isLast && hasReturn) {
              s"return ${s.toJS}"
            } else {<commit_msg>Finished preliminary implementation of dsl2.<commit_after>package org.hyperscala.javascript.dsl2

import org.powerscala.reflect._
import org.hyperscala.javascript.JavaScriptContent
import scala.collection.immutable.ListMap


/**
 * @author <NAME> <<EMAIL>>
 */
abstract class JavaScriptContext extends Statement[JavaScriptContent] {
  implicit val jsContext = this

  private lazy val fields = getClass.fields.filterNot(f => f.name.contains("$") || f.name == "jsContext")
  private lazy val variables = ListMap(fields.map(f => f[Any](this) -> f): _*)
  private var statements = List.empty[Statement[_]]

  def variable(v: Any): Option[String] = {
    variables.get(v).map(f => f.name)
  }

  protected def hasReturn = false

  protected def before(b: StringBuilder, depth: Int) = {}
  protected def write(b: StringBuilder, depth: Int): Unit = {
    // Write variables out first
    variables.foreach {
      case (value, field) => value match {
        case c: JavaScriptContext => {
          val line = s"var ${field.name} = ${c.toJS(depth + 1)}"
          writeLine(line, b, depth)
        }
        case s: Statement[_] => // Ignore statements
        case _ => {
          val line = s"var ${field.name} = ${JavaScriptContent.toJS(value)}"
          writeLine(line, b, depth)
        }
      }
    }

    // Write non-variables statements next
    val stmts = statements.reverse
    val last = if (stmts.nonEmpty) stmts.last else null
    stmts.foreach {
      case s => variables.get(s) match {
        case Some(field) => {
          val output = s match {     // Output statement as field
            case v: Variable[_] => {
              v.name = field.name
              v.content
            }
            case c: JavaScriptContext => c.toJS(depth + 1)
            case _ => s.content
          }
          val line = s"var ${field.name} = $output"
          writeLine(line, b, depth)
        }
        case None => {
          val isLast = s eq last
          if ((isLast && hasReturn) || s.sideEffects) {
            val line = if (isLast && hasReturn) {
              s"return ${s.toJS}"
            } else {
{'fraction_non_alphanumeric': 0.12634207240948814, 'fraction_numerical': 0.00149812734082397, 'mean_word_length': 2.2863002461033632, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 34, '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': '5399227', 'n_tokens_mistral': 1197, 'n_tokens_neox': 1171, 'n_words': 320}