Allow underscore in packages and forbid uppercase letters (KT-27900)
This commit is contained in:
@@ -16,6 +16,7 @@ import com.intellij.psi.PsiElementVisitor
|
|||||||
import com.intellij.psi.PsiNameIdentifierOwner
|
import com.intellij.psi.PsiNameIdentifierOwner
|
||||||
import com.intellij.ui.EditorTextField
|
import com.intellij.ui.EditorTextField
|
||||||
import com.siyeh.ig.psiutils.TestUtils
|
import com.siyeh.ig.psiutils.TestUtils
|
||||||
|
import org.intellij.lang.annotations.Language
|
||||||
import org.intellij.lang.regexp.RegExpFileType
|
import org.intellij.lang.regexp.RegExpFileType
|
||||||
import org.jetbrains.kotlin.idea.quickfix.RenameIdentifierFix
|
import org.jetbrains.kotlin.idea.quickfix.RenameIdentifierFix
|
||||||
import org.jetbrains.kotlin.lexer.KtTokens
|
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 != '_' }
|
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(
|
abstract class NamingConventionInspection(
|
||||||
private val entityName: String,
|
private val entityName: String,
|
||||||
private val defaultNamePattern: String,
|
@Language("RegExp") private val defaultNamePattern: String,
|
||||||
private vararg val rules: NamingRule
|
private vararg val rules: NamingRule
|
||||||
) : AbstractKotlinInspection() {
|
) : AbstractKotlinInspection() {
|
||||||
protected var nameRegex: Regex? = defaultNamePattern.toRegex()
|
protected var nameRegex: Regex? = defaultNamePattern.toRegex()
|
||||||
@@ -228,7 +233,7 @@ class LocalVariableNameInspection :
|
|||||||
)
|
)
|
||||||
|
|
||||||
class PackageNameInspection :
|
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 {
|
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean, session: LocalInspectionToolSession): PsiElementVisitor {
|
||||||
return packageDirectiveVisitor { directive ->
|
return packageDirectiveVisitor { directive ->
|
||||||
val qualifiedName = directive.qualifiedName
|
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 <code>one.two.Three</code> 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
|
||||||
+5
@@ -239,6 +239,11 @@ public class InspectionTestGenerated extends AbstractInspectionTest {
|
|||||||
runTest("idea/testData/inspections/naming/objectProperty/inspectionData/inspections.test");
|
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")
|
@TestMetadata("naming/privateProperty/inspectionData/inspections.test")
|
||||||
public void testNaming_privateProperty_inspectionData_Inspections_test() throws Exception {
|
public void testNaming_privateProperty_inspectionData_Inspections_test() throws Exception {
|
||||||
runTest("idea/testData/inspections/naming/privateProperty/inspectionData/inspections.test");
|
runTest("idea/testData/inspections/naming/privateProperty/inspectionData/inspections.test");
|
||||||
|
|||||||
Reference in New Issue
Block a user