Skip to content

Commit

Permalink
Merge pull request #1496 from bplommer/final-object
Browse files Browse the repository at this point in the history
Add rewriting rule RedundantSyntax.finalObject
  • Loading branch information
bjaglin committed Apr 7, 2022
2 parents 96cc57a + 3f6ebeb commit e7d4a20
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 0 deletions.
16 changes: 16 additions & 0 deletions docs/rules/RedundantSyntax.md
@@ -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.
Expand Up @@ -3,5 +3,6 @@ scalafix.internal.rule.ExplicitResultTypes
scalafix.internal.rule.NoAutoTupling
scalafix.internal.rule.NoValInForComprehension
scalafix.internal.rule.ProcedureSyntax
scalafix.internal.rule.RedundantSyntax
scalafix.internal.rule.RemoveUnused
scalafix.internal.rule.LeakingImplicitClassVal
@@ -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
}
@@ -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]
}
@@ -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"
}
@@ -0,0 +1,10 @@
package test.redundantSyntax

object FinalObject {
object Foo
private case object Bar
}

abstract class Class {
final def bar: String = "bar"
}

0 comments on commit e7d4a20

Please sign in to comment.