Skip to content

Commit 9f721bd

Browse files
committed
fix(ts): fix arrow functions
1 parent 991d3ef commit 9f721bd

3 files changed

Lines changed: 59 additions & 45 deletions

File tree

chapi-ast-typescript/src/main/antlr/TypeScriptParser.g4

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,7 @@ exportStatement
715715

716716
exportStatementTail
717717
: Default? declarationStatement #ExportElementDirectly
718-
| Default identifierName #ExportElementAsDefault
718+
| Default identifierName #ExportDefaultDeclaration
719719
| multipleExportElements (From StringLiteral)? #ExportElements
720720
| Multiply (As identifierName)? From StringLiteral #ExportModule
721721
| As Namespace identifierName eos #ExportAsNamespace
@@ -749,7 +749,7 @@ variableDeclarationList
749749
;
750750

751751
variableDeclaration
752-
: identifierName typeAnnotation? ('=' typeParameters? singleExpression)? // ECMAScript 6: Array & Object Matching
752+
: identifierName typeAnnotation? ('=' typeParameters? singleExpression)? // ECMAScript 6: Array & Object Matching
753753
| arrayLiteral
754754
| objectLiteral
755755
;
@@ -874,10 +874,13 @@ singleExpression
874874
| arrowFunctionDeclaration # ArrowFunctionExpressionL
875875
| classExpression # ClassExpressionL
876876

877-
| Yield ({this.notLineTerminator()}? expressionSequence)? # YieldExpression
877+
| singleExpression templateStringLiteral # TemplateStringExpression // ECMAScript 6
878+
| iteratorBlock # IteratorsExpression // ECMAScript 6
879+
| generatorBlock # GeneratorsExpression // ECMAScript 6
880+
| generatorFunctionDeclaration # GeneratorsFunctionExpression // ECMAScript 6
881+
| yieldStatement # YieldExpression // ECMAScript 6
878882
| Await singleExpression # AwaitExpression
879883

880-
881884
// TODO: careful use those
882885
| singleExpression '(' (argumentList ','?)? ')' # ArgumentsExpression
883886
// RealtionExpression will have conflict
@@ -919,7 +922,7 @@ singleExpression
919922

920923
| This # ThisExpression
921924
| Super # SuperExpression
922-
| typeArguments? identifierName # IdentifierExpression
925+
| typeArguments? identifierName singleExpression? # IdentifierExpression
923926
| literal # LiteralExpression
924927
| arrayLiteral # ArrayLiteralExpression
925928
| objectLiteral # ObjectLiteralExpression
@@ -929,6 +932,9 @@ singleExpression
929932
| htmlElements # HtmlElementExpression
930933
;
931934

935+
yieldStatement
936+
: Yield ({this.notLineTerminator()}? expressionSequence)? eos
937+
;
932938

933939
asExpression
934940
: singleExpression
@@ -973,6 +979,10 @@ unaryOperator
973979

974980

975981

982+
generatorFunctionDeclaration
983+
: Function '*' Identifier? '(' formalParameterList? ')' '{' functionBody '}'
984+
;
985+
976986
generatorBlock
977987
: '{' generatorDefinition (',' generatorDefinition)* ','? '}'
978988
;

chapi-ast-typescript/src/main/kotlin/chapi/ast/typescriptast/TypeScriptFullIdentListener.kt

