Skip to content

Commit

Permalink
Merge pull request #41 from custommonkey/feature/map-to-as
Browse files Browse the repository at this point in the history
Add rules to convert .map(_ => f) to .as(f) and .map(_ => ()) to .void
  • Loading branch information
DavidGregory084 authored Jul 18, 2022
2 parents 552447c + 5d9cd00 commit cdd4800
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
27 changes: 27 additions & 0 deletions modules/cats/input/src/main/scala/fix/AsTests.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
rule = TypelevelAs
*/
package fix

object AsTests {

def listMapLit = {
List(1, 2, 3).map(_ => 1) /* assert: TypelevelAs.as
^^^^^^^^^^^^^^^^^^^^^^^^^
.map(_ => f) can be replaced by .as(f) */
}

def listMapUnit = {
List(1, 2, 3).map(_ => ()) /* assert: TypelevelAs.as
^^^^^^^^^^^^^^^^^^^^^^^^^^
.map(_ => ()) can be replaced by .void */
}

def shouldBeIgnored = {
def f = "a"
List(1, 2, 3).map(_ => f)
List(1, 2, 3).map(i => i)
List(1, 2, 3).map(println(_))
}

}
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
org.typelevel.fix.MapSequence
org.typelevel.fix.UnusedShowInterpolator
org.typelevel.fix.As
54 changes: 54 additions & 0 deletions modules/cats/rules/src/main/scala/org/typelevel/fix/As.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2022 Typelevel
*
* 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 org.typelevel.fix

import scalafix.v1._
import scala.meta._

class As extends SemanticRule("TypelevelAs") {
override def fix(implicit doc: SemanticDocument): Patch =
doc.tree.collect {
// fa.map(_ => ())
case tree @ AnonymousMap(Lit.Unit()) => Patch.lint(VoidDiagnostic(tree))
// fa.map(_ => 1)
case tree @ AnonymousMap(_: Lit) => Patch.lint(AsDiagnostic(tree))
}.asPatch

}

object AnonymousMap {
def unapply(term: Term): Option[Term] = term match {
case Term.Apply(
Term.Select(_, Term.Name("map")),
Term.Function(List(Term.Param(_, Name.Anonymous(), _, _)), returnType) :: Nil
) =>
Some(returnType)
case _ => None
}
}

final case class AsDiagnostic(t: Tree) extends Diagnostic {
override def message: String = ".map(_ => f) can be replaced by .as(f)"
override def position: Position = t.pos
override def categoryID: String = "as"
}

final case class VoidDiagnostic(t: Tree) extends Diagnostic {
override def message: String = ".map(_ => ()) can be replaced by .void"
override def position: Position = t.pos
override def categoryID: String = "as"
}

0 comments on commit cdd4800

Please sign in to comment.