Apply same style for top-level and object properties
#KT-20437 Fixed
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
<html>
|
||||
<body>
|
||||
Reports names of properties in objects and companion objects that do not follow the recommended naming conventions.
|
||||
Reports names of properties in objects and companion objects, as well as top-level properties, that do not follow the recommended naming conventions.
|
||||
</body>
|
||||
</html>
|
||||
@@ -40,6 +40,10 @@ private val NO_UNDERSCORES = NamingRule("should not contain underscores") {
|
||||
'_' in it
|
||||
}
|
||||
|
||||
private val NO_START_UNDERSCORE = NamingRule("should not start with an underscore") {
|
||||
it.startsWith('_')
|
||||
}
|
||||
|
||||
private val NO_MIDDLE_UNDERSCORES = NamingRule("should not contain underscores in the middle or the end") {
|
||||
'_' in it.substring(1)
|
||||
}
|
||||
@@ -163,7 +167,7 @@ abstract class PropertyNameInspectionBase protected constructor(
|
||||
vararg rules: NamingRule
|
||||
) : NamingConventionInspection(entityName, defaultNamePattern, *rules) {
|
||||
|
||||
protected enum class PropertyKind { NORMAL, PRIVATE, OBJECT, CONST, LOCAL }
|
||||
protected enum class PropertyKind { NORMAL, PRIVATE, OBJECT_OR_TOP_LEVEL, CONST, LOCAL }
|
||||
|
||||
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor {
|
||||
return object : KtVisitorVoid() {
|
||||
@@ -178,7 +182,9 @@ abstract class PropertyNameInspectionBase protected constructor(
|
||||
private fun KtProperty.getKind(): PropertyKind = when {
|
||||
isLocal -> PropertyKind.LOCAL
|
||||
|
||||
containingClassOrObject is KtObjectDeclaration -> PropertyKind.OBJECT
|
||||
containingClassOrObject is KtObjectDeclaration -> PropertyKind.OBJECT_OR_TOP_LEVEL
|
||||
|
||||
isTopLevel -> PropertyKind.OBJECT_OR_TOP_LEVEL
|
||||
|
||||
hasModifier(KtTokens.CONST_KEYWORD) -> PropertyKind.CONST
|
||||
|
||||
@@ -192,7 +198,12 @@ class PropertyNameInspection :
|
||||
PropertyNameInspectionBase(PropertyKind.NORMAL, "Property", "[a-z][A-Za-z\\d]*", START_LOWER, NO_UNDERSCORES)
|
||||
|
||||
class ObjectPropertyNameInspection :
|
||||
PropertyNameInspectionBase(PropertyKind.OBJECT, "Object property", "[A-Za-z][_A-Za-z\\d]*")
|
||||
PropertyNameInspectionBase(
|
||||
PropertyKind.OBJECT_OR_TOP_LEVEL,
|
||||
"Object or top-level property",
|
||||
"[A-Za-z][_A-Za-z\\d]*",
|
||||
NO_START_UNDERSCORE
|
||||
)
|
||||
|
||||
class PrivatePropertyNameInspection :
|
||||
PropertyNameInspectionBase(PropertyKind.PRIVATE, "Private property", "_?[a-z][A-Za-z\\d]*", NO_MIDDLE_UNDERSCORES)
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
<problems>
|
||||
<problem>
|
||||
<file>test.kt</file>
|
||||
<line>5</line>
|
||||
<module>light_idea_test_case</module>
|
||||
<entry_point TYPE="file" FQNAME="temp:///src/test.kt" />
|
||||
<description>Object or top-level property name <code>_FOO</code> should not start with an underscore #loc</description>
|
||||
</problem>
|
||||
<problem>
|
||||
<file>test.kt</file>
|
||||
<line>21</line>
|
||||
<module>light_idea_test_case</module>
|
||||
<entry_point TYPE="file" FQNAME="temp:///src/test.kt" />
|
||||
<description>Object or top-level property name <code>_FOO</code> should not start with an underscore #loc</description>
|
||||
</problem>
|
||||
</problems>
|
||||
+1
@@ -0,0 +1 @@
|
||||
// INSPECTION_CLASS: org.jetbrains.kotlin.idea.inspections.ObjectPropertyNameInspection
|
||||
@@ -0,0 +1,34 @@
|
||||
val Foo: String = ""
|
||||
|
||||
var FOO_BAR: Int = 0
|
||||
|
||||
var _FOO: Int = 0
|
||||
|
||||
const val THREE = 3
|
||||
|
||||
val xyzzy = 1
|
||||
|
||||
fun foo() {
|
||||
val XYZZY = 1
|
||||
val BAR_BAZ = 2
|
||||
}
|
||||
|
||||
object Foo {
|
||||
val Foo: String = ""
|
||||
|
||||
var FOO_BAR: Int = 0
|
||||
|
||||
var _FOO: Int = 0
|
||||
}
|
||||
|
||||
class D {
|
||||
private val _foo: String
|
||||
|
||||
private val FOO_BAR: String
|
||||
|
||||
companion object {
|
||||
val Foo: String = ""
|
||||
|
||||
var FOO_BAR: Int = 0
|
||||
}
|
||||
}
|
||||
@@ -1,16 +1,9 @@
|
||||
<problems>
|
||||
<problem>
|
||||
<file>test.kt</file>
|
||||
<line>1</line>
|
||||
<line>27</line>
|
||||
<module>light_idea_test_case</module>
|
||||
<entry_point TYPE="file" FQNAME="temp:///src/test.kt" />
|
||||
<description>Property name <code>Foo</code> should start with a lowercase letter #loc</description>
|
||||
</problem>
|
||||
<problem>
|
||||
<file>test.kt</file>
|
||||
<line>3</line>
|
||||
<module>light_idea_test_case</module>
|
||||
<entry_point TYPE="file" FQNAME="temp:///src/test.kt" />
|
||||
<description>Property name <code>FOO_BAR</code> should not contain underscores #loc</description>
|
||||
<description>Property name <code>_Foo</code> should not start with an underscore #loc</description>
|
||||
</problem>
|
||||
</problems>
|
||||
|
||||
@@ -2,6 +2,8 @@ val Foo: String = ""
|
||||
|
||||
var FOO_BAR: Int = 0
|
||||
|
||||
var _FOO: Int = 0
|
||||
|
||||
const val THREE = 3
|
||||
|
||||
val xyzzy = 1
|
||||
@@ -22,6 +24,8 @@ class D {
|
||||
|
||||
private val FOO_BAR: String
|
||||
|
||||
val _Foo: String
|
||||
|
||||
companion object {
|
||||
val Foo: String = ""
|
||||
|
||||
|
||||
@@ -1,17 +1,6 @@
|
||||
/*
|
||||
* Copyright 2010-2017 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
* Copyright 2000-2017 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.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.idea.codeInsight;
|
||||
@@ -245,6 +234,12 @@ public class InspectionTestGenerated extends AbstractInspectionTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("naming/objectProperty/inspectionData/inspections.test")
|
||||
public void testNaming_objectProperty_inspectionData_Inspections_test() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspections/naming/objectProperty/inspectionData/inspections.test");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("naming/privateProperty/inspectionData/inspections.test")
|
||||
public void testNaming_privateProperty_inspectionData_Inspections_test() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspections/naming/privateProperty/inspectionData/inspections.test");
|
||||
|
||||
Reference in New Issue
Block a user