Skip to content

Commit

Permalink
Code clean-up as suggested by code inspections (#2149)
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-dingemans authored Jul 25, 2023
1 parent 673a2fa commit 557b33d
Show file tree
Hide file tree
Showing 10 changed files with 13 additions and 35 deletions.
8 changes: 4 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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),
) {
Expand Down Expand Up @@ -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),
) {
Expand All @@ -358,7 +358,7 @@ public class SomeRule : Rule(
override fun beforeFirstNode(editorConfig: EditorConfig) {
myEditorConfigProperty = editorConfig[MY_EDITOR_CONFIG_PROPERTY]
}

...
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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, '/')
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<RuleProvider> = emptySet(),
ignoreKtlintSuppressionRule: Boolean = true,
) = ArrayList<LintError>().apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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") &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 557b33d

Please sign in to comment.