Skip to content

CanBuildFrom: Add support for Class and simple form for To (fix #135) #136

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions scalafix/input/src/main/scala/fix/CanBuildFromSrc.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,11 @@ object CanBuildFromSrc {
cbf2("")
()
}

def f2[T, That](implicit cbf: CanBuildFrom[Nothing, T, That]): Foo[T, That] =
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously it would only take CanBuildFrom[Nothing, A, CC[B]] now it accepts CanBuildFrom[Nothing, A, B], but it won't rewrite e.to[CC] => cbf.fromSpecific(e)

new Foo

class Foo[T, That](implicit cbf: CanBuildFrom[Nothing, T, That]) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We now support Classes and Def. I don't see another place where we could insert a CanBuildFrom. Maybe on a type alias (type Foo = CanBuildFrom[Bar, Buz, Qux])?

val b = cbf()
}
}
7 changes: 7 additions & 0 deletions scalafix/output/src/main/scala/fix/CanBuildFromSrc.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,11 @@ object CanBuildFromSrc {
cbf2.newBuilder("")
()
}

def f2[T, That](implicit cbf: Factory[T, That]): Foo[T, That] =
new Foo

class Foo[T, That](implicit cbf: Factory[T, That]) {
val b = cbf.newBuilder
}
}
132 changes: 70 additions & 62 deletions scalafix/rules/src/main/scala/fix/CanBuildFrom.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,21 @@ import scala.collection.mutable

