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

Commit df92c43

Browse files
committed
Support abstract readonly properties
1 parent 9f22a05 commit df92c43

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
@@ -73,7 +73,7 @@ abstract class TsClassifierToKt(
7373
return this
7474
}
7575

76-
open fun needsNoImpl(node: PropertyDeclaration): Boolean = true
76+
open fun needsNoImpl(node: PropertyDeclaration): Boolean = !isAbstract(node)
7777
open fun isNullable(node: PropertyDeclaration): Boolean = false
7878
open fun isLambda(node: PropertyDeclaration): Boolean = false
7979

@@ -93,6 +93,8 @@ abstract class TsClassifierToKt(
9393
name,
9494
type = varType.copy(isNullable = varType.isNullable || isNullable(node)),
9595
isOverride = isOverride,
96+
isVar = !isReadonly(node),
97+
isAbstract = isAbstract(node),
9698
needsNoImpl = needsNoImpl(node)
9799
)
98100
}

src/main/kotlin/ts2kt/TsInterfaceToKtExtensions.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,13 @@ class TsInterfaceToKtExtensions(
5252
}
5353
}
5454

55-
override fun addVariable(symbol: Symbol?, name: String, type: KtType, extendsType: KtType?, typeParams: List<KtTypeParam>?, isVar: Boolean, needsNoImpl: Boolean, additionalAnnotations: List<KtAnnotation>, isOverride: Boolean) {
55+
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) {
5656
val typeParamsWithoutClashes = this.typeParams.fixIfClashWith(typeParams)
5757
val actualExtendsType = if (typeParamsWithoutClashes === this.typeParams) cachedExtendsType else getExtendsType(typeParamsWithoutClashes)
5858
val annotations = additionalAnnotations.withNativeAnnotation()
5959

6060
super.addVariable(symbol, name, type, actualExtendsType, typeParamsWithoutClashes merge typeParams, isVar,
61-
needsNoImpl = true, additionalAnnotations = annotations, isOverride = isOverride)
61+
needsNoImpl = !isAbstract, additionalAnnotations = annotations, isOverride = isOverride, isAbstract = isAbstract)
6262
}
6363

6464
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
@@ -183,6 +183,10 @@ class Stringify(
183183

184184
out.printIndent()
185185

186+
if (isAbstract) {
187+
out.print("$ABSTRACT ")
188+
}
189+
186190
// TODO remove hack
187191
printExternalIfNeed()
188192

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,8 @@ data class KtVariable(
171171
override var annotations: List<KtAnnotation>,
172172
val typeParams: List<KtTypeParam>?,
173173
var isVar: Boolean,
174-
val needsNoImpl: Boolean = true,
174+
val isAbstract: Boolean = false,
175+
val needsNoImpl: Boolean = !isAbstract,
175176
val isInInterface: Boolean,
176177
val isOverride: Boolean = false,
177178
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)