Skip to content

Commit

Permalink
Replace kotlin public data classes with Poko compiler plugin genera…
Browse files Browse the repository at this point in the history
…ted ones (#2136)

* Add poko library
* Repalce public data class with generated classes by Poko compiler
* Re-record new API surface
* Add simple bash script to verify if public API contains data classes
  • Loading branch information
mateuszkwiecinski authored Jul 28, 2023
1 parent 557b33d commit 4866beb
Show file tree
Hide file tree
Showing 23 changed files with 187 additions and 174 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/pull-request-with-code.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,24 @@ jobs:
run: ./gradlew build ktlintCheck --no-configuration-cache
- name: Build with dev Kotlin version
run: ./gradlew -PkotlinDev build ktlintCheck --no-configuration-cache

- name: Check `data class`es are not part of public API
run: |
found=0
# Loop through all .api files in the current directory and subdirectories
for file in $(find . -type f -name "*.api" ! -path "*/build/*"); do
# Check if the file contains the 'data class' specific text
if grep -q "public static synthetic fun copy$default" "$file"; then
echo "public 'data class' found in: $file"
found=1
fi
done
if [ $found -eq 0 ]; then
exit 0
else
echo "data classes found in one or more .api files. Visit https://github.com/pinterest/ktlint/issues/2133 for more details"
exit 1
fi
shell: bash
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ This project adheres to [Semantic Versioning](https://semver.org/).
* Add rule `class-signature`. This rule rewrites the class header to a consistent format. In code style `ktlint_official`, super types are always wrapped to a separate line. In other code styles, super types are only wrapped in classes having multiple super types. Especially for code style `ktlint_official` the class headers are rewritten in a more consistent format. See [examples in documentation](https://pinterest.github.io/ktlint/latest/rules/experimental/#class-signature). `class-signature` [#875](https://github.com/pinterest/ktlint/issues/1349), [#1349](https://github.com/pinterest/ktlint/issues/875)

### Removed
* As a part of public API stabilization, data classes are no longer used in the public API. As of that, functions like `copy()` or `componentN()` (used for destructuring declarations) are not available anymore. This is a binary incompatible change, breaking backwards compatibility. ([#2133](https://github.com/pinterest/ktlint/issues/2133))

### Fixed

Expand Down
1 change: 1 addition & 0 deletions build-logic/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ dependencies {
}
implementation(kotlinPlugin)
implementation(libs.dokka)
implementation(libs.poko)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
kotlin("jvm")
id("dev.drewhamilton.poko")
}

kotlin {
Expand Down
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ ec4j = "org.ec4j.core:ec4j-core:0.3.0"
picocli = "info.picocli:picocli:4.7.4"
logging = "io.github.microutils:kotlin-logging-jvm:3.0.5"
slf4j = "org.slf4j:slf4j-simple:2.0.7"
# TODO: update to 0.14.0 after upgrade to Kotlin Gradle Plugin 1.9
poko = "dev.drewhamilton.poko:poko-gradle-plugin:0.13.1"
# Use logback-classic as the logger for kotlin-logging / slf4j as it allow changing the log level at runtime.
logback = "ch.qos.logback:logback-classic:1.3.5"
# Required for logback.xml conditional configuration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ public class BaselineReporter(
// checking out this repository on a different path, the baseline will still be respected.
val relativeFile = Paths.get(file).relativeLocation()
out.println(""" <file name="${relativeFile.escapeXMLAttrValue()}">""")
for ((line, col, ruleId, _) in errList) {
out.println(""" <error line="$line" column="$col" source="$ruleId" />""")
for (err in errList) {
with(err) {
out.println(""" <error line="$line" column="$col" source="$ruleId" />""")
}
}
out.println(""" </file>""")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ public class CheckStyleReporter(
out.println("""<checkstyle version="8.0">""")
for ((file, errList) in acc.entries.sortedBy { it.key }) {
out.println(""" <file name="${file.escapeXMLAttrValue()}">""")
for ((line, col, ruleId, detail) in errList) {
val message = detail.escapeXMLAttrValue()
out.println(
""" <error line="$line" column="$col" severity="error" message="$message" source="$ruleId" />""",
)
for (err in errList) {
with(err) {
val message = detail.escapeXMLAttrValue()
out.println(
""" <error line="$line" column="$col" severity="error" message="$message" source="$ruleId" />""",
)
}
}
out.println(""" </file>""")
}
Expand Down
7 changes: 0 additions & 7 deletions ktlint-cli-reporter-core/api/ktlint-cli-reporter-core.api
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
public final class com/pinterest/ktlint/cli/reporter/core/api/KtlintCliError : java/io/Serializable {
public fun <init> (IILjava/lang/String;Ljava/lang/String;Lcom/pinterest/ktlint/cli/reporter/core/api/KtlintCliError$Status;)V
public final fun component1 ()I
public final fun component2 ()I
public final fun component3 ()Ljava/lang/String;
public final fun component4 ()Ljava/lang/String;
public final fun component5 ()Lcom/pinterest/ktlint/cli/reporter/core/api/KtlintCliError$Status;
public final fun copy (IILjava/lang/String;Ljava/lang/String;Lcom/pinterest/ktlint/cli/reporter/core/api/KtlintCliError$Status;)Lcom/pinterest/ktlint/cli/reporter/core/api/KtlintCliError;
public static synthetic fun copy$default (Lcom/pinterest/ktlint/cli/reporter/core/api/KtlintCliError;IILjava/lang/String;Ljava/lang/String;Lcom/pinterest/ktlint/cli/reporter/core/api/KtlintCliError$Status;ILjava/lang/Object;)Lcom/pinterest/ktlint/cli/reporter/core/api/KtlintCliError;
public fun equals (Ljava/lang/Object;)Z
public final fun getCol ()I
public final fun getDetail ()Ljava/lang/String;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.pinterest.ktlint.cli.reporter.core.api

import dev.drewhamilton.poko.Poko
import java.io.Serializable

/**
Expand All @@ -11,12 +12,13 @@ import java.io.Serializable
* [detail]: error message
* [status]: status of error
*/
public data class KtlintCliError(
val line: Int,
val col: Int,
val ruleId: String,
val detail: String,
val status: Status,
@Poko
public class KtlintCliError(
public val line: Int,
public val col: Int,
public val ruleId: String,
public val detail: String,
public val status: Status,
) : Serializable {
public enum class Status {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,10 @@ public class HtmlReporter(
acc.forEach { (file: String, ktlintCliErrors: MutableList<KtlintCliError>) ->
h3 { text(file) }
ul {
ktlintCliErrors.forEach { (line, col, ruleId, detail) ->
item("($line, $col): $detail ($ruleId)")
ktlintCliErrors.forEach { err ->
with(err) {
item("($line, $col): $detail ($ruleId)")
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,14 @@ public class JsonReporter(
out.println(""" "errors": [""")
val errIndexLast = errList.size - 1
for ((errIndex, err) in errList.withIndex()) {
val (line, col, ruleId, detail) = err
out.println(""" {""")
out.println(""" "line": $line,""")
out.println(""" "column": $col,""")
out.println(""" "message": "${detail.escapeJsonValue()}",""")
out.println(""" "rule": "$ruleId"""")
out.println(""" }${if (errIndex != errIndexLast) "," else ""}""")
with(err) {
out.println(""" {""")
out.println(""" "line": $line,""")
out.println(""" "column": $col,""")
out.println(""" "message": "${detail.escapeJsonValue()}",""")
out.println(""" "rule": "$ruleId"""")
out.println(""" }${if (errIndex != errIndexLast) "," else ""}""")
}
}
out.println(""" ]""")
out.println(""" }${if (index != indexLast) "," else ""}""")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,18 @@ public class PlainReporter(
if (groupByFile) {
val errList = acc[file] ?: return
out.println(colorFileName(file))
for ((line, col, ruleId, detail) in errList) {
val column =
if (pad) {
String.format("%-3s", col)
} else {
col
}
out.println(
" $line${":$column".colored()} $detail ${"($ruleId)".colored()}",
)
for (err in errList) {
with(err) {
val column =
if (pad) {
String.format("%-3s", col)
} else {
col
}
out.println(
" $line${":$column".colored()} $detail ${"($ruleId)".colored()}",
)
}
}
}
}
Expand Down
33 changes: 0 additions & 33 deletions ktlint-rule-engine-core/api/ktlint-rule-engine-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -342,11 +342,7 @@ public final class com/pinterest/ktlint/rule/engine/core/api/IndentConfig {
public fun <init> (Lcom/pinterest/ktlint/rule/engine/core/api/IndentConfig$IndentStyle;I)V
public fun <init> (Lorg/ec4j/core/model/PropertyType$IndentStyleValue;I)V
public final fun childIndentOf (Lorg/jetbrains/kotlin/com/intellij/lang/ASTNode;)Ljava/lang/String;
public final fun component1 ()Lcom/pinterest/ktlint/rule/engine/core/api/IndentConfig$IndentStyle;
public final fun component2 ()I
public final fun containsUnexpectedIndentChar (Ljava/lang/String;)Z
public final fun copy (Lcom/pinterest/ktlint/rule/engine/core/api/IndentConfig$IndentStyle;I)Lcom/pinterest/ktlint/rule/engine/core/api/IndentConfig;
public static synthetic fun copy$default (Lcom/pinterest/ktlint/rule/engine/core/api/IndentConfig;Lcom/pinterest/ktlint/rule/engine/core/api/IndentConfig$IndentStyle;IILjava/lang/Object;)Lcom/pinterest/ktlint/rule/engine/core/api/IndentConfig;
public fun equals (Ljava/lang/Object;)Z
public final fun getDisabled ()Z
public final fun getIndent ()Ljava/lang/String;
Expand Down Expand Up @@ -394,11 +390,6 @@ public final class com/pinterest/ktlint/rule/engine/core/api/Rule$About {
public fun <init> ()V
public fun <init> (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V
public synthetic fun <init> (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public final fun component1 ()Ljava/lang/String;
public final fun component2 ()Ljava/lang/String;
public final fun component3 ()Ljava/lang/String;
public final fun copy (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Lcom/pinterest/ktlint/rule/engine/core/api/Rule$About;
public static synthetic fun copy$default (Lcom/pinterest/ktlint/rule/engine/core/api/Rule$About;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;ILjava/lang/Object;)Lcom/pinterest/ktlint/rule/engine/core/api/Rule$About;
public fun equals (Ljava/lang/Object;)Z
public final fun getIssueTrackerUrl ()Ljava/lang/String;
public final fun getMaintainer ()Ljava/lang/String;
Expand All @@ -418,10 +409,6 @@ public abstract class com/pinterest/ktlint/rule/engine/core/api/Rule$VisitorModi

public final class com/pinterest/ktlint/rule/engine/core/api/Rule$VisitorModifier$RunAfterRule : com/pinterest/ktlint/rule/engine/core/api/Rule$VisitorModifier {
public fun <init> (Lcom/pinterest/ktlint/rule/engine/core/api/RuleId;Lcom/pinterest/ktlint/rule/engine/core/api/Rule$VisitorModifier$RunAfterRule$Mode;)V
public final fun component1 ()Lcom/pinterest/ktlint/rule/engine/core/api/RuleId;
public final fun component2 ()Lcom/pinterest/ktlint/rule/engine/core/api/Rule$VisitorModifier$RunAfterRule$Mode;
public final fun copy (Lcom/pinterest/ktlint/rule/engine/core/api/RuleId;Lcom/pinterest/ktlint/rule/engine/core/api/Rule$VisitorModifier$RunAfterRule$Mode;)Lcom/pinterest/ktlint/rule/engine/core/api/Rule$VisitorModifier$RunAfterRule;
public static synthetic fun copy$default (Lcom/pinterest/ktlint/rule/engine/core/api/Rule$VisitorModifier$RunAfterRule;Lcom/pinterest/ktlint/rule/engine/core/api/RuleId;Lcom/pinterest/ktlint/rule/engine/core/api/Rule$VisitorModifier$RunAfterRule$Mode;ILjava/lang/Object;)Lcom/pinterest/ktlint/rule/engine/core/api/Rule$VisitorModifier$RunAfterRule;
public fun equals (Ljava/lang/Object;)Z
public final fun getMode ()Lcom/pinterest/ktlint/rule/engine/core/api/Rule$VisitorModifier$RunAfterRule$Mode;
public final fun getRuleId ()Lcom/pinterest/ktlint/rule/engine/core/api/RuleId;
Expand All @@ -443,9 +430,6 @@ public final class com/pinterest/ktlint/rule/engine/core/api/Rule$VisitorModifie
public final class com/pinterest/ktlint/rule/engine/core/api/RuleId {
public static final field Companion Lcom/pinterest/ktlint/rule/engine/core/api/RuleId$Companion;
public fun <init> (Ljava/lang/String;)V
public final fun component1 ()Ljava/lang/String;
public final fun copy (Ljava/lang/String;)Lcom/pinterest/ktlint/rule/engine/core/api/RuleId;
public static synthetic fun copy$default (Lcom/pinterest/ktlint/rule/engine/core/api/RuleId;Ljava/lang/String;ILjava/lang/Object;)Lcom/pinterest/ktlint/rule/engine/core/api/RuleId;
public fun equals (Ljava/lang/Object;)Z
public final fun getRuleSetId ()Lcom/pinterest/ktlint/rule/engine/core/api/RuleSetId;
public final fun getValue ()Ljava/lang/String;
Expand Down Expand Up @@ -477,9 +461,6 @@ public final class com/pinterest/ktlint/rule/engine/core/api/RuleProviderKt {
public final class com/pinterest/ktlint/rule/engine/core/api/RuleSetId {
public static final field Companion Lcom/pinterest/ktlint/rule/engine/core/api/RuleSetId$Companion;
public fun <init> (Ljava/lang/String;)V
public final fun component1 ()Ljava/lang/String;
public final fun copy (Ljava/lang/String;)Lcom/pinterest/ktlint/rule/engine/core/api/RuleSetId;
public static synthetic fun copy$default (Lcom/pinterest/ktlint/rule/engine/core/api/RuleSetId;Ljava/lang/String;ILjava/lang/Object;)Lcom/pinterest/ktlint/rule/engine/core/api/RuleSetId;
public fun equals (Ljava/lang/Object;)Z
public final fun getValue ()Ljava/lang/String;
public fun hashCode ()I
Expand Down Expand Up @@ -510,8 +491,6 @@ public final class com/pinterest/ktlint/rule/engine/core/api/editorconfig/Editor
public fun <init> ([Lorg/ec4j/core/model/Property;)V
public final fun addPropertiesWithDefaultValueIfMissing ([Lcom/pinterest/ktlint/rule/engine/core/api/editorconfig/EditorConfigProperty;)Lcom/pinterest/ktlint/rule/engine/core/api/editorconfig/EditorConfig;
public final fun contains (Ljava/lang/String;)Z
public final fun copy (Ljava/util/Map;)Lcom/pinterest/ktlint/rule/engine/core/api/editorconfig/EditorConfig;
public static synthetic fun copy$default (Lcom/pinterest/ktlint/rule/engine/core/api/editorconfig/EditorConfig;Ljava/util/Map;ILjava/lang/Object;)Lcom/pinterest/ktlint/rule/engine/core/api/editorconfig/EditorConfig;
public fun equals (Ljava/lang/Object;)Z
public final fun filterBy (Ljava/util/Set;)Lcom/pinterest/ktlint/rule/engine/core/api/editorconfig/EditorConfig;
public final fun get (Lcom/pinterest/ktlint/rule/engine/core/api/editorconfig/EditorConfigProperty;)Ljava/lang/Object;
Expand All @@ -524,18 +503,6 @@ public final class com/pinterest/ktlint/rule/engine/core/api/editorconfig/Editor
public final class com/pinterest/ktlint/rule/engine/core/api/editorconfig/EditorConfigProperty {
public fun <init> (Lorg/ec4j/core/model/PropertyType;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function1;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V
public synthetic fun <init> (Lorg/ec4j/core/model/PropertyType;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function1;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public final fun component1 ()Lorg/ec4j/core/model/PropertyType;
public final fun component10 ()Ljava/lang/String;
public final fun component2 ()Ljava/lang/Object;
public final fun component3 ()Ljava/lang/Object;
public final fun component4 ()Ljava/lang/Object;
public final fun component5 ()Ljava/lang/Object;
public final fun component6 ()Lkotlin/jvm/functions/Function2;
public final fun component7 ()Lkotlin/jvm/functions/Function1;
public final fun component8 ()Ljava/lang/String;
public final fun component9 ()Ljava/lang/String;
public final fun copy (Lorg/ec4j/core/model/PropertyType;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function1;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Lcom/pinterest/ktlint/rule/engine/core/api/editorconfig/EditorConfigProperty;
public static synthetic fun copy$default (Lcom/pinterest/ktlint/rule/engine/core/api/editorconfig/EditorConfigProperty;Lorg/ec4j/core/model/PropertyType;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function1;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;ILjava/lang/Object;)Lcom/pinterest/ktlint/rule/engine/core/api/editorconfig/EditorConfigProperty;
public fun equals (Ljava/lang/Object;)Z
public final fun getAndroidStudioCodeStyleDefaultValue ()Ljava/lang/Object;
public final fun getDefaultValue ()Ljava/lang/Object;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@ import com.pinterest.ktlint.rule.engine.core.api.IndentConfig.IndentStyle.SPACE
import com.pinterest.ktlint.rule.engine.core.api.IndentConfig.IndentStyle.TAB
import com.pinterest.ktlint.rule.engine.core.api.editorconfig.INDENT_SIZE_PROPERTY
import com.pinterest.ktlint.rule.engine.core.api.editorconfig.INDENT_STYLE_PROPERTY
import dev.drewhamilton.poko.Poko
import org.ec4j.core.model.PropertyType
import org.jetbrains.kotlin.com.intellij.lang.ASTNode

public data class IndentConfig(
val indentStyle: IndentStyle,
@Poko
public class IndentConfig(
public val indentStyle: IndentStyle,
/**
* The number of spaces that is equivalent to one tab
*/
val tabWidth: Int,
public val tabWidth: Int,
) {
/**
* To use the [IndentConfig] in a rule, the following needs to be done:
Expand Down
Loading

0 comments on commit 4866beb

Please # to comment.