Skip to content

Commit 2c47f65

Browse files
committed
fix(ts): fix more tests
1 parent 8f0b8c4 commit 2c47f65

2 files changed

Lines changed: 35 additions & 27 deletions

File tree

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ constructSignature
355355
;
356356

357357
callSignature
358-
: typeParameters? parameterBlock (':' (typePredicateWithOperatorTypeRef | typeRef))?
358+
: typeParameters? '(' parameterList? ','? ')' (':' (typePredicateWithOperatorTypeRef | typeRef))?
359359
;
360360

361361
indexSignature
@@ -876,6 +876,12 @@ singleExpression
876876
| Yield ({this.notLineTerminator()}? expressionSequence)? # YieldExpression
877877
| Await singleExpression # AwaitExpression
878878

879+
880+
// TODO: careful use those
881+
| singleExpression '(' (argumentList ','?)? ')' # ArgumentsExpression
882+
// RealtionExpression will have conflict
883+
| singleExpression '<' typeArgumentList '>' '(' (argumentList ','?)? ')'# ArgumentsExpression
884+
879885
// respect precedence by order of sub-rules
880886
| singleExpression assignmentOperator singleExpression # AssignmentExpression
881887
| singleExpression '?' singleExpression ':' singleExpression # TernaryExpression
@@ -919,8 +925,6 @@ singleExpression
919925
| templateStringLiteral # TemplateStringExpression
920926
| singleExpression As asExpression # CastAsExpression
921927

922-
// TODO: careful use those
923-
| singleExpression typeArguments? '(' (argumentList ','?)? ')' # ArgumentsExpression
924928
| htmlElements # HtmlElementExpression
925929
;
926930

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

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -255,11 +255,11 @@ class TypeScriptFullIdentListener(node: TSIdentify) : TypeScriptAstListener() {
255255
return codeFunction
256256
}
257257

258-
// private fun buildImplements(typeList: TypeScriptParser.ClassOrInterfaceTypeListContext?): Array<String> {
259-
// return typeList?.typeReference()?.map { typeRefCtx ->
260-
// typeRefCtx.typeName().text
261-
// }?.toTypedArray() ?: arrayOf()
262-
// }
258+
private fun buildImplements(typeList: TypeScriptParser.ClassOrInterfaceTypeListContext?): Array<String> {
259+
return typeList?.parameterizedTypeRef()?.map { typeRefCtx ->
260+
typeRefCtx.typeName().text
261+
}?.toTypedArray() ?: arrayOf()
262+
}
263263

264264
override fun exitClassDeclaration(ctx: TypeScriptParser.ClassDeclarationContext?) {
265265
hasEnterClass = false
@@ -278,15 +278,14 @@ class TypeScriptFullIdentListener(node: TSIdentify) : TypeScriptAstListener() {
278278
Position = buildPosition(ctx)
279279
)
280280

281-
// if (ctx.interfaceExtendsClause() != null) {
282-
// val elements = buildImplements(ctx.interfaceExtendsClause().classOrInterfaceTypeList())
283-
// currentNode.Extend = elements[0]
284-
// }
285-
//
286-
// val objectTypeCtx = ctx.objectType()
287-
// if (objectTypeCtx.typeBody() != null) {
288-
// this.buildInterfaceBody(objectTypeCtx.typeBody().typeMemberList())
289-
// }
281+
if (ctx.interfaceExtendsClause() != null) {
282+
val elements = buildImplements(ctx.interfaceExtendsClause().classOrInterfaceTypeList())
283+
currentNode.Extend = elements[0]
284+
}
285+
286+
if (ctx.interfaceBody() != null) {
287+
this.buildInterfaceBody(ctx.interfaceBody().interfaceMemberList())
288+
}
290289

291290
nodeMap[nodeName] = currentNode
292291
}
@@ -296,7 +295,7 @@ class TypeScriptFullIdentListener(node: TSIdentify) : TypeScriptAstListener() {
296295
}
297296

298297
private fun buildInterfaceBody(typeMemberList: TypeScriptParser.InterfaceMemberListContext) {
299-
typeMemberList?.interfaceMember()?.forEach { memberContext ->
298+
typeMemberList.interfaceMember()?.forEach { memberContext ->
300299
when (val memberChild = memberContext.getChild(0)) {
301300
is TypeScriptParser.PropertySignatureContext -> {
302301
buildInterfacePropertySignature(memberChild)
@@ -783,22 +782,27 @@ class TypeScriptFullIdentListener(node: TSIdentify) : TypeScriptAstListener() {
783782
callSignCtx: TypeScriptParser.CallSignatureContext,
784783
currentFunc: CodeFunction
785784
) {
786-
// if (callSignCtx.parameterList() != null) {
787-
// val parameters = buildMethodParameters(callSignCtx.parameterList())
788-
// currentFunc.Parameters = parameters
789-
// }
790-
//
791-
// if (callSignCtx.typeAnnotation() != null) {
792-
// val returnType = buildReturnTypeByType(callSignCtx.typeAnnotation())
793-
// currentFunc.MultipleReturns += returnType
794-
// }
785+
if (callSignCtx.parameterList() != null) {
786+
val parameters = buildMethodParameters(callSignCtx.parameterList())
787+
currentFunc.Parameters = parameters
788+
}
789+
790+
if (callSignCtx.typeRef() != null) {
791+
val returnType = buildReturnTypeByTypeRef(callSignCtx.typeRef()!!)
792+
currentFunc.MultipleReturns += returnType
793+
}
795794
}
796795

797796
private fun buildReturnTypeByType(typeAnnotationContext: TypeScriptParser.TypeAnnotationContext?): CodeProperty =
798797
CodeProperty(
799798
TypeType = buildTypeAnnotation(typeAnnotationContext) ?: "", TypeValue = ""
800799
)
801800

801+
private fun buildReturnTypeByTypeRef(typeRefContext: TypeScriptParser.TypeRefContext): CodeProperty =
802+
CodeProperty(
803+
TypeType = processRef(typeRefContext) ?: "", TypeValue = ""
804+
)
805+
802806
override fun enterExpressionStatement(ctx: TypeScriptParser.ExpressionStatementContext?) {
803807
if (ctx?.expressionSequence() == null) return
804808

0 commit comments

Comments
 (0)