Skip to content

Commit

Permalink
Trying out extracting return type and checking for whitespaces.
Browse files Browse the repository at this point in the history
  • Loading branch information
ScrapCodes committed Mar 31, 2014
1 parent 8196dd6 commit fb9476e
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright (C) 2011-2012 the original author or authors.
// See the LICENCE.txt file distributed with this work for additional
// information regarding copyright ownership.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package org.scalastyle.scalariform

import _root_.scalariform.parser.{CompilationUnit, FullDefOrDcl, FunDefOrDcl, PatDefOrDcl}
import org.scalastyle._
import org.scalastyle.scalariform.VisitorHelper._

class ExplicitReturnTypeFormatChecker extends ScalariformChecker {
override protected val errorKey: String = "explicit.return.type.format"

override def verify(ast: CompilationUnit): List[ScalastyleError] = {
localvisit(ast).map {
case FullDefOrDclVisit(Left(p), _) => p.typedOpt
case FullDefOrDclVisit(Right(f), _) => f.returnTypeOpt
}.flatten.map(x => println("<" + x._1.associatedWhitespaceAndComments.text + ">")) // It is not possible to get these whitespaces.
List(PositionError(0))
}

case class FullDefOrDclVisit(defOrDcl: Either[PatDefOrDcl, FunDefOrDcl],
subs: List[FullDefOrDclVisit]) extends Clazz[FullDefOrDcl]()

private def localvisit(ast: Any): List[FullDefOrDclVisit] = ast match {
case t: FullDefOrDcl => {
t.defOrDcl match {
case f: FunDefOrDcl => List(FullDefOrDclVisit(Right(f), localvisit(f)))
case p: PatDefOrDcl => List(FullDefOrDclVisit(Left(p), localvisit(p)))
case _ => localvisit(t.defOrDcl)
}
}
case t: FunDefOrDcl => localvisit(t.funBodyOpt)
case t: Any => visit(t, localvisit)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright (C) 2011-2012 the original author or authors.
// See the LICENCE.txt file distributed with this work for additional
// information regarding copyright ownership.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package org.scalastyle.scalariform

import org.scalastyle.file.CheckerTest
import org.scalatest.junit.AssertionsForJUnit
import org.junit.Test

class ExplicitReturnTypeFormatCheckerTest extends AssertionsForJUnit with CheckerTest {
override protected val key: String = "explicit.return.type.format"
override protected val classUnderTest = classOf[ExplicitReturnTypeFormatChecker]

@Test def testKO(): Unit = {
val source =
"""
|package foobar
|
|class Dummy {
| val a: Int = 0
| def b: Int = 1
| var c: Int = 2
|}
|
""".stripMargin

assertErrors(List(columnError(1, 0)), source)
}

}

0 comments on commit fb9476e

Please sign in to comment.