File tree Expand file tree Collapse file tree 6 files changed +93
-0
lines changed
resources/META-INF/services
scala/scalafix/internal/rule
input/src/main/scala/test/redundantSyntax
output/src/main/scala/test/redundantSyntax Expand file tree Collapse file tree 6 files changed +93
-0
lines changed Original file line number Diff line number Diff line change 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.
Original file line number Diff line number Diff line change @@ -3,5 +3,6 @@ scalafix.internal.rule.ExplicitResultTypes
33scalafix.internal.rule.NoAutoTupling
44scalafix.internal.rule.NoValInForComprehension
55scalafix.internal.rule.ProcedureSyntax
6+ scalafix.internal.rule.RedundantSyntax
67scalafix.internal.rule.RemoveUnused
78scalafix.internal.rule.LeakingImplicitClassVal
Original file line number Diff line number Diff line change 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 number Diff line number Diff line change 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 number Diff line number Diff line change 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 number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments