Skip to content

Commit

Permalink
fix(ts): fix interface cases
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Nov 17, 2022
1 parent 2c47f65 commit 991d3ef
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 59 deletions.
3 changes: 2 additions & 1 deletion chapi-ast-typescript/src/main/antlr/TypeScriptParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ literalType
: BooleanLiteral
| StringLiteral
| numericLiteral
| keyword
;

arrowFunctionTypeExpression
Expand Down Expand Up @@ -341,8 +342,8 @@ interfaceMember
| getAccessor
| setAccessor
| methodSignature
| enumSignature
| propertySignature
| enumSignature
| '[' typeRef In (Keyof | Typeof)* typeRef ']' '?'? typeAnnotation
;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,34 +46,21 @@ open class TypeScriptAstListener : TypeScriptParserBaseListener() {
}

val typeContext = typeAnnotation.typeRef() ?: return ""
var type = typeContext.text

if (typeContext.children == null) {
return type
return typeContext.text
}

// when (val primaryCtx = typeContext.getChild(0)) {
// is TypeScriptParser.PrimaryContext -> {
// type = handleTypeAnnotationPrimary(primaryCtx, type)
// }
// }

return processRef(typeContext)
}

fun processRef(type: TypeScriptParser.TypeRefContext): String {
return type.text
}
fun processRef(typeRef: TypeScriptParser.TypeRefContext): String {
typeRef.conditionalTypeRef().unionTypeExpression().forEach {
return it.text
}

// private fun handleTypeAnnotationPrimary(typeContext: TypeScriptParser.PrimaryContext, typ: String?): String? {
// var typeStr = typ
// when (val childPrimaryCtx = typeContext.getChild(0)) {
// is TypeScriptParser.ParenthesizedPrimTypeContext -> {
// typeStr = childPrimaryCtx.typeRef().text
// }
// }
// return typeStr
// }
return typeRef.text
}

fun buildMethodParameters(paramListCtx: TypeScriptParser.ParameterListContext?): Array<CodeProperty> {
var parameters: Array<CodeProperty> = arrayOf()
Expand Down Expand Up @@ -111,7 +98,7 @@ open class TypeScriptAstListener : TypeScriptParserBaseListener() {
private fun buildRestParameter(restCtx: TypeScriptParser.RestParameterContext?): CodeProperty {
var paramType = ""
if (restCtx!!.typeAnnotation() != null) {
paramType = buildTypeAnnotation(restCtx.typeAnnotation())!!
paramType = buildTypeAnnotation(restCtx.typeAnnotation())
}

return CodeProperty(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ class TypeScriptFullIdentListener(node: TSIdentify) : TypeScriptAstListener() {
val heritageCtx = ctx.classHeritage()
if (heritageCtx.classImplementsClause() != null) {
val typeList = heritageCtx.classImplementsClause().classOrInterfaceTypeList()
// currentNode.Implements = buildImplements(typeList)
currentNode.Implements = buildImplements(typeList)
}

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

val isArrowFunc = annotation.typeRef() != null
if (isArrowFunc) {
val codeFunction = CodeFunction(
Name = typeValue
)
val param = CodeProperty(
TypeValue = "any", TypeType = typeType
)

val returnType = CodeProperty(
TypeType = annotation.typeRef().text, TypeValue = ""
)
val codeField = CodeField(TypeType = typeType, TypeValue = typeValue)
currentNode.Fields += codeField

codeFunction.Parameters += param
codeFunction.MultipleReturns += returnType

codeFunction.FilePath = filePath
currentNode.Functions += codeFunction
} else {
val codeField = CodeField(TypeType = typeType, TypeValue = typeValue)
currentNode.Fields += codeField
}
// val isArrowFunc = annotation.typeRef() != null
// if (isArrowFunc) {
// val codeFunction = CodeFunction(
// Name = typeValue
// )
// val param = CodeProperty(
// TypeValue = "any", TypeType = typeType
// )
//
// val returnType = CodeProperty(
// TypeType = annotation.typeRef().text, TypeValue = ""
// )
//
// codeFunction.Parameters += param
// codeFunction.MultipleReturns += returnType
//
// codeFunction.FilePath = filePath
// currentNode.Functions += codeFunction
// } else {
// }
}

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

CodeProperty(TypeType = "key", TypeValue = propText, ObjectValue = arrayOf(value))
}
//
// is TypeScriptParser.PropertyShorthandContext -> {
// val short = property.text
// val value = CodeProperty(TypeType = "value", TypeValue = short)
//
// CodeProperty(TypeType = "key", TypeValue = short, ObjectValue = arrayOf(value))
// }

is TypeScriptParser.PropertyShorthandContext -> {
val short = property.text
val value = CodeProperty(TypeType = "value", TypeValue = short)

CodeProperty(TypeType = "key", TypeValue = short, ObjectValue = arrayOf(value))
}

else -> {
null
Expand Down Expand Up @@ -950,7 +952,6 @@ class TypeScriptFullIdentListener(node: TSIdentify) : TypeScriptAstListener() {
// }



fun getNodeInfo(): CodeContainer {
for (entry in nodeMap) {
codeContainer.DataStructures += entry.value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -417,20 +417,17 @@ interface IEmployee extends IPerson {
empCode: number;
readonly empName: string;
empDept?:string;
getSalary: (number) => number; // arrow function
getSalary: (number) => number; // arrow type
getManagerName(number): string;
}
"""
val codeFile = TypeScriptAnalyser().analysis(code, "")
assertEquals(codeFile.DataStructures[0].Fields.size, 3)
assertEquals(codeFile.DataStructures[0].Functions.size, 2)
println(Json.encodeToString(codeFile))

val firstFunc = codeFile.DataStructures[0].Functions[0]
assertEquals(firstFunc.Name, "getSalary")
assertEquals(firstFunc.MultipleReturns[0].TypeType, "number")
assertEquals(firstFunc.Parameters[0].TypeType, "number")
assertEquals(codeFile.DataStructures[0].Fields.size, 4)
assertEquals(codeFile.DataStructures[0].Functions.size, 1)

val secondFunc = codeFile.DataStructures[0].Functions[1]
val secondFunc = codeFile.DataStructures[0].Functions[0]
assertEquals(secondFunc.Name, "getManagerName")
assertEquals(secondFunc.MultipleReturns[0].TypeType, "string")
}
Expand Down

0 comments on commit 991d3ef

Please sign in to comment.