-
Notifications
You must be signed in to change notification settings - Fork 0
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
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 -> | ||
if (statement.text?.startsWith("super") == true) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we check here for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good catch |
||
if (statement.nextSibling.text.contains("\n\n")) return | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()." | ||
) | ||
) | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,3 +12,5 @@ DoistRuleSet: | |
TodoPattern: | ||
active: true | ||
pattern: '// TODO(.+): .*' | ||
NewLineAfterSuperCall: | ||
active: true |
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
} | ||
} |
There was a problem hiding this comment.
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? 😇😅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LOL