Skip to content

Commit 991d3ef

Browse files
committed
fix(ts): fix interface cases
1 parent 2c47f65 commit 991d3ef

4 files changed

Lines changed: 45 additions & 59 deletions

File tree

chapi-ast-typescript/src/main/antlr/TypeScriptParser.g4

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ literalType
150150
: BooleanLiteral
151151
| StringLiteral
152152
| numericLiteral
153+
| keyword
153154
;
154155

155156
arrowFunctionTypeExpression
@@ -341,8 +342,8 @@ interfaceMember
341342
| getAccessor
342343
| setAccessor
343344
| methodSignature
344-
| enumSignature
345345
| propertySignature
346+
| enumSignature
346347
| '[' typeRef In (Keyof | Typeof)* typeRef ']' '?'? typeAnnotation
347348
;
348349

chapi-ast-typescript/src/main/kotlin/chapi/ast/typescriptast/TypeScriptAstListener.kt

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -46,34 +46,21 @@ open class TypeScriptAstListener : TypeScriptParserBaseListener() {
4646
}
4747

4848
val typeContext = typeAnnotation.typeRef() ?: return ""
49-
var type = typeContext.text
5049

5150
if (typeContext.children == null) {
52-
return type
51+
return typeContext.text
5352
}
5453

55-
// when (val primaryCtx = typeContext.getChild(0)) {
56-
// is TypeScriptParser.PrimaryContext -> {
57-
// type = handleTypeAnnotationPrimary(primaryCtx, type)
58-
// }
59-
// }
60-
6154
return processRef(typeContext)
6255
}
6356

64-
fun processRef(type: TypeScriptParser.TypeRefContext): String {
65-
return type.text
66-
}
57+
fun processRef(typeRef: TypeScriptParser.TypeRefContext): String {
58+
typeRef.conditionalTypeRef().unionTypeExpression().forEach {
59+
return it.text
60+
}
6761

68-
// private fun handleTypeAnnotationPrimary(typeContext: TypeScriptParser.PrimaryContext, typ: String?): String? {
69-
// var typeStr = typ
70-
// when (val childPrimaryCtx = typeContext.getChild(0)) {
71-
// is TypeScriptParser.ParenthesizedPrimTypeContext -> {
72-
// typeStr = childPrimaryCtx.typeRef().text
73-
// }
74-
// }
75-
// return typeStr
76-
// }
62+
return typeRef.text
63+
}
7764

