diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/NamingConventionInspections.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/NamingConventionInspections.kt
index 7ac2c3cfa94..37fd18f30e7 100644
--- a/idea/src/org/jetbrains/kotlin/idea/inspections/NamingConventionInspections.kt
+++ b/idea/src/org/jetbrains/kotlin/idea/inspections/NamingConventionInspections.kt
@@ -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) {
diff --git a/idea/testData/inspections/naming/class/inspectionData/expected.xml b/idea/testData/inspections/naming/class/inspectionData/expected.xml
index 79b8108cf57..4b7c78fdc2e 100644
--- a/idea/testData/inspections/naming/class/inspectionData/expected.xml
+++ b/idea/testData/inspections/naming/class/inspectionData/expected.xml
@@ -21,6 +21,14 @@
light_idea_test_case
Class naming convention
- Class name <code>FOO_BAR</code> doesn''t match regex '[A-Z][A-Za-z\d]*' #loc
+ Class name <code>FOO_BAR</code> should not contain underscores #loc
+
+
+ test.kt
+ 15
+ light_idea_test_case
+
+ Class naming convention
+ Class name <code>`A B`</code> may contain only letters and digits #loc
\ No newline at end of file
diff --git a/idea/testData/inspections/naming/class/test.kt b/idea/testData/inspections/naming/class/test.kt
index bc540eca39a..a1746608694 100644
--- a/idea/testData/inspections/naming/class/test.kt
+++ b/idea/testData/inspections/naming/class/test.kt
@@ -11,3 +11,5 @@ enum class Foo {
_Foo,
quuxDoo,
}
+
+class `A B`
\ No newline at end of file
diff --git a/idea/testData/inspections/naming/function/inspectionData/expected.xml b/idea/testData/inspections/naming/function/inspectionData/expected.xml
index d297390225e..494e38f73fd 100644
--- a/idea/testData/inspections/naming/function/inspectionData/expected.xml
+++ b/idea/testData/inspections/naming/function/inspectionData/expected.xml
@@ -13,4 +13,12 @@
Function name <code>FOO_BAR</code> should not contain underscores #loc
+
+ test.kt
+ 7
+ light_idea_test_case
+
+ Class naming convention
+ Function name <code>`a b`</code> may contain only letters and digits #loc
+
diff --git a/idea/testData/inspections/naming/function/test.kt b/idea/testData/inspections/naming/function/test.kt
index 484cec2b948..81e7dae21af 100644
--- a/idea/testData/inspections/naming/function/test.kt
+++ b/idea/testData/inspections/naming/function/test.kt
@@ -3,3 +3,5 @@ fun Foo() {}
fun FOO_BAR() {}
fun xyzzy() {}
+
+fun `a b`() {}
\ No newline at end of file