diff --git a/src/main/scala/org/scalastyle/scalariform/LowercasePatternMatchChecker.scala b/src/main/scala/org/scalastyle/scalariform/LowercasePatternMatchChecker.scala index 5d80eb42..b6dbd884 100644 --- a/src/main/scala/org/scalastyle/scalariform/LowercasePatternMatchChecker.scala +++ b/src/main/scala/org/scalastyle/scalariform/LowercasePatternMatchChecker.scala @@ -17,7 +17,7 @@ package org.scalastyle.scalariform import _root_.scalariform.lexer.Token -import _root_.scalariform.lexer.Tokens.VARID +import _root_.scalariform.lexer.Tokens._ // scalastyle:ignore underscore.import import _root_.scalariform.parser.CasePattern import _root_.scalariform.parser.CompilationUnit import org.scalastyle.PositionError @@ -31,18 +31,26 @@ class LowercasePatternMatchChecker extends ScalariformChecker { final def verify(ast: CompilationUnit): List[ScalastyleError] = { val it = for { f <- visit(map)(ast.immediateChildren.head) - if matches(f) - } yield PositionError(f.pattern.firstToken.offset) + t <- matches(f.pattern.tokens, Nil) + } yield PositionError(t.offset) it } - private def matches(t: CasePattern) = { - t.pattern.tokens match { - case List(t: Token) => t.tokenType == VARID && t.text.length() > 0 && t.text(0).isLower - case _ => false + @scala.annotation.tailrec + private def matches(tokens: List[Token], res: List[Token]): List[Token] = + tokens.headOption match { + case None => res + case Some(t @ Token(VARID, text, _, _)) if text.headOption.exists(_.isLower) => + val tail = tokens.tail + tail.headOption match { + case Some(Token(COMMA | RPAREN | AT, _, _, _)) => + matches(tail.tail, t :: res) + case None => t :: res + case _ => matches(tail.tail, res) + } + case _ => matches(tokens.tail, res) } - } private def map(t: CasePattern): List[CasePattern] = List(t) ::: visit(map)(t.pattern) ::: visit(map)(t.guardOption) diff --git a/src/test/scala/org/scalastyle/scalariform/LowercasePatternMatchCheckerTest.scala b/src/test/scala/org/scalastyle/scalariform/LowercasePatternMatchCheckerTest.scala index 85272767..226d95d7 100644 --- a/src/test/scala/org/scalastyle/scalariform/LowercasePatternMatchCheckerTest.scala +++ b/src/test/scala/org/scalastyle/scalariform/LowercasePatternMatchCheckerTest.scala @@ -42,7 +42,7 @@ class F1 { } """ - assertErrors(List(), source) + assertErrors(List(columnError(10, 14), columnError(10, 17)), source) } @Test def testKO(): Unit = { @@ -61,6 +61,6 @@ class F1 { } """ - assertErrors(List(columnError(11, 9)), source) + assertErrors(List(columnError(10, 14), columnError(10, 17), columnError(11, 9)), source) } }