<commit_before>package org.eclairjs.tools.generate

import java.util.Properties
import java.io.FileInputStream
import org.eclairjs.tools.generate.model._

abstract class GenerateJSBase {


  lazy val templates = {
    try {
      val prop = new Properties()
      prop.load(new FileInputStream("./src/main/resources/template.properties"))
      prop
    } catch { case e: Exception =>
      e.printStackTrace()
      sys.exit(1)
    }

  }


  def generate(file:File) : String = {
    val sb=new StringBuilder

    sb ++= getTemplate("copyright")

    generateIncludes(file:File, sb:StringBuilder)

    file.classes.filter(!_.isStatic) foreach( cls => generateClass(cls,sb))

    val statics=file.classes.filter(_.isStatic)
    val non_emptyStatics=statics.filter(cls=> cls.members.filter(!_.isConstructor()).length>0)
    if (!non_emptyStatics.isEmpty)
      {

        sb.append("//\n// static methods\n//\n")
        non_emptyStatics foreach( cls => {
          cls.members.filter(!_.isConstructor()) foreach( member =>
            member match {
              case method:Method => generateMethod(method,sb)
            }
            )

        })

      }
    sb.toString()
  }

  def generateIncludes(file:File, sb:StringBuilder): Unit =
  {

  }

  def isForNode()=false

   def generateConstructor(cls:Clazz, sb:StringBuilder): Unit
  def generateObject(cls:Clazz, sb:StringBuilder): Unit
  def generatePostlude(cls:Clazz, sb:StringBuilder): Unit

   def generateClass(cls:Clazz,sb:StringBuilder)  = {
      addNewlines(2,sb)
     generateClassDoc(cls:Clazz,sb:StringBuilder)
     addNewlines(2,sb)
     generateConstructorDoc(cls:Clazz,sb:StringBuilder)
     generateConstructor(cls,sb)
     addNewlines(1,sb)
    generateObject(cls,sb)

     cls.members.filter(!_.isConstructor()) foreach( member =>
        member match {
          case method:Method => generateMethod(method,sb)
        }
       )

     generatePostlude(cls,sb)

  }

  def generateClassDoc(cls:Clazz, sb:StringBuilder): Unit =
  {
    if (cls.comment.length>0)
    {
      sb ++= convertToJSDoc(cls.comment,cls)
    }
  }


  def mainConstructor(cls:Clazz):Method = {
    val constructors=cls.constructors()
    if (constructors.length==0)
      return null;
    if (constructors.length==1)
      return constructors(0)

    constructors.sortWith(_.parms.length < _.parms.length).last
  }

  def generateConstructorDoc(cls:Clazz, sb:StringBuilder): Unit =
  {
      val constr = mainConstructor(cls)
      if (constr!=null)
        {
          generateMethodDoc(constr,sb)
        }

  }

  def generateMethodDoc(method:Method, sb:StringBuilder): Unit =
  {
    if (method.comment.length>0)
    {
      sb ++= convertToJSDoc(method.comment,method)
    }
  }

