-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added minor checks for
@Rpc
services
- Loading branch information
Showing
5 changed files
with
154 additions
and
37 deletions.
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
78 changes: 78 additions & 0 deletions
78
...r-plugin-k2/src/main/core/kotlinx/rpc/codegen/checkers/FirRpcServiceDeclarationChecker.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,78 @@ | ||
/* | ||
* Copyright 2023-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package kotlinx.rpc.codegen.checkers | ||
|
||
import kotlinx.rpc.codegen.FirCheckersContext | ||
import kotlinx.rpc.codegen.FirRpcPredicates | ||
import kotlinx.rpc.codegen.checkers.diagnostics.FirRpcDiagnostics | ||
import kotlinx.rpc.codegen.common.RpcClassId | ||
import kotlinx.rpc.codegen.vsApi | ||
import org.jetbrains.kotlin.diagnostics.DiagnosticReporter | ||
import org.jetbrains.kotlin.diagnostics.reportOn | ||
import org.jetbrains.kotlin.fir.analysis.checkers.MppCheckerKind | ||
import org.jetbrains.kotlin.fir.analysis.checkers.context.CheckerContext | ||
import org.jetbrains.kotlin.fir.analysis.checkers.declaration.FirClassChecker | ||
import org.jetbrains.kotlin.fir.declarations.FirClass | ||
import org.jetbrains.kotlin.fir.declarations.FirSimpleFunction | ||
import org.jetbrains.kotlin.fir.declarations.utils.isSuspend | ||
import org.jetbrains.kotlin.fir.extensions.predicateBasedProvider | ||
import org.jetbrains.kotlin.fir.types.coneType | ||
|
||
class FirRpcServiceDeclarationChecker( | ||
@Suppress("unused") | ||
private val ctx: FirCheckersContext, | ||
) : FirClassChecker(MppCheckerKind.Common) { | ||
override fun check( | ||
declaration: FirClass, | ||
context: CheckerContext, | ||
reporter: DiagnosticReporter, | ||
) { | ||
if (!context.session.predicateBasedProvider.matches(FirRpcPredicates.rpc, declaration)) { | ||
return | ||
} | ||
|
||
if (declaration.typeParameters.isNotEmpty()) { | ||
reporter.reportOn( | ||
source = declaration.source, | ||
factory = FirRpcDiagnostics.TYPE_PARAMETERS_IN_RPC_INTERFACE, | ||
context = context, | ||
) | ||
} | ||
|
||
declaration.declarations.filterIsInstance<FirSimpleFunction>().onEach { function -> | ||
if (function.typeParameters.isNotEmpty()) { | ||
reporter.reportOn( | ||
source = function.source, | ||
factory = FirRpcDiagnostics.TYPE_PARAMETERS_IN_RPC_FUNCTION, | ||
context = context, | ||
) | ||
} | ||
|
||
val returnType = vsApi { function.returnTypeRef.coneType.toClassSymbolVS(context.session) } | ||
?: return@onEach | ||
|
||
if (returnType.classId != RpcClassId.flow && !function.isSuspend) { | ||
reporter.reportOn( | ||
source = function.source, | ||
factory = FirRpcDiagnostics.NON_SUSPENDING_REQUEST_WITHOUT_STREAMING_RETURN_TYPE, | ||
context = context, | ||
) | ||
} | ||
} | ||
.groupBy { it.name } | ||
.filter { (_, list) -> list.size > 1 } | ||
.forEach { name, functions -> | ||
functions.forEach { function -> | ||
reporter.reportOn( | ||
source = function.source, | ||
factory = FirRpcDiagnostics.AD_HOC_POLYMORPHISM_IN_RPC_SERVICE, | ||
a = functions.size, | ||
b = name, | ||
context = context, | ||
) | ||
} | ||
} | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
...r-plugin-k2/src/main/core/kotlinx/rpc/codegen/checkers/diagnostics/DiagnosticFactories.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,46 @@ | ||
/* | ||
* Copyright 2023-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
@file:Suppress("unused") | ||
|
||
package kotlinx.rpc.codegen.checkers.diagnostics | ||
|
||
import kotlinx.rpc.codegen.StrictMode | ||
import org.jetbrains.kotlin.diagnostics.KtDiagnosticFactory0 | ||
import org.jetbrains.kotlin.diagnostics.Severity | ||
import org.jetbrains.kotlin.diagnostics.SourceElementPositioningStrategies | ||
import org.jetbrains.kotlin.utils.DummyDelegate | ||
import kotlin.properties.ReadOnlyProperty | ||
import kotlin.reflect.KClass | ||
import kotlin.reflect.KProperty | ||
|
||
inline fun <reified T> modded0(mode: StrictMode): DiagnosticFactory0DelegateProviderOnNull { | ||
return DiagnosticFactory0DelegateProviderOnNull(mode, T::class) | ||
} | ||
|
||
class DiagnosticFactory0DelegateProviderOnNull( | ||
private val mode: StrictMode, | ||
private val psiType: KClass<*>, | ||
) { | ||
operator fun provideDelegate( | ||
@Suppress("unused") | ||
thisRef: Any?, | ||
prop: KProperty<*>, | ||
): ReadOnlyProperty<Any?, KtDiagnosticFactory0?> { | ||
val severity = when (mode) { | ||
StrictMode.ERROR -> Severity.ERROR | ||
StrictMode.WARNING -> Severity.WARNING | ||
StrictMode.NONE -> null | ||
} ?: return DummyDelegate(null) | ||
|
||
return DummyDelegate( | ||
KtDiagnosticFactory0( | ||
name = prop.name, | ||
severity = severity, | ||
defaultPositioningStrategy = SourceElementPositioningStrategies.DEFAULT, | ||
psiType = psiType, | ||
), | ||
) | ||
} | ||
} |
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