-
Notifications
You must be signed in to change notification settings - Fork 789
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5536 from bplommer/lint-fs2-compiler
Add scalafix linter for use of fs2 Sync compiler
- Loading branch information
Showing
7 changed files
with
67 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,12 @@ | ||
rules = [ | ||
Http4sFs2Linters | ||
Http4sGeneralLinters | ||
Http4sUseLiteralsSyntax | ||
OrganizeImports | ||
] | ||
|
||
triggered.rules = [ | ||
Http4sFs2Linters | ||
Http4sGeneralLinters | ||
Http4sUseLiteralsSyntax | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
scalafix-internal/input/src/main/scala/fix/Fs2SyncCompilerTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/* | ||
rule = Http4sFs2Linters | ||
*/ | ||
|
||
package fix | ||
|
||
import cats.effect._ | ||
import fs2._ | ||
|
||
object Fs2SyncCompilerTest { | ||
def usesSyncInnocently[F[_]](implicit F: Sync[F]) = F.delay(println("hi")) | ||
def usesSyncCompiler[F[_]](implicit F: Sync[F]) = Stream(1, 2, 3).covary[F].compile.drain // assert: Http4sFs2Linters.noFs2SyncCompiler | ||
def usesConcurrentCompiler[F[_]](implicit F: Concurrent[F]) = Stream(1, 2, 3).covary[F].compile.drain | ||
} |
3 changes: 2 additions & 1 deletion
3
scalafix-internal/rules/src/main/resources/META-INF/services/scalafix.v1.Rule
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
fix.GeneralLinters | ||
fix.UseLiteralsSyntax | ||
fix.UseLiteralsSyntax | ||
fix.Fs2Linters |
44 changes: 44 additions & 0 deletions
44
scalafix-internal/rules/src/main/scala-2/fix/Fs2Linters.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright 2021 http4s.org | ||
* | ||
* 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 | ||
|
||
import scalafix.v1._ | ||
|
||
import scala.meta._ | ||
|
||
class Fs2Linters extends SemanticRule("Http4sFs2Linters") { | ||
override def fix(implicit doc: SemanticDocument): Patch = | ||
doc.tree.collect { case Stream_compile_M(t @ Term.Select(_, _)) => | ||
t.synthetics.collect { case ApplyTree(_, List(ApplyTree(_, List(target)))) => | ||
target.symbol.collect { case Target_forSync_M(_) => | ||
Patch.lint(NoFs2SyncCompiler(t)) | ||
}.asPatch | ||
}.asPatch | ||
}.asPatch | ||
|
||
val Stream_compile_M = SymbolMatcher.exact("fs2/Stream#compile().") | ||
val Target_forSync_M = SymbolMatcher.exact("fs2/Compiler.TargetLowPriority#forSync().") | ||
} | ||
|
||
final case class NoFs2SyncCompiler(t: Tree) extends Diagnostic { | ||
override def message: String = | ||
"fs2's Sync compiler should be avoided. Usually this means a Sync constraint needs to be changed to Concurrent or upgraded to Async." | ||
|
||
override def position: Position = t.pos | ||
|
||
override def categoryID: String = "noFs2SyncCompiler" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters