|
| 1 | +package fix |
| 2 | + |
| 3 | +import scalafix._ |
| 4 | +import scalafix.syntax._ |
| 5 | +import scalafix.util._ |
| 6 | +import scala.meta._ |
| 7 | + |
| 8 | +object CanBuildFrom { |
| 9 | + def apply(paramss: List[List[Term.Param]], |
| 10 | + body: Term, |
| 11 | + ctx: RuleCtx, |
| 12 | + collectionCanBuildFrom: SymbolMatcher, |
| 13 | + nothing: SymbolMatcher)(implicit index: SemanticdbIndex): Patch = { |
| 14 | + // CanBuildFrom has def apply() but not CanBuild |
| 15 | + def emptyApply(param: Name): Boolean = { |
| 16 | + import scala.meta.contrib._ |
| 17 | + val matchCbf = SymbolMatcher.exact(ctx.index.symbol(param).get) |
| 18 | + body.exists{ |
| 19 | + case Term.Apply(Term.Select(matchCbf(_), _), Nil) => true |
| 20 | + case Term.Apply(matchCbf(_), Nil) => true |
| 21 | + case _ => false |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + paramss.flatten.collect{ |
| 26 | + case Term.Param( |
| 27 | + List(Mod.Implicit()), |
| 28 | + param, |
| 29 | + Some( |
| 30 | + Type.Apply( |
| 31 | + cbf @ collectionCanBuildFrom(_), |
| 32 | + List(p1, _, _) |
| 33 | + ) |
| 34 | + ), |
| 35 | + _ |
| 36 | + ) if !nothing.matches(p1) && !emptyApply(param) => new CanBuildFrom(param, cbf) |
| 37 | + }.map(_.toBuildFrom(body, ctx)).asPatch |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +// example: |
| 42 | +// implicit cbf: collection.generic.CanBuildFrom[C0, Int, CC[Int]] |
| 43 | +// param: cbf |
| 44 | +// cbf : collection.generic.CanBuildFrom |
| 45 | +case class CanBuildFrom(param: Name, cbf: Type) { |
| 46 | + def toBuildFrom(body: Term, ctx: RuleCtx)(implicit index: SemanticdbIndex): Patch = { |
| 47 | + |
| 48 | + val matchCbf = SymbolMatcher.exact(ctx.index.symbol(param).get) |
| 49 | + |
| 50 | + // cbf(x) / cbf.apply(x) => cbf.newBuilder(x) |
| 51 | + def replaceNewBuilder(tree: Tree, cbf2: Term, x: Term): Patch = |
| 52 | + ctx.replaceTree( |
| 53 | + tree, |
| 54 | + Term.Apply(Term.Select(cbf2, Term.Name("newBuilder")), List(x)).syntax |
| 55 | + ) |
| 56 | + |
| 57 | + val cbfCalls = |
| 58 | + body.collect { |
| 59 | + // cbf.apply(x) |
| 60 | + case ap @ Term.Apply(sel @ Term.Select(cbf2 @ matchCbf(_), apply), List(x)) => |
| 61 | + replaceNewBuilder(ap, cbf2, x) |
| 62 | + |
| 63 | + // cbf(x) |
| 64 | + case ap @ Term.Apply(cbf2 @ matchCbf(_), List(x)) => |
| 65 | + replaceNewBuilder(ap, cbf2, x) |
| 66 | + }.asPatch |
| 67 | + |
| 68 | + val parameterType = |
| 69 | + ctx.replaceTree(cbf, "collection.BuildFrom") |
| 70 | + |
| 71 | + parameterType + cbfCalls |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +object CanBuildFromNothing { |
| 76 | + def apply(paramss: List[List[Term.Param]], |
| 77 | + body: Term, |
| 78 | + ctx: RuleCtx, |
| 79 | + collectionCanBuildFrom: SymbolMatcher, |
| 80 | + nothing: SymbolMatcher, |
| 81 | + toTpe: SymbolMatcher)(implicit index: SemanticdbIndex): (Patch, Set[Tree]) = { |
| 82 | + val handledTo = Set.newBuilder[Tree] |
| 83 | + |
| 84 | + val patches = |
| 85 | + paramss.flatten.collect{ |
| 86 | + case |
| 87 | + Term.Param( |
| 88 | + List(Mod.Implicit()), |
| 89 | + param, |
| 90 | + Some( |
| 91 | + tpe @ Type.Apply( |
| 92 | + collectionCanBuildFrom(_), |
| 93 | + List( |
| 94 | + nothing(_), |
| 95 | + t, |
| 96 | + cct @ Type.Apply( |
| 97 | + cc, |
| 98 | + _ |
| 99 | + ) |
| 100 | + ) |
| 101 | + ) |
| 102 | + ), |
| 103 | + _ |
| 104 | + ) => new CanBuildFromNothing(param, tpe, t, cct, cc, body, ctx, toTpe) |
| 105 | + }.map{cbf => |
| 106 | + val (ps, ht) = cbf.toFactory |
| 107 | + handledTo ++= ht |
| 108 | + ps |
| 109 | + }.asPatch |
| 110 | + |
| 111 | + (patches, handledTo.result()) |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +// example: |
| 116 | +// implicit cbf: collection.generic.CanBuildFrom[Nothing, Int, CC[Int]] |
| 117 | +// |
| 118 | +// param: cbf |
| 119 | +// tpe : collection.generic.CanBuildFrom[Nothing, Int, CC[Int]] |
| 120 | +// cbf : CanBuildFrom |
| 121 | +// v : Int |
| 122 | +// cct : CC[Int] |
| 123 | +// cc : CC |
| 124 | +case class CanBuildFromNothing(param: Name, |
| 125 | + tpe: Type.Apply, |
| 126 | + t: Type, |
| 127 | + cct: Type.Apply, |
| 128 | + cc: Type, |
| 129 | + body: Term, |
| 130 | + ctx: RuleCtx, |
| 131 | + toTpe: SymbolMatcher) { |
| 132 | + def toFactory(implicit index: SemanticdbIndex): (Patch, Set[Tree]) = { |
| 133 | + val handledTo = Set.newBuilder[Tree] |
| 134 | + |
| 135 | + val matchCbf = SymbolMatcher.exact(ctx.index.symbol(param).get) |
| 136 | + |
| 137 | + // cbf() / cbf.apply => cbf.newBuilder |
| 138 | + def replaceNewBuilder(tree: Tree, cbf2: Term): Patch = |
| 139 | + ctx.replaceTree(tree, Term.Select(cbf2, Term.Name("newBuilder")).syntax) |
| 140 | + |
| 141 | + // don't patch cbf.apply twice (cbf.apply and cbf.apply()) |
| 142 | + val visitedCbfCalls = scala.collection.mutable.Set[Tree]() |
| 143 | + |
| 144 | + val cbfCalls = |
| 145 | + body.collect { |
| 146 | + // cbf.apply() |
| 147 | + case ap @ Term.Apply(sel @ Term.Select(cbf2 @ matchCbf(_), apply), Nil) => |
| 148 | + visitedCbfCalls += sel |
| 149 | + replaceNewBuilder(ap, cbf2) |
| 150 | + |
| 151 | + // cbf.apply |
| 152 | + case sel @ Term.Select(cbf2 @ matchCbf(_), ap) if (!visitedCbfCalls.contains(sel)) => |
| 153 | + replaceNewBuilder(sel, cbf2) |
| 154 | + |
| 155 | + // cbf() |
| 156 | + case ap @ Term.Apply(cbf2 @ matchCbf(_), Nil) => |
| 157 | + replaceNewBuilder(ap, cbf2) |
| 158 | + }.asPatch |
| 159 | + |
| 160 | + |
| 161 | + val matchCC = SymbolMatcher.exact(ctx.index.symbol(cc).get) |
| 162 | + |
| 163 | + // e.to[CC] => e.to(cbf) |
| 164 | + val toCalls = |
| 165 | + body.collect { |
| 166 | + case ap @ Term.ApplyType(Term.Select(_, to @ toTpe(_)), List(cc2 @ matchCC(_))) => |
| 167 | + handledTo += to |
| 168 | + |
| 169 | + // e.to[CC](*cbf*) extract implicit parameter |
| 170 | + val synth = ctx.index.synthetics.find(_.position.end == ap.pos.end).get |
| 171 | + val Term.Apply(_, List(implicitCbf)) = synth.text.parse[Term].get |
| 172 | + |
| 173 | + // This is a bit unsafe |
| 174 | + // https://github.com/scalameta/scalameta/issues/1636 |
| 175 | + if (implicitCbf.syntax == param.syntax) { |
| 176 | + trailingBrackets(to, ctx).map { case (open, close) => |
| 177 | + ctx.replaceTree(cc2, implicitCbf.syntax) + |
| 178 | + ctx.replaceToken(open, "(") + |
| 179 | + ctx.replaceToken(close, ")") |
| 180 | + }.asPatch |
| 181 | + } else Patch.empty |
| 182 | + |
| 183 | + }.asPatch |
| 184 | + |
| 185 | + // implicit cbf: collection.generic.CanBuildFrom[Nothing, Int, CC[Int]] => |
| 186 | + // implicit cbf: collection.Factory[Int, CC[Int]] |
| 187 | + val parameterType = |
| 188 | + ctx.replaceTree( |
| 189 | + tpe, |
| 190 | + Type.Apply(Type.Name("collection.Factory"), List(t, cct)).syntax |
| 191 | + ) |
| 192 | + |
| 193 | + (parameterType + cbfCalls + toCalls, handledTo.result()) |
| 194 | + } |
| 195 | +} |
0 commit comments