Skip to content
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

Update ktlint to 1.0.0 #94

Merged
merged 3 commits into from
Sep 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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