This repository was archived by the owner on Sep 15, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathCustom_Scala_FieldNamesChecker.scala
More file actions
127 lines (111 loc) · 3.77 KB
/
Copy pathCustom_Scala_FieldNamesChecker.scala
File metadata and controls
127 lines (111 loc) · 3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package codacy.patterns
import codacy.base.Pattern
import scala.meta._
import scala.util.matching.Regex
class Custom_Scala_FieldNamesChecker(configuration: Custom_Scala_FieldNamesChecker.Configuration) extends Pattern {
def this() = this(Custom_Scala_FieldNamesChecker.Configuration())
override def apply(tree: Tree): Set[Result] = {
tree
.collect {
//val definitions
case defn: Defn.Val =>
if (configuration.includeEnums || !isEnumValDefRegexOrDecl(defn.parent, Some(defn.rhs), defn.decltpe))
conflictingNames(defn.pats)
else
Iterable.empty
//var definitions
case defn: Defn.Var =>
if (configuration.includeEnums || !isEnumValDefRegexOrDecl(defn.parent, defn.rhs, defn.decltpe))
conflictingNames(defn.pats)
else
Iterable.empty
//class parameters
case defn: Defn.Class =>
defn.ctor.paramss.flatMap { params =>
params.collect {
case param if isConflictingName(param.name) =>
param.name
}
}
//parameter values (method parameters)
case param: Term.Param if isConflictingName(param.name) =>
List(param.name)
}
.flatten
.map { tree =>
Result(message(tree), tree)
}
.toSet
}
private[this] def isEnumValDefRegexOrDecl(parent: Option[Tree], expr: Option[Tree], tpe: Option[Type]) = {
val extendsEnumeration: Boolean = parent
.collect {
case template"{ ..$stats } with ..${ctorcalls: Seq[Init]} { $param => ..$stats2 }" =>
ctorcalls.exists {
case init"Enumeration" => true
case _ => false
}
}
.getOrElse(false)
lazy val exprIsValue: Boolean = expr.exists {
case q"${Term.Name(value)}" => value == "Value"
case q"${Term.Apply(Term.Name(name), _)}" => name == "Value"
case _ => false
}
lazy val exprIsRegex: Boolean = expr.exists {
case q"${lit: Lit}.r" => true
case _ => false
}
lazy val tpeIsValue: Boolean = tpe.exists {
case t"${Type.Name(value)}" => value == "Value"
case _ => false
}
exprIsRegex || (extendsEnumeration && (exprIsValue || tpeIsValue))
}
private[this] def isConstant(tree: Tree): Boolean = tree.parent.exists {
case t @ q"..$_ val ..$patsnel: $tpe = $expr" =>
configuration.allowConstants && t.parent
.flatMap(_.parent)
.exists {
case Defn.Object(_, _, _) => true
case Pkg.Object(_, _, _) => true
case _ => false
}
case _ => false
}
private[this] def conflictingNames(trees: Seq[Tree]) = {
trees.collect {
//tuples
case p"(..$exprsnel)" =>
exprsnel.filter(isConflictingName)
case single if isConflictingName(single) =>
List(single)
}.flatten
}
private[this] def isConflictingName(tree: Tree): Boolean = {
val name = tree match {
case q"${term: Pat.Var}" =>
Some(term.name.value)
case q"${name: Term.Name}" =>
Some(name.value)
case t =>
None
}
name.exists {
case name if isConstant(tree) => !configuration.constantsRegex.pattern.matcher(name).matches()
case name => !configuration.regex.pattern.matcher(name).matches()
}
}
private[this] def message(tree: Tree) = {
val regex = if (isConstant(tree)) configuration.constantsRegex else configuration.regex
Message(s"Field name '$tree' does not match the regular expression '$regex'")
}
}
object Custom_Scala_FieldNamesChecker {
case class Configuration(
regex: Regex = """^[a-z][A-Za-z0-9]*$""".r,
includeEnums: Boolean = false,
allowConstants: Boolean = true,
constantsRegex: Regex = """^[A-Za-z][A-Za-z0-9]*$""".r
)
}