Skip to content

Commit

Permalink
fix(ts): fix more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Nov 17, 2022
1 parent 8f0b8c4 commit 2c47f65
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 27 deletions.
10 changes: 7 additions & 3 deletions chapi-ast-typescript/src/main/antlr/TypeScriptParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ constructSignature
;

callSignature
: typeParameters? parameterBlock (':' (typePredicateWithOperatorTypeRef | typeRef))?
: typeParameters? '(' parameterList? ','? ')' (':' (typePredicateWithOperatorTypeRef | typeRef))?
;

indexSignature
Expand Down Expand Up @@ -876,6 +876,12 @@ singleExpression
| Yield ({this.notLineTerminator()}? expressionSequence)? # YieldExpression
| Await singleExpression # AwaitExpression


// TODO: careful use those
| singleExpression '(' (argumentList ','?)? ')' # ArgumentsExpression
// RealtionExpression will have conflict
| singleExpression '<' typeArgumentList '>' '(' (argumentList ','?)? ')'# ArgumentsExpression

// respect precedence by order of sub-rules
| singleExpression assignmentOperator singleExpression # AssignmentExpression
| singleExpression '?' singleExpression ':' singleExpression # TernaryExpression
Expand Down Expand Up @@ -919,8 +925,6 @@ singleExpression
| templateStringLiteral # TemplateStringExpression
| singleExpression As asExpression # CastAsExpression

// TODO: careful use those
| singleExpression typeArguments? '(' (argumentList ','?)? ')' # ArgumentsExpression
| htmlElements # HtmlElementExpression
;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,11 +255,11 @@ class TypeScriptFullIdentListener(node: TSIdentify) : TypeScriptAstListener() {
return codeFunction
}

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

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

// if (ctx.interfaceExtendsClause() != null) {
// val elements = buildImplements(ctx.interfaceExtendsClause().classOrInterfaceTypeList())
// currentNode.Extend = elements[0]
// }
//
// val objectTypeCtx = ctx.objectType()
// if (objectTypeCtx.typeBody() != null) {
// this.buildInterfaceBody(objectTypeCtx.typeBody().typeMemberList())
// }
if (ctx.interfaceExtendsClause() != null) {
val elements = buildImplements(ctx.interfaceExtendsClause().classOrInterfaceTypeList())
currentNode.Extend = elements[0]
}

if (ctx.interfaceBody() != null) {
this.buildInterfaceBody(ctx.interfaceBody().interfaceMemberList())
}

nodeMap[nodeName] = currentNode
}
Expand All @@ -296,7 +295,7 @@ class TypeScriptFullIdentListener(node: TSIdentify) : TypeScriptAstListener() {
}

private fun buildInterfaceBody(typeMemberList: TypeScriptParser.InterfaceMemberListContext) {
typeMemberList?.interfaceMember()?.forEach { memberContext ->
typeMemberList.interfaceMember()?.forEach { memberContext ->
when (val memberChild = memberContext.getChild(0)) {
is TypeScriptParser.PropertySignatureContext -> {
buildInterfacePropertySignature(memberChild)
Expand Down Expand Up @@ -783,22 +782,27 @@ class TypeScriptFullIdentListener(node: TSIdentify) : TypeScriptAstListener() {
callSignCtx: TypeScriptParser.CallSignatureContext,
currentFunc: CodeFunction
) {
// if (callSignCtx.parameterList() != null) {
// val parameters = buildMethodParameters(callSignCtx.parameterList())
// currentFunc.Parameters = parameters
// }
//
// if (callSignCtx.typeAnnotation() != null) {
// val returnType = buildReturnTypeByType(callSignCtx.typeAnnotation())
// currentFunc.MultipleReturns += returnType
// }
if (callSignCtx.parameterList() != null) {
val parameters = buildMethodParameters(callSignCtx.parameterList())
currentFunc.Parameters = parameters
}

if (callSignCtx.typeRef() != null) {
val returnType = buildReturnTypeByTypeRef(callSignCtx.typeRef()!!)
currentFunc.MultipleReturns += returnType
}
}

private fun buildReturnTypeByType(typeAnnotationContext: TypeScriptParser.TypeAnnotationContext?): CodeProperty =
CodeProperty(
TypeType = buildTypeAnnotation(typeAnnotationContext) ?: "", TypeValue = ""
)

private fun buildReturnTypeByTypeRef(typeRefContext: TypeScriptParser.TypeRefContext): CodeProperty =
CodeProperty(
TypeType = processRef(typeRefContext) ?: "", TypeValue = ""
)

override fun enterExpressionStatement(ctx: TypeScriptParser.ExpressionStatementContext?) {
if (ctx?.expressionSequence() == null) return

Expand Down

0 comments on commit 2c47f65

Please sign in to comment.