Skip to content

Commit

Permalink
Added option to whether to respect visibility modifiers or not
Browse files Browse the repository at this point in the history
  • Loading branch information
stefankoppier committed Jun 17, 2024
1 parent b97874a commit 652606c
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,28 @@ class MappieCommandLineProcessor : CommandLineProcessor {
valueDescription = "boolean",
description = "strictness of enum validation",
required = false,
),
CliOption(
optionName = OPTION_STRICTNESS_VISIBILITY,
valueDescription = "boolean",
description = "strictness of visibility modifiers",
required = false,
)
)

override fun processOption(option: AbstractCliOption, value: String, configuration: CompilerConfiguration) {
return when (option.optionName) {
OPTION_STRICTNESS_ENUMS -> configuration.put(ARGUMENT_STRICTNESS_ENUMS, value.toBooleanStrict())
OPTION_STRICTNESS_VISIBILITY -> configuration.put(ARGUMENT_STRICTNESS_VISIBILITY, value.toBooleanStrict())
else -> throw IllegalArgumentException("Unknown option ${option.optionName}")
}
}

companion object {
const val OPTION_STRICTNESS_ENUMS = "strictness.enums"
const val OPTION_STRICTNESS_VISIBILITY = "strictness.visibility"

val ARGUMENT_STRICTNESS_ENUMS = CompilerConfigurationKey<Boolean>(OPTION_STRICTNESS_ENUMS)
val ARGUMENT_STRICTNESS_VISIBILITY = CompilerConfigurationKey<Boolean>(OPTION_STRICTNESS_VISIBILITY)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.mappie

import io.github.mappie.MappieCommandLineProcessor.Companion.ARGUMENT_STRICTNESS_ENUMS
import io.github.mappie.MappieCommandLineProcessor.Companion.ARGUMENT_STRICTNESS_VISIBILITY
import org.jetbrains.kotlin.backend.common.extensions.IrGenerationExtension
import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys
import org.jetbrains.kotlin.cli.common.messages.MessageCollector
Expand All @@ -18,7 +19,10 @@ class MappieCompilerPluginRegistrar : CompilerPluginRegistrar() {
IrGenerationExtension.registerExtension(
MappieIrRegistrar(
messageCollector,
MappieConfiguration(StrictnessConfiguration(configuration.get(ARGUMENT_STRICTNESS_ENUMS, true)))
MappieConfiguration(StrictnessConfiguration(
enums = configuration.get(ARGUMENT_STRICTNESS_ENUMS, true),
visibility = configuration.get(ARGUMENT_STRICTNESS_VISIBILITY, false)
))
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ data class MappieConfiguration(

data class StrictnessConfiguration(
val enums: Boolean,
val visibility: Boolean,
)
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.github.mappie

import io.github.mappie.generation.IrTransformer
import io.github.mappie.util.error
import org.jetbrains.kotlin.backend.common.extensions.IrGenerationExtension
import org.jetbrains.kotlin.backend.common.extensions.IrPluginContext
import org.jetbrains.kotlin.cli.common.messages.MessageCollector
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ interface MappingValidation {
)

addAll(
mapping.unknowns
.map { Problem.error("Parameter ${it.first.asString()} does not occur as a parameter in constructor") }
mapping.unknowns.map {
Problem.error("Parameter ${it.first.asString()} does not occur as a parameter in constructor")
}
)

// TODO: make optional via configuration
if (!mapping.symbol.owner.visibility.isPublicAPI) {
if (!mapping.symbol.owner.visibility.isPublicAPI && context.configuration.strictness.visibility) {
add(Problem.error("Constructor is not public", location(mapping.symbol.owner)))
}
}
Expand Down
11 changes: 11 additions & 0 deletions gradle-plugin/src/main/kotlin/io/github/mappie/MappieExtension.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,26 @@ abstract class MappieExtension(private val project: Project) {
project.objects.newInstance(MappieStrictnessExtension::class.java)
} as MappieStrictnessExtension

/**
* Configuration options for the strictness of validations.
*/
fun strictness(configuration: MappieStrictnessExtension.() -> Unit) {
configuration(strictness)
}
}

abstract class MappieStrictnessExtension {

/**
* Whether to require all enum sources have a defined target.
*/
abstract val enums: Property<Boolean>

/**
* Whether to require called elements to be visible from the current scope.
*/
abstract val visibility: Property<Boolean>

internal companion object {
const val NAME = "mappie-strictness-extension"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,14 @@ class MappieGradlePlugin : KotlinCompilerPluginSupportPlugin {
override fun applyToCompilation(kotlinCompilation: KotlinCompilation<*>): Provider<List<SubpluginOption>> {
val extension = kotlinCompilation.project.extensions.getByType(MappieExtension::class.java)
return kotlinCompilation.target.project.provider {
listOf(
SubpluginOption("strictness.enums", lazy { extension.strictness.enums.get().toString() }),
)
buildList {
extension.strictness.enums.orNull?.apply {
add(SubpluginOption("strictness.enums", this.toString() ))
}
extension.strictness.visibility.orNull?.apply {
add(SubpluginOption("strictness.visibility", this.toString()))
}
}
}
}

Expand Down

0 comments on commit 652606c

Please sign in to comment.