@@ -227,6 +227,8 @@ module ts {
227
227
return children ( ( < TupleTypeNode > node ) . elementTypes ) ;
228
228
case SyntaxKind . UnionType :
229
229
return children ( ( < UnionTypeNode > node ) . types ) ;
230
+ case SyntaxKind . ParenType :
231
+ return child ( ( < ParenTypeNode > node ) . type ) ;
230
232
case SyntaxKind . ArrayLiteral :
231
233
return children ( ( < ArrayLiteral > node ) . elements ) ;
232
234
case SyntaxKind . ObjectLiteral :
@@ -1660,6 +1662,14 @@ module ts {
1660
1662
return finishNode ( node ) ;
1661
1663
}
1662
1664
1665
+ function parseParenType ( ) : ParenTypeNode {
1666
+ var node = < ParenTypeNode > createNode ( SyntaxKind . ParenType ) ;
1667
+ parseExpected ( SyntaxKind . OpenParenToken ) ;
1668
+ node . type = parseType ( ) ;
1669
+ parseExpected ( SyntaxKind . CloseParenToken ) ;
1670
+ return finishNode ( node ) ;
1671
+ }
1672
+
1663
1673
function parseFunctionType ( signatureKind : SyntaxKind ) : TypeLiteralNode {
1664
1674
var node = < TypeLiteralNode > createNode ( SyntaxKind . TypeLiteral ) ;
1665
1675
var member = < SignatureDeclaration > createNode ( signatureKind ) ;
@@ -1693,10 +1703,7 @@ module ts {
1693
1703
case SyntaxKind . OpenBracketToken :
1694
1704
return parseTupleType ( ) ;
1695
1705
case SyntaxKind . OpenParenToken :
1696
- case SyntaxKind . LessThanToken :
1697
- return parseFunctionType ( SyntaxKind . CallSignature ) ;
1698
- case SyntaxKind . NewKeyword :
1699
- return parseFunctionType ( SyntaxKind . ConstructSignature ) ;
1706
+ return parseParenType ( ) ;
1700
1707
default :
1701
1708
if ( isIdentifier ( ) ) {
1702
1709
return parseTypeReference ( ) ;
@@ -1720,18 +1727,18 @@ module ts {
1720
1727
case SyntaxKind . NewKeyword :
1721
1728
return true ;
1722
1729
case SyntaxKind . OpenParenToken :
1723
- // Only consider an ( as the start of a type if we have () or (id
1724
- // We don't want to consider things like (1) as a function type.
1730
+ // Only consider '(' the start of a type if followed by ')', '...', an identifier, a modifier,
1731
+ // or something that starts a type. We don't want to consider things like ' (1)' a type.
1725
1732
return lookAhead ( ( ) => {
1726
1733
nextToken ( ) ;
1727
- return token === SyntaxKind . CloseParenToken || isParameter ( ) ;
1734
+ return token === SyntaxKind . CloseParenToken || isParameter ( ) || isType ( ) ;
1728
1735
} ) ;
1729
1736
default :
1730
1737
return isIdentifier ( ) ;
1731
1738
}
1732
1739
}
1733
1740
1734
- function parseNonUnionType ( ) : TypeNode {
1741
+ function parsePrimaryType ( ) : TypeNode {
1735
1742
var type = parseNonArrayType ( ) ;
1736
1743
while ( ! scanner . hasPrecedingLineBreak ( ) && parseOptional ( SyntaxKind . OpenBracketToken ) ) {
1737
1744
parseExpected ( SyntaxKind . CloseBracketToken ) ;
@@ -1742,13 +1749,13 @@ module ts {
1742
1749
return type ;
1743
1750
}
1744
1751
1745
- function parseType ( ) : TypeNode {
1746
- var type = parseNonUnionType ( ) ;
1752
+ function parseUnionType ( ) : TypeNode {
1753
+ var type = parsePrimaryType ( ) ;
1747
1754
if ( token === SyntaxKind . BarToken ) {
1748
1755
var types = < NodeArray < TypeNode > > [ type ] ;
1749
1756
types . pos = type . pos ;
1750
1757
while ( parseOptional ( SyntaxKind . BarToken ) ) {
1751
- types . push ( parseNonUnionType ( ) ) ;
1758
+ types . push ( parsePrimaryType ( ) ) ;
1752
1759
}
1753
1760
types . end = getNodeEnd ( ) ;
1754
1761
var node = < UnionTypeNode > createNode ( SyntaxKind . UnionType , type . pos ) ;
@@ -1758,6 +1765,48 @@ module ts {
1758
1765
return type ;
1759
1766
}
1760
1767
1768
+ function isFunctionType ( ) : boolean {
1769
+ return token === SyntaxKind . LessThanToken || token === SyntaxKind . OpenParenToken && lookAhead ( ( ) => {
1770
+ nextToken ( ) ;
1771
+ if ( token === SyntaxKind . CloseParenToken || token === SyntaxKind . DotDotDotToken ) {
1772
+ // ( )
1773
+ // ( ...
1774
+ return true ;
1775
+ }
1776
+ if ( isIdentifier ( ) || isModifier ( token ) ) {
1777
+ nextToken ( ) ;
1778
+ if ( token === SyntaxKind . ColonToken || token === SyntaxKind . CommaToken ||
1779
+ token === SyntaxKind . QuestionToken || token === SyntaxKind . EqualsToken ||
1780
+ isIdentifier ( ) || isModifier ( token ) ) {
1781
+ // ( id :
1782
+ // ( id ,
1783
+ // ( id ?
1784
+ // ( id =
1785
+ // ( modifier id
1786
+ return true ;
1787
+ }
1788
+ if ( token === SyntaxKind . CloseParenToken ) {
1789
+ nextToken ( ) ;
1790
+ if ( token === SyntaxKind . EqualsGreaterThanToken ) {
1791
+ // ( id ) =>
1792
+ return true ;
1793
+ }
1794
+ }
1795
+ }
1796
+ return false ;
1797
+ } ) ;
1798
+ }
1799
+
1800
+ function parseType ( ) : TypeNode {
1801
+ if ( isFunctionType ( ) ) {
1802
+ return parseFunctionType ( SyntaxKind . CallSignature ) ;
1803
+ }
1804
+ if ( token === SyntaxKind . NewKeyword ) {
1805
+ return parseFunctionType ( SyntaxKind . ConstructSignature ) ;
1806
+ }
1807
+ return parseUnionType ( ) ;
1808
+ }
1809
+
1761
1810
function parseTypeAnnotation ( ) : TypeNode {
1762
1811
return parseOptional ( SyntaxKind . ColonToken ) ? parseType ( ) : undefined ;
1763
1812
}
0 commit comments