Skip to content

Commit

Permalink
feat: <ts> add namespace as packagename support
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Feb 11, 2020
1 parent 9a2607c commit a38a124
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,25 @@ class TypeScriptFullIdentListener(private var node: TSIdentify) : TypeScriptAstL
private var defaultNode = CodeDataStruct()
private var currentFunction = CodeFunction(IsConstructor = false)
private var currentType: String = ""
private var namespaceName : String = ""

private var classNodeStack = Stack<CodeDataStruct>()
private var methodMap = mutableMapOf<String, CodeFunction>()

override fun enterNamespaceDeclaration(ctx: TypeScriptParser.NamespaceDeclarationContext?) {
this.namespaceName = ctx!!.namespaceName().text
}

override fun exitNamespaceDeclaration(ctx: TypeScriptParser.NamespaceDeclarationContext?) {
this.namespaceName = ""
}

override fun enterClassDeclaration(ctx: TypeScriptParser.ClassDeclarationContext?) {
val nodeName = ctx!!.Identifier().text
currentNode = CodeDataStruct(
Type = "Class",
NodeName = nodeName
NodeName = nodeName,
Package = this.namespaceName
)

val heritageCtx = ctx.classHeritage()
Expand Down Expand Up @@ -142,7 +152,8 @@ class TypeScriptFullIdentListener(private var node: TSIdentify) : TypeScriptAstL

currentNode = CodeDataStruct(
Type = currentType,
NodeName = nodeName
NodeName = nodeName,
Package = this.namespaceName
)

if (ctx.interfaceExtendsClause() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,4 +367,18 @@ let sumArrow = (x: number, y: number): number => {
assertEquals(functions[0].Parameters[1].TypeValue, "y")
assertEquals(functions[0].MultipleReturns[0].TypeType, "number")
}

@Test
internal fun shouldIdentifyNameSpaceAsPackage() {
val code = """
export namespace Polygons {
export class Triangle { }
export class Square { }
}
"""
val codeFile = TypeScriptAnalyser().analysis(code, "")
assertEquals(codeFile.DataStructures.size, 2)
assertEquals(codeFile.DataStructures[0].Package, "Polygons")
assertEquals(codeFile.DataStructures[1].Package, "Polygons")
}
}

0 comments on commit a38a124

Please # to comment.