Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Upgrade Scalastyle to 1.1.0. #342

Merged
merged 1 commit into from Jan 6, 2020
Merged

Upgrade Scalastyle to 1.1.0. #342

merged 1 commit into from Jan 6, 2020

Conversation

mwz
Copy link
Member

@mwz mwz commented Jan 6, 2020

I finally found some time to fork and publish a new version of Scalastyle.
See the release notes and the following thread for more details.

This PR updates Scalastyle to the new version. Here are the changes as far as the rules repository and the quality profiles in SonarQube are concerned:

New Scalastyle rules:

  • New ForLoopChecker rule - omit braces if you have a yield clause, otherwise, surround the contents with curly-braces, even if the contents are only a single line.
  • New WhileBraceChecker - rule - it's recommended to never omit braces when using while.
  • New CaseBraceChecker rule - braces aren't required in case clauses.

Scalastyle rule changes:

  • HeaderMatchesChecker has new parameter regex to indicate whether to treat the header string as a regular expression; defaults to false
  • MethodLengthChecker has new parameter ignoreEmpty to exclude empty lines from being counted; defaults to false
  • ForBraceChecker has a new parameter singleLineAllowed to indicate whether a one-line for expressions with parentheses are allowed.
  • NonASCIICharacterChecker has a new parameter allowStringLiterals to indicate whether non-ASCII scripts in string literals should be allowed.

It's getting difficult to keep track of the count of how many templates, non-template rules and template instances there are, so this diff of the generated inspections file between 1.0 and 1.1 was really useful in figuring that out.

--- /Users/michaelwizner/dev/sonar-scala/inspections-1.0.scala	2020-01-06 01:44:52.000000000 +0000
+++ /Users/michaelwizner/dev/sonar-scala/inspections-1.1.scala	2020-01-06 02:03:21.000000000 +0000
@@ -60,6 +60,13 @@
           label = "Header",
           description = "The lines to compare against",
           default = ""
+        ),
+        Param(
+          name = "regex",
+          typ = BooleanType,
+          label = "Header Regex",
+          description = "Whether to treat the header string as a regular expression",
+          default = "false"
         )
       )
     ),
@@ -560,6 +567,13 @@
           label = "Ignore comments",
           description = "If set to true, comment lines in method body won't be counted",
           default = "false"
+        ),
+        Param(
+          name = "ignoreEmpty",
+          typ = BooleanType,
+          label = "Ignore empty lines",
+          description = "If set to true, empty lines in method body won't be counted",
+          default = "false"
         )
       )
     ),
@@ -923,7 +937,9 @@
       id = "for.brace",
       label = "Use braces in for comprehensions",
       description = "Checks that braces are used in for comprehensions",
-      extraDescription = None,
+      extraDescription = Some("""The singleLineAllowed property allows for constructions of the type:
+
+    for (i <- List(1,2,3)) yield i"""),
       justification = Some(
         """Usage of braces (rather than parentheses) within a for comprehension mean that you don't have to specify a semi-colon at the end of every line:
 
@@ -942,6 +958,40 @@
   To fix it, replace the () with {}. And then remove the ; at the end of the lines."""
       ),
       defaultLevel = WarningLevel,
+      params = List(
+        Param(
+          name = "singleLineAllowed",
+          typ = BooleanType,
+          label = "Allow parentheses for single-line for",
+          description = "For with parentheses allowed if everything is on one line",
+          default = "false"
+        )
+      )
+    ),
+    ScalastyleInspection(
+      clazz = "org.scalastyle.scalariform.ForLoopChecker",
+      id = "for.loop",
+      label = "Use parentheses in for loops",
+      description = "Checks that parentheses are used in for loops",
+      extraDescription = None,
+      justification = Some(
+        """For-comprehensions which lack a yield clause is actually a loop rather than a functional comprehension and it is usually
+   more readable to string the generators together between parentheses rather than using the syntactically-confusing } { construct:
+
+   for (x <- board.rows; y <- board.files) {
+     printf("(%d, %d)", x, y)
+   }
+
+   is preferred to
+
+   for {
+     x <- board.rows
+     y <- board.files
+   } {
+     printf("(%d, %d)", x, y)
+   }"""
+      ),
+      defaultLevel = WarningLevel,
       params = List()
     ),
     ScalastyleInspection(
@@ -1056,30 +1106,39 @@
     ScalastyleInspection(
       clazz = "org.scalastyle.scalariform.NonASCIICharacterChecker",
       id = "non.ascii.character.disallowed",
-      label = "Non ascii characters are not allowed",
-      description = "Some editors are unfriendly to non ascii characters.",
+      label = "Non ASCII characters are not allowed",
+      description = "Some editors are unfriendly to non ASCII characters.",
       extraDescription = None,
       justification = Some(
         """Scala allows unicode characters as operators and some editors misbehave when they see non-ascii character.
-            In a project collaborated by a community of developers. This check can be helpful in such situations.
+    In a project collaborated by a community of developers. This check can be helpful in such situations.
 
 
-            "value".match {
-            case "value" => println("matched")
-            ...
-            }
+    "value".match {
+    case "value" => println("matched")
+    ...
+    }
 
-            is preferred to
+    is preferred to
 
-            "value".match {
-            case "value" ‚áí println("matched")
-            ...
-            }
+    "value".match {
+    case "value" ‚áí println("matched")
+    ...
+    }
 
-            To fix it, replace the (unicode operator)‚áí with =>."""
+    To fix it, replace the (unicode operator)‚áí with =>."""
       ),
       defaultLevel = WarningLevel,
-      params = List()
+      params = List(
+        Param(
+          name = "allowStringLiterals",
+          typ = BooleanType,
+          label = "Allow non-ASCII scripts in string literals.",
+          description =
+            "White lists Unicode characters recognized by `\\p'{'Alnum'}'\\p'{'Punct'}'\\p'{'Sc'}'\\p'{'Space'}'` but not symbols like Emoji.",
+          default = "false"
+        )
+      )
     ),
     ScalastyleInspection(
       clazz = "org.scalastyle.file.IndentationChecker",
@@ -1230,6 +1289,30 @@
           default = "^set.+$"
         )
       )
+    ),
+    ScalastyleInspection(
+      clazz = "org.scalastyle.scalariform.WhileBraceChecker",
+      id = "while.brace",
+      label = "While body should have braces",
+      description = "Checks that while body have braces",
+      extraDescription = None,
+      justification = Some(
+        "While cannot be used in a pure-functional manner, that's why it's recommended to never omit braces according to Scala Style Guide."
+      ),
+      defaultLevel = WarningLevel,
+      params = List()
+    ),
+    ScalastyleInspection(
+      clazz = "org.scalastyle.scalariform.CaseBraceChecker",
+      id = "disallow.case.brace",
+      label = "Omit braces in case clauses",
+      description = "Checks that braces aren't used in case clauses",
+      extraDescription = None,
+      justification = Some(
+        "Braces aren't required in case clauses. They should be omitted according to Scala Style Guide."
+      ),
+      defaultLevel = WarningLevel,
+      params = List()
     )
   )
   val AllInspectionsByClass: Map[String, ScalastyleInspection] = AllInspections.map(i => i.clazz -> i).toMap

Closes #16.

Copy link
Contributor

@BalmungSan BalmungSan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome!

@mwz mwz merged commit e054d43 into master Jan 6, 2020
@mwz mwz deleted the update-scalastyle branch January 6, 2020 23:29
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rule for ProcedureDefinitionChecker not found in Scalastyle repository
2 participants