Skip to content

Commit

Permalink
Fixes #11 does not handle package objects
Browse files Browse the repository at this point in the history
Added package object names, default [a-z][A-Za-z]*
Changed object names to ignore package objects
Corrected handling of scalariform errors

Also better management of default values for regexps
  • Loading branch information
matthewfarwell committed Apr 3, 2012
1 parent dacea88 commit 88b45ae
Show file tree
Hide file tree
Showing 11 changed files with 114 additions and 31 deletions.
9 changes: 7 additions & 2 deletions src/main/resources/scalastyle_definition.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,17 @@
</checker>
<checker class="org.scalastyle.scalariform.ClassNamesChecker" id="class.name" defaultLevel="warning">
<parameters>
<parameter name="regex" type="string" default="[A-Z][A-Za-z]*" />
<parameter name="regex" type="string" default="^[A-Z][A-Za-z]*$" />
</parameters>
</checker>
<checker class="org.scalastyle.scalariform.ObjectNamesChecker" id="object.name" defaultLevel="warning">
<parameters>
<parameter name="regex" type="string" default="[A-Z][A-Za-z]*" />
<parameter name="regex" type="string" default="^[A-Z][A-Za-z]*$" />
</parameters>
</checker>
<checker class="org.scalastyle.scalariform.PackageObjectNamesChecker" id="package.object.name" defaultLevel="warning">
<parameters>
<parameter name="regex" type="string" default="^[a-z][A-Za-z]*$" />
</parameters>
</checker>
<checker class="org.scalastyle.scalariform.EqualsHashCodeChecker" id="equals.hash.code" defaultLevel="warning"/>
Expand Down
6 changes: 6 additions & 0 deletions src/main/resources/scalastyle_messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,9 @@ structural.type.message = Avoid using structural types
structural.type.label = Structural type
structural.type.description = Check that structural types are not used.

package.object.name.message = Package object name does not match the regular expression ''{0}''
package.object.name.label = Package object name
package.object.name.description = Check that package object names match a regular expression
package.object.name.regex.label = Regular expression
package.object.name.regex.description = The package object names must match this regular expression