  def generateMethod(method:Method,sb:StringBuilder)  = {
    addNewlines(2,sb)
    generateMethodDoc(method,sb)
    addNewlines(1,sb)

    // if method has multiple parm lists, generate unique name
    val methodName=method.getDistinctName()

    sb.append(method.parent.name)
    if (method.parent.isStatic)
      sb.append(".")
    else
      sb.append(".prototype.")
    sb.append(methodName).append(" = function(")
      .append(method.parmList()).append(") {\n")

    sb ++= getTemplate("defaultBody")

    val body =getMethodBody(method)
    if (body.length>0)
      sb ++= body.split("\n").map("// "+_).mkString("\n")  // body commented out

    sb.append("\n}\n")
<commit_msg>generate JSDoc for functions withou it<commit_after>package org.eclairjs.tools.generate

import java.util.Properties
import java.io.FileInputStream
import org.eclairjs.tools.generate.model._

abstract class GenerateJSBase {


  lazy val templates = {
    try {
      val prop = new Properties()
      prop.load(new FileInputStream("./src/main/resources/template.properties"))
      prop
    } catch { case e: Exception =>
      e.printStackTrace()
      sys.exit(1)
    }

  }


  def generate(file:File) : String = {
    val sb=new StringBuilder

    sb ++= getTemplate("copyright")

    generateIncludes(file:File, sb:StringBuilder)

    file.classes.filter(!_.isStatic) foreach( cls => generateClass(cls,sb))

    val statics=file.classes.filter(_.isStatic)
    val non_emptyStatics=statics.filter(cls=> cls.members.filter(!_.isConstructor()).length>0)
    if (!non_emptyStatics.isEmpty)
      {

        sb.append("//\n// static methods\n//\n")
        non_emptyStatics foreach( cls => {
          cls.members.filter(!_.isConstructor()) foreach( member =>
            member match {
              case method:Method => generateMethod(method,sb)
            }
            )

        })

      }
    sb.toString()
  }

  def generateIncludes(file:File, sb:StringBuilder): Unit =
  {

  }

  def isForNode()=false

   def generateConstructor(cls:Clazz, sb:StringBuilder): Unit
  def generateObject(cls:Clazz, sb:StringBuilder): Unit
  def generatePostlude(cls:Clazz, sb:StringBuilder): Unit

   def generateClass(cls:Clazz,sb:StringBuilder)  = {
      addNewlines(2,sb)
     generateClassDoc(cls:Clazz,sb:StringBuilder)
     addNewlines(2,sb)
     generateConstructorDoc(cls:Clazz,sb:StringBuilder)
     generateConstructor(cls,sb)
     addNewlines(1,sb)
    generateObject(cls,sb)

     cls.members.filter(!_.isConstructor()) foreach( member =>
        member match {
          case method:Method => generateMethod(method,sb)
        }
       )

     generatePostlude(cls,sb)

  }

  def generateClassDoc(cls:Clazz, sb:StringBuilder): Unit =
  {
    if (cls.comment.length>0)
    {
      sb ++= convertToJSDoc(cls.comment,cls)
    }
  }


  def mainConstructor(cls:Clazz):Method = {
    val constructors=cls.constructors()
    if (constructors.length==0)
      return null;
    if (constructors.length==1)
      return constructors(0)

    constructors.sortWith(_.parms.length < _.parms.length).last
  }

  def generateConstructorDoc(cls:Clazz, sb:StringBuilder): Unit =
  {
      val constr = mainConstructor(cls)
      if (constr!=null)
        {
          generateMethodDoc(constr,sb)
        }

  }

  def generateMethodDoc(method:Method, sb:StringBuilder): Unit =
  {
    val comment = if (method.comment.length>0) method.comment
          else """ /** \n */"""
    sb ++= convertToJSDoc(comment,method)
  }

  def generateMethod(method:Method,sb:StringBuilder)  = {
    addNewlines(2,sb)
    generateMethodDoc(method,sb)
    addNewlines(1,sb)

    // if method has multiple parm lists, generate unique name
    val methodName=method.getDistinctName()

    sb.append(method.parent.name)
    if (method.parent.isStatic)
      sb.append(".")
    else
      sb.append(".prototype.")
    sb.append(methodName).append(" = function(")
      .append(method.parmList()).append(") {\n")

    sb ++= getTemplate("defaultBody")

    val body =getMethodBody(method)
    if (body.length>0)
      sb ++= body.split("\n").map("// "+_).mkString("\n")  // body commented out

    sb.append("\n}\n")

{'fraction_non_alphanumeric': 0.13293794300593084, 'fraction_numerical': 0.0037610299435845507, 'mean_word_length': 2.9873125720876588, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 27, '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': '7663592', 'n_tokens_mistral': 2222, 'n_tokens_neox': 2157, 'n_words': 370}