-
Notifications
You must be signed in to change notification settings - Fork 39
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
Recommendation 6.1.10 no tivial getters or setters are allowed #499
Merged
Merged
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
ed07d2b
feature/recommendation-6.1.10(#445)
aktsay6 3a0fff8
feature/recommendation-6.1.10(#445)
aktsay6 6f976ea
feature/recommendation-6.1.10(#445)
aktsay6 d07ef0e
feature/recommendation-6.1.10(#445)
aktsay6 62e39ee
feature/recommendation-6.1.10(#445)
aktsay6 8fec247
Merge branch 'master' into feature/recommendation-6.1.10(#445)
aktsay6 7bd4e65
feature/recommendation-6.1.10(#445)
aktsay6 49e8262
feature/recommendation-6.1.10(#445)
aktsay6 217d5d6
feature/recommendation-6.1.10(#445)
aktsay6 6a9fd5d
Merge branch 'master' into feature/recommendation-6.1.10(#445)
aktsay6 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
97 changes: 97 additions & 0 deletions
97
diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/TrivialPropertyAccessors.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,97 @@ | ||
package org.cqfn.diktat.ruleset.rules | ||
|
||
import com.pinterest.ktlint.core.Rule | ||
import com.pinterest.ktlint.core.ast.ElementType.BINARY_EXPRESSION | ||
import com.pinterest.ktlint.core.ast.ElementType.BLOCK | ||
import com.pinterest.ktlint.core.ast.ElementType.BLOCK_COMMENT | ||
import com.pinterest.ktlint.core.ast.ElementType.EOL_COMMENT | ||
import com.pinterest.ktlint.core.ast.ElementType.LBRACE | ||
import com.pinterest.ktlint.core.ast.ElementType.PROPERTY_ACCESSOR | ||
import com.pinterest.ktlint.core.ast.ElementType.RBRACE | ||
import com.pinterest.ktlint.core.ast.ElementType.REFERENCE_EXPRESSION | ||
import com.pinterest.ktlint.core.ast.ElementType.VALUE_PARAMETER_LIST | ||
import com.pinterest.ktlint.core.ast.ElementType.WHITE_SPACE | ||
import com.pinterest.ktlint.core.ast.isWhiteSpace | ||
import org.cqfn.diktat.common.config.rules.RulesConfig | ||
import org.cqfn.diktat.ruleset.constants.Warnings | ||
import org.cqfn.diktat.ruleset.utils.findAllNodesWithSpecificType | ||
import org.cqfn.diktat.ruleset.utils.getFirstChildWithType | ||
import org.cqfn.diktat.ruleset.utils.getIdentifierName | ||
import org.cqfn.diktat.ruleset.utils.hasChildOfType | ||
import org.cqfn.diktat.ruleset.utils.prettyPrint | ||
import org.jetbrains.kotlin.com.intellij.lang.ASTNode | ||
import org.jetbrains.kotlin.psi.KtBinaryExpression | ||
import org.jetbrains.kotlin.psi.KtPropertyAccessor | ||
|
||
/** | ||
* This rule checks if there are any trivial getters and setters and, if so, deletes them | ||
*/ | ||
class TrivialPropertyAccessors(private val configRules: List<RulesConfig>) : Rule("trivial-property-accessors") { | ||
private lateinit var emitWarn: ((offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit) | ||
private var isFixMode: Boolean = false | ||
|
||
companion object { | ||
private val EXCESS_CHILDREN_TYPES = listOf(LBRACE, RBRACE, WHITE_SPACE, EOL_COMMENT, BLOCK_COMMENT) | ||
} | ||
|
||
override fun visit(node: ASTNode, | ||
autoCorrect: Boolean, | ||
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit) { | ||
emitWarn = emit | ||
isFixMode = autoCorrect | ||
|
||
if (node.elementType == PROPERTY_ACCESSOR) { | ||
handlePropertyAccessors(node) | ||
print(node.prettyPrint()) | ||
} | ||
} | ||
|
||
private fun handlePropertyAccessors(node: ASTNode) { | ||
if ((node.psi as KtPropertyAccessor).isGetter) { | ||
handleGetAccessor(node) | ||
} else { | ||
handleSetAccessor(node) | ||
} | ||
} | ||
|
||
@Suppress("UnsafeCallOnNullableType") | ||
private fun handleSetAccessor(node: ASTNode) { | ||
val valueParamName = node | ||
.getFirstChildWithType(VALUE_PARAMETER_LIST)!! | ||
.firstChildNode | ||
.getIdentifierName()!! | ||
.text | ||
|
||
if (node.hasChildOfType(BLOCK)) { | ||
val block = node.getFirstChildWithType(BLOCK)!! | ||
|
||
val blockChildren = block.getChildren(null).filter { it.elementType !in EXCESS_CHILDREN_TYPES } | ||
|
||
if (blockChildren.size == 1 | ||
&& blockChildren.first().elementType == BINARY_EXPRESSION | ||
&& (blockChildren.first().psi as KtBinaryExpression).left?.text == "field" | ||
&& (blockChildren.first().psi as KtBinaryExpression).right?.text == valueParamName | ||
) { | ||
raiseWarning(node) | ||
} | ||
} | ||
} | ||
|
||
@Suppress("UnsafeCallOnNullableType") | ||
private fun handleGetAccessor(node: ASTNode) { | ||
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. Please add a comment that this handles both |
||
val references = node.findAllNodesWithSpecificType(REFERENCE_EXPRESSION) | ||
if (references.singleOrNull()?.text == "field") { | ||
raiseWarning(node) | ||
} | ||
} | ||
|
||
private fun raiseWarning(node: ASTNode) { | ||
Warnings.TRIVIAL_ACCESSORS_ARE_NOT_RECOMMENDED.warnAndFix(configRules, emitWarn, isFixMode, node.text, node.startOffset, node) { | ||
val property = (node.psi as KtPropertyAccessor).property.node | ||
if (node.treePrev.isWhiteSpace()) { | ||
property.removeChild(node.treePrev) | ||
} | ||
property.removeChild(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
16 changes: 16 additions & 0 deletions
16
...rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter6/TrivialPropertyAccessorsFixTest.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 generated.WarningNames.TRIVIAL_ACCESSORS_ARE_NOT_RECOMMENDED | ||
import org.cqfn.diktat.util.FixTestBase | ||
import org.cqfn.diktat.ruleset.rules.TrivialPropertyAccessors | ||
import org.junit.jupiter.api.Tag | ||
import org.junit.jupiter.api.Test | ||
|
||
class TrivialPropertyAccessorsFixTest : FixTestBase("test/chapter6/properties", ::TrivialPropertyAccessors) { | ||
@Test | ||
@Tag(TRIVIAL_ACCESSORS_ARE_NOT_RECOMMENDED) | ||
fun `fix trivial setters and getters`() { | ||
fixAndCompare("TrivialPropertyAccessorsExpected.kt", "TrivialPropertyAccessorsTest.kt") | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
...ules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter6/TrivialPropertyAccessorsWarnTest.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,64 @@ | ||
package org.cqfn.diktat.ruleset.chapter6 | ||
|
||
import com.pinterest.ktlint.core.LintError | ||
import generated.WarningNames.TRIVIAL_ACCESSORS_ARE_NOT_RECOMMENDED | ||
import org.cqfn.diktat.ruleset.constants.Warnings | ||
import org.cqfn.diktat.ruleset.rules.DIKTAT_RULE_SET_ID | ||
import org.cqfn.diktat.ruleset.rules.TrivialPropertyAccessors | ||
import org.cqfn.diktat.util.LintTestBase | ||
import org.junit.jupiter.api.Tag | ||
import org.junit.jupiter.api.Test | ||
|
||
class TrivialPropertyAccessorsWarnTest : LintTestBase(::TrivialPropertyAccessors) { | ||
private val ruleId = "$DIKTAT_RULE_SET_ID:trivial-property-accessors" | ||
|
||
@Test | ||
@Tag(TRIVIAL_ACCESSORS_ARE_NOT_RECOMMENDED) | ||
fun `should trigger on trivial getter and setter`() { | ||
lintMethod( | ||
""" | ||
|class Test { | ||
| val prop: Int = 0 | ||
| get() { return field } | ||
| set(value) { field = value } | ||
|} | ||
""".trimMargin(), | ||
LintError(3, 8, ruleId, "${Warnings.TRIVIAL_ACCESSORS_ARE_NOT_RECOMMENDED.warnText()} get() { return field }", true), | ||
LintError(4, 8, ruleId, "${Warnings.TRIVIAL_ACCESSORS_ARE_NOT_RECOMMENDED.warnText()} set(value) { field = value }", true) | ||
) | ||
} | ||
|
||
@Test | ||
@Tag(TRIVIAL_ACCESSORS_ARE_NOT_RECOMMENDED) | ||
fun `should trigger on trivial getter and setter equal case`() { | ||
lintMethod( | ||
""" | ||
|class Test { | ||
| val prop: Int = 0 | ||
| get() = field | ||
|} | ||
""".trimMargin(), | ||
LintError(3, 8, ruleId, "${Warnings.TRIVIAL_ACCESSORS_ARE_NOT_RECOMMENDED.warnText()} get() = field", true) | ||
) | ||
} | ||
|
||
@Test | ||
@Tag(TRIVIAL_ACCESSORS_ARE_NOT_RECOMMENDED) | ||
fun `should not trigger on getter and setter`() { | ||
lintMethod( | ||
""" | ||
|class Test { | ||
| val prop: Int = 0 | ||
| get() { | ||
| val b = someLogic(field) | ||
| return b | ||
| } | ||
| set(value) { | ||
| val res = func(value) | ||
| field = res | ||
| } | ||
|} | ||
""".trimMargin() | ||
) | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
diktat-rules/src/test/resources/test/chapter6/properties/TrivialPropertyAccessorsExpected.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,11 @@ | ||
package test.chapter6.properties | ||
|
||
class Some { | ||
var prop: Int = 0 | ||
|
||
val prop2: Int = 0 | ||
|
||
var propNotChange: Int = 7 | ||
get() { return someCoolLogic(field) } | ||
set(value) { anotherCoolLogic(value) } | ||
} |
14 changes: 14 additions & 0 deletions
14
diktat-rules/src/test/resources/test/chapter6/properties/TrivialPropertyAccessorsTest.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,14 @@ | ||
package test.chapter6.properties | ||
|
||
class Some { | ||
var prop: Int = 0 | ||
get() = field | ||
set(value) { field = value } | ||
|
||
val prop2: Int = 0 | ||
get() { return field } | ||
|
||
var propNotChange: Int = 7 | ||
get() { return someCoolLogic(field) } | ||
set(value) { anotherCoolLogic(value) } | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
?