Skip to content

Commit

Permalink
Fix bug that adds using incorrectly.
Browse files Browse the repository at this point in the history
  • Loading branch information
mrdziuban committed Oct 7, 2024
1 parent c522220 commit 40a830a
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 13 deletions.
5 changes: 5 additions & 0 deletions input/src/main/scala-3/fix/GivenAndUsingTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,9 @@ object WithExplicitUsing {
def test(using i: Int): Int = i
test(using 1)
}
object WithApplyAfterUsing {
given i: Int = 1
def test(using i: Int): String => String = s => s
test("")
}
// format: on
5 changes: 5 additions & 0 deletions output/src/main/scala-3/fix/GivenAndUsingTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,9 @@ object WithExplicitUsing {
def test(using i: Int): Int = i
test(using 1)
}
object WithApplyAfterUsing {
given i: Int = 1
def test(using i: Int): String => String = s => s
test("")
}
// format: on
25 changes: 12 additions & 13 deletions rules/src/main/scala/fix/GivenAndUsing.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package fix

import fix.matchers.ApplyImplicitArgs
import fix.matchers.{ApplyImplicitArgs, ImplicitOrUsingMod}
import scalafix.lint.LintSeverity
import scalafix.v1._

Expand Down Expand Up @@ -59,25 +59,24 @@ class GivenAndUsing extends SemanticRule("GivenAndUsing") {
}

private def onlyImplicitOrUsingParams(d: Defn.Def): Boolean =
d.paramClauseGroups.forall(_.paramClauses.forall(_.mod.exists(m => m.is[Mod.Implicit] || m.is[Mod.Using])))
d.paramClauseGroups.forall(_.paramClauses.forall(_.mod.exists {
case ImplicitOrUsingMod(_) => true
case _ => false
}))

private def replaceWithUsing(paramss: List[List[Term.Param]])(implicit doc: SemanticDocument): List[APatch] = {
val usingPatch = paramss.reverse.flatten
.collectFirst {
case p: Term.Param if p.mods.exists(mod => mod.is[Mod.Implicit] || mod.is[Mod.Using]) =>
val pp = p.mods
.find(_.is[Mod.Implicit])
.toList
.flatMap(_.tokens)
.headOption
.collectFirst { case p @ ImplicitOrUsingMod(mod) =>
if (mod.is[Mod.Using])
APatch.Empty
else {
val pp = mod.tokens.headOption
.map(t => Patch.replaceToken(t, "using"))
.asPatch
APatch.Using(pp, p.symbol.owner)
}
}
val lintPatchers = paramss.flatMap(_.collectFirst {
case p: Term.Param if p.mods.exists(mod => mod.is[Mod.Implicit] || mod.is[Mod.Using]) =>
p.mods.find(mod => mod.is[Mod.Implicit] || mod.is[Mod.Using])
}.toList.flatten) match {
val lintPatchers = paramss.flatMap(_.collectFirst { case ImplicitOrUsingMod(mod) => mod }.toList) match {
case Nil => List.empty
case _ :: Nil => List.empty
case other =>
Expand Down
27 changes: 27 additions & 0 deletions rules/src/main/scala/fix/matchers/ImplicitOrUsingMod.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2022 Arktekk
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package fix.matchers

import scala.meta._

object ImplicitOrUsingMod {
def unapply(mod: Mod): Option[Mod] =
if (mod.is[Mod.Implicit] || mod.is[Mod.Using]) Some(mod) else None

def unapply(param: Term.Param): Option[Mod] =
param.mods.collectFirst { case ImplicitOrUsingMod(mod) => mod }
}

0 comments on commit 40a830a

Please sign in to comment.