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

Commit d2931f3

Browse files
author
Albert Meltzer
committed
LowercasePatternMatch: find more invalid patterns
This includes pattern extractors, not just standalone vals.
1 parent 810b1cb commit d2931f3

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

src/main/scala/org/scalastyle/scalariform/LowercasePatternMatchChecker.scala

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
package org.scalastyle.scalariform
1818

1919
import _root_.scalariform.lexer.Token
20-
import _root_.scalariform.lexer.Tokens.VARID
20+
import _root_.scalariform.lexer.Tokens._ // scalastyle:ignore underscore.import
2121
import _root_.scalariform.parser.CasePattern
2222
import _root_.scalariform.parser.CompilationUnit
2323
import org.scalastyle.PositionError
@@ -31,18 +31,26 @@ class LowercasePatternMatchChecker extends ScalariformChecker {
3131
final def verify(ast: CompilationUnit): List[ScalastyleError] = {
3232
val it = for {
3333
f <- visit(map)(ast.immediateChildren.head)
34-
if matches(f)
35-
} yield PositionError(f.pattern.firstToken.offset)
34+
t <- matches(f.pattern.tokens, Nil)
35+
} yield PositionError(t.offset)
3636

3737
it
3838
}
3939

40-
private def matches(t: CasePattern) = {
41-
t.pattern.tokens match {
42-
case List(t: Token) => t.tokenType == VARID && t.text.length() > 0 && t.text(0).isLower
43-
case _ => false
40+
@scala.annotation.tailrec
41+
private def matches(tokens: List[Token], res: List[Token]): List[Token] =
42+
tokens.headOption match {
43+
case None => res
44+
case Some(t @ Token(VARID, text, _, _)) if text.headOption.exists(_.isLower) =>
45+
val tail = tokens.tail
46+
tail.headOption match {
47+
case Some(Token(COMMA | RPAREN | AT, _, _, _)) =>
48+
matches(tail.tail, t :: res)
49+
case None => t :: res
50+
case _ => matches(tail.tail, res)
51+
}
52+
case _ => matches(tokens.tail, res)
4453
}
45-
}
4654

4755
private def map(t: CasePattern): List[CasePattern] =
4856
List(t) ::: visit(map)(t.pattern) ::: visit(map)(t.guardOption)

src/test/scala/org/scalastyle/scalariform/LowercasePatternMatchCheckerTest.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class F1 {
4242
}
4343
"""
4444

45-
assertErrors(List(), source)
45+
assertErrors(List(columnError(10, 14), columnError(10, 17)), source)
4646
}
4747

4848
@Test def testKO(): Unit = {
@@ -61,6 +61,6 @@ class F1 {
6161
}
6262
"""
6363

64-
assertErrors(List(columnError(11, 9)), source)
64+
assertErrors(List(columnError(10, 14), columnError(10, 17), columnError(11, 9)), source)
6565
}
6666
}

0 commit comments

Comments
 (0)