Skip to content

Commit

Permalink
fix compiler plugin options not working
Browse files Browse the repository at this point in the history
  • Loading branch information
DatL4g committed Jan 23, 2024
1 parent 6a7fd3c commit fd2f7a9
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 6 deletions.
3 changes: 2 additions & 1 deletion sample/src/main/kotlin/KSPTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ data class KSPTest<T>(
@Secret val charSeq: CharSequence = "abcdefg",
@Secret val javaCharSeq: java.lang.CharSequence = secret as java.lang.CharSequence,
@Secret val builder: StringBuilder = StringBuilder("string-builder"),
@Secret val appendable: Appendable = builder
@Secret val appendable: Appendable = builder,
@Secret val buffer: StringBuffer = StringBuffer("string-buffer")
) {
val other: String = "test"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ class ToStringTransformer(
val charSequence = expression.type.isAnyCharSequence(config.secretMaskNull)
val stringBuilder = expression.type.isAnyStringBuilder(config.secretMaskNull)
val appendable = expression.type.isAnyAppendable(config.secretMaskNull)
val stringBuffer = expression.type.isStringBuffer(config.secretMaskNull)

if (string || charSequence || stringBuilder || appendable) {
if (string || charSequence || stringBuilder || appendable || stringBuffer) {
if (expression.symbol.owner.matchesAnyProperty(secretProperties)) {
return DeclarationIrBuilder(pluginContext, expression.symbol).irString(config.secretMask)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ object JavaSignatureValues {
@JvmField val charSequence = getPublicSignature(JavaStandardNames.LANG_PACKAGE_FQ_NAME, "CharSequence")
@JvmField val stringBuilder = getPublicSignature(JavaStandardNames.LANG_PACKAGE_FQ_NAME, "StringBuilder")
@JvmField val appendable = getPublicSignature(JavaStandardNames.LANG_PACKAGE_FQ_NAME, "Appendable")
@JvmField val stringBuffer = getPublicSignature(JavaStandardNames.LANG_PACKAGE_FQ_NAME, "StringBuffer")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package dev.datlag.sekret

import com.google.auto.service.AutoService
import org.jetbrains.kotlin.compiler.plugin.AbstractCliOption
import org.jetbrains.kotlin.compiler.plugin.CliOption
import org.jetbrains.kotlin.compiler.plugin.CommandLineProcessor
import org.jetbrains.kotlin.compiler.plugin.ExperimentalCompilerApi
import org.jetbrains.kotlin.config.CompilerConfiguration
import org.jetbrains.kotlin.config.CompilerConfigurationKey

@OptIn(ExperimentalCompilerApi::class)
@AutoService(CommandLineProcessor::class)
class SekretCommandLineProcessor : CommandLineProcessor {
override val pluginId: String = "sekretPlugin"

override val pluginOptions: Collection<AbstractCliOption> = listOf(
CliOption(
optionName = KEY_SECRET_MASK.toString(), valueDescription = "<fqname>",
description = "Mask for Strings annotated with @Secret"
),
CliOption(
optionName = KEY_SECRET_MASK_NULL.toString(), valueDescription = "<true|false>",
description = "Apply mask to nullable values or not"
)
)

override fun processOption(option: AbstractCliOption, value: String, configuration: CompilerConfiguration) {
super.processOption(option, value, configuration)

when (option.optionName) {
KEY_SECRET_MASK.toString() -> configuration.put(KEY_SECRET_MASK, value)
KEY_SECRET_MASK_NULL.toString() -> configuration.put(KEY_SECRET_MASK_NULL, value.toBoolean())
}
}
}

val KEY_SECRET_MASK = CompilerConfigurationKey<String>("secretMask")
val KEY_SECRET_MASK_NULL = CompilerConfigurationKey<Boolean>("secretMaskNull")
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,4 @@ class SekretComponentRegistrar : CompilerPluginRegistrar() {
}
})
}
}

val KEY_SECRET_MASK = CompilerConfigurationKey<String>("secretMask")
val KEY_SECRET_MASK_NULL = CompilerConfigurationKey<Boolean>("secretMaskNull")
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,8 @@ fun IrType.isAnyAppendable(nullable: Boolean = false): Boolean {
getPublicSignature(StandardNames.TEXT_PACKAGE_FQ_NAME, "Appendable"),
nullable = nullable
) || this.matchesPossibleNull(JavaSignatureValues.appendable, nullable = nullable)
}

fun IrType.isStringBuffer(nullable: Boolean = false): Boolean {
return this.matchesPossibleNull(JavaSignatureValues.stringBuffer, nullable = nullable)
}

0 comments on commit fd2f7a9

Please sign in to comment.