scala.reflect.internal.util.BatchSourceFile Scala Examples

The following examples show how to use scala.reflect.internal.util.BatchSourceFile. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
Example 1
Source File: IntegrationTest.scala    From scala-sculpt   with Apache License 2.0 5 votes vote down vote up
// Copyright (C) 2015-2020 Lightbend Inc. <http://lightbend.com>

package com.lightbend.tools.sculpt

import scala.tools.nsc.{ Settings, Global }
import scala.tools.nsc.io.VirtualDirectory
import scala.reflect.internal.util.BatchSourceFile

object Scaffold {

  val classes: String = {
    // this will be e.g. "2.11" or "2.12"
    val majorScalaVersion = {
      val v = scala.util.Properties.versionNumberString
      if (v matches ".*-(pre-\\w+|M\\d+|RC\\d+)") {
        v
      } else {
        v.split('.').take(2).mkString(".")
      }
    }
    val relative = s"./target/scala-$majorScalaVersion/classes"
    val file = new java.io.File(relative)
    assert(file.exists)
    file.getAbsolutePath
  }

  def defaultSettings: Settings = {
    val settings = new Settings
    settings.processArgumentString(
      s"-usejavacp -Xplugin:$classes -Xplugin-require:sculpt")
    settings.outputDirs.setSingleOutput(
      new VirtualDirectory("(memory)", None))
    settings
  }

  def analyze(code: String, classMode: Boolean = false): String = {
    val out = java.io.File.createTempFile("sculpt", "json", null)
    val modeSetting =
      if (classMode)
        " -P:sculpt:mode=class"
      else
        ""
    val settings = defaultSettings
    settings.processArgumentString(s"-P:sculpt:out=$out$modeSetting")
    val sources = List(new BatchSourceFile("<test>", code))
    val compiler = new Global(settings)
    (new compiler.Run).compileSources(sources)
    scala.io.Source.fromFile(out).mkString
  }

}

class IntegrationTest extends munit.FunSuite {
  def check(s: Sample): Unit = {
    assert(s.json == Scaffold.analyze(s.source))
    assert(s.classJson == Scaffold.analyze(s.source, classMode = true))
  }
  for (sample <- Samples.samples)
    test(sample.name) {
      check(sample)
    }
} 
Example 2
Source File: AnalyzerTest.scala    From scala-commons   with MIT License 5 votes vote down vote up
package com.avsystem.commons
package analyzer

import org.scalactic.source.Position
import org.scalatest.Assertions

import scala.reflect.internal.util.BatchSourceFile
import scala.tools.nsc.plugins.Plugin
import scala.tools.nsc.{Global, Settings}

trait AnalyzerTest { this: Assertions =>
  val settings = new Settings
  settings.usejavacp.value = true
  settings.pluginOptions.value ++= List("AVSystemAnalyzer:+_")

  val compiler: Global = new Global(settings) { global =>
    override protected def loadRoughPluginsList(): List[Plugin] =
      new AnalyzerPlugin(global) :: super.loadRoughPluginsList()
  }

  def compile(source: String): Unit = {
    compiler.reporter.reset()
    val run = new compiler.Run
    run.compileSources(List(new BatchSourceFile("test.scala", source)))
  }

  def assertErrors(source: String)(implicit pos: Position): Unit = {
    compile(source)
    assert(compiler.reporter.hasErrors)
  }

  def assertErrors(errors: Int, source: String)(implicit pos: Position): Unit = {
    compile(source)
    assert(compiler.reporter.errorCount == errors)
  }

  def assertNoErrors(source: String)(implicit pos: Position): Unit = {
    compile(source)
    assert(!compiler.reporter.hasErrors)
  }
} 
Example 3
Source File: Parsing.scala    From sctags   with Apache License 2.0 5 votes vote down vote up
package sctags

import scala.tools.nsc.{Global, CompilationUnits}
import scala.tools.nsc.io.AbstractFile
import scala.tools.nsc.ast.Trees
import scala.tools.nsc.ast.parser.SyntaxAnalyzer
import scala.reflect.internal.util.BatchSourceFile

import java.io.File;

trait Parsing { this: SCTags.type =>
  import compiler.syntaxAnalyzer._
  import compiler._

  def parse(af: AbstractFile): Tree =
    parse(new CompilationUnit(new BatchSourceFile(af)))

  def parse(f: File): Tree =
    parse(AbstractFile.getFile(f))

  def parse(fname: String): Tree =
    parse(AbstractFile.getFile(fname))

  def parse(cu: CompilationUnit): Tree = {
    new compiler.Run
    val parser = new UnitParser(cu)
    val tree = parser.compilationUnit
    compiler.analyzer.newNamer(compiler.analyzer.rootContext(cu, tree, false)).enterSym(tree)
    tree
  }
}