From 3bd55c63cff0306ba725e9a61a856b2c7ab36008 Mon Sep 17 00:00:00 2001 From: Phodal Huang Date: Tue, 22 Feb 2022 23:15:55 +0800 Subject: [PATCH] fix: fix default items not path issue --- .gitignore | 1 + .../app/analyser/TypeScriptAnalyserApp.kt | 2 +- .../app/analyser/TypeScriptAnalyserAppTest.kt | 19 +++++++++++++++++ .../TypeScriptFullIdentListener.kt | 21 +++++++++---------- .../TypeScriptFullIdentListenerTest.kt | 3 ++- 5 files changed, 33 insertions(+), 13 deletions(-) create mode 100644 chapi-application/src/test/kotlin/chapi/app/analyser/TypeScriptAnalyserAppTest.kt diff --git a/.gitignore b/.gitignore index 8f878816..4f5b6f5b 100644 --- a/.gitignore +++ b/.gitignore @@ -34,3 +34,4 @@ local.properties chapi.json *.ipr *.iws +output.json diff --git a/chapi-application/src/main/kotlin/chapi/app/analyser/TypeScriptAnalyserApp.kt b/chapi-application/src/main/kotlin/chapi/app/analyser/TypeScriptAnalyserApp.kt index 23d77e08..6da259d3 100644 --- a/chapi-application/src/main/kotlin/chapi/app/analyser/TypeScriptAnalyserApp.kt +++ b/chapi-application/src/main/kotlin/chapi/app/analyser/TypeScriptAnalyserApp.kt @@ -6,7 +6,7 @@ import chapi.app.analyser.support.BaseAnalyser import chapi.ast.typescriptast.TypeScriptAnalyser import chapi.domain.core.CodeDataStruct -class TypeScriptAnalyserApp(config: ChapiConfig) : BaseAnalyser(config) { +class TypeScriptAnalyserApp(config: ChapiConfig = ChapiConfig(language = "typescript")) : BaseAnalyser(config) { override fun analysisByFiles(files: Array): Array { var nodeInfos: Array = arrayOf() for (file in files) { diff --git a/chapi-application/src/test/kotlin/chapi/app/analyser/TypeScriptAnalyserAppTest.kt b/chapi-application/src/test/kotlin/chapi/app/analyser/TypeScriptAnalyserAppTest.kt new file mode 100644 index 00000000..3ac63d5e --- /dev/null +++ b/chapi-application/src/test/kotlin/chapi/app/analyser/TypeScriptAnalyserAppTest.kt @@ -0,0 +1,19 @@ +package chapi.app.analyser + +import kotlinx.serialization.encodeToString +import kotlinx.serialization.json.Json +import org.junit.jupiter.api.Disabled +import org.junit.jupiter.api.Test +import java.io.FileWriter + +internal class TypeScriptAnalyserAppTest { + @Test + @Disabled + fun shouldIdentifySamePackage() { + val testPath = "/Volumes/source/archguard/archguard-frontend/archguard/src" + val nodes = TypeScriptAnalyserApp().analysisNodeByPath(testPath) + println(nodes) + + FileWriter("output.json").use { it.write(Json.encodeToString(nodes)) } + } +} 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 60e2e889..3a58ae02 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 @@ -7,14 +7,12 @@ import chapi.infra.Stack class TypeScriptFullIdentListener(private var node: TSIdentify) : TypeScriptAstListener() { private var hasAnnotation: Boolean = false; + private var hasEnterClass = false private var currentExprIdent: String? = "" private var localVars = mutableMapOf() - private var dataStructQueue = arrayOf() - private var hasEnterClass = false private var nodeMap = mutableMapOf() - private var codeContainer: CodeContainer = - CodeContainer(FullName = node.fileName) + private var codeContainer: CodeContainer = CodeContainer(FullName = node.fileName) private var currentNode = CodeDataStruct() private var defaultNode = CodeDataStruct() @@ -22,9 +20,7 @@ class TypeScriptFullIdentListener(private var node: TSIdentify) : TypeScriptAstL private var namespaceName: String = "" private var currentAnnotations = arrayOf() - private var classNodeStack = - Stack() - private var methodMap = mutableMapOf() + private var classNodeStack = Stack() override fun enterNamespaceDeclaration(ctx: TypeScriptParser.NamespaceDeclarationContext?) { this.namespaceName = ctx!!.namespaceName().text @@ -358,7 +354,7 @@ class TypeScriptFullIdentListener(private var node: TSIdentify) : TypeScriptAstL when (childType) { "IdentifierExpressionContext" -> { val context = ctx as IdentifierExpressionContext - currentExprIdent += context.identifierName().text + currentExprIdent = context.identifierName().text if (context.singleExpression() != null) { parseSingleExpression(context.singleExpression()) @@ -552,10 +548,12 @@ class TypeScriptFullIdentListener(private var node: TSIdentify) : TypeScriptAstL override fun enterExpressionStatement(ctx: TypeScriptParser.ExpressionStatementContext?) { println("enterExpressionStatement : " + ctx!!.text) - for (singleExprCtx in ctx.expressionSequence().singleExpression()) { - val singleCtxType = singleExprCtx::class.java.simpleName + if(ctx.expressionSequence() == null) { + return + } - when (singleCtxType) { + for (singleExprCtx in ctx.expressionSequence().singleExpression()) { + when (val singleCtxType = singleExprCtx::class.java.simpleName) { "ArgumentsExpressionContext" -> { val codeCall = CodeCall() @@ -662,6 +660,7 @@ class TypeScriptFullIdentListener(private var node: TSIdentify) : TypeScriptAstL } if (defaultNode.Functions.isNotEmpty()) { defaultNode.NodeName = "default" + defaultNode.FilePath = codeContainer.FullName codeContainer.DataStructures += defaultNode } 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 eb94f9fc..47810980 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 @@ -258,8 +258,9 @@ function Sum(x: number, y: number) : void { } """ - val codeFile = TypeScriptAnalyser().analysis(normalClassFunction, "") + val codeFile = TypeScriptAnalyser().analysis(normalClassFunction, "demo.ts") assertEquals(codeFile.DataStructures[0].NodeName, "default") + assertEquals(codeFile.DataStructures[0].FilePath, "demo.ts") assertEquals(codeFile.DataStructures[0].Functions[0].Name, "Sum") assertEquals(codeFile.DataStructures[0].Functions[0].Parameters.size, 2) assertEquals(codeFile.DataStructures[0].Functions[0].MultipleReturns.size, 1)