Skip to content

Commit

Permalink
Make ExplicitResultTypes scope aware
Browse files Browse the repository at this point in the history
  • Loading branch information
Olafur Pall Geirsson committed Oct 6, 2019
1 parent 75db131 commit 162db39
Show file tree
Hide file tree
Showing 31 changed files with 1,590 additions and 67 deletions.
18 changes: 14 additions & 4 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ inThisBuild(
List(
onLoadMessage := s"Welcome to scalafix ${version.value}",
scalaVersion := scala212,
crossScalaVersions := List(scala212, scala211)
crossScalaVersions := List(scala212, scala211),
fork.in(Test, test) := true
)
)

cancelable.in(Global) := true

noPublish

// force javac to fork by setting javaHome to get error messages during compilation,
Expand Down Expand Up @@ -69,7 +72,8 @@ lazy val rules = project
.in(file("scalafix-rules"))
.settings(
moduleName := "scalafix-rules",
description := "Built-in Scalafix rules"
description := "Built-in Scalafix rules",
libraryDependencies += "org.scalameta" % "mtags" % "0.7.6" cross CrossVersion.full
)
.dependsOn(core)

Expand All @@ -93,6 +97,9 @@ lazy val cli = project
mainClass in assembly := Some("scalafix.v1.Main"),
assemblyJarName in assembly := "scalafix.jar",
libraryDependencies ++= Seq(
"ch.epfl.scala" %% "bloop-launcher" % "1.3.3",
"ch.epfl.scala" % "bsp4j" % "2.0.0-M4+10-61e61e87",
"org.scala-lang.modules" %% "scala-java8-compat" % "0.9.0",
"com.martiansoftware" % "nailgun-server" % "0.9.1",
jgit,
"ch.qos.logback" % "logback-classic" % "1.2.3",
Expand Down Expand Up @@ -126,6 +133,7 @@ lazy val testsInput = project
scalacOptions += warnUnusedImports.value, // For RemoveUnused
scalacOptions += "-Ywarn-unused", // For RemoveUnusedTerms
logLevel := Level.Error, // avoid flood of compiler warnings
libraryDependencies += "com.twitter" %% "bijection-core" % "0.9.5",
testsInputOutputSetting,
coverageEnabled := false
)
Expand All @@ -140,7 +148,8 @@ lazy val testsOutput = project
"-Xlint"
),
testsInputOutputSetting,
coverageEnabled := false
coverageEnabled := false,
libraryDependencies += "com.twitter" %% "bijection-core" % "0.9.5"
)

lazy val testkit = project
Expand Down Expand Up @@ -170,7 +179,8 @@ lazy val unit = project
libraryDependencies ++= List(
jgit,
semanticdbPluginLibrary,
scalatest
scalatest,
"org.scalameta" %% "testkit" % scalametaV
),
compileInputs.in(Compile, compile) := {
compileInputs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,13 @@ final case class ScalafixArgumentsImpl(args: Args = Args.default)
): ScalafixArguments =
copy(args = args.copy(toolClasspath = classLoader))

override def withPaths(paths: util.List[Path]): ScalafixArguments =
override def withPaths(paths: util.List[Path]): ScalafixArguments = {
copy(
args = args.copy(
files = paths.asScala.iterator.map(AbsolutePath(_)(args.cwd)).toList
)
)
}

override def withExcludedPaths(
matchers: util.List[PathMatcher]
Expand Down
25 changes: 21 additions & 4 deletions scalafix-cli/src/main/scala/scalafix/internal/v1/Args.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ import scalafix.internal.jgit.JGitDiff
import scalafix.internal.reflect.ClasspathOps
import scalafix.v1.Configuration
import scalafix.v1.RuleDecoder
import scala.tools.nsc.interactive.Global
import scala.tools.nsc.Settings
import scala.tools.nsc.reporters.ConsoleReporter

class Section(val name: String) extends StaticAnnotation

Expand Down Expand Up @@ -257,11 +260,11 @@ case class Args(
): Configured[Rules] = {
val rulesConf = this.rulesConf(() => base)
val decoder = ruleDecoder(scalafixConfig)

val configuration = Configuration()
.withConf(base)
.withScalaVersion(scalaVersion)
.withScalacOptions(scalacOptions)
.withScalacClasspath(classpath.entries)
decoder.read(rulesConf).andThen(_.withConfiguration(configuration))
}

Expand Down Expand Up @@ -348,6 +351,15 @@ case class Args(
}
}

