generated from amazon-archives/__template_Apache-2.0
-
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
193be16
feat: deprecatedUntilVersion ktlint rule
0marperez 4a2750b
self review
0marperez 7b633b8
self review
0marperez 20d2e15
small bug fix
0marperez 3ab8bdb
feedback
0marperez ad703b6
name change for annotation
0marperez 026c907
self review
0marperez 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
36 changes: 36 additions & 0 deletions
36
ktlint/style-rules/src/main/kotlin/software/aws/ktlint/rules/DeprecatedUntilVersionRule.kt
This file contains hidden or 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,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") } | ||
| 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, | ||
| ) | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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
101 changes: 101 additions & 0 deletions
101
...t/style-rules/src/test/kotlin/software/aws/ktlint/rules/DeprecatedUntilVersionRuleTest.kt
This file contains hidden or 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,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(), | ||
| ), | ||
| ) | ||
| } | ||
| } |
Oops, something went wrong.
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.
naming: slf4j-version