Skip to content

Commit e57af30

Browse files
committed
fix(ts): fix import of conflict
1 parent 6e1890b commit e57af30

5 files changed

Lines changed: 24 additions & 11 deletions

File tree

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,8 @@ importDefault
411411

412412
aliasName
413413
: identifierName (As identifierName)?
414+
// for import { of } from 'rxjs';
415+
| Of
414416
;
415417

416418
importNamespace
@@ -779,12 +781,12 @@ singleExpression
779781
| arrowFunctionDeclaration # ArrowFunctionExpression // ECMAScript 6
780782
| jsxArrowFunction # JsxArrowFunctionExpression
781783
| Class Identifier? classTail # ClassExpression
782-
| singleExpression '[' expressionSequence ']' # MemberIndexExpression
783-
| singleExpression '?'? '!'? '.' '#'? identifierName nestedTypeGeneric? # MemberDotExpression
784+
// this.form.value?.id?.[0]
785+
| singleExpression '?'? '!'? '.''[' expressionSequence ']' # MemberIndexExpression
784786
// for: `onHotUpdateSuccess?.();`
785-
| singleExpression '?'? '!'? '.' '#'? '(' identifierName? ')' # MemberDotExpression
786787
// onChange?.(userName || password || null)
787-
| singleExpression '?'? '!'? '.' '#'? '('? singleExpression? ')'? # MemberDotExpression
788+
| singleExpression '?'? '!'? '.' '#'? identifierName? nestedTypeGeneric? # MemberDotExpression
789+
788790
// samples: `error?.response?.data?.message ?? error.message;`
789791
| singleExpression '??' singleExpression # NullCoalesceExpression
790792
| singleExpression '!' # PropCheckExpression
@@ -986,7 +988,6 @@ keyword
986988
| Namespace
987989
| Number
988990
| Boolean
989-
| Of
990991
;
991992

992993
getter

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -361,9 +361,15 @@ class TypeScriptFullIdentListener(node: TSIdentify) : TypeScriptAstListener() {
361361

362362
if (ctx.moduleItems() != null) {
363363
for (nameContext in ctx.moduleItems().aliasName()) {
364-
codeImport.UsageName += nameContext.identifierName()[0].text
365-
if (nameContext.As() != null) {
366-
codeImport.AsName += nameContext.identifierName()[1].text
364+
if (nameContext.identifierName().isNotEmpty()) {
365+
codeImport.UsageName += nameContext.identifierName()[0].text
366+
if (nameContext.As() != null) {
367+
codeImport.AsName += nameContext.identifierName()[1].text
368+
}
369+
}
370+
if (nameContext.Of() != null) {
371+
codeImport.UsageName += nameContext.Of().text
372+
codeImport.AsName += nameContext.Of().text
367373
}
368374
}
369375
}
@@ -690,7 +696,7 @@ class TypeScriptFullIdentListener(node: TSIdentify) : TypeScriptAstListener() {
690696
val memberDot = it as TypeScriptParser.MemberDotExpressionContext
691697
when (val subName = memberDot.singleExpression()::class.java.simpleName) {
692698
"ParenthesizedExpressionContext" -> {
693-
params += parseParenthesizedExpression(memberDot.singleExpression().first())
699+
params += parseParenthesizedExpression(memberDot.singleExpression())
694700
}
695701
"ArgumentsExpressionContext" -> {
696702
// request.get('/api/v1/xxx?id=1').then(function(response){console.log(response)}).catch()

chapi-ast-typescript/src/test/kotlin/chapi/ast/typescriptast/TypeScriptAnalyserTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class TypeScriptAnalyserTest {
7070
}
7171

7272
@Test
73-
// @Disabled
73+
@Disabled
7474
fun someBug() {
7575
val dir = File("/Users/phodal/bug-ui-system")
7676
dir.walkTopDown().forEach {

chapi-ast-typescript/src/test/kotlin/chapi/ast/typescriptast/TypeScriptFullIdentListenerTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1065,7 +1065,7 @@ export class ELKLayout {
10651065
}
10661066

10671067
@Test
1068-
internal fun forOfStatement() {
1068+
fun forOfStatement() {
10691069
val code = """
10701070
function dfs() {
10711071
if (node in graph) {

chapi-ast-typescript/src/test/kotlin/chapi/ast/typescriptast/TypeScriptRegressionTest.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ exports.test = test;
2323
fun of_keyword_lost() {
2424
val code = """
2525
import {EMPTY, Observable, of} from 'rxjs';
26+
27+
if (node in graph) {
28+
for (const n of graph[node]) {
29+
30+
}
31+
}
2632
"""
2733

2834
TypeScriptAnalyser().analysis(code, "index.tsx")

0 commit comments

Comments
 (0)