Allow underscore in packages and forbid uppercase letters (KT-27900)

This commit is contained in:
Nikolay Krasko
2018-12-05 17:38:23 +03:00
parent e0d3abc819
commit 2373c9d838
9 changed files with 25 additions and 2 deletions
@@ -16,6 +16,7 @@ import com.intellij.psi.PsiElementVisitor
import com.intellij.psi.PsiNameIdentifierOwner
import com.intellij.ui.EditorTextField
import com.siyeh.ig.psiutils.TestUtils
import org.intellij.lang.annotations.Language
import org.intellij.lang.regexp.RegExpFileType
import org.jetbrains.kotlin.idea.quickfix.RenameIdentifierFix
import org.jetbrains.kotlin.lexer.KtTokens
@@ -56,9 +57,13 @@ private val NO_BAD_CHARACTERS_OR_UNDERSCORE = NamingRule("may contain only lette
it.any { c -> c !in 'a'..'z' && c !in 'A'..'Z' && c !in '0'..'9' && c != '_' }
}
private val ONLY_LOWER_DIGITS_OR_UNDERSCORE = NamingRule("may contain only lowercase letters, digits or underscores") {
it.any { c -> c !in 'a'..'z' && c !in '0'..'9' && c != '_' }
}
abstract class NamingConventionInspection(
private val entityName: String,
private val defaultNamePattern: String,
@Language("RegExp") private val defaultNamePattern: String,
private vararg val rules: NamingRule
) : AbstractKotlinInspection() {
protected var nameRegex: Regex? = defaultNamePattern.toRegex()
@@ -228,7 +233,7 @@ class LocalVariableNameInspection :
)
class PackageNameInspection :
NamingConventionInspection("Package", "[a-z][A-Za-z\\d]*(\\.[a-z][A-Za-z\\d]*)*", NO_UNDERSCORES) {
NamingConventionInspection("Package", "[a-z_][a-z\\d_]*(\\.[a-z_][a-z\\d_]*)*", ONLY_LOWER_DIGITS_OR_UNDERSCORE) {
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean, session: LocalInspectionToolSession): PsiElementVisitor {
return packageDirectiveVisitor { directive ->
val qualifiedName = directive.qualifiedName
@@ -0,0 +1 @@
package one.two.three
@@ -0,0 +1 @@
package one.two.Three
@@ -0,0 +1,7 @@
<problems>
<problem>
<file>hasUpperCase.kt</file>
<line>1</line>
<description>Package name &lt;code&gt;one.two.Three&lt;/code&gt; doesn't match regex '[a-z][A-Za-z\d]*_?(\.[a-z][A-Za-z\d]*_?)*' #loc</description>
</problem>
</problems>
@@ -0,0 +1 @@
// INSPECTION_CLASS: org.jetbrains.kotlin.idea.inspections.PackageNameInspection
@@ -0,0 +1 @@
package com.fun_.test
@@ -0,0 +1 @@
package org.so_so.example
@@ -0,0 +1 @@
package com.example._123name
@@ -239,6 +239,11 @@ public class InspectionTestGenerated extends AbstractInspectionTest {
runTest("idea/testData/inspections/naming/objectProperty/inspectionData/inspections.test");
}
@TestMetadata("naming/package/inspectionData/inspections.test")
public void testNaming_package_inspectionData_Inspections_test() throws Exception {
runTest("idea/testData/inspections/naming/package/inspectionData/inspections.test");
}
@TestMetadata("naming/privateProperty/inspectionData/inspections.test")
public void testNaming_privateProperty_inspectionData_Inspections_test() throws Exception {
runTest("idea/testData/inspections/naming/privateProperty/inspectionData/inspections.test");