object CanBuildFrom {
def apply(paramss: List[List[Term.Param]],
body: Term,
stats: List[Tree],
ctx: RuleCtx,
collectionCanBuildFrom: SymbolMatcher,
nothing: SymbolMatcher)(implicit index: SemanticdbIndex): Patch = {
// CanBuildFrom has def apply() but not CanBuild
def emptyApply(param: Name): Boolean = {
import scala.meta.contrib._
val matchCbf = SymbolMatcher.exact(ctx.index.symbol(param).get)
body.exists {
case Term.Apply(Term.Select(matchCbf(_), _), Nil) => true
case Term.Apply(matchCbf(_), Nil) => true
case _ => false
}
stats.exists(
_.exists {
case Term.Apply(Term.Select(matchCbf(_), _), Nil) => true
case Term.Apply(matchCbf(_), Nil) => true
case _ => false
}
)
}

paramss.flatten
Expand All @@ -38,7 +40,7 @@ object CanBuildFrom {
) if !nothing.matches(p1) && !emptyApply(param) =>
new CanBuildFrom(param, cbf)
}
.map(_.toBuildFrom(body, ctx))
.map(_.toBuildFrom(stats, ctx))
.asPatch
}
}
Expand All @@ -48,7 +50,7 @@ object CanBuildFrom {
// param: cbf
// cbf : collection.generic.CanBuildFrom
case class CanBuildFrom(param: Name, cbf: Type) {
def toBuildFrom(body: Term, ctx: RuleCtx)(implicit index: SemanticdbIndex): Patch = {
def toBuildFrom(stats: List[Tree], ctx: RuleCtx)(implicit index: SemanticdbIndex): Patch = {

val matchCbf = SymbolMatcher.exact(ctx.index.symbol(param).get)

Expand All @@ -60,15 +62,17 @@ case class CanBuildFrom(param: Name, cbf: Type) {
)

val cbfCalls =
body.collect {
// cbf.apply(x)
case ap @ Term.Apply(sel @ Term.Select(cbf2 @ matchCbf(_), apply), List(x)) =>
replaceNewBuilder(ap, cbf2, x)

// cbf(x)
case ap @ Term.Apply(cbf2 @ matchCbf(_), List(x)) =>
replaceNewBuilder(ap, cbf2, x)
}.asPatch
stats
.map(_.collect {
// cbf.apply(x)
case ap @ Term.Apply(sel @ Term.Select(cbf2 @ matchCbf(_), apply), List(x)) =>
replaceNewBuilder(ap, cbf2, x)

// cbf(x)
case ap @ Term.Apply(cbf2 @ matchCbf(_), List(x)) =>
replaceNewBuilder(ap, cbf2, x)
}.asPatch)
.asPatch

val parameterType =
ctx.replaceTree(cbf, "BuildFrom")
Expand All @@ -79,12 +83,13 @@ case class CanBuildFrom(param: Name, cbf: Type) {

object CanBuildFromNothing {
def apply(paramss: List[List[Term.Param]],
body: Term,
stats: List[Tree],
ctx: RuleCtx,
collectionCanBuildFrom: SymbolMatcher,
nothing: SymbolMatcher,
toTpe: SymbolMatcher,
handledTo: mutable.Set[Tree])(implicit index: SemanticdbIndex): Patch = {

paramss.flatten
.collect {
case Term.Param(
Expand All @@ -96,16 +101,13 @@ object CanBuildFromNothing {
List(
nothing(_),
t,
cct @ Type.Apply(
cc,
_
)
toT
)
)
),
_
) =>
new CanBuildFromNothing(param, tpe, t, cct, cc, body, ctx, toTpe, handledTo)
new CanBuildFromNothing(param, tpe, t, toT, stats, ctx, toTpe, handledTo)
}
.map(_.toFactory)
.asPatch
Expand All @@ -119,14 +121,12 @@ object CanBuildFromNothing {
// tpe : collection.generic.CanBuildFrom[Nothing, Int, CC[Int]]
// cbf : CanBuildFrom
// v : Int
// cct : CC[Int]
// cc : CC
// toT : CC[Int]
case class CanBuildFromNothing(param: Name,
tpe: Type.Apply,
t: Type,
cct: Type.Apply,
cc: Type,
body: Term,
toT: Type,
stats: List[Tree],
ctx: RuleCtx,
toTpe: SymbolMatcher,
handledTo: mutable.Set[Tree]) {
Expand All @@ -141,53 +141,61 @@ case class CanBuildFromNothing(param: Name,
val visitedCbfCalls = mutable.Set[Tree]()

val cbfCalls =
body.collect {
// cbf.apply()
case ap @ Term.Apply(sel @ Term.Select(cbf2 @ matchCbf(_), apply), Nil) =>
visitedCbfCalls += sel
replaceNewBuilder(ap, cbf2)

// cbf.apply
case sel @ Term.Select(cbf2 @ matchCbf(_), ap) if (!visitedCbfCalls.contains(sel)) =>
replaceNewBuilder(sel, cbf2)

// cbf()
case ap @ Term.Apply(cbf2 @ matchCbf(_), Nil) =>
replaceNewBuilder(ap, cbf2)
}.asPatch

val matchCC = SymbolMatcher.exact(ctx.index.symbol(cc).get)
stats
.map(_.collect {
// cbf.apply()
case ap @ Term.Apply(sel @ Term.Select(cbf2 @ matchCbf(_), apply), Nil) =>
visitedCbfCalls += sel
replaceNewBuilder(ap, cbf2)

// cbf.apply
case sel @ Term.Select(cbf2 @ matchCbf(_), ap) if (!visitedCbfCalls.contains(sel)) =>
replaceNewBuilder(sel, cbf2)

// cbf()
case ap @ Term.Apply(cbf2 @ matchCbf(_), Nil) =>
replaceNewBuilder(ap, cbf2)
}.asPatch)
.asPatch

// e.to[CC] => cbf.fromSpecific(e)
val toCalls =
body.collect {
case ap @ Term.ApplyType(Term.Select(e, to @ toTpe(_)), List(cc2 @ matchCC(_))) =>
handledTo += to
val toCalls = toT match {
case cct @ Type.Apply(cc, _) => {
val matchCC = SymbolMatcher.exact(ctx.index.symbol(cc).get)

stats
.map(_.collect {
case ap @ Term.ApplyType(Term.Select(e, to @ toTpe(_)), List(cc2 @ matchCC(_))) =>
handledTo += to

// e.to[CC](*cbf*) extract implicit parameter
val synth = ctx.index.synthetics.find(_.position.end == ap.pos.end).get
val Term.Apply(_, List(implicitCbf)) = synth.text.parse[Term].get
// e.to[CC](*cbf*) extract implicit parameter
val synth = ctx.index.synthetics.find(_.position.end == ap.pos.end).get
val Term.Apply(_, List(implicitCbf)) = synth.text.parse[Term].get

// This is a bit unsafe
// https://github.com/scalameta/scalameta/issues/1636
if (implicitCbf.syntax == param.syntax) {
// This is a bit unsafe
// https://github.com/scalameta/scalameta/issues/1636
if (implicitCbf.syntax == param.syntax) {

// .to[CC]
val apToRemove = ap.tokens.slice(e.tokens.end - ap.tokens.start, ap.tokens.size)
// .to[CC]
val apToRemove = ap.tokens.slice(e.tokens.end - ap.tokens.start, ap.tokens.size)

ctx.removeTokens(apToRemove) +
ctx.addLeft(e, implicitCbf.syntax + ".fromSpecific(") +
ctx.addRight(e, ")")
} else Patch.empty
ctx.removeTokens(apToRemove) +
ctx.addLeft(e, implicitCbf.syntax + ".fromSpecific(") +
ctx.addRight(e, ")")
} else Patch.empty

}.asPatch
}.asPatch)
.asPatch
}
case _ => Patch.empty
}

// implicit cbf: collection.generic.CanBuildFrom[Nothing, Int, CC[Int]] =>
// implicit cbf: Factory[Int, CC[Int]]
val parameterType =
ctx.replaceTree(
tpe,
Type.Apply(Type.Name("Factory"), List(t, cct)).syntax
Type.Apply(Type.Name("Factory"), List(t, toT)).syntax
)

parameterType + cbfCalls + toCalls
Expand Down
19 changes: 17 additions & 2 deletions scalafix/rules/src/main/scala/fix/Stable212Base.scala
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,23 @@ trait Stable212Base extends CrossCompatibility { self: SemanticRule =>
val useSites =
ctx.tree.collect {
case Defn.Def(_, _, _, paramss, _, body) =>
CanBuildFromNothing(paramss, body, ctx, collectionCanBuildFrom, nothing, toTpe, handledTo) +
CanBuildFrom(paramss, body, ctx, collectionCanBuildFrom, nothing)
CanBuildFromNothing(paramss,
List(body),
ctx,
collectionCanBuildFrom,
nothing,
toTpe,
handledTo) +
CanBuildFrom(paramss, List(body), ctx, collectionCanBuildFrom, nothing)
case Defn.Class(_, _, _, Ctor.Primary(_, _, paramss), Template(_, _, _, stats)) =>
CanBuildFromNothing(paramss,
stats,
ctx,
collectionCanBuildFrom,
nothing,
toTpe,
handledTo) +
CanBuildFrom(paramss, stats, ctx, collectionCanBuildFrom, nothing)
}.asPatch

val imports =
Expand Down