Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add NewLineAfterSuperCall Rule #25

Merged
merged 4 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/main/kotlin/com/doist/detekt/NewLineAfterSuperCall.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
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 ->

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bit ironic but ... Can we add an empty line here, please? 😇😅

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LOL

if (statement.text?.startsWith("super") == true) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we check here for super. (i.e. with the dot at the end), just for extra safety ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch

if (statement.nextSibling.text.contains("\n\n")) return
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there's likely a cleaner way to do this, but I am not super on top of detekt APIs so for now sticking with this as it works!

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) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👏

@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)
}
}
Loading