-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Error on calls on compile dsl (#134)
- Loading branch information
1 parent
69019f5
commit 975d3dc
Showing
6 changed files
with
124 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
...src/main/kotlin/tech/mappie/validation/problems/classes/CompileTimeReceiverDslProblems.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package tech.mappie.validation.problems.classes | ||
|
||
import org.jetbrains.kotlin.ir.expressions.IrCall | ||
import org.jetbrains.kotlin.ir.types.classOrNull | ||
import org.jetbrains.kotlin.ir.util.fileEntry | ||
import tech.mappie.resolving.ClassMappingRequest | ||
import tech.mappie.resolving.classes.sources.ValueMappingSource | ||
import tech.mappie.util.CLASS_ID_OBJECT_MAPPING_CONSTRUCTOR | ||
import tech.mappie.util.location | ||
import org.jetbrains.kotlin.name.Name | ||
import tech.mappie.validation.Problem | ||
import tech.mappie.validation.ValidationContext | ||
|
||
class CompileTimeReceiverDslProblems private constructor( | ||
private val context: ValidationContext, | ||
private val mappings: List<Pair<IrCall, ProblemSource>>, | ||
) { | ||
private enum class ProblemSource { EXTENSION, DISPATCH } | ||
|
||
fun all(): List<Problem> = mappings.map { (call, source) -> | ||
val name = call.symbol.owner.name.asString() | ||
Problem.error( | ||
when (source) { | ||
ProblemSource.EXTENSION -> "The function $name was called as an extension method on the mapping dsl which does not exist after compilation" | ||
ProblemSource.DISPATCH -> "The function $name was called on the mapping dsl which does not exist after compilation" | ||
}, | ||
location(context.function.fileEntry, call), | ||
buildList { | ||
if (call.symbol.owner.name == Name.identifier("run")) { | ||
add("Did you mean to use kotlin.run?") | ||
} | ||
} | ||
) | ||
} | ||
|
||
companion object { | ||
fun of(context: ValidationContext, mapping: ClassMappingRequest): CompileTimeReceiverDslProblems { | ||
val mappings = mapping.mappings.values | ||
.filter { it.size == 1 } | ||
.map { it.single() } | ||
.filterIsInstance<ValueMappingSource>() | ||
.map { it.expression } | ||
.filterIsInstance<IrCall>() | ||
|
||
val dispatch = mappings | ||
.filter { it.hasIncorrectDispatchReceiver(context) } | ||
.map { it to ProblemSource.DISPATCH } | ||
|
||
val extension = mappings | ||
.filter { it.hasIncorrectExtensionReceiver(context) } | ||
.map { it to ProblemSource.EXTENSION } | ||
|
||
return CompileTimeReceiverDslProblems(context, dispatch + extension) | ||
} | ||
|
||
private fun IrCall.hasIncorrectExtensionReceiver(context: ValidationContext) = | ||
extensionReceiver?.type?.classOrNull == context.pluginContext.referenceClass(CLASS_ID_OBJECT_MAPPING_CONSTRUCTOR) | ||
|
||
private fun IrCall.hasIncorrectDispatchReceiver(context: ValidationContext) = | ||
dispatchReceiver?.type?.classOrNull == context.pluginContext.referenceClass(CLASS_ID_OBJECT_MAPPING_CONSTRUCTOR) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters