From 557b33d8fcbb22378e50fc1fedade07554e8d461 Mon Sep 17 00:00:00 2001 From: Paul Dingemans Date: Tue, 25 Jul 2023 20:57:25 +0200 Subject: [PATCH] Code clean-up as suggested by code inspections (#2149) --- CHANGELOG.md | 8 ++++---- .../ktlint/cli/internal/KtlintCommandLine.kt | 15 --------------- .../internal/SuppressionLocatorBuilderTest.kt | 2 +- .../standard/rules/ArgumentListWrappingRule.kt | 2 +- .../ruleset/standard/rules/CommentSpacingRule.kt | 2 +- .../ruleset/standard/rules/FunctionLiteral.kt | 4 +--- .../standard/rules/TrailingCommaOnCallSiteRule.kt | 2 +- .../rules/TrailingCommaOnDeclarationSiteRule.kt | 4 ++-- .../ktlint/ruleset/standard/rules/WrappingRule.kt | 5 +---- .../rules/internal/importordering/PatternEntry.kt | 4 +--- 10 files changed, 13 insertions(+), 35 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e4213870c3..6f9e7f4be0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -309,10 +309,10 @@ The rule id's in `com.pinterest.ktlint.ruleset.core.api.Rule` have been changed When wrapping a rule from the ktlint project and modifying its behavior, please change the `ruleId` and `about` fields in the wrapped rule, so that it is clear to users whenever they use the original rule provided by KtLint versus a modified version which is not maintained by the KtLint project. -The typealias `com.pinterest.ktlint.core.api.EditorConfigProperties` has been replaced with `com.pinterest.ktlint.rule.engine.core.api.EditorConfig`. The interface `com.pinterest.ktlint.core.api.UsesEditorConfigProperties` has been removed. Instead, the Rule property `usesEditorConfigProperties` needs to be set. As a result of those changes, the `beforeFirstNode` function in each rule has to changed to something like below: +The typealias `com.pinterest.ktlint.core.api.EditorConfigProperties` has been replaced with `com.pinterest.ktlint.rule.engine.core.api.EditorConfig`. The interface `com.pinterest.ktlint.core.api.UsesEditorConfigProperties` has been removed. Instead, the Rule property `usesEditorConfigProperties` needs to be set. As a result of those changes, the `beforeFirstNode` function in each rule has to changed to something like below: ```kotlin -public class SomeRule : Rule( + class SomeRule : Rule( ruleId = RuleId("some-rule-set:some-rule"), usesEditorConfigProperties = setOf(MY_EDITOR_CONFIG_PROPERTY), ) { @@ -349,7 +349,7 @@ When wrapping a rule from the ktlint project and modifying its behavior, please The typealias `com.pinterest.ktlint.core.api.EditorConfigProperties` has been replaced with `com.pinterest.ktlint.rule.engine.core.api.EditorConfig`. The interface `com.pinterest.ktlint.core.api.UsesEditorConfigProperties` has been removed. Instead, the Rule property `usesEditorConfigProperties` needs to be set. As a result of those changes, the `beforeFirstNode` function in each rule has to changed to something like below: ```kotlin -public class SomeRule : Rule( + class SomeRule : Rule( ruleId = RuleId("some-rule-set:some-rule"), usesEditorConfigProperties = setOf(MY_EDITOR_CONFIG_PROPERTY), ) { @@ -358,7 +358,7 @@ public class SomeRule : Rule( override fun beforeFirstNode(editorConfig: EditorConfig) { myEditorConfigProperty = editorConfig[MY_EDITOR_CONFIG_PROPERTY] } - + ... } ``` diff --git a/ktlint-cli/src/main/kotlin/com/pinterest/ktlint/cli/internal/KtlintCommandLine.kt b/ktlint-cli/src/main/kotlin/com/pinterest/ktlint/cli/internal/KtlintCommandLine.kt index 9961d6deaf..faa6101dac 100644 --- a/ktlint-cli/src/main/kotlin/com/pinterest/ktlint/cli/internal/KtlintCommandLine.kt +++ b/ktlint-cli/src/main/kotlin/com/pinterest/ktlint/cli/internal/KtlintCommandLine.kt @@ -41,7 +41,6 @@ import picocli.CommandLine.ParameterException import picocli.CommandLine.Parameters import java.io.File import java.nio.file.FileSystems -import java.nio.file.Path import java.nio.file.Paths import java.util.Locale import java.util.concurrent.ArrayBlockingQueue @@ -52,8 +51,6 @@ import java.util.concurrent.TimeUnit import java.util.concurrent.atomic.AtomicBoolean import java.util.concurrent.atomic.AtomicInteger import kotlin.concurrent.thread -import kotlin.io.path.pathString -import kotlin.io.path.relativeToOrSelf import kotlin.system.exitProcess private lateinit var logger: KLogger @@ -749,15 +746,3 @@ internal fun exitKtLintProcess(status: Int): Nothing { logger.debug { "Exit ktlint with exit code: $status" } exitProcess(status) } - -/** - * Gets the relative route of the path. Also adjusts the slashes for uniformity between file systems. - */ -internal val Path.relativeRoute: String - get() { - val rootPath = Paths.get("").toAbsolutePath() - return this - .relativeToOrSelf(rootPath) - .pathString - .replace(File.separatorChar, '/') - } diff --git a/ktlint-rule-engine/src/test/kotlin/com/pinterest/ktlint/rule/engine/internal/SuppressionLocatorBuilderTest.kt b/ktlint-rule-engine/src/test/kotlin/com/pinterest/ktlint/rule/engine/internal/SuppressionLocatorBuilderTest.kt index d1fd275808..1c1317a40f 100644 --- a/ktlint-rule-engine/src/test/kotlin/com/pinterest/ktlint/rule/engine/internal/SuppressionLocatorBuilderTest.kt +++ b/ktlint-rule-engine/src/test/kotlin/com/pinterest/ktlint/rule/engine/internal/SuppressionLocatorBuilderTest.kt @@ -388,7 +388,7 @@ class SuppressionLocatorBuilderTest { private fun lint( code: String, - editorConfigOverride: EditorConfigOverride = EditorConfigOverride.EMPTY_EDITOR_CONFIG_OVERRIDE, + editorConfigOverride: EditorConfigOverride = EMPTY_EDITOR_CONFIG_OVERRIDE, ruleProviders: Set = emptySet(), ignoreKtlintSuppressionRule: Boolean = true, ) = ArrayList().apply { diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ArgumentListWrappingRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ArgumentListWrappingRule.kt index 8c316b689e..84242d55e3 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ArgumentListWrappingRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ArgumentListWrappingRule.kt @@ -244,7 +244,7 @@ public class ArgumentListWrappingRule : ?.any { it.isWhiteSpaceWithNewline() } == true private fun ASTNode.isPartOfDotQualifiedAssignmentExpression(): Boolean = - treeParent?.treeParent?.elementType == ElementType.BINARY_EXPRESSION && + treeParent?.treeParent?.elementType == BINARY_EXPRESSION && treeParent?.treeParent?.children()?.find { it.elementType == ElementType.DOT_QUALIFIED_EXPRESSION } != null private fun ASTNode.prevWhiteSpaceWithNewLine(): ASTNode? { diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/CommentSpacingRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/CommentSpacingRule.kt index 36dbb76567..b7549a5b62 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/CommentSpacingRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/CommentSpacingRule.kt @@ -23,7 +23,7 @@ public class CommentSpacingRule : StandardRule("comment-spacing") { node.upsertWhitespaceBeforeMe(" ") } } - val text = node.getText() + val text = node.text if (text.length != 2 && !text.startsWith("// ") && !text.startsWith("//noinspection") && diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/FunctionLiteral.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/FunctionLiteral.kt index 8a98cb0f23..8d6e905173 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/FunctionLiteral.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/FunctionLiteral.kt @@ -243,9 +243,7 @@ public class FunctionLiteralRule : emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit, autoCorrect: Boolean, ) { - require(arrow.elementType == ARROW) { - Unit - } + require(arrow.elementType == ARROW) arrow .nextLeaf() .takeIf { it.isWhiteSpace() } diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/TrailingCommaOnCallSiteRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/TrailingCommaOnCallSiteRule.kt index 5ca274a435..d31fb28a69 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/TrailingCommaOnCallSiteRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/TrailingCommaOnCallSiteRule.kt @@ -218,7 +218,7 @@ public class TrailingCommaOnCallSiteRule : } else { prevCodeLeaf() } - return codeLeaf?.takeIf { it.elementType == ElementType.COMMA } + return codeLeaf?.takeIf { it.elementType == COMMA } } private enum class TrailingCommaState { diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/TrailingCommaOnDeclarationSiteRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/TrailingCommaOnDeclarationSiteRule.kt index 79cb5a2e52..0b6677350c 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/TrailingCommaOnDeclarationSiteRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/TrailingCommaOnDeclarationSiteRule.kt @@ -251,7 +251,7 @@ public class TrailingCommaOnDeclarationSiteRule : children() .lastOrNull { it.psi is KtEnumEntry } ?.children() - ?.singleOrNull { it.elementType == ElementType.SEMICOLON } + ?.singleOrNull { it.elementType == SEMICOLON } ?: lastChildNode private fun ASTNode.reportAndCorrectTrailingCommaNodeBefore( @@ -424,7 +424,7 @@ public class TrailingCommaOnDeclarationSiteRule : } else { prevCodeLeaf() } - return codeLeaf?.takeIf { it.elementType == ElementType.COMMA } + return codeLeaf?.takeIf { it.elementType == COMMA } } private fun containsLineBreakInLeavesRange( diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/WrappingRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/WrappingRule.kt index 2a72cfee9b..7548c5de31 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/WrappingRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/WrappingRule.kt @@ -491,10 +491,7 @@ public class WrappingRule : val rToken = lToken.nextSibling { it.elementType == rElementType } return rToken?.treeParent == lToken.treeParent } - if (nextCodeSibling?.textContains('\n') == false) { - return true - } - return false + return nextCodeSibling?.textContains('\n') == false } private fun rearrangeArrow( diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/internal/importordering/PatternEntry.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/internal/importordering/PatternEntry.kt index 929c15ef73..0a731ce34a 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/internal/importordering/PatternEntry.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/internal/importordering/PatternEntry.kt @@ -65,9 +65,7 @@ public class PatternEntry( if (withSubpackages != other.withSubpackages) return false if (hasAlias != other.hasAlias) return false - if (packageName != other.packageName) return false - - return true + return packageName == other.packageName } override fun hashCode(): Int {