Add a rule for invalid characters in names

This commit is contained in:
Dmitry Jemerov
2018-01-02 13:17:31 +01:00
parent f83c5344d7
commit 84d63051f9
5 changed files with 46 additions and 9 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
@@ -48,6 +48,14 @@ private val NO_MIDDLE_UNDERSCORES = NamingRule("should not contain underscores i
'_' in it.substring(1)
}
private val NO_BAD_CHARACTERS = NamingRule("may contain only letters and digits") {
it.any { c -> c !in 'a'..'z' && c !in 'A'..'Z' && c !in '0'..'9' }
}
private val NO_BAD_CHARACTERS_OR_UNDERSCORE = NamingRule("may contain only letters, digits or underscores") {
it.any { c -> c !in 'a'..'z' && c !in 'A'..'Z' && c !in '0'..'9' && c != '_' }
}
abstract class NamingConventionInspection(
private val entityName: String,
private val defaultNamePattern: String,
@@ -94,7 +102,7 @@ abstract class NamingConventionInspection(
class ClassNameInspection : NamingConventionInspection(
"Class",
"[A-Z][A-Za-z\\d]*",
START_UPPER, NO_UNDERSCORES
START_UPPER, NO_UNDERSCORES, NO_BAD_CHARACTERS
) {
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor {
return object : KtVisitorVoid() {
@@ -112,7 +120,7 @@ class ClassNameInspection : NamingConventionInspection(
class EnumEntryNameInspection : NamingConventionInspection(
"Enum entry",
"[A-Z]([A-Za-z\\d]*|[A-Z_\\d]*)",
START_UPPER
START_UPPER, NO_BAD_CHARACTERS_OR_UNDERSCORE
) {
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor {
return object : KtVisitorVoid() {
@@ -126,7 +134,7 @@ class EnumEntryNameInspection : NamingConventionInspection(
class FunctionNameInspection : NamingConventionInspection(
"Function",
"[a-z][A-Za-z\\d]*",
START_LOWER, NO_UNDERSCORES
START_LOWER, NO_UNDERSCORES, NO_BAD_CHARACTERS
) {
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor {
return object : KtVisitorVoid() {
@@ -195,24 +203,33 @@ abstract class PropertyNameInspectionBase protected constructor(
}
class PropertyNameInspection :
PropertyNameInspectionBase(PropertyKind.NORMAL, "Property", "[a-z][A-Za-z\\d]*", START_LOWER, NO_UNDERSCORES)
PropertyNameInspectionBase(
PropertyKind.NORMAL, "Property", "[a-z][A-Za-z\\d]*",
START_LOWER, NO_UNDERSCORES, NO_BAD_CHARACTERS
)
class ObjectPropertyNameInspection :
PropertyNameInspectionBase(
PropertyKind.OBJECT_OR_TOP_LEVEL,
"Object or top-level property",
"[A-Za-z][_A-Za-z\\d]*",
NO_START_UNDERSCORE
NO_START_UNDERSCORE, NO_BAD_CHARACTERS_OR_UNDERSCORE
)
class PrivatePropertyNameInspection :
PropertyNameInspectionBase(PropertyKind.PRIVATE, "Private property", "_?[a-z][A-Za-z\\d]*", NO_MIDDLE_UNDERSCORES)
PropertyNameInspectionBase(
PropertyKind.PRIVATE, "Private property", "_?[a-z][A-Za-z\\d]*",
NO_MIDDLE_UNDERSCORES, NO_BAD_CHARACTERS_OR_UNDERSCORE
)
class ConstPropertyNameInspection :
PropertyNameInspectionBase(PropertyKind.CONST, "Const property", "[A-Z][_A-Z\\d]*")
class LocalVariableNameInspection :
PropertyNameInspectionBase(PropertyKind.LOCAL, "Local variable", "[a-z][A-Za-z\\d]*", START_LOWER, NO_UNDERSCORES)
PropertyNameInspectionBase(
PropertyKind.LOCAL, "Local variable", "[a-z][A-Za-z\\d]*",
START_LOWER, NO_UNDERSCORES, NO_BAD_CHARACTERS
)
class PackageNameInspection :
NamingConventionInspection("Package", "[a-z][A-Za-z\\d]*(\\.[a-z][A-Za-z\\d]*)*", NO_UNDERSCORES) {
@@ -21,6 +21,14 @@
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="temp:///src/test.kt" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Class naming convention</problem_class>
<description>Class name &lt;code&gt;FOO_BAR&lt;/code&gt; doesn''t match regex '[A-Z][A-Za-z\d]*' #loc</description>
<description>Class name &lt;code&gt;FOO_BAR&lt;/code&gt; should not contain underscores #loc</description>
</problem>
<problem>
<file>test.kt</file>
<line>15</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="temp:///src/test.kt" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Class naming convention</problem_class>
<description>Class name &lt;code&gt;`A B`&lt;/code&gt; may contain only letters and digits #loc</description>
</problem>
</problems>
+2
View File
@@ -11,3 +11,5 @@ enum class Foo {
_Foo,
quuxDoo,
}
class `A B`
@@ -13,4 +13,12 @@
<entry_point TYPE="file" FQNAME="temp:///src/test.kt" />
<description>Function name &lt;code&gt;FOO_BAR&lt;/code&gt; should not contain underscores #loc</description>
</problem>
<problem>
<file>test.kt</file>
<line>7</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="temp:///src/test.kt" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Class naming convention</problem_class>
<description>Function name &lt;code&gt;`a b`&lt;/code&gt; may contain only letters and digits #loc</description>
</problem>
</problems>
+2
View File
@@ -3,3 +3,5 @@ fun Foo() {}
fun FOO_BAR() {}
fun xyzzy() {}
fun `a b`() {}