-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #381 from gabro/denotation-ops
Add resultType extension method on Symbol
- Loading branch information
Showing
7 changed files
with
81 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
scalafix-core/shared/src/main/scala/scalafix/internal/util/DenotationOps.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package scalafix.internal.util | ||
|
||
import scala.meta._ | ||
|
||
object DenotationOps { | ||
val defaultDialect = | ||
dialects.Scala212.copy(allowMethodTypes = true, allowTypeLambdas = true) | ||
|
||
def resultType( | ||
symbol: Symbol, | ||
denot: Denotation, | ||
dialect: Dialect): Option[Type] = { | ||
def getDeclType(tpe: Type): Type = tpe match { | ||
case Type.Method(_, tpe) if denot.isDef => tpe | ||
case Type.Lambda(_, tpe) if denot.isDef => getDeclType(tpe) | ||
case Type.Method((Term.Param(_, _, Some(tpe), _) :: Nil) :: Nil, _) | ||
if denot.isVar => | ||
// Workaround for https://github.com/scalameta/scalameta/issues/1100 | ||
tpe | ||
case x => | ||
x | ||
} | ||
val signature = | ||
if (denot.isVal || denot.isDef | denot.isVar) denot.signature | ||
else { | ||
throw new UnsupportedOperationException( | ||
s"Can't parse type for denotation $denot, denot.info=${denot.signature}") | ||
} | ||
val input = Input.Denotation(signature, symbol) | ||
(dialect, input).parse[Type].toOption.map(getDeclType) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
scalafix-tests/shared/src/main/scala/test/DenotationOpsTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package test | ||
|
||
object DenotationOpsTest { | ||
def m(x: Int, y: String): List[String] = List(y) | ||
var x = true | ||
val y = m(42, "hey") | ||
} |
33 changes: 33 additions & 0 deletions
33
scalafix-tests/unit/src/test/scala/scalafix/tests/DenotationOpsTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package scalafix.tests | ||
|
||
import scala.meta._ | ||
import scala.meta.contrib._ | ||
import scalafix.syntax._ | ||
import scalafix.internal.util.DenotationOps | ||
|
||
class DenotationOpsTest extends BaseSemanticTest("DenotationOpsTest") { | ||
|
||
test("resultType") { | ||
val source = docs.input.parse[Source].get | ||
source.collect { | ||
case t @ Pat.Var(Name("x")) => | ||
for { | ||
symbol <- t.symbol | ||
resultType <- symbol.resultType | ||
} yield assert(resultType isEqual t"Boolean") | ||
|
||
case t @ Pat.Var(Name("y")) => | ||
for { | ||
symbol <- t.symbol | ||
resultType <- symbol.resultType | ||
} yield assert(resultType isEqual t"List[String]") | ||
|
||
case t: Defn.Def if t.name.value == "m" => | ||
for { | ||
symbol <- t.symbol | ||
resultType <- symbol.resultType | ||
} yield assert(resultType isEqual t"List[String]") | ||
} | ||
} | ||
|
||
} |