-
Notifications
You must be signed in to change notification settings - Fork 509
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Add new experimental rule `no-empty-file` for all code styles. Kotlin file may not be empty. Co-authored-by: ao0000 <[email protected]>
- Loading branch information
1 parent
3e56387
commit 1a5790e
Showing
5 changed files
with
188 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
...t-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/NoEmptyFileRule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.pinterest.ktlint.ruleset.standard.rules | ||
|
||
import com.pinterest.ktlint.rule.engine.core.api.ElementType | ||
import com.pinterest.ktlint.rule.engine.core.api.Rule | ||
import com.pinterest.ktlint.rule.engine.core.api.children | ||
import com.pinterest.ktlint.rule.engine.core.api.isPartOfComment | ||
import com.pinterest.ktlint.rule.engine.core.api.isRoot | ||
import com.pinterest.ktlint.rule.engine.core.api.isWhiteSpace | ||
import com.pinterest.ktlint.ruleset.standard.StandardRule | ||
import org.jetbrains.kotlin.com.intellij.lang.ASTNode | ||
|
||
public class NoEmptyFileRule : StandardRule(id = "no-empty-file"), Rule.Experimental { | ||
override fun beforeVisitChildNodes( | ||
node: ASTNode, | ||
autoCorrect: Boolean, | ||
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit, | ||
) { | ||
node | ||
.takeIf { it.isRoot() } | ||
?.takeIf { it.isEmptyFile() } | ||
?.let { emit(0, "File '${node.getFileName()}' should not be empty", false) } | ||
} | ||
|
||
private fun ASTNode.getFileName() = | ||
psi | ||
.containingFile | ||
.virtualFile | ||
.name | ||
.replace("\\", "/") // Ensure compatibility with Windows OS | ||
.substringAfterLast("/") | ||
|
||
private fun ASTNode.isEmptyFile(): Boolean = | ||
null == | ||
children() | ||
.firstOrNull { | ||
!it.isWhiteSpace() && | ||
!it.isPartOfComment() && | ||
it.elementType != ElementType.PACKAGE_DIRECTIVE && | ||
it.elementType != ElementType.IMPORT_LIST && | ||
!(it.elementType == ElementType.SCRIPT && it.text.isBlank()) | ||
} | ||
} |
136 changes: 136 additions & 0 deletions
136
...andard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/NoEmptyFileRuleTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
package com.pinterest.ktlint.ruleset.standard.rules | ||
|
||
import com.pinterest.ktlint.test.KtLintAssertThat.Companion.assertThatRule | ||
import org.junit.jupiter.api.Test | ||
|
||
class NoEmptyFileRuleTest { | ||
private val noEmptyFileRuleAssertThat = assertThatRule { NoEmptyFileRule() } | ||
|
||
@Test | ||
fun `Given non-empty kotlin file then ignore the rule for this file`() { | ||
val code = | ||
""" | ||
package foo | ||
fun main() { | ||
println("foo") | ||
} | ||
""".trimIndent() | ||
noEmptyFileRuleAssertThat(code).hasNoLintViolations() | ||
} | ||
|
||
@Test | ||
fun `Given an empty kotlin file then do a return lint error`() { | ||
val code = EMPTY_FILE | ||
noEmptyFileRuleAssertThat(code) | ||
.asFileWithPath("/some/path/Tmp.kt") | ||
.hasLintViolationWithoutAutoCorrect(1, 1, "File 'Tmp.kt' should not be empty") | ||
} | ||
|
||
@Test | ||
fun `Given an empty kotlin script file then do a return lint error`() { | ||
val code = EMPTY_FILE | ||
noEmptyFileRuleAssertThat(code) | ||
.asFileWithPath("/some/path/Tmp.kts") | ||
.asKotlinScript() | ||
.hasLintViolationWithoutAutoCorrect(1, 1, "File 'Tmp.kts' should not be empty") | ||
} | ||
|
||
@Test | ||
fun `Given only package statement in kotlin file then do a return lint error`() { | ||
val code = | ||
""" | ||
package foo | ||
""".trimIndent() | ||
|
||
noEmptyFileRuleAssertThat(code) | ||
.asFileWithPath("/some/path/Tmp.kt") | ||
.hasLintViolationWithoutAutoCorrect(1, 1, "File 'Tmp.kt' should not be empty") | ||
} | ||
|
||
@Test | ||
fun `Given only import statement in kotlin file then do a return lint error`() { | ||
val code = | ||
""" | ||
import foo.Bar | ||
""".trimIndent() | ||
noEmptyFileRuleAssertThat(code) | ||
.asFileWithPath("/some/path/Tmp.kt") | ||
.hasLintViolationWithoutAutoCorrect(1, 1, "File 'Tmp.kt' should not be empty") | ||
} | ||
|
||
@Test | ||
fun `Given only package and import statements in kotlin file then do a return lint error`() { | ||
val code = | ||
""" | ||
package foo | ||
import foo.Bar | ||
""".trimIndent() | ||
noEmptyFileRuleAssertThat(code) | ||
.asFileWithPath("/some/path/Tmp.kt") | ||
.hasLintViolationWithoutAutoCorrect(1, 1, "File 'Tmp.kt' should not be empty") | ||
} | ||
|
||
@Test | ||
fun `Given only package, import statements and comments in kotlin file then do a return lint error`() { | ||
val code = | ||
""" | ||
package foo | ||
import foo.Bar | ||
// some comment | ||
/* | ||
* some comment | ||
*/ | ||
/** | ||
* some comment | ||
*/ | ||
""".trimIndent() | ||
noEmptyFileRuleAssertThat(code) | ||
.asFileWithPath("/some/path/Tmp.kt") | ||
.hasLintViolationWithoutAutoCorrect(1, 1, "File 'Tmp.kt' should not be empty") | ||
} | ||
|
||
@Test | ||
fun `Given non-empty kotlin file then ignore this file`() { | ||
val code = | ||
""" | ||
package foo | ||
fun main() { | ||
println("foo") | ||
} | ||
""".trimIndent() | ||
noEmptyFileRuleAssertThat(code) | ||
.asFileWithPath("/some/path/Tmp.kt") | ||
.hasNoLintViolations() | ||
} | ||
|
||
@Test | ||
fun `x x`() { | ||
val code = | ||
""" | ||
plugins { | ||
id("ktlint-publication-library") | ||
} | ||
dependencies { | ||
implementation(projects.ktlintLogger) | ||
implementation(projects.ktlintRuleEngine) | ||
implementation(projects.ktlintCliRulesetCore) | ||
api(libs.assertj) | ||
api(libs.junit5) | ||
api(libs.janino) | ||
api(libs.jimfs) | ||
} | ||
""".trimIndent() | ||
noEmptyFileRuleAssertThat(code) | ||
.asFileWithPath("/some/path/Tmp.kts") | ||
.asKotlinScript() | ||
.hasNoLintViolations() | ||
} | ||
|
||
private companion object { | ||
private const val EMPTY_FILE = "" | ||
} | ||
} |