Skip to content
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

Add rules to convert .map(_ => f) to .as(f) and .map(_ => ()) to .void #41

Merged
merged 4 commits into from
Jul 18, 2022
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
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"
}