Skip to content

Commit e134234

Browse files
authored
Merge pull request #69 from ForteScarlet/jsPromise-attributes
Support `baseName`, `suffix` and `asProperty` for `@JsPromise`
2 parents e7e6607 + 93568ad commit e134234

File tree

6 files changed

+58
-11
lines changed

6 files changed

+58
-11
lines changed

runtime/suspend-transform-annotation/src/commonMain/kotlin/love/forte/plugin/suspendtrans/annotation/SuspendTransformAnnotation.kt

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,10 @@ public expect annotation class Api4Js()
3737
@OptionalExpectation
3838
public expect annotation class JvmBlocking(
3939
/**
40-
* 生成函数的基础名称,如果为空则为当前函数名。
41-
* 最终生成的函数名为 [baseName] + [suffix]。
40+
* The base name of synthetic function
41+
* or the current function name if empty.
42+
*
43+
* The final function name is: [baseName] + [suffix]
4244
*/
4345
val baseName: String = "",
4446

@@ -57,7 +59,7 @@ public expect annotation class JvmBlocking(
5759
* val fooBlocking: T get() = runInBlocking { foo() }
5860
* ```
5961
*
60-
* 只有函数没有参数时有效。
62+
* Note: If [asProperty] == `true`, the function cannot have parameters.
6163
*
6264
*/
6365
val asProperty: Boolean = false
@@ -97,8 +99,7 @@ public expect annotation class JvmAsync(
9799
* val fooAsync: Future<T> get() = runInAsync { foo() }
98100
* ```
99101
*
100-
* 只有函数没有参数时有效。
101-
*
102+
* Note: If [asProperty] == `true`, the function cannot have parameters.
102103
*/
103104
val asProperty: Boolean = false
104105
)
@@ -108,4 +109,21 @@ public expect annotation class JvmAsync(
108109
@Target(AnnotationTarget.FUNCTION, AnnotationTarget.CLASS)
109110
@Retention(AnnotationRetention.BINARY)
110111
@OptionalExpectation
111-
public expect annotation class JsPromise()
112+
public expect annotation class JsPromise(
113+
val baseName: String = "",
114+
val suffix: String = "Async",
115+
/**
116+
* 是否转化为 property 的形式:
117+
*
118+
* ```kotlin
119+
* suspend fun foo(): T = ...
120+
*
121+
* // Generated
122+
* val fooAsync: Promise<T> get() = runInAsync { foo() }
123+
* ```
124+
*
125+
* Note: If [asProperty] == `true`, the function cannot have parameters.
126+
*
127+
*/
128+
val asProperty: Boolean = false
129+
)

runtime/suspend-transform-annotation/src/jsMain/kotlin/love.forte.plugin.suspendtrans/annotation/SuspendTransformAnnotationJs.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,8 @@ public annotation class ExperimentalJsApi
1010

1111
@Target(AnnotationTarget.FUNCTION, AnnotationTarget.CLASS)
1212
@Retention(AnnotationRetention.BINARY)
13-
public actual annotation class JsPromise
13+
public actual annotation class JsPromise(
14+
actual val baseName: String,
15+
actual val suffix: String,
16+
actual val asProperty: Boolean,
17+
)

tests/test-js/build.gradle.kts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,17 @@ kotlin {
5555
dependencies {
5656
implementation(kotlin("stdlib"))
5757
api(libs.kotlinx.coroutines.core)
58+
api(project(":runtime:suspend-transform-annotation"))
59+
api(project(":runtime:suspend-transform-runtime"))
5860
}
5961
}
6062
}
6163
}
6264

6365
extensions.getByType<SuspendTransformGradleExtension>().apply {
66+
includeRuntime = false
67+
includeAnnotation = false
68+
6469
transformers[TargetPlatform.JS] = mutableListOf(
6570
SuspendTransformConfiguration.jsPromiseTransformer.copy(
6671
copyAnnotationExcludes = listOf(
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
@file:OptIn(ExperimentalJsExport::class)
2+
@file:JsExport
3+
4+
import love.forte.plugin.suspendtrans.annotation.JsPromise
5+
6+
interface AsPropertyInterface {
7+
@JsExport.Ignore
8+
@JsPromise(asProperty = true)
9+
suspend fun value(): String
10+
}
11+
12+
class AsPropertyImpl : AsPropertyInterface {
13+
@JsExport.Ignore
14+
@JsPromise(asProperty = true)
15+
override suspend fun value(): String = "Hello, World"
16+
}

tests/test-js/src/jsMain/kotlin/FooInterface.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class FooImpl : FooInterface {
2121
override suspend fun run1(value: Int): String = value.toString()
2222

2323
@JsExport.Ignore
24-
@JsPromise // TODO asProperty
24+
@JsPromise(asProperty = true)
2525
override suspend fun run2(): String = ""
2626
}
2727

@@ -35,7 +35,7 @@ interface FooInterface2 {
3535
suspend fun run1(value: Int): String
3636

3737
@JsExport.Ignore
38-
@JsPromise // TODO asProperty
38+
@JsPromise(asProperty = true)
3939
suspend fun run2(): String
4040
}
4141

@@ -46,7 +46,7 @@ class FooImpl2 : FooInterface2 {
4646
override suspend fun run1(value: Int): String = value.toString()
4747

4848
@JsExport.Ignore
49-
@JsPromise // TODO asProperty
49+
@JsPromise(asProperty = true)
5050
override suspend fun run2(): String = ""
5151
}
5252

@@ -75,6 +75,6 @@ class FooImpl3 : FooInterface3 {
7575
override suspend fun run1(value: Int): String = value.toString()
7676

7777
@JsExport.Ignore
78-
@JsPromise // asProperty
78+
@JsPromise(asProperty = true)
7979
override suspend fun run2(): String = ""
8080
}

tests/test-jvm/build.gradle.kts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,14 @@ dependencies {
4141
api(kotlin("stdlib"))
4242
api(kotlin("test-junit5"))
4343
api(kotlin("reflect"))
44+
api(project(":runtime:suspend-transform-annotation"))
45+
api(project(":runtime:suspend-transform-runtime"))
4446
api(libs.kotlinx.coroutines.core)
4547
}
4648

4749
extensions.getByType<SuspendTransformGradleExtension>().apply {
50+
includeRuntime = false
51+
includeAnnotation = false
4852
// useJvmDefault()
4953
transformers[TargetPlatform.JVM] = mutableListOf(
5054
// Add `kotlin.OptIn` to copyAnnotationExcludes

0 commit comments

Comments
 (0)