Repository: detekt/detekt (kotlin)

Issue:
EmptyKtFile should be able to ignore package declaration
Rule: EmptyKtFile

## Expected Behavior of the rule

There should be a way to exclude the package declaration. A package declaration says "where" a kotlin file is in the package hierarchy, and is generally not considered content of the file.

## Context

There are rules like MissingPackageDeclaration as well as IDE automations that may cause empty kotlin files to end up with a package delcaration. Without this feature, the `EmptyKtFile` is practically useless in many projects.

Pull request description:
Fixes https://github.com/detekt/detekt/issues/6788

Patch:
diff --git a/detekt-rules-empty/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/empty/EmptyKotlinFile.kt b/detekt-rules-empty/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/empty/EmptyKotlinFile.kt
index 3a08c057dfd6..c02f6381c50b 100644
--- a/detekt-rules-empty/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/empty/EmptyKotlinFile.kt
+++ b/detekt-rules-empty/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/empty/EmptyKotlinFile.kt
@@ -13,7 +13,13 @@ import org.jetbrains.kotlin.psi.KtFile
 class EmptyKotlinFile(config: Config) : EmptyRule(config) {
 
     override fun visitKtFile(file: KtFile) {
-        if (file.text.isNullOrBlank()) {
+        var text = file.text
+        val packageDirective = file.packageDirective
+        if (packageDirective != null) {
+            val range = packageDirective.textRange
+            text = text.removeRange(range.startOffset, range.endOffset)
+        }
+        if (text.isNullOrBlank()) {
             report(
                 CodeSmell(
                     issue,


Test patch:
diff --git a/detekt-rules-empty/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/empty/EmptyKotlinFileSpec.kt b/detekt-rules-empty/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/empty/EmptyKotlinFileSpec.kt
new file mode 100644
index 000000000000..9497369d2a24
--- /dev/null
+++ b/detekt-rules-empty/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/empty/EmptyKotlinFileSpec.kt
@@ -0,0 +1,37 @@
+package io.gitlab.arturbosch.detekt.rules.empty
+
+import io.gitlab.arturbosch.detekt.api.Config
+import io.gitlab.arturbosch.detekt.test.assertThat
+import io.gitlab.arturbosch.detekt.test.compileAndLint
+import io.gitlab.arturbosch.detekt.test.lint
+import org.junit.jupiter.api.Test
+
+class EmptyKotlinFileSpec {
+    private val subject = EmptyKotlinFile(Config.empty)
+
+    private val codeWithPackageStatement = """
+            package my.packagee
+    """.trimIndent()
+
+    private val codeWithFunction = """
+            package my.packagee
+            
+            fun myFunction() {}
+    """.trimIndent()
+
+    @Test
+    fun `reports empty if file is blank`() {
+        val code = ""
+        assertThat(subject.lint(code)).hasSize(1)
+    }
+
+    @Test
+    fun `does report file with package statement`() {
+        assertThat(subject.compileAndLint(codeWithPackageStatement)).hasSize(1)
+    }
+
+    @Test
+    fun `does not report file with code`() {
+        assertThat(subject.compileAndLint(codeWithFunction)).hasSize(0)
+    }
+}