Skip to content

Commit 80ffc7d

Browse files
authored
Upgrade to latest scalameta pre-release. (#157)
* Upgrade to latest scalameta pre-release. It seems scalameta/scalameta#847 broke a few rewrites. However, I still prefer the new behavior since I belive synthetic .apply and should belong to aliases and that the rewrites that broke with this change were only conincidentally working. Once scalameta/scalameta#846 is fixed then we should be able to go back to the old behavior, if we so chose. * Clean up scripted tests. - only one project: cross-build - test syntactic and semantic rewrites - use buildDirectory as sourcepath
1 parent d83ff7f commit 80ffc7d

File tree

44 files changed

+163
-252
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+163
-252
lines changed

project/Dependencies.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import sbt._
22
/* scalafmt: { maxColumn = 120 }*/
33

44
object Dependencies {
5-
val scalametaV = "1.8.0-604-2128ff7b"
5+
val scalametaV = "1.8.0-650-890aeec1"
66
val paradiseV = "3.0.0-308-ec15a2f8"
77
val metaconfigV = "0.3.2"
88
var testClasspath: String = "empty"

scalafix-cli/src/main/scala/scalafix/cli/Cli.scala

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -194,28 +194,13 @@ case class ScalafixOptions(
194194
case (Some(cp), Some(sp)) =>
195195
val tryMirror = for {
196196
mirror <- Try {
197-
// TODO(olafur) offline.Mirror needs to document what can go wrong in the types.
198-
// see https://github.com/scalameta/scalameta/issues/814
199-
val files =
200-
sp.split(File.pathSeparator)
201-
.flatMap(FileOps.listFiles)
202-
.filter { x =>
203-
// need to filter here because of https://github.com/scalameta/scalameta/issues/814
204-
new File(x).parse[Source] match {
205-
case parsers.Parsed.Success(_) => true
206-
case _ => false
207-
}
208-
}
209-
if (files.isEmpty) throw Cli.EmptySourcepath // need to fix offline.Mirror :v
210-
val mirror =
211-
Mirror(cp, files.mkString(File.pathSeparator))
197+
val mirror = Mirror(Classpath(cp), Sourcepath(sp))
212198
mirror.sources // ugh. need to trigger lazy to validate that all files parse
213199
mirror
214200
}
215201
} yield Option(mirror)
216202
tryMirror match {
217203
case Success(x) => Ok(x)
218-
case scala.util.Failure(Cli.EmptySourcepath) => Ok(None)
219204
case scala.util.Failure(e) => ConfError.msg(e.toString).notOk
220205
}
221206
case (None, None) =>
@@ -252,7 +237,6 @@ object Cli {
252237
val default = ScalafixOptions()
253238
// Run this at the end of the world, calls sys.exit.
254239

255-
case object EmptySourcepath extends Exception(s"No sources parse")
256240
case class NonZeroExitCode(n: Int)
257241
extends Exception(s"Expected exit code 0. Got exit code $n")
258242
def main(args: Array[String]): Unit = {

scalafix-core/src/main/scala/scalafix/patch/OrganizeImports.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import scalafix.patch.TreePatch.AddGlobalImport
99
import scalafix.patch.TreePatch.RemoveGlobalImport
1010
import scalafix.rewrite.RewriteCtx
1111
import scalafix.syntax._
12+
import scalafix.syntax.XtensionRefSymbolOpt
1213
import scalafix.util.CanonicalImport
1314

1415
private[this] class OrganizeImports[T] private (implicit ctx: RewriteCtx,
@@ -25,7 +26,7 @@ private[this] class OrganizeImports[T] private (implicit ctx: RewriteCtx,
2526

2627
def fullyQualifiedName(ref: Ref): Option[Ref] =
2728
for {
28-
sym <- mirror.symbol(ref).toOption
29+
sym <- ref.symbolOpt
2930
name <- sym.to[Ref].toOption
3031
strippedRoot = name.transform {
3132
case q"_root_.$nme" => nme

scalafix-core/src/main/scala/scalafix/patch/Replacer.scala

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,12 @@ import scalafix.rewrite.RewriteCtx
1010
import scalafix.patch.TreePatch.AddGlobalImport
1111
import scalafix.patch.TreePatch.Rename
1212

13+
import org.scalameta.logger
14+
1315
private[this] class Replacer(implicit ctx: RewriteCtx, mirror: Mirror) {
1416
object `:withSymbol:` {
1517
def unapply(ref: Ref): Option[(Ref, Symbol)] =
16-
Try(
17-
mirror.symbol(ref) match {
18-
case semantic.Completed.Success(symbol) =>
19-
Some(ref -> symbol.normalized)
20-
case _ => None
21-
}
22-
).toOption.flatten
18+
ref.symbolOpt.map(sym => ref -> sym.normalized)
2319
}
2420

2521
def toTokenPatches(ast: Tree, replacements: Seq[Replace]): Seq[Patch] = {
@@ -29,17 +25,17 @@ private[this] class Replacer(implicit ctx: RewriteCtx, mirror: Mirror) {
2925
override def apply(tree: Tree): Unit = {
3026
tree match {
3127
case (ref: Ref) `:withSymbol:` symbol =>
32-
builder ++=
33-
replacements
34-
.find { x =>
35-
x.from == symbol
36-
}
37-
.toList
38-
.flatMap(
39-
replace =>
40-
ctx.addLeft(ref.tokens.head, replace.to.syntax) +:
41-
(ref.tokens.map(TokenPatch.Remove.apply) ++
42-
replace.additionalImports.map(x => AddGlobalImport(x))))
28+
val patches = replacements
29+
.find { x =>
30+
x.from == symbol
31+
}
32+
.toList
33+
.flatMap { replace =>
34+
ctx.addLeft(ref.tokens.head, replace.to.syntax) +:
35+
(ref.tokens.map(TokenPatch.Remove.apply) ++
36+
replace.additionalImports.map(x => AddGlobalImport(x)))
37+
}
38+
builder ++= patches
4339
case imp: Import => // Do nothing
4440
case _ => super.apply(tree)
4541
}
@@ -84,8 +80,7 @@ object Renamer {
8480
if (renameSymbols.isEmpty) None
8581
else
8682
for {
87-
// TODO(olafur) avoid Try() once don't require `Mirror` in Replacer.
88-
symbol <- mirror.symbol(arg).toOption
83+
symbol <- arg.symbolOpt
8984
rename <- renameSymbols.find(_.matches(symbol))
9085
tok <- arg.tokens.headOption
9186
} yield tok -> rename.to

scalafix-core/src/main/scala/scalafix/rewrite/NoAutoTupling.scala

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ case class NoAutoTupling(mirror: Mirror) extends SemanticRewrite(mirror) {
2121
val fixed = MutableSet.empty[Seq[Term.Arg]]
2222
ctx.tree.collect {
2323
case q"${fun: Term.Ref}(...$argss)" if argss.nonEmpty =>
24-
mirror
25-
.symbol(fun)
26-
.toOption
24+
fun.symbolOpt
2725
.collect {
2826
case Symbol.Global(_, Signature.Method(_, jvmSignature)) =>
2927
val argListSignatures =

scalafix-core/src/main/scala/scalafix/rewrite/Xor2Either.scala

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ case class Xor2Either(mirror: Mirror) extends SemanticRewrite(mirror) {
1010
override def rewrite(ctx: RewriteCtx): Patch = {
1111
val importImplicits = ctx.tree.collectFirst {
1212
case t: Term.Name
13-
if mirror
14-
.symbol(t)
15-
.toOption
13+
if t.symbolOpt
1614
.exists(_.normalized == Symbol("_root_.cats.data.Xor.map.")) =>
1715
ctx.addGlobalImport(importer"cats.implicits._")
1816
}

scalafix-core/src/main/scala/scalafix/syntax/package.scala

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import scala.collection.IterableLike
44
import scala.collection.generic.CanBuildFrom
55
import scala.collection.mutable
66
import scala.meta._
7-
import scala.meta.semantic.Completed
87
import scala.meta.semantic.Signature
98
import scala.meta.semantic.Symbol
109
import scala.meta.tokens.Token
@@ -60,6 +59,10 @@ package object syntax {
6059
}
6160
}
6261

62+
implicit class XtensionRefSymbolOpt(ref: Ref)(implicit mirror: Mirror) {
63+
def symbolOpt: Option[Symbol] = Try(ref.symbol).toOption
64+
}
65+
6366
implicit class XtensionTermRef(ref: Term.Ref) {
6467
def toTypeRef: Type.Ref = ref match {
6568
case Term.Name(name) => Type.Name(name)
@@ -80,18 +83,6 @@ package object syntax {
8083
}
8184
}
8285

83-
implicit class XtensionCompleted[T](completed: Completed[T]) {
84-
// TODO(olafur) contribute these upstream
85-
def toOption: Option[T] = completed match {
86-
case Completed.Success(e) => Some(e)
87-
case _ => None
88-
}
89-
def toEither: Either[Exception, T] = completed match {
90-
case Completed.Success(e) => Right(e)
91-
case Completed.Error(e) => Left(e)
92-
}
93-
def toTry: Try[T] = Try(completed.get)
94-
}
9586
implicit class XtensionSymbol(symbol: Symbol) {
9687

9788
/** Returns simplified version of this Symbol.

scalafix-sbt/src/main/scala/scalafix/sbt/ScalafixPlugin.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ object ScalafixPlugin extends AutoPlugin {
5757
.map(x => "--config" :: x.getAbsolutePath :: Nil)
5858
.getOrElse(Nil)
5959
val log = streams.value.log
60-
val sourcepath = scalahostSourcepath.value.flatten
60+
val sourcepath = List(baseDirectory.in(ThisBuild).value)
6161
val classpath = scalahostClasspath.value.flatMap(_.map(_.data))
6262
def format(files: Seq[File]): String =
6363
files.map(_.getAbsolutePath).mkString(File.pathSeparator)

scalafix-sbt/src/sbt-test/sbt-scalafix/basic/.scalafix.conf

Lines changed: 0 additions & 1 deletion
This file was deleted.

scalafix-sbt/src/sbt-test/sbt-scalafix/basic/build.sbt

Lines changed: 0 additions & 40 deletions
This file was deleted.

scalafix-sbt/src/sbt-test/sbt-scalafix/basic/p1/src/main/scala/Test.scala

Lines changed: 0 additions & 7 deletions
This file was deleted.

scalafix-sbt/src/sbt-test/sbt-scalafix/basic/p1/src/test/scala/Test.scala

Lines changed: 0 additions & 7 deletions
This file was deleted.

scalafix-sbt/src/sbt-test/sbt-scalafix/basic/p2/src/main/scala/Test.scala

Lines changed: 0 additions & 7 deletions
This file was deleted.

scalafix-sbt/src/sbt-test/sbt-scalafix/basic/p2/src/test/scala/Test.scala

Lines changed: 0 additions & 7 deletions
This file was deleted.

scalafix-sbt/src/sbt-test/sbt-scalafix/basic/p3/src/main/scala/Test.scala

Lines changed: 0 additions & 7 deletions
This file was deleted.

scalafix-sbt/src/sbt-test/sbt-scalafix/basic/p3/src/test/scala/Test.scala

Lines changed: 0 additions & 7 deletions
This file was deleted.

scalafix-sbt/src/sbt-test/sbt-scalafix/config/build.sbt

Lines changed: 0 additions & 20 deletions
This file was deleted.

scalafix-sbt/src/sbt-test/sbt-scalafix/config/myscalafix.conf

Lines changed: 0 additions & 1 deletion
This file was deleted.

scalafix-sbt/src/sbt-test/sbt-scalafix/config/project/ScalafixTestUtility.scala

Lines changed: 0 additions & 1 deletion
This file was deleted.

scalafix-sbt/src/sbt-test/sbt-scalafix/config/project/build.properties

Lines changed: 0 additions & 1 deletion
This file was deleted.

scalafix-sbt/src/sbt-test/sbt-scalafix/config/project/plugins.sbt

Lines changed: 0 additions & 1 deletion
This file was deleted.

scalafix-sbt/src/sbt-test/sbt-scalafix/config/src/main/scala/Test.scala

Lines changed: 0 additions & 7 deletions
This file was deleted.

scalafix-sbt/src/sbt-test/sbt-scalafix/config/test

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
rewrites = [
2+
ProcedureSyntax
3+
NoAutoTupling
4+
]
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
lazy val root = project
2+
.in(file("."))
3+
.aggregate(
4+
scala211,
5+
scala210,
6+
scala212
7+
)
8+
9+
lazy val scala210 = project.settings(scalaVersion := "2.10.5")
10+
lazy val scala211 = project.settings(scalaVersion := "2.11.11")
11+
lazy val scala212 = project.settings(scalaVersion := "2.12.2")
12+
13+
TaskKey[Unit]("check") := {
14+
val assertContentMatches: ((String, String) => Boolean) =
15+
ScalafixTestUtility.assertContentMatches(streams.value) _
16+
val expected =
17+
"""object Main {
18+
| def foo(a: (Int, String)) = a
19+
| foo((1, "str"))
20+
| def main(args: Array[String]): Unit = {
21+
| println(1)
22+
| }
23+
|}""".stripMargin
24+
val testExpected = expected.replaceFirst("Main", "MainTest")
25+
def unchanged(code: String) =
26+
code
27+
.replace(": Unit =", "")
28+
.replace("((", "(")
29+
.replace("\"))", "\")")
30+
31+
val results: Seq[Boolean] =
32+
Seq(scala210, scala211, scala212).flatMap { project =>
33+
val prefix = project.id
34+
Seq(
35+
assertContentMatches(
36+
prefix + "/src/test/scala/MainTest.scala",
37+
if (prefix.contains("210")) unchanged(testExpected)
38+
else testExpected
39+
),
40+
assertContentMatches(
41+
prefix + "/src/main/scala/Main.scala",
42+
if (prefix.contains("210")) unchanged(expected)
43+
else expected
44+
)
45+
)
46+
}
47+
48+
if (results.contains(false)) sys.error("Assertions failed.")
49+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
object Main {
2+
def foo(a: (Int, String)) = a
3+
foo(1, "str")
4+
def main(args: Array[String]) {
5+
println(1)
6+
}
7+
}

0 commit comments

Comments
 (0)