Skip to content

Commit

Permalink
feat: <ts> add interface method support
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Feb 10, 2020
1 parent 5eff8e9 commit 7b239c4
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,29 @@ open class TypeScriptAstListener() : TypeScriptParserBaseListener() {
}

fun buildTypeAnnotation(typeAnnotation: TypeScriptParser.TypeAnnotationContext?): String? {
return typeAnnotation!!.type_().text
val typeContext = typeAnnotation!!.type_()

var type = typeContext.text
when (typeContext.getChild(0)::class.java.simpleName) {
"PrimaryContext" -> {
type = handleTypeAnnotationPrimary(typeContext, type)
}
}
return type
}

private fun handleTypeAnnotationPrimary(typeContext: TypeScriptParser.Type_Context, typ: String?): String? {
var typeStr = typ
val primaryContext = typeContext.getChild(0) as TypeScriptParser.PrimaryContext
val childPrimaryCtx = primaryContext.getChild(0)

when (childPrimaryCtx::class.java.simpleName) {
"ParenthesizedPrimTypeContext" -> {
val parentCtx = childPrimaryCtx as TypeScriptParser.ParenthesizedPrimTypeContext
typeStr = parentCtx.type_().text
}
}
return typeStr
}

fun buildMethodParameters(paramListCtx: TypeScriptParser.ParameterListContext?): Array<CodeProperty> {
Expand All @@ -60,10 +82,11 @@ open class TypeScriptAstListener() : TypeScriptParserBaseListener() {
}
"PredefinedTypeContext" -> {
val predefinedTypeContext = childNode as TypeScriptParser.PredefinedTypeContext
CodeProperty(
val predefinedType = CodeProperty(
TypeValue = "any",
TypeType = predefinedTypeContext.text
)
parameters += predefinedType
}
else -> {
println("enterFunctionDeclaration -> TypeScriptAstListener::buildMethodParameters")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,18 +158,29 @@ class TypeScriptFullIdentListener(private var node: TSIdentify) : TypeScriptAstL
nodeMap[nodeName] = currentNode
}

override fun exitInterfaceDeclaration(ctx: TypeScriptParser.InterfaceDeclarationContext?) {
currentNode = CodeDataStruct()
}

fun buildInterfaceBody(typeMemberList: TypeScriptParser.TypeMemberListContext?) {
for (memberContext in typeMemberList!!.typeMember()) {
val memberChild = memberContext.getChild(0)
val childType = memberChild::class.java.simpleName

when(childType) {
when (childType) {
"PropertySignaturContext" -> {
buildInterfacePropertySignature(memberChild as TypeScriptParser.PropertySignaturContext)
}
"MethodSignatureContext" -> {
val methodSignCtx = memberChild as TypeScriptParser.MethodSignatureContext
currentFunction = CodeFunction(
Name = methodSignCtx.propertyName().text
)

fillMethodFromCallSignature(methodSignCtx.callSignature())

currentNode.Functions += currentFunction
currentFunction = CodeFunction()
}
else -> {
println("enterInterfaceDeclaration -> buildInterfaceBody")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,4 +282,30 @@ export interface IPerson {
assertEquals(codeFile.DataStructures.size, 1)
assertEquals(codeFile.DataStructures[0].Fields.size, 2)
}

@Test
internal fun shouldIdentifyInterfacePropertyFunction() {
val code = """
interface IEmployee extends IPerson {
empCode: number;
readonly empName: string;
empDept?:string;
getSalary: (number) => number; // arrow function
getManagerName(number): string;
}
"""
val codeFile = TypeScriptAnalyser().analysis(code, "")
assertEquals(codeFile.DataStructures[0].Fields.size, 3)
assertEquals(codeFile.DataStructures[0].Functions.size, 2)

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

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

0 comments on commit 7b239c4

Please sign in to comment.