diff --git a/chapi-ast-typescript/src/main/kotlin/chapi/ast/typescriptast/TypeScriptFullIdentListener.kt b/chapi-ast-typescript/src/main/kotlin/chapi/ast/typescriptast/TypeScriptFullIdentListener.kt index eaf3c53d..46df9746 100644 --- a/chapi-ast-typescript/src/main/kotlin/chapi/ast/typescriptast/TypeScriptFullIdentListener.kt +++ b/chapi-ast-typescript/src/main/kotlin/chapi/ast/typescriptast/TypeScriptFullIdentListener.kt @@ -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() private var methodMap = mutableMapOf() + 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() @@ -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) { diff --git a/chapi-ast-typescript/src/test/kotlin/chapi/ast/typescriptast/TypeScriptFullIdentListenerTest.kt b/chapi-ast-typescript/src/test/kotlin/chapi/ast/typescriptast/TypeScriptFullIdentListenerTest.kt index c7adaa07..7134b6bb 100644 --- a/chapi-ast-typescript/src/test/kotlin/chapi/ast/typescriptast/TypeScriptFullIdentListenerTest.kt +++ b/chapi-ast-typescript/src/test/kotlin/chapi/ast/typescriptast/TypeScriptFullIdentListenerTest.kt @@ -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") + } }