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 all 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,17 +13,17 @@ import java.io.File | |
| import java.util.Properties | ||
|
|
||
| /** | ||
| * Matches @DeprecatedUntilVersion with either named args (major=x, minor=y) or positional args (x, y) | ||
| * Matches @PlannedRemoval with either named args (major=x, minor=y) or positional args (x, y) | ||
| */ | ||
| internal fun deprecatedUntilVersionRegex(major: Int, minor: Int): Regex = | ||
| internal fun plannedRemovalRegex(major: Int, minor: Int): Regex = | ||
| Regex( | ||
| """@DeprecatedUntilVersion\s*\(\s*(?:major\s*=\s*$major\s*,\s*minor\s*=\s*$minor\s*|\s*$major\s*,\s*$minor\s*)\s*\)""", | ||
| """@PlannedRemoval\s*\(\s*(?:major\s*=\s*$major\s*,\s*minor\s*=\s*$minor\s*|\s*$major\s*,\s*$minor\s*)\s*\)""", | ||
| ) | ||
|
|
||
| /** | ||
| * Creates a ktlint rule that detects APIs annotated with @DeprecatedUntilVersion for the upcoming minor version. | ||
| * Creates a ktlint rule that detects APIs annotated with @PlannedRemoval for the upcoming minor version. | ||
| */ | ||
| class DeprecatedApiRule : Rule(RuleId("minor-version-strategy-rules:deprecated-apis"), About()) { | ||
| class PlannedRemovalRule : Rule(RuleId("$RULE_SET:planned-removal"), About()) { | ||
| override fun beforeVisitChildNodes( | ||
| node: ASTNode, | ||
| autoCorrect: Boolean, | ||
|
|
@@ -38,12 +38,12 @@ class DeprecatedApiRule : Rule(RuleId("minor-version-strategy-rules:deprecated-a | |
| val majorVersion = sdkVersion[0].toInt() | ||
| val minorVersion = sdkVersion[1].toInt() | ||
|
|
||
| val regex = deprecatedUntilVersionRegex(majorVersion, minorVersion + 1) | ||
| val regex = plannedRemovalRegex(majorVersion, minorVersion + 1) | ||
| if (regex.containsMatchIn(node.text)) { | ||
| emit( | ||
| node.startOffset, | ||
| "The deprecated API is scheduled for removal, please remove it before releasing the next minor version.", | ||
| true, | ||
| "API is scheduled for removal, remove it before releasing the next minor version.", | ||
| false, | ||
|
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. This is to indicate if the rule can be auto-corrected. It should've been false from the beginning. |
||
| ) | ||
| } | ||
| } | ||
|
|
||
38 changes: 0 additions & 38 deletions
38
...nt/minor-version-rules/src/test/kotlin/software/aws/ktlint/rules/DeprecatedApiRuleTest.kt
This file was deleted.
Oops, something went wrong.
38 changes: 38 additions & 0 deletions
38
...t/minor-version-rules/src/test/kotlin/software/aws/ktlint/rules/PlannedRemovalRuleTest.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,38 @@ | ||
| /* | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package software.aws.ktlint.rules | ||
|
|
||
| import kotlin.test.Test | ||
| import kotlin.test.assertFalse | ||
| import kotlin.test.assertTrue | ||
|
|
||
| class PlannedRemovalRuleTest { | ||
| fun runRegexTestCases(minor: Int, major: Int) { | ||
| val regex = plannedRemovalRegex(major, minor) | ||
|
|
||
| assertTrue(regex.containsMatchIn("@PlannedRemoval($major,$minor)")) | ||
| assertTrue(regex.containsMatchIn("@PlannedRemoval($major,$minor )")) | ||
| assertTrue(regex.containsMatchIn("@PlannedRemoval($major, $minor)")) | ||
| assertTrue(regex.containsMatchIn("@PlannedRemoval($major ,$minor)")) | ||
| assertTrue(regex.containsMatchIn("@PlannedRemoval( $major,$minor)")) | ||
| assertTrue(regex.containsMatchIn("@PlannedRemoval( $major , $minor )")) | ||
| assertTrue(regex.containsMatchIn("@PlannedRemoval(major=$major,minor=$minor)")) | ||
| assertTrue(regex.containsMatchIn("@PlannedRemoval( major= $major , minor= $minor )")) | ||
|
|
||
| assertFalse(regex.containsMatchIn("@PlannedRemoval")) | ||
| assertFalse(regex.containsMatchIn("@PlannedRemoval()")) | ||
| assertFalse(regex.containsMatchIn("@PlannedRemoval($minor,$minor)")) | ||
| assertFalse(regex.containsMatchIn("@PlannedRemoval($major,$major)")) | ||
| assertFalse(regex.containsMatchIn("@PlannedRemoval($minor,$major)")) | ||
| } | ||
|
|
||
| @Test | ||
| fun testRegex() { | ||
| runRegexTestCases(0, 1) | ||
| runRegexTestCases(1, 70) | ||
| runRegexTestCases(100, 1_000_000) | ||
| } | ||
| } |
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/PlannedRemovalRule.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 @PlannedRemoval to also be annotated with @Deprecated. | ||
| */ | ||
| class PlannedRemovalRule : Rule(RuleId("$RULE_SET:planned-removal"), 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 plannedRemoval = annotations.any { it.text.startsWith("@PlannedRemoval") } | ||
|
|
||
| if (plannedRemoval && !deprecated) { | ||
| emit( | ||
| node.startOffset, | ||
| "APIs annotated with @PlannedRemoval 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
101 changes: 101 additions & 0 deletions
101
ktlint/style-rules/src/test/kotlin/software/aws/ktlint/rules/PlannedRemovalRuleTest.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 PlannedRemovalRuleTest { | ||
| val ruleEngine = KtLintRuleEngine( | ||
| setOf( | ||
| RuleProvider { PlannedRemovalRule() }, | ||
| ), | ||
| ) | ||
|
|
||
| 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( | ||
| """ | ||
| @PlannedRemoval(1, 2) | ||
| @Deprecated | ||
| class Foo {} | ||
| """.trimIndent(), | ||
| ), | ||
| ) | ||
|
|
||
| assertEquals( | ||
| false, | ||
| hasLintingErrors( | ||
| """ | ||
| @Deprecated | ||
| @PlannedRemoval(1, 2) | ||
| class Foo {} | ||
| """.trimIndent(), | ||
| ), | ||
| ) | ||
|
|
||
| assertEquals( | ||
| false, | ||
| hasLintingErrors( | ||
| """ | ||
| @Deprecated | ||
| class Foo {} | ||
| """.trimIndent(), | ||
| ), | ||
| ) | ||
|
|
||
| assertEquals( | ||
| true, | ||
| hasLintingErrors( | ||
| """ | ||
| @PlannedRemoval(1, 2) | ||
| class Foo {} | ||
| """.trimIndent(), | ||
| ), | ||
| ) | ||
|
|
||
| assertEquals( | ||
| true, | ||
| hasLintingErrors( | ||
| """ | ||
| @PlannedRemoval(1, 2) | ||
| class Foo {} | ||
|
|
||
| @Deprecated | ||
| class Bar {} | ||
| """.trimIndent(), | ||
| ), | ||
| ) | ||
|
|
||
| assertEquals( | ||
| true, | ||
| hasLintingErrors( | ||
| """ | ||
| @PlannedRemoval(1, 2) | ||
| class Foo {} | ||
|
|
||
| @PlannedRemoval(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.
Can you explain this
RULE_SETrefactor?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.
Do you mean the
MinorVersionRuleSetProviderone or in general?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.
In general, why do we need it?
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.
Every rule ID needs to have a rule-set ID followed by
:and then the name of the rule. This is just to stop repeating the rule-set ID so many times.