-
Notifications
You must be signed in to change notification settings - Fork 2
feat: plannedRemoval ktlint rule #136
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
Changes from 4 commits
193be16
4a2750b
7b633b8
20d2e15
3ab8bdb
ad703b6
026c907
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 |
|---|---|---|
|
|
@@ -8,7 +8,9 @@ import com.pinterest.ktlint.cli.ruleset.core.api.RuleSetProviderV3 | |
| import com.pinterest.ktlint.rule.engine.core.api.RuleProvider | ||
| import com.pinterest.ktlint.rule.engine.core.api.RuleSetId | ||
|
|
||
| class MinorVersionRuleSetProvider : RuleSetProviderV3(RuleSetId("minor-version-strategy-rules")) { | ||
| internal const val RULE_SET = "minor-version-strategy-rules" | ||
|
Member
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. Can you explain this
Contributor
Author
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. Do you mean the
Member
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. In general, why do we need it?
Contributor
Author
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. Every rule ID needs to have a rule-set ID followed by |
||
|
|
||
| class MinorVersionRuleSetProvider : RuleSetProviderV3(RuleSetId(RULE_SET)) { | ||
| override fun getRuleProviders() = setOf( | ||
| RuleProvider { DeprecatedApiRule() }, | ||
| ) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| /* | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package software.aws.ktlint.rules | ||
|
|
||
| import com.pinterest.ktlint.rule.engine.core.api.ElementType | ||
| import com.pinterest.ktlint.rule.engine.core.api.Rule | ||
| import com.pinterest.ktlint.rule.engine.core.api.RuleId | ||
| import org.jetbrains.kotlin.com.intellij.lang.ASTNode | ||
|
|
||
| /** | ||
| * Creates a ktlint rule that forces APIs annotated with @DeprecatedUntilVersion to also be annotated with @Deprecated. | ||
| */ | ||
| class DeprecatedUntilVersionRule : Rule(RuleId("$RULE_SET:deprecated-until-version"), About()) { | ||
| override fun beforeVisitChildNodes( | ||
| node: ASTNode, | ||
| autoCorrect: Boolean, | ||
| emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit, | ||
| ) { | ||
| if (node.elementType == ElementType.MODIFIER_LIST) { | ||
| val annotations = node.getChildren(null).filter { it.elementType == ElementType.ANNOTATION_ENTRY } | ||
| val deprecated = annotations.any { it.text.startsWith("@Deprecated") && !it.text.contains("@DeprecatedUntilVersion") } | ||
| val deprecatedUntilVersion = annotations.any { it.text.startsWith("@DeprecatedUntilVersion") } | ||
|
|
||
| if (deprecatedUntilVersion && !deprecated) { | ||
| emit( | ||
| node.startOffset, | ||
| "APIs annotated with @DeprecatedUntilVersion must also be annotated with @Deprecated", | ||
| false, | ||
| ) | ||
| } | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| /* | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package software.aws.ktlint.rules | ||
|
|
||
| import com.pinterest.ktlint.rule.engine.api.Code | ||
| import com.pinterest.ktlint.rule.engine.api.KtLintRuleEngine | ||
| import com.pinterest.ktlint.rule.engine.core.api.RuleProvider | ||
| import kotlin.test.Test | ||
| import kotlin.test.assertEquals | ||
|
|
||
| class DeprecatedUntilVersionRuleTest { | ||
| val ruleEngine = KtLintRuleEngine( | ||
| setOf( | ||
| RuleProvider { DeprecatedUntilVersionRule() }, | ||
| ), | ||
| ) | ||
|
|
||
| private fun hasLintingErrors(codeSnippet: String): Boolean { | ||
| val code = Code.fromSnippet(codeSnippet) | ||
| var hasErrors = false | ||
| ruleEngine.lint(code) { | ||
| // Error callback function | ||
| hasErrors = true | ||
| } | ||
| return hasErrors | ||
| } | ||
|
|
||
| @Test | ||
| fun testRule() { | ||
| assertEquals( | ||
| false, | ||
| hasLintingErrors( | ||
| """ | ||
| @DeprecatedUntilVersion(1, 2) | ||
| @Deprecated | ||
| class Foo {} | ||
| """.trimIndent(), | ||
| ), | ||
| ) | ||
|
|
||
| assertEquals( | ||
| false, | ||
| hasLintingErrors( | ||
| """ | ||
| @Deprecated | ||
| @DeprecatedUntilVersion(1, 2) | ||
| class Foo {} | ||
| """.trimIndent(), | ||
| ), | ||
| ) | ||
|
|
||
| assertEquals( | ||
| false, | ||
| hasLintingErrors( | ||
| """ | ||
| @Deprecated | ||
| class Foo {} | ||
| """.trimIndent(), | ||
| ), | ||
| ) | ||
|
|
||
| assertEquals( | ||
| true, | ||
| hasLintingErrors( | ||
| """ | ||
| @DeprecatedUntilVersion(1, 2) | ||
| class Foo {} | ||
| """.trimIndent(), | ||
| ), | ||
| ) | ||
|
|
||
| assertEquals( | ||
| true, | ||
| hasLintingErrors( | ||
| """ | ||
| @DeprecatedUntilVersion(1, 2) | ||
| class Foo {} | ||
|
|
||
| @Deprecated | ||
| class Bar {} | ||
| """.trimIndent(), | ||
| ), | ||
| ) | ||
|
|
||
| assertEquals( | ||
| true, | ||
| hasLintingErrors( | ||
| """ | ||
| @DeprecatedUntilVersion(1, 2) | ||
| class Foo {} | ||
|
|
||
| @DeprecatedUntilVersion(1, 2) | ||
| class Bar {} | ||
| """.trimIndent(), | ||
| ), | ||
| ) | ||
| } | ||
| } |
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.
naming: slf4j-version