7865
fun buildMethodParameters(paramListCtx: TypeScriptParser.ParameterListContext?): Array<CodeProperty> {
7966
var parameters: Array<CodeProperty> = arrayOf()
@@ -111,7 +98,7 @@ open class TypeScriptAstListener : TypeScriptParserBaseListener() {
11198
private fun buildRestParameter(restCtx: TypeScriptParser.RestParameterContext?): CodeProperty {
11299
var paramType = ""
113100
if (restCtx!!.typeAnnotation() != null) {
114-
paramType = buildTypeAnnotation(restCtx.typeAnnotation())!!
101+
paramType = buildTypeAnnotation(restCtx.typeAnnotation())
115102
}
116103

117104
return CodeProperty(

chapi-ast-typescript/src/main/kotlin/chapi/ast/typescriptast/TypeScriptFullIdentListener.kt

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ class TypeScriptFullIdentListener(node: TSIdentify) : TypeScriptAstListener() {
176176
val heritageCtx = ctx.classHeritage()
177177
if (heritageCtx.classImplementsClause() != null) {
178178
val typeList = heritageCtx.classImplementsClause().classOrInterfaceTypeList()
179-
// currentNode.Implements = buildImplements(typeList)
179+
currentNode.Implements = buildImplements(typeList)
180180
}
181181

182182
if (heritageCtx.classExtendsClause() != null) {
@@ -323,28 +323,30 @@ class TypeScriptFullIdentListener(node: TSIdentify) : TypeScriptAstListener() {
323323
val typeType = buildTypeAnnotation(annotation)
324324
val typeValue = signCtx.propertyName().text
325325

326-
val isArrowFunc = annotation.typeRef() != null
327-
if (isArrowFunc) {
328-
val codeFunction = CodeFunction(
329-
Name = typeValue
330-
)
331-
val param = CodeProperty(
332-
TypeValue = "any", TypeType = typeType
333-
)
334326

335-
val returnType = CodeProperty(
336-
TypeType = annotation.typeRef().text, TypeValue = ""
337-
)
327+
val codeField = CodeField(TypeType = typeType, TypeValue = typeValue)
328+
currentNode.Fields += codeField
338329

339-
codeFunction.Parameters += param
340-
codeFunction.MultipleReturns += returnType
341-
342-
codeFunction.FilePath = filePath
343-
currentNode.Functions += codeFunction
344-
} else {
345-
val codeField = CodeField(TypeType = typeType, TypeValue = typeValue)
346-
currentNode.Fields += codeField
347-
}
330+
// val isArrowFunc = annotation.typeRef() != null
331+
// if (isArrowFunc) {
332+
// val codeFunction = CodeFunction(
333+
// Name = typeValue
334+
// )
335+
// val param = CodeProperty(
336+
// TypeValue = "any", TypeType = typeType
337+
// )
338+
//
339+
// val returnType = CodeProperty(
340+
// TypeType = annotation.typeRef().text, TypeValue = ""
341+
// )
342+
//
343+
// codeFunction.Parameters += param
344+
// codeFunction.MultipleReturns += returnType
345+
//
346+
// codeFunction.FilePath = filePath
347+
// currentNode.Functions += codeFunction
348+
// } else {
349+
// }
348350
}
349351

350352
override fun enterFromBlock(ctx: TypeScriptParser.FromBlockContext?) {
@@ -757,13 +759,13 @@ class TypeScriptFullIdentListener(node: TSIdentify) : TypeScriptAstListener() {
757759

758760
CodeProperty(TypeType = "key", TypeValue = propText, ObjectValue = arrayOf(value))
759761
}
760-
//
761-
// is TypeScriptParser.PropertyShorthandContext -> {
762-
// val short = property.text
763-
// val value = CodeProperty(TypeType = "value", TypeValue = short)
764-
//
765-
// CodeProperty(TypeType = "key", TypeValue = short, ObjectValue = arrayOf(value))
766-
// }
762+
763+
is TypeScriptParser.PropertyShorthandContext -> {
764+
val short = property.text
765+
val value = CodeProperty(TypeType = "value", TypeValue = short)
766+
767+
CodeProperty(TypeType = "key", TypeValue = short, ObjectValue = arrayOf(value))
768+
}
767769

768770
else -> {
769771
null
@@ -950,7 +952,6 @@ class TypeScriptFullIdentListener(node: TSIdentify) : TypeScriptAstListener() {
950952
// }
951953

952954

953-
954955
fun getNodeInfo(): CodeContainer {
955956
for (entry in nodeMap) {
956957
codeContainer.DataStructures += entry.value

chapi-ast-typescript/src/test/kotlin/chapi/ast/typescriptast/TypeScriptFullIdentListenerTest.kt

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -417,20 +417,17 @@ interface IEmployee extends IPerson {
417417
empCode: number;
418418
readonly empName: string;
419419
empDept?:string;
420-
getSalary: (number) => number; // arrow function
420+
getSalary: (number) => number; // arrow type
421421
getManagerName(number): string;
422422
}
423423
"""
424424
val codeFile = TypeScriptAnalyser().analysis(code, "")
425-
assertEquals(codeFile.DataStructures[0].Fields.size, 3)
426-
assertEquals(codeFile.DataStructures[0].Functions.size, 2)
425+
println(Json.encodeToString(codeFile))
427426

428-
val firstFunc = codeFile.DataStructures[0].Functions[0]
429-
assertEquals(firstFunc.Name, "getSalary")
430-
assertEquals(firstFunc.MultipleReturns[0].TypeType, "number")
431-
assertEquals(firstFunc.Parameters[0].TypeType, "number")
427+
assertEquals(codeFile.DataStructures[0].Fields.size, 4)
428+
assertEquals(codeFile.DataStructures[0].Functions.size, 1)
432429

433-
val secondFunc = codeFile.DataStructures[0].Functions[1]
430+
val secondFunc = codeFile.DataStructures[0].Functions[0]
434431
assertEquals(secondFunc.Name, "getManagerName")
435432
assertEquals(secondFunc.MultipleReturns[0].TypeType, "string")
436433
}

0 commit comments

Comments
 (0)