Skip to content

Commit

Permalink
Fixes #5 magic number can now comply with v x = 5
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewfarwell committed Jun 4, 2012
1 parent 3f4f977 commit 09e5bae
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 22 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,13 +189,31 @@ No Parameters

## Class org.scalastyle.scalariform.MagicNumberChecker - Checks for use of magic numbers

A simple assignment to a val is not considered to be a magic number, for example:

val foo = 4

is not a magic number, but

var foo = 4

is considered to be a magic number.

* id - magic.number
* default level - WarningLevel

### Parameters

* Ignore (list of numbers to ignore)

### Example

<check level="warning" class="org.scalastyle.scalariform.MagicNumberChecker" enabled="true">
<parameters>
<parameter name="ignore">-1,0,1,2</parameter>
</parameters>
</check>

## Class org.scalastyle.scalariform.NoCloneChecker - Check that classes and objects do not define the clone() method

* id - no.clone
Expand Down
34 changes: 29 additions & 5 deletions src/main/scala/org/scalastyle/scalariform/MagicNumberChecker.scala
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,23 @@ class MagicNumberChecker extends ScalariformChecker {
def verify(ast: CompilationUnit): List[ScalastyleError] = {
val ignores = getString("ignore", DefaultIgnore).split(",").toSet

// println("ast=" + ast)

val it = for (
val intList = for (
t <- localvisit(ast.immediateChildren(0));
f <- traverse(t);
if (matches(f, ignores))
) yield {
PositionError(f.position)
f
}

val valList = for (
t <- localvisitVal(ast.immediateChildren(0));
f <- traverseVal(t);
g <- toOption(f)
) yield {
g
}

it.toList
intList.filter(t => !valList.contains(t.t)).map(t => PositionError(t.position)).toList
}

case class ExprVisit(t: Expr, position: Int, contents: List[ExprVisit]) extends Clazz[Expr]()
Expand Down Expand Up @@ -87,4 +93,22 @@ class MagicNumberChecker extends ScalariformChecker {
case t: Expr => List(ExprVisit(t, t.firstToken.startIndex, localvisit(t.contents)))
case t: Any => visit(t, localvisit)
}

case class PatDefOrDclVisit(t: PatDefOrDcl, valOrVarToken: Token, pattern: List[PatDefOrDclVisit], otherPatterns: List[PatDefOrDclVisit],
equalsClauseOption: List[PatDefOrDclVisit]) extends Clazz[Expr]()

private def localvisitVal(ast: Any): List[PatDefOrDclVisit] = ast match {
case t: PatDefOrDcl => List(PatDefOrDclVisit(t, t.valOrVarToken, localvisitVal(t.pattern),
localvisitVal(t.otherPatterns), localvisitVal(t.equalsClauseOption)))
case t: Any => visit(t, localvisitVal)
}

private def traverseVal(t: PatDefOrDclVisit): List[PatDefOrDclVisit] = t :: t.equalsClauseOption.map(traverseVal(_)).flatten

private def toOption(t: PatDefOrDclVisit): Option[Expr] = {
t.t.equalsClauseOption match {
case Some((equals: Token, expr: Expr)) if (t.t.valOrVarToken.tokenType == VAL && toIntegerLiteralExprElement(expr.contents).isDefined) => Some(expr)
case _ => None
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,23 @@ class MagicNumberCheckerTest extends AssertionsForJUnit with CheckerTest {
val key = "magic.number"
val classUnderTest = classOf[MagicNumberChecker]

// @Test def testVal() = {
// val source = """
//package foobar
//
//class Foobar {
// val foo0 = -2
// val foo1 = -1
// val foo2 = 0
// val foo3 = 1
// val foo4 = 2
// val foo5 = 3
// val foo6 = 4
//}
//""";
//
// assertErrors(List(), source)
// }
@Test def testVal() {
val source = """
package foobar
class Foobar {
val foo0 = -2
val foo1 = -1
val foo2 = 0
val foo3 = 1
val foo4 = 2
val foo5 = 3
val foo6 = 4
}
""";

assertErrors(List(), source)
}

@Test def testVar() {
val source = """
Expand Down

0 comments on commit 09e5bae

Please sign in to comment.