-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rule 6.1.3 Do not use the primary constructor if it is empty and has …
…no sense (#528) * Rule 6.1.3 ### What's done: Made rule, added tests and documentations
- Loading branch information
Showing
12 changed files
with
163 additions
and
0 deletions.
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
35 changes: 35 additions & 0 deletions
35
diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/AvoidEmptyPrimaryConstructor.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,35 @@ | ||
package org.cqfn.diktat.ruleset.rules | ||
|
||
import com.pinterest.ktlint.core.Rule | ||
import com.pinterest.ktlint.core.ast.ElementType.CLASS | ||
import org.cqfn.diktat.common.config.rules.RulesConfig | ||
import org.cqfn.diktat.ruleset.constants.Warnings.EMPTY_PRIMARY_CONSTRUCTOR | ||
import org.jetbrains.kotlin.com.intellij.lang.ASTNode | ||
import org.jetbrains.kotlin.psi.KtClass | ||
|
||
class AvoidEmptyPrimaryConstructor(private val configRules: List<RulesConfig>) : Rule("avoid-empty-primary-constructor") { | ||
|
||
|
||
private var isFixMode: Boolean = false | ||
private lateinit var emitWarn: ((offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit) | ||
|
||
override fun visit(node: ASTNode, | ||
autoCorrect: Boolean, | ||
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit) { | ||
emitWarn = emit | ||
isFixMode = autoCorrect | ||
|
||
if (node.elementType == CLASS) | ||
checkCLass(node.psi as KtClass) | ||
} | ||
|
||
@Suppress("UnsafeCallOnNullableType") | ||
private fun checkCLass(ktClass: KtClass) { | ||
if(ktClass.primaryConstructor?.valueParameters?.isNotEmpty() != false || ktClass.primaryConstructorModifierList != null) | ||
return | ||
EMPTY_PRIMARY_CONSTRUCTOR.warnAndFix(configRules, emitWarn, isFixMode, ktClass.nameIdentifier!!.text, | ||
ktClass.node.startOffset, ktClass.node) { | ||
ktClass.node.removeChild(ktClass.primaryConstructor!!.node) | ||
} | ||
} | ||
} |
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
16 changes: 16 additions & 0 deletions
16
...-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter6/EmptyPrimaryConstructorFixTest.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,16 @@ | ||
package org.cqfn.diktat.ruleset.chapter6 | ||
|
||
import generated.WarningNames | ||
import org.cqfn.diktat.ruleset.rules.AvoidEmptyPrimaryConstructor | ||
import org.cqfn.diktat.util.FixTestBase | ||
import org.junit.jupiter.api.Tag | ||
import org.junit.jupiter.api.Test | ||
|
||
class EmptyPrimaryConstructorFixTest: FixTestBase("test/chapter6/primary_constructor", ::AvoidEmptyPrimaryConstructor) { | ||
|
||
@Test | ||
@Tag(WarningNames.EMPTY_PRIMARY_CONSTRUCTOR) | ||
fun `should remove empty primary constructor`() { | ||
fixAndCompare("EmptyPCExpected.kt", "EmptyPCTest.kt") | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
...rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter6/EmptyPrimaryConstructorWarnTest.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,62 @@ | ||
package org.cqfn.diktat.ruleset.chapter6 | ||
|
||
import com.pinterest.ktlint.core.LintError | ||
import generated.WarningNames | ||
import org.cqfn.diktat.ruleset.constants.Warnings.EMPTY_PRIMARY_CONSTRUCTOR | ||
import org.cqfn.diktat.ruleset.rules.AvoidEmptyPrimaryConstructor | ||
import org.cqfn.diktat.ruleset.rules.DIKTAT_RULE_SET_ID | ||
import org.cqfn.diktat.util.LintTestBase | ||
import org.junit.jupiter.api.Tag | ||
import org.junit.jupiter.api.Test | ||
|
||
class EmptyPrimaryConstructorWarnTest: LintTestBase(::AvoidEmptyPrimaryConstructor) { | ||
private val ruleId = "$DIKTAT_RULE_SET_ID:avoid-empty-primary-constructor" | ||
|
||
@Test | ||
@Tag(WarningNames.EMPTY_PRIMARY_CONSTRUCTOR) | ||
fun `simple classes with empty primary constructor`() { | ||
lintMethod( | ||
""" | ||
|class Some() { | ||
| val a = 10 | ||
| constructor(a: String): this() { | ||
| this.a = a | ||
| } | ||
|} | ||
| | ||
|class Some1() { | ||
| val a = 10 | ||
| companion object {} | ||
|} | ||
| | ||
|class Some2 { | ||
| val a = 10 | ||
| constructor(a: String): this() { | ||
| this.a = a | ||
| } | ||
|} | ||
| | ||
|class Some3 private constructor () { | ||
| | ||
|} | ||
""".trimMargin(), | ||
LintError(1,1,ruleId, "${EMPTY_PRIMARY_CONSTRUCTOR.warnText()} Some", true), | ||
LintError(8,1,ruleId, "${EMPTY_PRIMARY_CONSTRUCTOR.warnText()} Some1", true) | ||
) | ||
} | ||
|
||
@Test | ||
@Tag(WarningNames.EMPTY_PRIMARY_CONSTRUCTOR) | ||
fun `correct example with empty primary constructor and modifiers`() { | ||
lintMethod( | ||
""" | ||
|class Some1 private constructor () { | ||
| | ||
|} | ||
| | ||
|class Some2 @Inject constructor() { | ||
|} | ||
""".trimMargin() | ||
) | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
diktat-rules/src/test/resources/test/chapter6/primary_constructor/EmptyPCExpected.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,18 @@ | ||
package test.chapter6.primary_constructor | ||
|
||
class Test { | ||
var a: Int = 0 | ||
var b: Int = 0 | ||
} | ||
|
||
class Test { | ||
var a = "Property" | ||
|
||
init { | ||
println("some init") | ||
} | ||
|
||
constructor(a: String): this() { | ||
this.a = a | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
diktat-rules/src/test/resources/test/chapter6/primary_constructor/EmptyPCTest.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,18 @@ | ||
package test.chapter6.primary_constructor | ||
|
||
class Test() { | ||
var a: Int = 0 | ||
var b: Int = 0 | ||
} | ||
|
||
class Test() { | ||
var a = "Property" | ||
|
||
init { | ||
println("some init") | ||
} | ||
|
||
constructor(a: String): this() { | ||
this.a = a | ||
} | ||
} |
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