Skip to content

Commit e7d4a20

Browse files
authored
Merge pull request #1496 from bplommer/final-object
Add rewriting rule RedundantSyntax.finalObject
2 parents 96cc57a + 3f6ebeb commit e7d4a20

File tree

6 files changed

+93
-0
lines changed

6 files changed

+93
-0
lines changed

docs/rules/RedundantSyntax.md

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
layout: docs
3+
id: RedundantSyntax
4+
title: RedundantSyntax
5+
---
6+
7+
This rule removes redundant syntax. Currently the only syntax it removes is the `final` keyword on an `object`.
8+
9+
Example:
10+
11+
```diff
12+
- final object foo
13+
+ object Foo
14+
```
15+
16+
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.

scalafix-rules/src/main/resources/META-INF/services/scalafix.v1.Rule

+1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ scalafix.internal.rule.ExplicitResultTypes
33
scalafix.internal.rule.NoAutoTupling
44
scalafix.internal.rule.NoValInForComprehension
55
scalafix.internal.rule.ProcedureSyntax
6+
scalafix.internal.rule.RedundantSyntax
67
scalafix.internal.rule.RemoveUnused
78
scalafix.internal.rule.LeakingImplicitClassVal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package scalafix.internal.rule
2+
3+
import scala.meta._
4+
5+
import metaconfig.Configured
6+
import scalafix.util.TokenList
7+
import scalafix.v1._
8+
9+
class RedundantSyntax(config: RedundantSyntaxConfig)
10+
extends SyntacticRule("RedundantSyntax") {
11+
def this() = this(RedundantSyntaxConfig())
12+
override def withConfiguration(config: Configuration): Configured[Rule] =
13+
config.conf
14+
.getOrElse("redundantSyntax", "RedundantSyntax")(
15+
RedundantSyntaxConfig.default
16+
)
17+
.map(new RedundantSyntax(_))
18+
19+
override def description: String =
20+
"Removes redundant syntax such as `final` modifiers on an object"
21+
override def isRewrite: Boolean = true
22+
23+
override def fix(implicit doc: SyntacticDocument): Patch =
24+
doc.tree.collect {
25+
case o: Defn.Object
26+
if config.finalObject && o.mods.exists(_.is[Mod.Final]) =>
27+
Patch.removeTokens {
28+
o.tokens.find(_.is[Token.KwFinal]).toIterable.flatMap { finalTok =>
29+
finalTok :: TokenList(o.tokens).trailingSpaces(finalTok).toList
30+
}
31+
}
32+
}.asPatch
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package scalafix.internal.rule
2+
3+
import metaconfig._
4+
import metaconfig.annotation._
5+
import metaconfig.generic.Surface
6+
7+
final case class RedundantSyntaxConfig(
8+
@Description("Remove final modifier from objects")
9+
finalObject: Boolean = true
10+
)
11+
12+
object RedundantSyntaxConfig {
13+
val default: RedundantSyntaxConfig = RedundantSyntaxConfig()
14+
implicit val reader: ConfDecoder[RedundantSyntaxConfig] =
15+
generic.deriveDecoder[RedundantSyntaxConfig](default)
16+
implicit val surface: Surface[RedundantSyntaxConfig] =
17+
generic.deriveSurface[RedundantSyntaxConfig]
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
rules = RedundantSyntax
3+
RedundantSyntax.finalObject = true
4+
*/
5+
6+
package test.redundantSyntax
7+
8+
final object FinalObject {
9+
final object Foo
10+
private final case object Bar
11+
}
12+
13+
abstract class Class {
14+
final def bar: String = "bar"
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package test.redundantSyntax
2+
3+
object FinalObject {
4+
object Foo
5+
private case object Bar
6+
}
7+
8+
abstract class Class {
9+
final def bar: String = "bar"
10+
}

0 commit comments

Comments
 (0)