-
Notifications
You must be signed in to change notification settings - Fork 185
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 #1496 from bplommer/final-object
Add rewriting rule RedundantSyntax.finalObject
- Loading branch information
Showing
6 changed files
with
93 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
--- | ||
layout: docs | ||
id: RedundantSyntax | ||
title: RedundantSyntax | ||
--- | ||
|
||
This rule removes redundant syntax. Currently the only syntax it removes is the `final` keyword on an `object`. | ||
|
||
Example: | ||
|
||
```diff | ||
- final object foo | ||
+ object Foo | ||
``` | ||
|
||
Note: in Scala 2.12 and earlier removing the `final` modifier will slightly change the resulting bytecode - see [this bug ticket](https://github.com/scala/bug/issues/11094) for further information. |
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
33 changes: 33 additions & 0 deletions
33
scalafix-rules/src/main/scala/scalafix/internal/rule/RedundantSyntax.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,33 @@ | ||
package scalafix.internal.rule | ||
|
||
import scala.meta._ | ||
|
||
import metaconfig.Configured | ||
import scalafix.util.TokenList | ||
import scalafix.v1._ | ||
|
||
class RedundantSyntax(config: RedundantSyntaxConfig) | ||
extends SyntacticRule("RedundantSyntax") { | ||
def this() = this(RedundantSyntaxConfig()) | ||
override def withConfiguration(config: Configuration): Configured[Rule] = | ||
config.conf | ||
.getOrElse("redundantSyntax", "RedundantSyntax")( | ||
RedundantSyntaxConfig.default | ||
) | ||
.map(new RedundantSyntax(_)) | ||
|
||
override def description: String = | ||
"Removes redundant syntax such as `final` modifiers on an object" | ||
override def isRewrite: Boolean = true | ||
|
||
override def fix(implicit doc: SyntacticDocument): Patch = | ||
doc.tree.collect { | ||
case o: Defn.Object | ||
if config.finalObject && o.mods.exists(_.is[Mod.Final]) => | ||
Patch.removeTokens { | ||
o.tokens.find(_.is[Token.KwFinal]).toIterable.flatMap { finalTok => | ||
finalTok :: TokenList(o.tokens).trailingSpaces(finalTok).toList | ||
} | ||
} | ||
}.asPatch | ||
} |
18 changes: 18 additions & 0 deletions
18
scalafix-rules/src/main/scala/scalafix/internal/rule/RedundantSyntaxConfig.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,18 @@ | ||
package scalafix.internal.rule | ||
|
||
import metaconfig._ | ||
import metaconfig.annotation._ | ||
import metaconfig.generic.Surface | ||
|
||
final case class RedundantSyntaxConfig( | ||
@Description("Remove final modifier from objects") | ||
finalObject: Boolean = true | ||
) | ||
|
||
object RedundantSyntaxConfig { | ||
val default: RedundantSyntaxConfig = RedundantSyntaxConfig() | ||
implicit val reader: ConfDecoder[RedundantSyntaxConfig] = | ||
generic.deriveDecoder[RedundantSyntaxConfig](default) | ||
implicit val surface: Surface[RedundantSyntaxConfig] = | ||
generic.deriveSurface[RedundantSyntaxConfig] | ||
} |
15 changes: 15 additions & 0 deletions
15
scalafix-tests/input/src/main/scala/test/redundantSyntax/FinalObject.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,15 @@ | ||
/* | ||
rules = RedundantSyntax | ||
RedundantSyntax.finalObject = true | ||
*/ | ||
|
||
package test.redundantSyntax | ||
|
||
final object FinalObject { | ||
final object Foo | ||
private final case object Bar | ||
} | ||
|
||
abstract class Class { | ||
final def bar: String = "bar" | ||
} |
10 changes: 10 additions & 0 deletions
10
scalafix-tests/output/src/main/scala/test/redundantSyntax/FinalObject.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,10 @@ | ||
package test.redundantSyntax | ||
|
||
object FinalObject { | ||
object Foo | ||
private case object Bar | ||
} | ||
|
||
abstract class Class { | ||
final def bar: String = "bar" | ||
} |