Skip to content

Commit

Permalink
Update ktlint to 1.0.0 (#94)
Browse files Browse the repository at this point in the history
* Update ktlint to 1.0.0

Due to the reasons exposed in pinterest/ktlint#2044, this is not trivial - we need to rewrite the fixes manually or remove them altogether. Not being able to rely on the embedded kotlin compiler plugin for this is less than ideal, I wanted to stay away from ASTNode as much as possible :(

TODO:
- [x] Fix spotless version
- [x] Fix ViewModelInjection autofix
- [x] Fix PreviewPublic autofix

* Fix ComposePreviewPublic autofix

* Use ASTNode for ComposeViewModelInjection autofix
  • Loading branch information
mrmans0n authored Sep 10, 2023
1 parent 621ac3f commit cfc1f87
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
4 changes: 3 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ subprojects {
spotless {
kotlin {
target "**/*.kt"
ktlint(libs.versions.ktlint.get())
// ktlint(libs.versions.ktlint.get())
ktlint("0.50.0")

licenseHeaderFile rootProject.file('spotless/copyright.txt')
}
groovyGradle {
Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[versions]
kotlin = "1.9.10"
ktlint = "0.50.0"
ktlint = "1.0.0"
detekt = "1.23.1"
junit = "5.10.0"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ package io.nlopez.compose.rules

import io.nlopez.rules.core.ComposeKtVisitor
import io.nlopez.rules.core.Emitter
import io.nlopez.rules.core.util.firstChildLeafOrSelf
import io.nlopez.rules.core.util.isPreview
import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafPsiElement
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.KtFunction
import org.jetbrains.kotlin.psi.psiUtil.isPublic
Expand All @@ -19,7 +21,14 @@ class ComposePreviewPublic : ComposeKtVisitor {

emitter.report(function, ComposablesPreviewShouldNotBePublic, true)
if (autoCorrect) {
function.addModifier(KtTokens.PRIVATE_KEYWORD)
// Ideally if the kotlin embeddable compiler exposes what we need, this would be it:
// function.addModifier(KtTokens.PRIVATE_KEYWORD)

// For now we need to do it by hand with ASTNode: find the "fun" modifier, and prepend "private".
val node = function.node.findChildByType(KtTokens.FUN_KEYWORD)
?.firstChildLeafOrSelf() as? LeafPsiElement
?: return
node.rawReplaceWithText(KtTokens.PRIVATE_KEYWORD.value + " " + KtTokens.FUN_KEYWORD.value)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,13 @@ class ComposeViewModelInjection : ComposeKtVisitor {

when {
// If there are no parameters, we will insert the code directly
lastParameters.isEmpty() -> parameterList.addParameter(newParam)
lastParameters.isEmpty() -> {
// Ideally this should be:
// parameterList.addParameter(newParam)
// but since Kotlin 1.9 we can't use these methods without crashing.
(parameterList.node.lastChildLeafOrSelf() as LeafPsiElement)
.rawReplaceWithText("${newParam.text})")
}
// If the last element is a function, we need to preserve the trailing lambda, so we will insert
// the code before that last param
lastParameters.last().typeReference?.typeElement is KtFunctionType -> {
Expand All @@ -92,9 +98,19 @@ class ComposeViewModelInjection : ComposeKtVisitor {
lastToken.rawReplaceWithText("${lastToken.text} $newCode,")
}
}

// Add as the last parameter
else -> {
parameterList.addParameter(newParam)
// Ideally this should just be:
// parameterList.addParameter(newParam)
// but since Kotlin 1.9 we can't use these methods without crashing.

val hasTrailingComma = composable.valueParameters.last().node.nextCodeSibling()?.text == ","
// If it has a trailing comma, no need to add a new one
val preCommaIfNeeded = if (hasTrailingComma) "" else ","
// And if it has a trailing comma, we'll need to preserve that in the style
val trailingCommaIfNeeded = if (hasTrailingComma) "," else ""
(parameterList.node.lastChildLeafOrSelf() as LeafPsiElement)
.rawReplaceWithText("$preCommaIfNeeded${newParam.text}$trailingCommaIfNeeded)")
}
}

Expand Down

0 comments on commit cfc1f87

Please sign in to comment.