def configuredGlobal: Configured[Option[Global]] =
Configured.ok {
val settings = new Settings()
settings.YpresentationAnyThread.value = true
settings.classpath.value = classpath.syntax
val reporter = new ConsoleReporter(settings)
Some(new Global(settings, reporter))
}

def validate: Configured[ValidatedArgs] = {
baseConfig.andThen {
case (base, scalafixConfig, delegator) =>
Expand All @@ -356,9 +368,13 @@ case class Args(
configuredSymtab |@|
configuredRules(base, scalafixConfig) |@|
resolvedPathReplace |@|
configuredDiffDisable
configuredDiffDisable |@|
configuredGlobal
).map {
case ((((root, symtab), rulez), pathReplace), diffDisable) =>
case (
((((root, symtab), rulez), pathReplace), diffDisable),
global
) =>
ValidatedArgs(
this,
symtab,
Expand All @@ -369,7 +385,8 @@ case class Args(
pathReplace,
diffDisable,
delegator,
semanticdbFilterMatcher
semanticdbFilterMatcher,
global
)
}
}
Expand Down
20 changes: 19 additions & 1 deletion scalafix-cli/src/main/scala/scalafix/internal/v1/MainOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import scalafix.internal.config.PrintStreamReporter
import scalafix.internal.diff.DiffUtils
import scalafix.v1.SyntacticDocument
import scalafix.v1.SemanticDocument
import scala.meta.interactive.InteractiveSemanticdb

object MainOps {

Expand Down Expand Up @@ -185,6 +186,22 @@ object MainOps {
}
}

def compileWithGlobal(
args: ValidatedArgs,
doc: SyntacticDocument
): Option[TextDocument] = {
args.global.map { g =>
InteractiveSemanticdb
.toTextDocument(g, doc.input.text)
.copy(
// TODO(olafur): avoid this MD5 compute
md5 = FingerprintOps.md5(
StandardCharsets.UTF_8.encode(CharBuffer.wrap(doc.input.chars))
)
)
}
}

def unsafeHandleFile(args: ValidatedArgs, file: AbsolutePath): ExitStatus = {
val input = args.input(file)
val tree = LazyValue.later { () =>
Expand All @@ -198,7 +215,8 @@ object MainOps {
doc,
relpath,
args.classLoader,
args.symtab
args.symtab,
() => compileWithGlobal(args, doc)
)
val (fix, messages) =
args.rules.semanticPatch(sdoc, args.args.autoSuppressLinterErrors)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import scalafix.internal.config.ScalafixConfig
import scalafix.internal.diff.DiffDisable
import scala.meta.internal.symtab.SymbolTable
import scalafix.internal.config.FilterMatcher
import scala.tools.nsc.interactive.Global

case class ValidatedArgs(
args: Args,
Expand All @@ -21,7 +22,8 @@ case class ValidatedArgs(
pathReplace: AbsolutePath => AbsolutePath,
diffDisable: DiffDisable,
callback: DelegatingMainCallback,
semanticdbFileFilter: FilterMatcher
semanticdbFileFilter: FilterMatcher,
global: Option[Global]
) {

def input(file: AbsolutePath): Input = {
Expand Down
20 changes: 20 additions & 0 deletions scalafix-core/src/main/scala/scalafix/internal/v1/Rules.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import scalafix.v1.SemanticDocument
import scalafix.v1.SemanticRule
import scalafix.v1.SyntacticDocument
import scalafix.v1.SyntacticRule
import scala.util.control.NonFatal
import scala.collection.mutable

case class Rules(rules: List[Rule] = Nil) {

Expand All @@ -32,6 +34,24 @@ case class Rules(rules: List[Rule] = Nil) {
def syntacticRules: List[SyntacticRule] = rules.collect {
case s: SyntacticRule => s
}
def afterComplete(): List[(Rule, Throwable)] = {
foreachRule(_.afterComplete())
}
def beforeStart(): List[(Rule, Throwable)] = {
foreachRule(_.beforeStart())
}

private def foreachRule(fn: Rule => Unit): List[(Rule, Throwable)] = {
val buf = mutable.ListBuffer.empty[(Rule, Throwable)]
rules.foreach { rule =>
try fn(rule)
catch {
case NonFatal(e) =>
buf += (rule -> e)
}
}
buf.result()
}

def addSuppression(
tokens: Tokens,
Expand Down
11 changes: 10 additions & 1 deletion scalafix-core/src/main/scala/scalafix/v1/Configuration.scala
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package scalafix.v1
import metaconfig.Conf
import scala.meta.io.AbsolutePath

final class Configuration private (
val scalaVersion: String,
val scalacOptions: List[String],
val scalacClasspath: List[AbsolutePath],
val conf: Conf
) {

Expand All @@ -15,21 +17,27 @@ final class Configuration private (
copy(scalacOptions = options)
}

def withScalacClasspath(classpath: List[AbsolutePath]): Configuration = {
copy(scalacClasspath = classpath)
}

def withConf(conf: Conf): Configuration = {
copy(conf = conf)
}

override def toString: String =
s"Configuration($scalaVersion, $scalacOptions, $conf)"
s"Configuration($scalaVersion, $scalacOptions, $scalacClasspath, $conf)"

private[this] def copy(
scalaVersion: String = this.scalaVersion,
scalacOptions: List[String] = this.scalacOptions,
scalacClasspath: List[AbsolutePath] = this.scalacClasspath,
conf: Conf = this.conf
): Configuration = {
new Configuration(
scalaVersion = scalaVersion,
scalacOptions = scalacOptions,
scalacClasspath = scalacClasspath,
conf = conf
)
}
Expand All @@ -40,6 +48,7 @@ object Configuration {
new Configuration(
scala.util.Properties.versionNumberString,
Nil,
Nil,
Conf.Obj()
)
}
Expand Down
11 changes: 9 additions & 2 deletions scalafix-core/src/main/scala/scalafix/v1/SemanticDocument.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import scala.meta.io.RelativePath
import scalafix.internal.v1._
import scalafix.util.MatchingParens
import scalafix.util.TokenList
import scala.tools.nsc.interactive.Global
import scala.meta.internal.semanticdb.TextDocument

final class SemanticDocument private[scalafix] (
private[scalafix] val internal: InternalSemanticDoc
Expand Down Expand Up @@ -52,7 +54,8 @@ object SemanticDocument {
doc: SyntacticDocument,
path: RelativePath,
classLoader: ClassLoader,
symtab: SymbolTable
symtab: SymbolTable,
compile: () => Option[TextDocument]
): SemanticDocument = {
val semanticdbRelPath = s"META-INF/semanticdb/$path.semanticdb"
Option(classLoader.getResourceAsStream(semanticdbRelPath)) match {
Expand All @@ -67,7 +70,11 @@ object SemanticDocument {
val impl = new InternalSemanticDoc(doc, sdoc, symtab)
new SemanticDocument(impl)
case None =>
throw Error.MissingSemanticdb(semanticdbRelPath)
compile() match {
case None => throw Error.MissingSemanticdb(semanticdbRelPath)
case Some(sdoc) =>
new SemanticDocument(new InternalSemanticDoc(doc, sdoc, symtab))
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package scala.meta.internal.proxy

import scala.meta.internal.pc.MetalsGlobal
import scala.reflect.internal.util.Position

object GlobalProxy {
def typedTreeAt(g: MetalsGlobal, pos: Position): g.Tree = {
g.typedTreeAt(pos)
}
}
Loading

0 comments on commit 162db39

Please sign in to comment.