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

Commit

Permalink
LowercasePatternMatch: find more invalid patterns
Browse files Browse the repository at this point in the history
This includes pattern extractors, not just standalone vals.
  • Loading branch information
Albert Meltzer committed Jul 15, 2021
1 parent 03297ec commit ca3eec5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class F1 {
}
"""

assertErrors(List(), source)
assertErrors(List(columnError(10, 14), columnError(10, 17)), source)
}

@Test def testKO(): Unit = {
Expand All @@ -61,6 +61,6 @@ class F1 {
}
"""

assertErrors(List(columnError(11, 9)), source)
assertErrors(List(columnError(10, 14), columnError(10, 17), columnError(11, 9)), source)
}
}

0 comments on commit ca3eec5

Please sign in to comment.