9 changes: 2 additions & 7 deletions src/main/scala/org/scalastyle/Checker.scala
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,9 @@ object Checker {
type CheckerClass = Class[_ <: Checker[_]]

def parseScalariform(source: String): Option[ScalariformAst] = {
try {
val (hiddenTokenInfo, tokens) = ScalaLexer.tokeniseFull(source, true)
val (hiddenTokenInfo, tokens) = ScalaLexer.tokeniseFull(source, true)

Some(ScalariformAst(new ScalaParser(tokens.toArray).compilationUnitOrScript(), hiddenTokenInfo))
} catch {
// TODO improve error logging here
case e: Exception => None
}
Some(ScalariformAst(new ScalaParser(tokens.toArray).compilationUnitOrScript(), hiddenTokenInfo))
}

def parseLines(source: String): Lines = Lines(source.split("\n").scanLeft(Line("", 0, 0)) {
Expand Down
28 changes: 24 additions & 4 deletions src/main/scala/org/scalastyle/scalariform/ClassNamesChecker.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import org.scalastyle._
import org.scalastyle.FileSpec

class ClassNamesChecker extends ScalariformChecker {
val DefaultRegex = "[A-Z][A-Za-z]*"
val DefaultRegex = "^[A-Z][A-Za-z]*$"
val errorKey = "class.name"

def verify(ast: CompilationUnit): List[ScalastyleError] = {
Expand All @@ -43,16 +43,36 @@ class ClassNamesChecker extends ScalariformChecker {
}

class ObjectNamesChecker extends ScalariformChecker {
val DefaultRegex = "[A-Z][A-Za-z]*"
val DefaultRegex = "^[A-Z][A-Za-z]*$"
val errorKey = "object.name"

def verify(ast: CompilationUnit): List[PositionError] = {
val regexString = getString("regex", DefaultRegex)
val regex = regexString.r

val it = for (
List(left, right) <- ast.tokens.sliding(2);
if (left.tokenType == OBJECT && (regex findAllIn (right.getText)).size == 0)
List(left, middle, right) <- ast.tokens.sliding(3);
if (left.tokenType != PACKAGE && middle.tokenType == OBJECT && (regex findAllIn (right.getText)).size == 0)
) yield {
PositionError(right.startIndex, List(regexString))
}

it.toList
}
}


class PackageObjectNamesChecker extends ScalariformChecker {
val DefaultRegex = "^[a-z][A-Za-z]*$"
val errorKey = "package.object.name"

def verify(ast: CompilationUnit): List[PositionError] = {
val regexString = getString("regex", DefaultRegex)
val regex = regexString.r

val it = for (
List(left, middle, right) <- ast.tokens.sliding(3);
if (left.tokenType == PACKAGE && middle.tokenType == OBJECT && (regex findAllIn (right.getText)).size == 0)
) yield {
PositionError(right.startIndex, List(regexString))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class FileLengthCheckerTest extends AssertionsForJUnit with CheckerTest {
@Test def testZero() = {
val source = """
package foobar
import foobar
object Foobar {
}
""";
Expand All @@ -46,7 +46,7 @@ import foobar
@Test def testOne() = {
val source = """
package foobar
import foobar
object Foobar {
}
object Barbar {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class FileLineLengthCheckerTest extends AssertionsForJUnit with CheckerTest {
@Test def testNoMax() = {
val source = """
package foobar
import foobar
object Foobar {
}
""";
Expand All @@ -48,7 +48,7 @@ import foobar
@Test def testWithOneMax() = {
val source = """
package foobar
import foobar
object Foobar {
}
""";
Expand All @@ -59,7 +59,7 @@ import foobar
@Test def testWithTwoMax() = {
val source = """
package foobar
import foobar
object Foobar {
}
object Barbar {
Expand All @@ -73,7 +73,7 @@ import foobar
val source = """
package foobar
import# #foo
import# #java.lang._
object Barbar {
}
""".replaceAll("#","\t");
Expand Down
6 changes: 3 additions & 3 deletions src/test/scala/org/scalastyle/file/FileTabCheckerTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class FileTabCheckerTest extends AssertionsForJUnit with CheckerTest {
@Test def testZero() = {
val source = """
package foobar
import foobar
object Foobar {
}
""";
Expand All @@ -48,7 +48,7 @@ import foobar
@Test def testOne() = {
val source = """
package foobar
import foobar
#object Foobar {
}
""".replaceAll("#", "\t");
Expand All @@ -59,7 +59,7 @@ import foobar
@Test def testTwo() = {
val source = """
package foobar
import foobar
#object Foobar {
}
#object Barbar {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class HeaderMatchesCheckerTest extends AssertionsForJUnit with CheckerTest {
@Test def testOK() = {
val source = licence + """
package foobar
import foobar
object Foobar {
}
""";
Expand All @@ -66,7 +66,7 @@ import foobar
@Test def testKO() = {
val source = licence.replaceAll("BASIS,", "XXX") + """
package foobar
import foobar
object Foobar {
}
""";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class WhitespaceEndOfLineCheckerTest extends AssertionsForJUnit with CheckerTest
@Test def testZero() = {
val source = """
package foobar
import foobar
object Foobar {
}
""";
Expand All @@ -47,7 +47,7 @@ object Foobar {
@Test def testOne() = {
val source = """
package foobar##
import foobar
object Foobar {
}
""".replaceAll("#", " ");
Expand All @@ -58,7 +58,7 @@ object Foobar {
@Test def testTwo() = {
val source = """
package foobar~
import foobar#
class foobar#
object Foobar {
}
""".replaceAll("~", " ").replaceAll("#", "\t");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class foobar {
}
""";

assertErrors(List(columnError(4, 6, List("[A-Z][A-Za-z]*")), columnError(5, 8, List("[A-Z][A-Za-z]*"))), source)
assertErrors(List(columnError(4, 6, List("^[A-Z][A-Za-z]*$")), columnError(5, 8, List("^[A-Z][A-Za-z]*$"))), source)
}
}

Expand All @@ -66,7 +66,7 @@ class ObjectNamesCheckerTest extends AssertionsForJUnit with CheckerTest {
val source = """
package foobar
class Foobar {
object Foobar {
val foo = 1
}
""";
Expand All @@ -84,6 +84,63 @@ object foobar {
}
""";

assertErrors(List(columnError(4, 7, List("[A-Z][A-Za-z]*")), columnError(5, 9, List("[A-Z][A-Za-z]*"))), source)
assertErrors(List(columnError(4, 7, List("^[A-Z][A-Za-z]*$")), columnError(5, 9, List("^[A-Z][A-Za-z]*$"))), source)
}

@Test def testPackageObject() = {
val source = """
package foobar
package object foobar {
object barbar {
}
}
""";

assertErrors(List(columnError(5, 9, List("^[A-Z][A-Za-z]*$"))), source)
}
}


class PackageObjectNamesCheckerTest extends AssertionsForJUnit with CheckerTest {
val key = "package.object.name"
val classUnderTest = classOf[PackageObjectNamesChecker]

@Test def testZero() = {
val source = """
package foobar
package object foobar {
val foo = 1
}
""";

assertErrors(List(), source)
}

@Test def testOne() = {
val source = """
package foobar
package object Foobar {
}
package object Barbar {
}
""";

assertErrors(List(columnError(4, 15, List("^[a-z][A-Za-z]*$")), columnError(6, 15, List("^[a-z][A-Za-z]*$"))), source)
}

@Test def testPackageObject() = {
val source = """
package foobar
object foobar {
object barbar {
}
}
""";

assertErrors(List(), source)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,6 @@ class foobar {
}
""";

assertErrors(List(columnError(4, 6, List("[A-Z][A-Za-z]*")), columnError(14, 8, List("[A-Z][A-Za-z]*"))), source)
assertErrors(List(columnError(4, 6, List("^[A-Z][A-Za-z]*$")), columnError(14, 8, List("^[A-Z][A-Za-z]*$"))), source)
}
}

0 comments on commit 88b45ae

Please sign in to comment.