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 TypelevelEitherSyntax Rule #182

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
10 changes: 7 additions & 3 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ ThisBuild / developers ++= List(
tlGitHubDev("DavidGregory084", "David Gregory")
)

ThisBuild / semanticdbEnabled := true
ThisBuild / semanticdbVersion := scalafixSemanticdb.revision
ThisBuild / scalafixScalaBinaryVersion := CrossVersion.binaryScalaVersion(scalaVersion.value)
ThisBuild / semanticdbEnabled := true
ThisBuild / semanticdbVersion := scalafixSemanticdb.revision

lazy val `typelevel-scalafix` = project
.in(file("."))
Expand All @@ -39,6 +38,11 @@ lazy val cats = scalafixProject("cats")
"org.typelevel" %% "cats-core" % CatsVersion
)
)
.outputSettings(
libraryDependencies ++= Seq(
"org.typelevel" %% "cats-core" % CatsVersion
)
)

// typelevel/cats-effect Scalafix rules
lazy val catsEffect = scalafixProject("cats-effect")
Expand Down
16 changes: 16 additions & 0 deletions modules/cats/input/src/main/scala/fix/EitherSyntax.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
rule = TypelevelEitherSyntax
*/
package fix

object EitherSyntax {

def shouldBeReplaced = {
Right(())
}

def shouldBeIgnored = {
Right(1)
}

}
14 changes: 14 additions & 0 deletions modules/cats/output/src/main/scala/fix/EitherSyntax.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package fix

import cats.syntax.either._
object EitherSyntax {

def shouldBeReplaced = {
Either.unit
}

def shouldBeIgnored = {
Right(1)
}

}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
org.typelevel.fix.MapSequence
org.typelevel.fix.UnusedShowInterpolator
org.typelevel.fix.As
org.typelevel.fix.EitherSyntax
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* 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._
import scala.collection.mutable.ListBuffer

class EitherSyntax extends SemanticRule("TypelevelEitherSyntax") {

override def fix(implicit doc: SemanticDocument): Patch = {
var docContainsImports: Boolean = false
val patches: ListBuffer[Patch] = ListBuffer.empty[Patch]

doc.tree.traverse {
case ImportCatsSyntaxAll(_) =>
docContainsImports = true
case ImportCatsSyntaxEither(_) =>
docContainsImports = true
case expr @ Term.Apply.After_4_6_0(Term.Name("Right"), Term.ArgClause(List(Lit.Unit()), _)) =>
if (!docContainsImports)
patches += Patch.addGlobalImport(
Importer(
Term.Select(
Term.Select(Term.Name("cats"), Term.Name("syntax")),
Term.Name("either")
),
List(Importee.Wildcard())
)
)

patches += Patch.replaceTree(expr, "Either.unit")
}

patches.asPatch
}
}

object ImportCatsSyntaxAll {
def unapply(t: Tree): Option[String] = t match {
case Import(
List(
Importer(
Term.Select(
Term.Select(Term.Name("cats"), Term.Name("syntax")),
Term.Name("all")
),
List(Importee.Wildcard())
)
)
) =>
Some("cats.syntax.all._")
case _ => None
}
}

object ImportCatsSyntaxEither {
def unapply(t: Tree): Option[String] = t match {
case Import(
List(
Importer(
Term.Select(
Term.Select(Term.Name("cats"), Term.Name("syntax")),
Term.Name("either")
),
importee
)
)
) =>
if (
importee.exists {
case Importee.Wildcard() => true
case _ => false
}
) Some("cats.syntax.either._")
else if (
importee.exists {
case Importee.Name(Name.Indeterminate("catsSyntaxEitherObject")) => true
case _ => false
}
) Some("cats.syntax.either.catsSyntaxEitherObject")
else None
case _ => None
}
}