Skip to content
This repository was archived by the owner on Nov 6, 2019. It is now read-only.

Commit 7294654

Browse files
committed
Support abstract readonly properties
1 parent 400d23d commit 7294654

File tree

7 files changed

+19
-6
lines changed

7 files changed

+19
-6
lines changed

src/main/kotlin/ts2kt/TsClassifierToKt.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ abstract class TsClassifierToKt(
7777
return this
7878
}
7979

80-
open fun needsNoImpl(node: PropertyDeclaration): Boolean = true
80+
open fun needsNoImpl(node: PropertyDeclaration): Boolean = !isAbstract(node)
8181
open fun isNullable(node: PropertyDeclaration): Boolean = false
8282
open fun isLambda(node: PropertyDeclaration): Boolean = false
8383

@@ -97,6 +97,8 @@ abstract class TsClassifierToKt(
9797
name,
9898
type = varType.copy(isNullable = varType.isNullable || isNullable(node)),
9999
isOverride = isOverride,
100+
isVar = !isReadonly(node),
101+
isAbstract = isAbstract(node),
100102
needsNoImpl = needsNoImpl(node)
101103
)
102104
}

src/main/kotlin/ts2kt/TsInterfaceToKtExtensions.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,13 @@ class TsInterfaceToKtExtensions(
5757
super.translateGetterAndSetter(node, extendsType = cachedExtendsType)
5858
}
5959

60-
override fun addVariable(symbol: Symbol?, name: String, type: KtType, extendsType: KtType?, typeParams: List<KtTypeParam>?, isVar: Boolean, needsNoImpl: Boolean, additionalAnnotations: List<KtAnnotation>, isOverride: Boolean) {
60+
override fun addVariable(symbol: Symbol?, name: String, type: KtType, extendsType: KtType?, typeParams: List<KtTypeParam>?, isVar: Boolean, isAbstract: Boolean, needsNoImpl: Boolean, additionalAnnotations: List<KtAnnotation>, isOverride: Boolean) {
6161
val typeParamsWithoutClashes = this.typeParams.fixIfClashWith(typeParams)
6262
val actualExtendsType = if (typeParamsWithoutClashes === this.typeParams) cachedExtendsType else getExtendsType(typeParamsWithoutClashes)
6363
val annotations = additionalAnnotations.withNativeAnnotation()
6464

6565
super.addVariable(symbol, name, type, actualExtendsType, typeParamsWithoutClashes merge typeParams, isVar,
66-
needsNoImpl = true, additionalAnnotations = annotations, isOverride = isOverride)
66+
needsNoImpl = !isAbstract, additionalAnnotations = annotations, isOverride = isOverride, isAbstract = isAbstract)
6767
}
6868

6969
override fun addFunction(symbol: Symbol?, name: String, callSignature: KtCallSignature, extendsType: KtType?, needsNoImpl: Boolean, additionalAnnotations: List<KtAnnotation>, isOverride: Boolean, isOperator: Boolean, isAbstract: Boolean) {

src/main/kotlin/ts2kt/TypeScriptToKotlinBase.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ abstract class TypeScriptToKotlinBase(
1616

1717
open val defaultAnnotations: List<KtAnnotation> = listOf()
1818

19-
open fun addVariable(symbol: Symbol?, name: String, type: KtType, extendsType: KtType? = null, typeParams: List<KtTypeParam>? = null, isVar: Boolean = true, needsNoImpl: Boolean = true, additionalAnnotations: List<KtAnnotation> = listOf(), isOverride: Boolean = false) {
19+
open fun addVariable(symbol: Symbol?, name: String, type: KtType, extendsType: KtType? = null, typeParams: List<KtTypeParam>? = null, isVar: Boolean = true, isAbstract: Boolean = false, needsNoImpl: Boolean = !isAbstract, additionalAnnotations: List<KtAnnotation> = listOf(), isOverride: Boolean = false) {
2020
val annotations = defaultAnnotations + additionalAnnotations
21-
addDeclaration(symbol, KtVariable(KtName(name), KtTypeAnnotation(type), extendsType?.let { KtHeritageType(it) }, annotations, typeParams, isVar = isVar, needsNoImpl = needsNoImpl, isInInterface = isInterface, isOverride = isOverride, hasOpenModifier = hasMembersOpenModifier))
21+
addDeclaration(symbol, KtVariable(KtName(name), KtTypeAnnotation(type), extendsType?.let { KtHeritageType(it) }, annotations, typeParams, isVar = isVar, needsNoImpl = needsNoImpl, isInInterface = isInterface, isOverride = isOverride, hasOpenModifier = hasMembersOpenModifier && !isAbstract, isAbstract = isAbstract))
2222
}
2323

2424
open fun addFunction(symbol: Symbol?, name: String, callSignature: KtCallSignature, extendsType: KtType? = null, needsNoImpl: Boolean = true, additionalAnnotations: List<KtAnnotation> = listOf(), isOverride: Boolean = false, isOperator: Boolean = false, isAbstract: Boolean = false) {
@@ -38,6 +38,10 @@ abstract class TypeScriptToKotlinBase(
3838
return node.modifiers?.arr?.any { it.kind == SyntaxKind.AbstractKeyword } == true
3939
}
4040

41+
fun isReadonly(node: Node): Boolean {
42+
return node.modifiers?.arr?.any { it.kind == SyntaxKind.ReadonlyKeyword } == true
43+
}
44+
4145
// TODO
4246
open fun visitList(node: typescriptServices.ts.Node) {
4347
forEachChild(this, node)

src/main/kotlin/ts2kt/kotlin/ast/Stringify.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,10 @@ class Stringify(
202202

203203
out.printIndent()
204204

205+
if (isAbstract) {
206+
out.print("$ABSTRACT ")
207+
}
208+
205209
if (variable.extendsType == null) {
206210
// TODO remove hack
207211
printExternalIfNeed()

src/main/kotlin/ts2kt/kotlin/ast/ast.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,8 @@ data class KtVariable(
175175
override var annotations: List<KtAnnotation>,
176176
val typeParams: List<KtTypeParam>?,
177177
var isVar: Boolean,
178-
val needsNoImpl: Boolean = true,
178+
val isAbstract: Boolean = false,
179+
val needsNoImpl: Boolean = !isAbstract,
179180
val isInInterface: Boolean,
180181
val isOverride: Boolean = false,
181182
val hasOpenModifier: Boolean
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package abstract
22

33
abstract external class AbstractFoo {
4+
abstract val x: Number
45
open fun open(): Unit = definedExternally
56
abstract fun close(): Unit
67
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
declare abstract class AbstractFoo {
2+
abstract readonly x: number;
23
open(): void
34
abstract close(): void
45
}

0 commit comments

Comments
 (0)