From e87a90cc8ea4461573e72c2f91f24bfb363f1872 Mon Sep 17 00:00:00 2001 From: Phodal Huang Date: Tue, 15 Nov 2022 17:22:30 +0800 Subject: [PATCH] fix(ts): fix element in query --- .../src/main/antlr/TypeScriptParser.g4 | 2 ++ .../TypeScriptFullIdentListener.kt | 2 +- .../ast/typescriptast/TypeScriptAnalyserTest.kt | 17 ++++++++++++++++- .../ast/typescriptast/TypeScriptBugTest.kt | 12 +++++++++++- 4 files changed, 30 insertions(+), 3 deletions(-) diff --git a/chapi-ast-typescript/src/main/antlr/TypeScriptParser.g4 b/chapi-ast-typescript/src/main/antlr/TypeScriptParser.g4 index 494c72a3..ebc89a4c 100644 --- a/chapi-ast-typescript/src/main/antlr/TypeScriptParser.g4 +++ b/chapi-ast-typescript/src/main/antlr/TypeScriptParser.g4 @@ -782,6 +782,8 @@ singleExpression | singleExpression '?'? '!'? '.' '#'? identifierName nestedTypeGeneric? # MemberDotExpression // for: `onHotUpdateSuccess?.();` | singleExpression '?'? '!'? '.' '#'? '(' identifierName? ')' # MemberDotExpression + // onChange?.(userName || password || null) + | singleExpression '?'? '!'? '.' '#'? '(' singleExpression? ')' # MemberDotExpression // samples: `error?.response?.data?.message ?? error.message;` | singleExpression '??' singleExpression # NullCoalesceExpression | singleExpression '!' # PropCheckExpression 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 c29626c2..9a3a489f 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 @@ -690,7 +690,7 @@ class TypeScriptFullIdentListener(node: TSIdentify) : TypeScriptAstListener() { val memberDot = it as TypeScriptParser.MemberDotExpressionContext when (val subName = memberDot.singleExpression()::class.java.simpleName) { "ParenthesizedExpressionContext" -> { - params += parseParenthesizedExpression(memberDot.singleExpression()) + params += parseParenthesizedExpression(memberDot.singleExpression().first()) } "ArgumentsExpressionContext" -> { // request.get('/api/v1/xxx?id=1').then(function(response){console.log(response)}).catch() diff --git a/chapi-ast-typescript/src/test/kotlin/chapi/ast/typescriptast/TypeScriptAnalyserTest.kt b/chapi-ast-typescript/src/test/kotlin/chapi/ast/typescriptast/TypeScriptAnalyserTest.kt index cf1f818e..eefd389f 100644 --- a/chapi-ast-typescript/src/test/kotlin/chapi/ast/typescriptast/TypeScriptAnalyserTest.kt +++ b/chapi-ast-typescript/src/test/kotlin/chapi/ast/typescriptast/TypeScriptAnalyserTest.kt @@ -3,8 +3,9 @@ package chapi.ast.typescriptast import chapi.domain.core.DataStructType import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.Test +import java.io.File -internal class TypeScriptAnalyserTest { +class TypeScriptAnalyserTest { @Test internal fun shouldAnalysisTypeScriptMultipleClass() { val content = this::class.java.getResource("/grammar/Class.ts")!!.readText() @@ -66,4 +67,18 @@ internal class TypeScriptAnalyserTest { assertEquals(codeFile.PackageName, "@.grammar.AbstractClass") assertEquals(codeFile.DataStructures[0].Package, "@.grammar.AbstractClass") } + + @Test + @Ignore + fun someBug() { + val dir = File("/Users/phodal/bug-ui-system") + dir.walkTopDown().forEach { + if (it.extension == "ts" || it.extension == "js") { + val content = it.readText() + println(it.absolutePath) + val codeFile = TypeScriptAnalyser().analysis(content, it.name) + println(codeFile) + } + } + } } diff --git a/chapi-ast-typescript/src/test/kotlin/chapi/ast/typescriptast/TypeScriptBugTest.kt b/chapi-ast-typescript/src/test/kotlin/chapi/ast/typescriptast/TypeScriptBugTest.kt index dc9fa232..bd8cb524 100644 --- a/chapi-ast-typescript/src/test/kotlin/chapi/ast/typescriptast/TypeScriptBugTest.kt +++ b/chapi-ast-typescript/src/test/kotlin/chapi/ast/typescriptast/TypeScriptBugTest.kt @@ -20,11 +20,21 @@ exports.test = test; } @Test - fun backend_arrow_function2() { + fun of_keyword_lost() { val code = """ import {EMPTY, Observable, of} from 'rxjs'; """ TypeScriptAnalyser().analysis(code, "index.tsx") } + + @Test + fun member_dot_issue() { + val code = """export class DemoComponent implements OnInit, ControlValueAccessor { + ngOnInit(): void { + this.onChange?.(userName || password || null); + } +}""" + TypeScriptAnalyser().analysis(code, "index.tsx") + } }