-
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.
New rule to check if @Preview (Jetpack Compose) functions end with 'P…
…review' suffix and are also private. Part 1 (#1726) ### What's done: * Provide warn logic * Add tests
- Loading branch information
1 parent
6d7f952
commit fbf2a57
Showing
9 changed files
with
179 additions
and
1 deletion.
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
87 changes: 87 additions & 0 deletions
87
...es/src/main/kotlin/com/saveourtool/diktat/ruleset/rules/chapter3/PreviewAnnotationRule.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,87 @@ | ||
package com.saveourtool.diktat.ruleset.rules.chapter3 | ||
|
||
import com.saveourtool.diktat.common.config.rules.RulesConfig | ||
import com.saveourtool.diktat.ruleset.constants.Warnings.PREVIEW_ANNOTATION | ||
import com.saveourtool.diktat.ruleset.rules.DiktatRule | ||
import com.saveourtool.diktat.ruleset.utils.getAllChildrenWithType | ||
import com.saveourtool.diktat.ruleset.utils.getIdentifierName | ||
|
||
import org.jetbrains.kotlin.KtNodeTypes.ANNOTATION_ENTRY | ||
import org.jetbrains.kotlin.KtNodeTypes.FUN | ||
import org.jetbrains.kotlin.KtNodeTypes.MODIFIER_LIST | ||
import org.jetbrains.kotlin.com.intellij.lang.ASTNode | ||
import org.jetbrains.kotlin.psi.KtNamedFunction | ||
import org.jetbrains.kotlin.psi.psiUtil.isPrivate | ||
|
||
/** | ||
* This rule checks, whether the method has `@Preview` annotation (Jetpack Compose) | ||
* If so, such method should be private and function name should end with `Preview` suffix | ||
*/ | ||
class PreviewAnnotationRule(configRules: List<RulesConfig>) : DiktatRule( | ||
NAME_ID, | ||
configRules, | ||
listOf(PREVIEW_ANNOTATION) | ||
) { | ||
override fun logic(node: ASTNode) { | ||
if (node.elementType == FUN) { | ||
checkFunctionSignature(node) | ||
} | ||
} | ||
|
||
private fun checkFunctionSignature(node: ASTNode) { | ||
node.findChildByType(MODIFIER_LIST)?.let { modList -> | ||
doCheck(node, modList) | ||
} | ||
} | ||
|
||
private fun doCheck(functionNode: ASTNode, modeList: ASTNode) { | ||
if (modeList.getAllChildrenWithType(ANNOTATION_ENTRY).isEmpty()) { | ||
return | ||
} | ||
|
||
val previewAnnotationNode = modeList.getAllChildrenWithType(ANNOTATION_ENTRY).firstOrNull { | ||
it.text.contains("$ANNOTATION_SYMBOL$PREVIEW_ANNOTATION_TEXT") | ||
} | ||
|
||
previewAnnotationNode?.let { | ||
val functionName = functionNode.getIdentifierName()?.text ?: return | ||
|
||
// warn if function is not private | ||
if (!((functionNode.psi as KtNamedFunction).isPrivate())) { | ||
PREVIEW_ANNOTATION.warnAndFix( | ||
configRules, | ||
emitWarn, | ||
isFixMode, | ||
"$functionName method should be private", | ||
functionNode.startOffset, | ||
functionNode | ||
) { | ||
// provide fix | ||
} | ||
} | ||
|
||
// warn if function has no `Preview` suffix | ||
if (!isMethodHasPreviewSuffix(functionName)) { | ||
PREVIEW_ANNOTATION.warnAndFix( | ||
configRules, | ||
emitWarn, | ||
isFixMode, | ||
"$functionName method should has `Preview` suffix", | ||
functionNode.startOffset, | ||
functionNode | ||
) { | ||
// provide fix | ||
} | ||
} | ||
} | ||
} | ||
|
||
private fun isMethodHasPreviewSuffix(functionName: String) = | ||
functionName.contains(PREVIEW_ANNOTATION_TEXT) | ||
|
||
companion object { | ||
const val ANNOTATION_SYMBOL = "@" | ||
const val NAME_ID = "preview-annotation" | ||
const val PREVIEW_ANNOTATION_TEXT = "Preview" | ||
} | ||
} |
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
67 changes: 67 additions & 0 deletions
67
...ules/src/test/kotlin/com/saveourtool/diktat/ruleset/chapter3/PreviewAnnotationWarnTest.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,67 @@ | ||
package com.saveourtool.diktat.ruleset.chapter3 | ||
|
||
import com.saveourtool.diktat.api.DiktatError | ||
import com.saveourtool.diktat.common.config.rules.DIKTAT_RULE_SET_ID | ||
import com.saveourtool.diktat.ruleset.constants.Warnings | ||
import com.saveourtool.diktat.ruleset.rules.chapter3.PreviewAnnotationRule | ||
import com.saveourtool.diktat.util.LintTestBase | ||
import generated.WarningNames | ||
import org.junit.jupiter.api.Tag | ||
import org.junit.jupiter.api.Test | ||
|
||
|
||
class PreviewAnnotationWarnTest : LintTestBase(::PreviewAnnotationRule) { | ||
private val ruleId = "$DIKTAT_RULE_SET_ID:${PreviewAnnotationRule.NAME_ID}" | ||
|
||
@Test | ||
@Tag(WarningNames.PREVIEW_ANNOTATION) | ||
fun `no warn`() { | ||
lintMethod( | ||
""" | ||
|@Preview | ||
|@Composable | ||
|private fun BannerPreview() {} | ||
""".trimMargin() | ||
) | ||
} | ||
|
||
@Test | ||
@Tag(WarningNames.PREVIEW_ANNOTATION) | ||
fun `method is not private`() { | ||
lintMethod( | ||
""" | ||
|@Preview | ||
|@Composable | ||
|fun BannerPreview() {} | ||
""".trimMargin(), | ||
DiktatError(1, 1, ruleId, "${Warnings.PREVIEW_ANNOTATION.warnText()} BannerPreview method should be private", false), | ||
) | ||
} | ||
|
||
@Test | ||
@Tag(WarningNames.PREVIEW_ANNOTATION) | ||
fun `method has no preview suffix`() { | ||
lintMethod( | ||
""" | ||
|@Preview | ||
|@Composable | ||
|private fun Banner() {} | ||
""".trimMargin(), | ||
DiktatError(1, 1, ruleId, "${Warnings.PREVIEW_ANNOTATION.warnText()} Banner method should has `Preview` suffix", false), | ||
) | ||
} | ||
|
||
@Test | ||
@Tag(WarningNames.PREVIEW_ANNOTATION) | ||
fun `method has no preview suffix and is not private`() { | ||
lintMethod( | ||
""" | ||
|@Preview | ||
|@Composable | ||
|fun Banner() {} | ||
""".trimMargin(), | ||
DiktatError(1, 1, ruleId, "${Warnings.PREVIEW_ANNOTATION.warnText()} Banner method should be private", false), | ||
DiktatError(1, 1, ruleId, "${Warnings.PREVIEW_ANNOTATION.warnText()} Banner method should has `Preview` suffix", false), | ||
) | ||
} | ||
} |
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