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

Commit 1f38100

Browse files
committed
Support protected methods and properties
1 parent 7294654 commit 1f38100

11 files changed

+78
-21
lines changed

src/main/kotlin/ts2kt/TsClassifierToKt.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,15 @@ abstract class TsClassifierToKt(
9999
isOverride = isOverride,
100100
isVar = !isReadonly(node),
101101
isAbstract = isAbstract(node),
102-
needsNoImpl = needsNoImpl(node)
102+
needsNoImpl = needsNoImpl(node),
103+
accessModifier = getAccessModifier(node)
103104
)
104105
}
105106

106107
open fun TsClassifierToKt.addFunction(name: String, isOverride: Boolean, needsNoImpl: Boolean, node: MethodDeclaration) {
107108
val symbol = typeMapper.typeChecker.getSymbolResolvingAliases(node)
108109
node.toKotlinCallSignatureOverloads(typeMapper).forEach { callSignature ->
109-
addFunction(symbol, name, callSignature, isOverride = isOverride, needsNoImpl = needsNoImpl(node), isAbstract = isAbstract(node))
110+
addFunction(symbol, name, callSignature, isOverride = isOverride, needsNoImpl = needsNoImpl(node), isAbstract = isAbstract(node), accessModifier = getAccessModifier(node))
110111
}
111112

112113
assert(node.body == null, "An function in declarations file should not have body, function '${this.name}.$name'")
@@ -123,7 +124,7 @@ abstract class TsClassifierToKt(
123124

124125
override fun visitSignatureDeclaration(node: SignatureDeclaration) {
125126
node.toKotlinCallSignatureOverloads(typeMapper).forEach { callSignature ->
126-
addFunction(null, INVOKE, callSignature, needsNoImpl = false, additionalAnnotations = listOf(NATIVE_INVOKE_ANNOTATION), isOperator = true)
127+
addFunction(null, INVOKE, callSignature, needsNoImpl = false, additionalAnnotations = listOf(NATIVE_INVOKE_ANNOTATION), isOperator = true, accessModifier = getAccessModifier(node))
127128
}
128129
}
129130

src/main/kotlin/ts2kt/TsInterfaceToKt.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,13 @@ open class TsInterfaceToKt(
3030
override fun TsClassifierToKt.addFunction(name: String, isOverride: Boolean, needsNoImpl: Boolean, node: MethodDeclaration) {
3131
val isOptional = node.questionToken != null
3232
val symbol = typeMapper.typeChecker.getSymbolResolvingAliases(node)
33+
val accessModifier = getAccessModifier(node)
3334
if (isOptional) {
3435
val call = node.toKotlinCallSignature(typeMapper)
35-
addVariable(symbol, name, type = createFunctionType(call.params, call.returnType.type).copy(isNullable = true), typeParams = call.typeParams, isVar = false, needsNoImpl = true, isOverride = isOverride)
36+
addVariable(symbol, name, type = createFunctionType(call.params, call.returnType.type).copy(isNullable = true), typeParams = call.typeParams, isVar = false, needsNoImpl = true, isOverride = isOverride, accessModifier = accessModifier)
3637
} else {
3738
node.toKotlinCallSignatureOverloads(typeMapper).forEach { call ->
38-
addFunction(symbol, name, call, needsNoImpl = false, isOverride = isOverride)
39+
addFunction(symbol, name, call, needsNoImpl = false, isOverride = isOverride, accessModifier = accessModifier)
3940
}
4041
}
4142

src/main/kotlin/ts2kt/TsInterfaceToKtExtensions.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,21 +57,22 @@ 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, isAbstract: 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, accessModifier: AccessModifier) {
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 = !isAbstract, additionalAnnotations = annotations, isOverride = isOverride, isAbstract = isAbstract)
66+
needsNoImpl = !isAbstract, additionalAnnotations = annotations, isOverride = isOverride, isAbstract = isAbstract,
67+
accessModifier = accessModifier)
6768
}
6869

69-
override fun addFunction(symbol: Symbol?, name: String, callSignature: KtCallSignature, extendsType: KtType?, needsNoImpl: Boolean, additionalAnnotations: List<KtAnnotation>, isOverride: Boolean, isOperator: Boolean, isAbstract: Boolean) {
70+
override fun addFunction(symbol: Symbol?, name: String, callSignature: KtCallSignature, extendsType: KtType?, needsNoImpl: Boolean, additionalAnnotations: List<KtAnnotation>, isOverride: Boolean, isOperator: Boolean, isAbstract: Boolean, accessModifier: AccessModifier) {
7071
val typeParamsWithoutClashes = this.typeParams.fixIfClashWith(callSignature.typeParams)
7172
val actualExtendsType = if (typeParamsWithoutClashes === this.typeParams) cachedExtendsType else getExtendsType(typeParamsWithoutClashes)
7273
val annotations = additionalAnnotations.withNativeAnnotation()
7374

74-
super.addFunction(symbol, name, KtCallSignature(callSignature.params, typeParamsWithoutClashes merge callSignature.typeParams, callSignature.returnType), actualExtendsType, true, annotations, isOverride, isOperator, isAbstract)
75+
super.addFunction(symbol, name, KtCallSignature(callSignature.params, typeParamsWithoutClashes merge callSignature.typeParams, callSignature.returnType), actualExtendsType, true, annotations, isOverride, isOperator, isAbstract, accessModifier)
7576
}
7677

7778
}

src/main/kotlin/ts2kt/TypeScriptToKotlin.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ class TypeScriptToKotlin(
9696
}
9797
}
9898
val varType = d.type?.let { typeMapper.mapType(it) } ?: KtType(ANY)
99-
addVariable(symbol, name, varType, additionalAnnotations = additionalAnnotations)
99+
addVariable(symbol, name, varType, additionalAnnotations = additionalAnnotations, accessModifier = getAccessModifier(node))
100100
}
101101
}
102102

