com.pinterest.ktlint.ruleset.standard.rules.NoEmptyFileRule.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ktlint-ruleset-standard Show documentation
Show all versions of ktlint-ruleset-standard Show documentation
An anti-bikeshedding Kotlin linter with built-in formatter.
package com.pinterest.ktlint.ruleset.standard.rules
import com.pinterest.ktlint.rule.engine.core.api.ElementType
import com.pinterest.ktlint.rule.engine.core.api.RuleId
import com.pinterest.ktlint.rule.engine.core.api.SinceKtlint
import com.pinterest.ktlint.rule.engine.core.api.SinceKtlint.Status.EXPERIMENTAL
import com.pinterest.ktlint.rule.engine.core.api.SinceKtlint.Status.STABLE
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
@SinceKtlint("0.50", EXPERIMENTAL)
@SinceKtlint("1.0", STABLE)
public class NoEmptyFileRule : StandardRule(id = "no-empty-file") {
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())
}
}
public val NO_EMPTY_FILE_RULE_ID: RuleId = NoEmptyFileRule().ruleId
© 2015 - 2024 Weber Informatics LLC | Privacy Policy