<commit_before>/*
 * Scala.js (https://www.scala-js.org/)
 *
 * Copyright EPFL.
 *
 * Licensed under Apache License 2.0
 * (https://www.apache.org/licenses/LICENSE-2.0).
 *
 * See the NOTICE file distributed with this work for
 * additional information regarding copyright ownership.
 */

package org.scalajs.jsenv.nodejs

import java.io.{Console => _, _}

import org.scalajs.core.tools.io._
import org.scalajs.core.tools.jsdep.ResolvedJSDependency
import org.scalajs.jsenv._

import org.scalajs.core.ir.Utils.escapeJS

class JSDOMNodeJSEnv private[jsenv] (
    executable: String,
    args: Seq[String],
    env: Map[String, String],
    internal: Unit
) extends AbstractNodeJSEnv(executable, args, env, sourceMap = false) {

  @deprecated("Use org.scalajs.jsenv.jsdomnodejs.JSDOMNodeJSEnv.", "0.6.18")
  def this(
      executable: String = "node",
      args: Seq[String] = Seq.empty,
      env: Map[String, String] = Map.empty
  ) = {
    this(executable, args, env, internal = ())
  }

  protected def vmName: String = "Node.js with JSDOM"

  override def jsRunner(libs: Seq[ResolvedJSDependency],
      code: VirtualJSFile): JSRunner = {
    new DOMNodeRunner(libs, code)
  }

  override def asyncRunner(libs: Seq[ResolvedJSDependency],
      code: VirtualJSFile): AsyncJSRunner = {
    new AsyncDOMNodeRunner(libs, code)
  }

  override def comRunner(libs: Seq[ResolvedJSDependency],
      code: VirtualJSFile): ComJSRunner = {
    new ComDOMNodeRunner(libs, code)
  }

  protected class DOMNodeRunner(libs: Seq[ResolvedJSDependency], code: VirtualJSFile)
      extends ExtRunner(libs, code) with AbstractDOMNodeRunner

  protected class AsyncDOMNodeRunner(libs: Seq[ResolvedJSDependency], code: VirtualJSFile)
      extends AsyncExtRunner(libs, code) with AbstractDOMNodeRunner

  protected class ComDOMNodeRunner(libs: Seq[ResolvedJSDependency], code: VirtualJSFile)
      extends AsyncDOMNodeRunner(libs, code) with NodeComJSRunner

  protected trait AbstractDOMNodeRunner extends AbstractNodeRunner {

    protected def codeWithJSDOMContext(): Seq[VirtualJSFile] = {
      val scriptsFiles = (getLibJSFiles() :+ code).map {
        case file: FileVirtualFile => file.file
        case file                  => libCache.materialize(file)
      }
      val scriptsURIsAsJSStrings = scriptsFiles.map { file =>
        "\"" + escapeJS(file.toURI.toASCIIString) + "\""
      }
      val scriptsURIsJSArray = scriptsURIsAsJSStrings.mkString("[", ", ", "]")

      val jsDOMCode = {
        s"""
           |(function () {
           |  var jsdom = require("jsdom");
           |
           |  if (typeof jsdom.JSDOM === "function") {
           |    // jsdom >= 10.0.0
           |    var virtualConsole = new jsdom.VirtualConsole()
           |      .sendTo(console, { omitJSDOMErrors: true });
           |    virtualConsole.on("jsdomError", function (error) {
           |      try {
           |        // Display as much info about the error as possible
           |        if (error.detail && error.detail.stack) {
           |          console.error("" + error.detail);
           |          console.error(error.detail.stack);
           |        } else {
           |          console.error(error);
           |        }
           |      } finally {
           |        // Whatever happens, kill the process so that the run fails
           |        process.exit(1);
           |      }
           |    });
           |
           |    var dom = new jsdom.JSDOM("", {<commit_msg>[no-master] Fix #3458: Hack around React's dev mode error triggering hack.<commit_after>/*
 * Scala.js (https://www.scala-js.org/)
 *
 * Copyright EPFL.
 *
 * Licensed under Apache License 2.0
 * (https://www.apache.org/licenses/LICENSE-2.0).
 *
 * See the NOTICE file distributed with this work for
 * additional information regarding copyright ownership.
 */

package org.scalajs.jsenv.nodejs

import java.io.{Console => _, _}

import org.scalajs.core.tools.io._
import org.scalajs.core.tools.jsdep.ResolvedJSDependency
import org.scalajs.jsenv._

import org.scalajs.core.ir.Utils.escapeJS

class JSDOMNodeJSEnv private[jsenv] (
    executable: String,
    args: Seq[String],
    env: Map[String, String],
    internal: Unit
) extends AbstractNodeJSEnv(executable, args, env, sourceMap = false) {

  @deprecated("Use org.scalajs.jsenv.jsdomnodejs.JSDOMNodeJSEnv.", "0.6.18")
  def this(
      executable: String = "node",
      args: Seq[String] = Seq.empty,
      env: Map[String, String] = Map.empty
  ) = {
    this(executable, args, env, internal = ())
  }

  protected def vmName: String = "Node.js with JSDOM"

  override def jsRunner(libs: Seq[ResolvedJSDependency],
      code: VirtualJSFile): JSRunner = {
    new DOMNodeRunner(libs, code)
  }

  override def asyncRunner(libs: Seq[ResolvedJSDependency],
      code: VirtualJSFile): AsyncJSRunner = {
    new AsyncDOMNodeRunner(libs, code)
  }

  override def comRunner(libs: Seq[ResolvedJSDependency],
      code: VirtualJSFile): ComJSRunner = {
    new ComDOMNodeRunner(libs, code)
  }

  protected class DOMNodeRunner(libs: Seq[ResolvedJSDependency], code: VirtualJSFile)
      extends ExtRunner(libs, code) with AbstractDOMNodeRunner

  protected class AsyncDOMNodeRunner(libs: Seq[ResolvedJSDependency], code: VirtualJSFile)
      extends AsyncExtRunner(libs, code) with AbstractDOMNodeRunner

  protected class ComDOMNodeRunner(libs: Seq[ResolvedJSDependency], code: VirtualJSFile)
      extends AsyncDOMNodeRunner(libs, code) with NodeComJSRunner

  protected trait AbstractDOMNodeRunner extends AbstractNodeRunner {

    protected def codeWithJSDOMContext(): Seq[VirtualJSFile] = {
      val scriptsFiles = (getLibJSFiles() :+ code).map {
        case file: FileVirtualFile => file.file
        case file                  => libCache.materialize(file)
      }
      val scriptsURIsAsJSStrings = scriptsFiles.map { file =>
        "\"" + escapeJS(file.toURI.toASCIIString) + "\""
      }
      val scriptsURIsJSArray = scriptsURIsAsJSStrings.mkString("[", ", ", "]")

      val jsDOMCode = {
        s"""
           |(function () {
           |  var jsdom = require("jsdom");
           |
           |  if (typeof jsdom.JSDOM === "function") {
           |    // jsdom >= 10.0.0
           |    var virtualConsole = new jsdom.VirtualConsole()
           |      .sendTo(console, { omitJSDOMErrors: true });
           |    virtualConsole.on("jsdomError", function (error) {
           |      /* #3458 Counter-hack the hack that React's development mode
           |       * uses to bypass browsers' debugging tools. If we detect
           |       * that we are called from that hack, we do nothing.
           |       */
           |      var isWithinReactsInvokeGuardedCallbackDevHack_issue3458 =
           |        new Error("").stack.indexOf("invokeGuardedCallbackDev") >= 0;
           |      if (isWithinReactsInvokeGuardedCallbackDevHack_issue3458)
           |        return;
           |
           |      try {
           |        // Display as much info about the error as possible
           |        if (error.detail && error.detail.stack) {
           |          console.error("" + error.detail);
           |          console.error(error.detail.stack);
           |        } else {
           |          console.error(error);
           |        }
           |      } finally {
           |        // Whatever happens, kill the process so that the run fails
           |        process.exit(1);
           |      }
           |    });
           |
           |    var dom = new jsdom.JSDOM("", {
{'fraction_non_alphanumeric': 0.11200633998150838, 'fraction_numerical': 0.00567956676793026, 'mean_word_length': 2.383378016085791, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 14, 'https://': 4, 'lorem ipsum': 0, 'www.': 4, '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': '5012849', 'n_tokens_mistral': 2346, 'n_tokens_neox': 2271, 'n_words': 503}