Lines changed: 36 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -96,36 +96,36 @@ class TypeScriptFullIdentListener(node: TSIdentify) : TypeScriptAstListener() {
9696
): CodeField {
9797
val key = it.getChild(0).text
9898
val singleExpression = it.singleExpression()
99-
val field = CodeField(TypeKey = key, TypeValue = "", Modifiers = modifiers)
10099

101-
// val lastExpr = singleExpression.last()
102-
// val field = CodeField(TypeKey = key, TypeValue = lastExpr.text, Modifiers = modifiers)
103-
//
104-
// when (lastExpr) {
105-
// is TypeScriptParser.LiteralExpressionContext -> {
106-
// if (lastExpr.literal().StringLiteral() != null) {
107-
// field.TypeValue = unQuote(lastExpr.text)
108-
// field.TypeType = "String"
109-
// }
110-
// }
111-
//
112-
// is IdentifierExpressionContext -> {
113-
// singleExprToFieldCall(field, lastExpr.singleExpression(), lastExpr.identifierName().text)
114-
// }
115-
//
116-
// is TypeScriptParser.YieldExpressionContext -> {
117-
// if (lastExpr.yieldStatement().expressionSequence() != null) {
118-
// val singeExprs = lastExpr.yieldStatement().expressionSequence().singleExpression()
119-
// singeExprs.forEach { expr ->
120-
// singleExprToFieldCall(field, expr, expr.text)
121-
// }
122-
// }
123-
// }
124-
//
125-
// else -> {
126-
// // println("variableToFields -> ${lastExpr.text} === ${lastExpr.javaClass.simpleName}")
127-
// }
128-
// }
100+
val lastExpr = singleExpression
101+
val field = CodeField(TypeKey = key, TypeValue = lastExpr.text, Modifiers = modifiers)
102+
103+
when (lastExpr) {
104+
is TypeScriptParser.LiteralExpressionContext -> {
105+
if (lastExpr.literal().StringLiteral() != null) {
106+
field.TypeValue = unQuote(lastExpr.text)
107+
field.TypeType = "String"
108+
}
109+
}
110+
111+
is IdentifierExpressionContext -> {
112+
singleExprToFieldCall(field, lastExpr.singleExpression(), lastExpr.identifierName().text)
113+
}
114+
115+
is TypeScriptParser.YieldExpressionContext -> {
116+
if (lastExpr.yieldStatement().expressionSequence() != null) {
117+
val singeExprs = lastExpr.yieldStatement().expressionSequence().singleExpression()
118+
singeExprs.forEach { expr ->
119+
singleExprToFieldCall(field, expr, expr.text)
120+
}
121+
}
122+
}
123+
124+
else -> {
125+
// println("variableToFields -> ${lastExpr.text} === ${lastExpr.javaClass.simpleName}")
126+
}
127+
}
128+
129129
return field
130130
}
131131

@@ -941,16 +941,13 @@ class TypeScriptFullIdentListener(node: TSIdentify) : TypeScriptAstListener() {
941941
CodeProperty(TypeValue = typeValue, TypeType = "")
942942
}?.toTypedArray() ?: arrayOf()
943943

944-
// override fun enterExportDefaultDeclaration(ctx: TypeScriptParser.ExportDefaultDeclarationContext?) {
945-
// val singleExpr = ctx!!.singleExpression()
946-
// if (singleExpr != null) {
947-
// if (!singleExpr.text.contains("(")) {
948-
// currentNode.Exports += CodeExport(singleExpr.text)
949-
// defaultNode.Exports += CodeExport(singleExpr.text)
950-
// }
951-
// }
952-
// }
953-
944+
override fun enterExportDefaultDeclaration(ctx: TypeScriptParser.ExportDefaultDeclarationContext?) {
945+
val name = ctx!!.identifierName()
946+
if (name != null) {
947+
currentNode.Exports += CodeExport(name.text)
948+
defaultNode.Exports += CodeExport(name.text)
949+
}
950+
}
954951

955952
fun getNodeInfo(): CodeContainer {
956953
for (entry in nodeMap) {

chapi-ast-typescript/src/test/kotlin/chapi/ast/typescriptast/TypeScriptAnalyserTest.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package chapi.ast.typescriptast
22

33
import chapi.domain.core.DataStructType
4+
import kotlinx.serialization.encodeToString
5+
import kotlinx.serialization.json.Json
46
import org.junit.jupiter.api.Assertions.assertEquals
57
import org.junit.jupiter.api.Disabled
68
import org.junit.jupiter.api.Test
@@ -45,10 +47,15 @@ class TypeScriptAnalyserTest {
4547
val content = this::class.java.getResource("/grammar/Function.ts")!!.readText()
4648
val codeFile = TypeScriptAnalyser().analysis(content, "")
4749

50+
println(Json.encodeToString(codeFile))
51+
4852
assertEquals(codeFile.DataStructures.size, 1)
4953
assertEquals(codeFile.DataStructures[0].NodeName, "default")
5054
val functions = codeFile.DataStructures[0].Functions
51-
assertEquals(functions.size, 9)
55+
assertEquals(functions.size, 3)
56+
57+
val fields = codeFile.DataStructures[0].Fields
58+
assertEquals(fields.size, 6)
5259
}
5360

5461
@Test

0 commit comments

Comments
 (0)