From ec2628295c7e7abf6d4b56ccc03d8c28f20a165e Mon Sep 17 00:00:00 2001 From: Phodal Huang Date: Thu, 17 Nov 2022 11:55:47 +0800 Subject: [PATCH] fix(ts): fix function decl error issues --- .../src/main/antlr/TypeScriptParser.g4 | 11 ++++++----- .../ast/typescriptast/TypeScriptFullIdentListener.kt | 7 ++++++- .../typescriptast/TypeScriptFullIdentListenerTest.kt | 2 ++ 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/chapi-ast-typescript/src/main/antlr/TypeScriptParser.g4 b/chapi-ast-typescript/src/main/antlr/TypeScriptParser.g4 index 576a6ee4..d225c899 100644 --- a/chapi-ast-typescript/src/main/antlr/TypeScriptParser.g4 +++ b/chapi-ast-typescript/src/main/antlr/TypeScriptParser.g4 @@ -356,7 +356,7 @@ constructSignature ; callSignature - : typeParameters? '(' parameterList? ','? ')' (':' (typePredicateWithOperatorTypeRef | typeRef))? + : typeParameters? '(' parameterList? ','? ')' (':' (typePredicateWithOperatorTypeRef | typeRef))? eos? ; indexSignature @@ -401,7 +401,7 @@ enumMember // Function Declaration functionDeclaration - : propertyMemberBase? Function '*'? identifierName callSignature functionBody? SemiColon? + : propertyMemberBase? Function '*'? identifierName callSignature '{' functionBody '}' eos? ; functionBody @@ -831,7 +831,7 @@ breakStatement returnStatement - : Return ({this.notLineTerminator()}? expressionSequence)? eos + : Return ({this.notLineTerminator()}? expressionSequence)? eos? // | Return '(' htmlElements ')' eos ; @@ -857,7 +857,7 @@ debuggerStatement expressionStatement - : {this.notOpenBraceAndNotFunction()}? expressionSequence eos + : {this.notOpenBraceAndNotFunction()}? expressionSequence eos? ; @@ -923,6 +923,7 @@ singleExpression | yieldStatement # YieldExpression // ECMAScript 6 | Await singleExpression # AwaitExpression | typeArguments? identifierName singleExpression? # IdentifierExpression + | typeArguments expressionSequence? # GenericTypes | literal # LiteralExpression | arrayLiteral # ArrayLiteralExpression @@ -981,7 +982,7 @@ unaryOperator generatorFunctionDeclaration - : Function '*' Identifier? '(' formalParameterList? ')' '{' functionBody '}' + : Function '*' Identifier? '(' formalParameterList? ')' '{' functionBody? '}' ; generatorBlock 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 afb4610e..4e4d83c4 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 @@ -754,7 +754,12 @@ class TypeScriptFullIdentListener(node: TSIdentify) : TypeScriptAstListener() { return objectLiteral.propertyAssignment().mapNotNull { property -> when (property) { is TypeScriptParser.PropertyExpressionAssignmentContext -> { - val text = singleExpToText(property.singleExpression()) + val text = if(property.singleExpression() != null) { + property.singleExpression().text + } else { + property.text + } + val value = CodeProperty(TypeType = "value", TypeValue = text) val propText = property.propertyName().text diff --git a/chapi-ast-typescript/src/test/kotlin/chapi/ast/typescriptast/TypeScriptFullIdentListenerTest.kt b/chapi-ast-typescript/src/test/kotlin/chapi/ast/typescriptast/TypeScriptFullIdentListenerTest.kt index f7896eb4..8ba7b15b 100644 --- a/chapi-ast-typescript/src/test/kotlin/chapi/ast/typescriptast/TypeScriptFullIdentListenerTest.kt +++ b/chapi-ast-typescript/src/test/kotlin/chapi/ast/typescriptast/TypeScriptFullIdentListenerTest.kt @@ -605,6 +605,8 @@ export function querySystemInfo() { val dataStruct = codeFile.DataStructures[0] val calls = dataStruct.Functions[0].FunctionCalls + println(Json.encodeToString(calls)) + assertEquals(1, calls.size) assertEquals(1, dataStruct.Fields.size); assertEquals("systemInfoApi", dataStruct.Fields[0].TypeKey);