src/main/kotlin/ts2kt/TypeScriptToKotlinBase.kt

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,23 @@ 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, isAbstract: Boolean = false, needsNoImpl: Boolean = !isAbstract, 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, accessModifier: AccessModifier = AccessModifier.PUBLIC) {
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 && !isAbstract, isAbstract = isAbstract))
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, accessModifier = accessModifier))
2222
}
2323

24-
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) {
24+
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, accessModifier: AccessModifier = AccessModifier.PUBLIC) {
2525
val annotations = defaultAnnotations + additionalAnnotations
26-
addDeclaration(symbol, KtFunction(KtName(name), callSignature, extendsType?.let { KtHeritageType(it) }, annotations, needsNoImpl = needsNoImpl, isOverride = isOverride, hasOpenModifier = hasMembersOpenModifier && !isAbstract, isOperator = isOperator, isAbstract = isAbstract))
26+
addDeclaration(symbol, KtFunction(KtName(name), callSignature, extendsType?.let { KtHeritageType(it) }, annotations, needsNoImpl = needsNoImpl, isOverride = isOverride, hasOpenModifier = hasMembersOpenModifier && !isAbstract, isOperator = isOperator, isAbstract = isAbstract, accessModifier = accessModifier))
2727
}
2828

2929
protected fun addDeclaration(symbol: Symbol?, declaration: KtMember) {
30-
declarations += declaration
31-
if (symbol != null) {
32-
val values = declarationsBySymbol.getOrPut(symbol) { mutableListOf() }
33-
values += declaration
30+
if (declaration.accessModifier.usable) {
31+
declarations += declaration
32+
if (symbol != null) {
33+
val values = declarationsBySymbol.getOrPut(symbol) { mutableListOf() }
34+
values += declaration
35+
}
3436
}
3537
}
3638

@@ -42,6 +44,14 @@ abstract class TypeScriptToKotlinBase(
4244
return node.modifiers?.arr?.any { it.kind == SyntaxKind.ReadonlyKeyword } == true
4345
}
4446

47+
fun getAccessModifier(node: Node): AccessModifier {
48+
return when {
49+
node.modifiers?.arr?.any { it.kind == SyntaxKind.ProtectedKeyword } == true -> AccessModifier.PROTECTED
50+
node.modifiers?.arr?.any { it.kind == SyntaxKind.PrivateKeyword } == true -> AccessModifier.PRIVATE
51+
else -> AccessModifier.PUBLIC
52+
}
53+
}
54+
4555
// TODO
4656
open fun visitList(node: typescriptServices.ts.Node) {
4757
forEachChild(this, node)

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ private val EQ_NO_IMPL = " = $NO_IMPL"
1313
private val NO_IMPL_PROPERTY_GETTER = " get()" + EQ_NO_IMPL
1414
private val NO_IMPL_PROPERTY_SETTER = " set(value)" + EQ_NO_IMPL
1515
private val EXTERNAL = "external"
16+
private val PROTECTED = "protected"
1617
private val ABSTRACT = "abstract"
1718
private val INLINE = "inline"
1819
private val OPEN = "open"
@@ -142,6 +143,10 @@ class Stringify(
142143

143144
out.printIndent()
144145

146+
if (accessModifier == AccessModifier.PROTECTED) {
147+
out.print("$PROTECTED ")
148+
}
149+
145150
if (isAbstract) {
146151
out.print("$ABSTRACT ")
147152
}
@@ -202,6 +207,10 @@ class Stringify(
202207

203208
out.printIndent()
204209

210+
if (accessModifier == AccessModifier.PROTECTED) {
211+
out.print("$PROTECTED ")
212+
}
213+
205214
if (isAbstract) {
206215
out.print("$ABSTRACT ")
207216
}

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,9 @@ interface KtAnnotated {
8888
var annotations: List<KtAnnotation>
8989
}
9090

91-
interface KtMember : KtNode, KtNamed, KtAnnotated
91+
interface KtMember : KtNode, KtNamed, KtAnnotated {
92+
val accessModifier: AccessModifier
93+
}
9294

9395
interface KtExtensionAware : KtMember {
9496
val extendsType: KtHeritageType?
@@ -126,6 +128,9 @@ data class KtClassifier(
126128
val hasOpenModifier: Boolean,
127129
val isAbstract: Boolean
128130
) : KtMember, AbstractKtNode(), KtWithMembers {
131+
132+
override val accessModifier: AccessModifier = AccessModifier.PUBLIC
133+
129134
override fun accept(visitor: KtVisitor) {
130135
visitor.visitClassifier(this)
131136
}
@@ -161,7 +166,8 @@ data class KtFunction(
161166
val isOverride: Boolean = false,
162167
val hasOpenModifier: Boolean = false,
163168
val isOperator: Boolean = false,
164-
val isAbstract: Boolean = false
169+
val isAbstract: Boolean = false,
170+
override val accessModifier: AccessModifier = AccessModifier.PUBLIC
165171
) : KtMember, KtExtensionAware, AbstractKtNode() {
166172
override fun accept(visitor: KtVisitor) {
167173
visitor.visitFunction(this)
@@ -179,14 +185,17 @@ data class KtVariable(
179185
val needsNoImpl: Boolean = !isAbstract,
180186
val isInInterface: Boolean,
181187
val isOverride: Boolean = false,
182-
val hasOpenModifier: Boolean
188+
val hasOpenModifier: Boolean,
189+
override val accessModifier: AccessModifier = AccessModifier.PUBLIC
183190
) : KtMember, KtExtensionAware, AbstractKtNode() {
184191
override fun accept(visitor: KtVisitor) {
185192
visitor.visitVariable(this)
186193
}
187194
}
188195

189196
data class KtEnumEntry(override var name: KtName, val value: String? = null) : KtMember, AbstractKtNode() {
197+
override val accessModifier: AccessModifier = AccessModifier.PUBLIC
198+
190199
override var annotations = listOf<KtAnnotation>()
191200

192201
override fun accept(visitor: KtVisitor) {
@@ -251,3 +260,7 @@ data class KtTypeAnnotation(var type: KtType, val isVararg: Boolean = false) : A
251260
visitor.visitTypeAnnotation(this)
252261
}
253262
}
263+
264+
enum class AccessModifier(val usable: Boolean) {
265+
PUBLIC(true), PROTECTED(true), INTERNAL(false), PRIVATE(false)
266+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package accessModifierMethods
2+
3+
external open class FooWithAccessModifierMethods {
4+
open fun publicMethod(s: String): String = definedExternally
5+
protected open fun protectedMethod(s: String): String = definedExternally
6+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
declare class FooWithAccessModifierMethods {
2+
public publicMethod(s: string): string;
3+
protected protectedMethod(s: string): string;
4+
private privateMethod(s: string): string;
5+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package accessModifierVariables
2+
3+
external open class FooWithAccessModifierVariables {
4+
open var publicVar: Number = definedExternally
5+
protected open var protectedVar: Number = definedExternally
6+
}

0 commit comments

Comments
 (0)