Skip to content

Commit

Permalink
fix: fix default items not path issue
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Feb 22, 2022
1 parent aa8a7fc commit 3bd55c6
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ local.properties
chapi.json
*.ipr
*.iws
output.json
Original file line number Diff line number Diff line change
Expand Up @@ -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<AbstractFile>): Array<CodeDataStruct> {
var nodeInfos: Array<CodeDataStruct> = arrayOf()
for (file in files) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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)) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,20 @@ 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<String, String>()
private var dataStructQueue = arrayOf<CodeDataStruct>()
private var hasEnterClass = false

private var nodeMap = mutableMapOf<String, CodeDataStruct>()
private var codeContainer: CodeContainer =
CodeContainer(FullName = node.fileName)
private var codeContainer: CodeContainer = CodeContainer(FullName = node.fileName)

private var currentNode = CodeDataStruct()
private var defaultNode = CodeDataStruct()
private var currentFunction = CodeFunction(IsConstructor = false)
private var namespaceName: String = ""
private var currentAnnotations = arrayOf<CodeAnnotation>()

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

override fun enterNamespaceDeclaration(ctx: TypeScriptParser.NamespaceDeclarationContext?) {
this.namespaceName = ctx!!.namespaceName().text
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 3bd55c6

Please # to comment.