From d54c1f06a4bd0c8588eba287918a87961e3c0417 Mon Sep 17 00:00:00 2001 From: Phodal Huang Date: Thu, 17 Nov 2022 08:32:52 +0800 Subject: [PATCH] fix(ts): fix class abstract error issue --- .../src/main/antlr/TypeScriptParser.g4 | 10 +++++----- .../chapi/ast/typescriptast/TypeScriptAstListener.kt | 10 +++++----- .../ast/typescriptast/TypeScriptFullIdentListener.kt | 5 ++--- 3 files changed, 12 insertions(+), 13 deletions(-) diff --git a/chapi-ast-typescript/src/main/antlr/TypeScriptParser.g4 b/chapi-ast-typescript/src/main/antlr/TypeScriptParser.g4 index 327abf86..353494e4 100644 --- a/chapi-ast-typescript/src/main/antlr/TypeScriptParser.g4 +++ b/chapi-ast-typescript/src/main/antlr/TypeScriptParser.g4 @@ -355,7 +355,7 @@ constructSignature ; callSignature - : typeParameters? parameterBlock ( ':' typePredicateWithOperatorTypeRef | typeAnnotation)? + : typeParameters? parameterBlock (':' (typePredicateWithOperatorTypeRef | typeRef))? ; indexSignature @@ -437,7 +437,7 @@ classMemberList classMember : constructorDeclaration | indexSignature - | decoratorList? propertyMemberDeclaration + | decoratorList? propertyMemberDeclaration eos ; constructorDeclaration @@ -450,14 +450,14 @@ constructorDeclaration // ; propertyMemberDeclaration - : propertyMemberBase propertyName '!'? '?'? typeAnnotation? initializer? SemiColon # PropertyDeclarationExpression - | propertyMemberBase propertyName callSignature ( ('{' functionBody '}') | SemiColon) # MethodDeclarationExpression + : propertyMemberBase propertyName '!'? '?'? typeAnnotation? initializer? # PropertyDeclarationExpression + | propertyMemberBase propertyName callSignature ( ('{' functionBody '}')) # MethodDeclarationExpression | propertyMemberBase (getAccessor | setAccessor) # GetterSetterDeclarationExpression | abstractDeclaration # AbstractMemberDeclaration ; abstractDeclaration - : Abstract (Identifier callSignature | variableStatement) eos + : Abstract (Identifier '?'? callSignature | variableStatement) ; //propertyMember diff --git a/chapi-ast-typescript/src/main/kotlin/chapi/ast/typescriptast/TypeScriptAstListener.kt b/chapi-ast-typescript/src/main/kotlin/chapi/ast/typescriptast/TypeScriptAstListener.kt index b4713c3b..7fd3dae7 100644 --- a/chapi-ast-typescript/src/main/kotlin/chapi/ast/typescriptast/TypeScriptAstListener.kt +++ b/chapi-ast-typescript/src/main/kotlin/chapi/ast/typescriptast/TypeScriptAstListener.kt @@ -58,7 +58,11 @@ open class TypeScriptAstListener : TypeScriptParserBaseListener() { // } // } - return processRef(type) + return processRef(typeContext) + } + + fun processRef(type: TypeScriptParser.TypeRefContext): String { + return type.text } // private fun handleTypeAnnotationPrimary(typeContext: TypeScriptParser.PrimaryContext, typ: String?): String? { @@ -154,8 +158,4 @@ open class TypeScriptAstListener : TypeScriptParserBaseListener() { } return annotation } - - private fun processRef(type: String): String { - return type - } } 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 ad77941f..bd11c16d 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 @@ -5,7 +5,6 @@ import chapi.ast.antlr.TypeScriptParser.IdentifierExpressionContext import chapi.ast.antlr.TypeScriptParser.ParenthesizedExpressionContext import chapi.domain.core.* import chapi.infra.Stack -import org.antlr.v4.runtime.ParserRuleContext import org.antlr.v4.runtime.tree.TerminalNodeImpl class TypeScriptFullIdentListener(node: TSIdentify) : TypeScriptAstListener() { @@ -226,8 +225,8 @@ class TypeScriptFullIdentListener(node: TSIdentify) : TypeScriptAstListener() { ) val callSignCtx = childCtx.callSignature() - if (callSignCtx.typeAnnotation() != null) { - codeFunction.ReturnType = buildTypeAnnotation(callSignCtx.typeAnnotation())!! + if (callSignCtx.typeRef() != null) { + codeFunction.ReturnType = processRef(callSignCtx.typeRef()) } codeFunction.FilePath = filePath