Skip to content

Commit

Permalink
Merge pull request #25 from Doist/prateek/new_line_after_super_rule
Browse files Browse the repository at this point in the history
Add `NewLineAfterSuperCall` Rule
  • Loading branch information
hackertronix authored Aug 9, 2024
2 parents 982da19 + 85f9dd0 commit 51e04ef
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/main/kotlin/com/doist/detekt/NewLineAfterSuperCall.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.doist.detekt

import io.gitlab.arturbosch.detekt.api.CodeSmell
import io.gitlab.arturbosch.detekt.api.Config
import io.gitlab.arturbosch.detekt.api.Debt
import io.gitlab.arturbosch.detekt.api.Entity
import io.gitlab.arturbosch.detekt.api.Issue
import io.gitlab.arturbosch.detekt.api.Rule
import io.gitlab.arturbosch.detekt.api.Severity
import org.jetbrains.kotlin.psi.KtNamedFunction

class NewLineAfterSuperCall(config: Config = Config.empty) : Rule(config) {
override val issue = Issue(
javaClass.simpleName,
Severity.Style,
"Ensure there is a new line after every call to a super() method.",
Debt.FIVE_MINS
)

override fun visitNamedFunction(function: KtNamedFunction) {
super.visitNamedFunction(function)

function.bodyBlockExpression?.statements?.forEachIndexed { index, statement ->
if (statement.text?.startsWith("super.") == true) {
if (statement.nextSibling.text.contains("\n\n")) return
report(
CodeSmell(
issue,
Entity.from(statement),
"There should be a new line after the call to super()."
)
)
}
}
}
}
2 changes: 2 additions & 0 deletions src/main/resources/config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ DoistRuleSet:
TodoPattern:
active: true
pattern: '// TODO(.+): .*'
NewLineAfterSuperCall:
active: true
39 changes: 39 additions & 0 deletions src/test/kotlin/com/doist/detekt/NewLineAfterSuperCallTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.doist.detekt


import io.gitlab.arturbosch.detekt.rules.KotlinCoreEnvironmentTest
import io.gitlab.arturbosch.detekt.test.lint
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test

@KotlinCoreEnvironmentTest
internal class NewLineAfterSuperCallTest(private val env: KotlinCoreEnvironment) {
@Test
fun `should report when there is no blank line after super() call`() {
val code = """
override fun foo() {
super.foo()
bar.bazinga()
}
"""

val findings = NewLineAfterSuperCall().lint(code)
assertEquals(1, findings.size)
assertEquals("There should be a new line after the call to super().", findings.first().message)
}

@Test
fun `should not report when there is a blank line after super() call`() {
val code = """
override fun foo() {
super.foo()
bar.bazinga()
}
"""

val findings = NewLineAfterSuperCall().lint(code)
assertEquals(0, findings.size)
}
}

0 comments on commit 51e04ef

Please sign in to comment.