From f83c5344d7f94c833077bf1761d8cc58411453be Mon Sep 17 00:00:00 2001 From: Dmitry Jemerov Date: Fri, 29 Dec 2017 17:35:11 +0100 Subject: [PATCH] Apply same style for top-level and object properties #KT-20437 Fixed --- .../ObjectPropertyName.html | 2 +- .../NamingConventionInspections.kt | 17 ++++++++-- .../inspectionData/expected.xml | 16 +++++++++ .../inspectionData/inspections.test | 1 + .../inspections/naming/objectProperty/test.kt | 34 +++++++++++++++++++ .../property/inspectionData/expected.xml | 11 ++---- .../inspections/naming/property/test.kt | 4 +++ .../codeInsight/InspectionTestGenerated.java | 21 +++++------- 8 files changed, 80 insertions(+), 26 deletions(-) create mode 100644 idea/testData/inspections/naming/objectProperty/inspectionData/expected.xml create mode 100644 idea/testData/inspections/naming/objectProperty/inspectionData/inspections.test create mode 100644 idea/testData/inspections/naming/objectProperty/test.kt diff --git a/idea/resources/inspectionDescriptions/ObjectPropertyName.html b/idea/resources/inspectionDescriptions/ObjectPropertyName.html index 21db1f56673..b2e2161f23a 100644 --- a/idea/resources/inspectionDescriptions/ObjectPropertyName.html +++ b/idea/resources/inspectionDescriptions/ObjectPropertyName.html @@ -1,5 +1,5 @@ -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. \ No newline at end of file diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/NamingConventionInspections.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/NamingConventionInspections.kt index 67e1c703d9f..7ac2c3cfa94 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/NamingConventionInspections.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/NamingConventionInspections.kt @@ -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) diff --git a/idea/testData/inspections/naming/objectProperty/inspectionData/expected.xml b/idea/testData/inspections/naming/objectProperty/inspectionData/expected.xml new file mode 100644 index 00000000000..d2b4a809085 --- /dev/null +++ b/idea/testData/inspections/naming/objectProperty/inspectionData/expected.xml @@ -0,0 +1,16 @@ + + + test.kt + 5 + light_idea_test_case + + Object or top-level property name <code>_FOO</code> should not start with an underscore #loc + + + test.kt + 21 + light_idea_test_case + + Object or top-level property name <code>_FOO</code> should not start with an underscore #loc + + diff --git a/idea/testData/inspections/naming/objectProperty/inspectionData/inspections.test b/idea/testData/inspections/naming/objectProperty/inspectionData/inspections.test new file mode 100644 index 00000000000..b3c467eec91 --- /dev/null +++ b/idea/testData/inspections/naming/objectProperty/inspectionData/inspections.test @@ -0,0 +1 @@ +// INSPECTION_CLASS: org.jetbrains.kotlin.idea.inspections.ObjectPropertyNameInspection \ No newline at end of file diff --git a/idea/testData/inspections/naming/objectProperty/test.kt b/idea/testData/inspections/naming/objectProperty/test.kt new file mode 100644 index 00000000000..a65d29c6a3d --- /dev/null +++ b/idea/testData/inspections/naming/objectProperty/test.kt @@ -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 + } +} \ No newline at end of file diff --git a/idea/testData/inspections/naming/property/inspectionData/expected.xml b/idea/testData/inspections/naming/property/inspectionData/expected.xml index c83909d433a..4713fb622c7 100644 --- a/idea/testData/inspections/naming/property/inspectionData/expected.xml +++ b/idea/testData/inspections/naming/property/inspectionData/expected.xml @@ -1,16 +1,9 @@ test.kt - 1 + 27 light_idea_test_case - Property name <code>Foo</code> should start with a lowercase letter #loc - - - test.kt - 3 - light_idea_test_case - - Property name <code>FOO_BAR</code> should not contain underscores #loc + Property name <code>_Foo</code> should not start with an underscore #loc diff --git a/idea/testData/inspections/naming/property/test.kt b/idea/testData/inspections/naming/property/test.kt index c7cd37f9438..abc1562487e 100644 --- a/idea/testData/inspections/naming/property/test.kt +++ b/idea/testData/inspections/naming/property/test.kt @@ -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 = "" diff --git a/idea/tests/org/jetbrains/kotlin/idea/codeInsight/InspectionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/codeInsight/InspectionTestGenerated.java index dbea889ec07..940020ff4ef 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/codeInsight/InspectionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/codeInsight/InspectionTestGenerated.java @@ -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");