@@ -1038,6 +1038,72 @@ export class SimpleLanguageService implements ISimpleLanguageService {
1038
1038
}
1039
1039
}
1040
1040
1041
+ /**
1042
+ * Formats a CallExpression into an ICallExpression.
1043
+ * @param {CallExpression }
1044
+ * @returns {ICallExpression }
1045
+ */
1046
+ private formatCallExpression ( statement : CallExpression ) : ICallExpression {
1047
+ const exp = statement . expression ;
1048
+ const typeExpressions = statement . typeArguments == null ? null : statement . typeArguments . map ( typeArg => this . getTypeExpression ( typeArg ) ) ;
1049
+ let typeExpression : TypeExpression = [ ] ;
1050
+ if ( typeExpressions != null ) {
1051
+ typeExpressions . forEach ( ( typeExp , index ) => {
1052
+ typeExp . forEach ( part => typeExpression . push ( part ) ) ;
1053
+ if ( index !== typeExpressions . length - 1 ) typeExpression . push ( ", " ) ;
1054
+ } ) ;
1055
+ }
1056
+ const typeFlattened = typeExpression == null || typeExpression . length < 1 ? null : this . serializeTypeExpression ( typeExpression ) ;
1057
+ const typeBindings = typeExpression == null || typeExpression . length < 1 ? null : this . takeTypeBindings ( typeExpression ) ;
1058
+ let property : ArbitraryValue = null ;
1059
+ let method : ArbitraryValue = null ;
1060
+
1061
+ if ( this . isIdentifierObject ( exp ) ) {
1062
+ method = this . getNameOfMember ( exp , false , true ) ;
1063
+ }
1064
+
1065
+ if ( this . isPropertyAccessExpression ( exp ) ) {
1066
+ property = this . getNameOfMember ( exp . expression ) ;
1067
+ method = this . getNameOfMember ( exp . name , false , true ) ;
1068
+ }
1069
+
1070
+ if ( method == null ) {
1071
+ throw new TypeError ( `${ this . formatCallExpression . name } could not format a CallExpression with an expression of kind ${ SyntaxKind [ exp . kind ] } ` ) ;
1072
+ }
1073
+
1074
+ return {
1075
+ arguments : {
1076
+ startsAt : statement . arguments . pos ,
1077
+ endsAt : statement . arguments . end ,
1078
+ argumentsList : this . formatArguments ( statement )
1079
+ } ,
1080
+ property,
1081
+ method,
1082
+ type : {
1083
+ expression : typeExpression . length < 1 ? null : typeExpression ,
1084
+ flattened : typeFlattened ,
1085
+ bindings : typeBindings
1086
+ }
1087
+ } ;
1088
+
1089
+ }
1090
+
1091
+ /**
1092
+ *Formats the given Statement into an ICallExpression.
1093
+ * @param {Statement|Expression } statement
1094
+ * @returns {ICallExpression }
1095
+ */
1096
+ private getCallExpression ( statement : Statement | Expression ) : ICallExpression {
1097
+ if ( this . isCallExpression ( statement ) ) {
1098
+ return this . formatCallExpression ( statement ) ;
1099
+ }
1100
+
1101
+ if ( this . isExpressionStatement ( statement ) ) {
1102
+ return this . getCallExpression ( statement . expression ) ;
1103
+ }
1104
+ throw new TypeError ( `${ this . getCallExpression . name } could not format a CallExpression of kind ${ SyntaxKind [ statement . kind ] } ` ) ;
1105
+ }
1106
+
1041
1107
/**
1042
1108
* Gets and formats all CallExpressions associated with the given statements.
1043
1109
* These hold information such as the arguments the members are invoked with, generic type
@@ -1049,46 +1115,8 @@ export class SimpleLanguageService implements ISimpleLanguageService {
1049
1115
const expressions : ICallExpression [ ] = [ ] ;
1050
1116
1051
1117
statements . forEach ( statement => {
1052
-
1053
- if ( this . isExpressionStatement ( statement ) ) {
1054
- const exp = statement . expression ;
1055
-
1056
- if ( this . isCallExpression ( exp ) ) {
1057
- const expExp = exp . expression ;
1058
-
1059
- if ( this . isPropertyAccessExpression ( expExp ) ) {
1060
- const property = this . getNameOfMember ( expExp . expression ) ;
1061
- const method = this . getNameOfMember ( expExp . name , false , true ) ;
1062
- const typeExpressions = exp . typeArguments == null ? null : exp . typeArguments . map ( typeArg => this . getTypeExpression ( typeArg ) ) ;
1063
- let typeExpression : TypeExpression = [ ] ;
1064
-
1065
- if ( typeExpressions != null ) {
1066
- typeExpressions . forEach ( ( typeExp , index ) => {
1067
- typeExp . forEach ( part => typeExpression . push ( part ) ) ;
1068
- if ( index !== typeExpressions . length - 1 ) typeExpression . push ( ", " ) ;
1069
- } ) ;
1070
- }
1071
-
1072
- const typeFlattened = typeExpression == null || typeExpression . length < 1 ? null : this . serializeTypeExpression ( typeExpression ) ;
1073
- const typeBindings = typeExpression == null || typeExpression . length < 1 ? null : this . takeTypeBindings ( typeExpression ) ;
1074
-
1075
- expressions . push ( {
1076
- arguments : {
1077
- startsAt : exp . arguments . pos ,
1078
- endsAt : exp . arguments . end ,
1079
- argumentsList : this . formatArguments ( exp )
1080
- } ,
1081
- property,
1082
- method,
1083
- type : {
1084
- expression : typeExpression . length < 1 ? null : typeExpression ,
1085
- flattened : typeFlattened ,
1086
- bindings : typeBindings
1087
-
1088
- }
1089
- } ) ;
1090
- }
1091
- }
1118
+ if ( this . isCallExpression ( statement ) || this . isExpressionStatement ( statement ) ) {
1119
+ expressions . push ( this . getCallExpression ( statement ) ) ;
1092
1120
}
1093
1121
} ) ;
1094
1122
return expressions ;
@@ -1510,10 +1538,10 @@ export class SimpleLanguageService implements ISimpleLanguageService {
1510
1538
/**
1511
1539
* Takes the path identifier of an expression. For instance, if it has a path, it returns it.
1512
1540
* @param {ArbitraryValue } expression
1513
- * @returns {string }
1541
+ * @returns {ArbitraryValue[] }
1514
1542
*/
1515
- private takePath ( expression : ArbitraryValue ) : string {
1516
- return expression instanceof BindingIdentifier && expression . path != null ? expression . path : "" ;
1543
+ private takePath ( expression : ArbitraryValue ) : ArbitraryValue [ ] {
1544
+ return expression instanceof BindingIdentifier && expression . path != null ? expression . path : [ ] ;
1517
1545
}
1518
1546
1519
1547
/**
@@ -1571,12 +1599,12 @@ export class SimpleLanguageService implements ISimpleLanguageService {
1571
1599
const baseNameStringified = this . takeBase ( baseName ) ;
1572
1600
const pathStringified = this . takePath ( baseName ) ;
1573
1601
1574
- const path = name . name == null ? null : [ pathStringified , ...this . getPathOfExpression ( name . name ) ] . filter ( part => {
1602
+ const path = name . name == null ? null : [ ... pathStringified , ...this . getPathOfExpression ( name . name ) ] . filter ( part => {
1575
1603
if ( typeof part === "string" ) return part . length > 0 ;
1576
1604
return part != null ;
1577
1605
} ) ;
1578
1606
1579
- return new BindingIdentifier ( baseNameStringified , path == null || path . length === 0 ? null : this . flattenPath ( path ) ) ;
1607
+ return new BindingIdentifier ( baseNameStringified , path ) ;
1580
1608
}
1581
1609
1582
1610
if ( this . isCallExpression ( name ) ) {
@@ -1588,12 +1616,12 @@ export class SimpleLanguageService implements ISimpleLanguageService {
1588
1616
const baseNameStringified = this . takeBase ( baseName ) ;
1589
1617
const pathStringified = this . takePath ( baseName ) ;
1590
1618
1591
- const path = name . argumentExpression == null ? null : [ pathStringified , ...this . getPathOfExpression ( name . argumentExpression ) ] . filter ( part => {
1619
+ const path = name . argumentExpression == null ? null : [ ... pathStringified , ...this . getPathOfExpression ( name . argumentExpression ) ] . filter ( part => {
1592
1620
if ( typeof part === "string" ) return part . length > 0 ;
1593
1621
return part != null ;
1594
1622
} ) ;
1595
1623
1596
- return new BindingIdentifier ( baseNameStringified , path == null || path . length === 0 ? null : this . flattenPath ( path ) ) ;
1624
+ return new BindingIdentifier ( baseNameStringified , path ) ;
1597
1625
}
1598
1626
1599
1627
throw new TypeError ( `${ this . getNameOfMember . name } could not compute the name of a ${ SyntaxKind [ name . kind ] } .` ) ;
@@ -1861,22 +1889,6 @@ export class SimpleLanguageService implements ISimpleLanguageService {
1861
1889
return obj ;
1862
1890
}
1863
1891
1864
- /**
1865
- * Flattens the given path down to a string.
1866
- * @param {ArbitraryValue[] } path
1867
- * @returns {string }
1868
- */
1869
- private flattenPath ( path : ArbitraryValue [ ] ) : string {
1870
- return path . map ( part => {
1871
- if ( typeof part === "string" ) {
1872
- if ( part . startsWith ( "[" ) && part . endsWith ( "]" ) ) return part ;
1873
- return `["${ part } "]` ;
1874
- }
1875
-
1876
- return `[${ part } ]` ;
1877
- } ) . join ( "" ) ;
1878
- }
1879
-
1880
1892
/**
1881
1893
* Formats a concrete ParameterDeclaration and returns an IParameter.
1882
1894
* @param {ParameterDeclaration } parameter
0 commit comments