@@ -1549,3 +1549,181 @@ func getImportTypeNodeLiteral(node *Node) *Node {
1549
1549
}
1550
1550
return nil
1551
1551
}
1552
+
1553
+ func IsExpressionNode (node * Node ) bool {
1554
+ switch node .Kind {
1555
+ case KindSuperKeyword , KindNullKeyword , KindTrueKeyword , KindFalseKeyword , KindRegularExpressionLiteral ,
1556
+ KindArrayLiteralExpression , KindObjectLiteralExpression , KindPropertyAccessExpression , KindElementAccessExpression ,
1557
+ KindCallExpression , KindNewExpression , KindTaggedTemplateExpression , KindAsExpression , KindTypeAssertionExpression ,
1558
+ KindSatisfiesExpression , KindNonNullExpression , KindParenthesizedExpression , KindFunctionExpression ,
1559
+ KindClassExpression , KindArrowFunction , KindVoidExpression , KindDeleteExpression , KindTypeOfExpression ,
1560
+ KindPrefixUnaryExpression , KindPostfixUnaryExpression , KindBinaryExpression , KindConditionalExpression ,
1561
+ KindSpreadElement , KindTemplateExpression , KindOmittedExpression , KindJsxElement , KindJsxSelfClosingElement ,
1562
+ KindJsxFragment , KindYieldExpression , KindAwaitExpression , KindMetaProperty :
1563
+ return true
1564
+ case KindExpressionWithTypeArguments :
1565
+ return ! IsHeritageClause (node .Parent )
1566
+ case KindQualifiedName :
1567
+ for node .Parent .Kind == KindQualifiedName {
1568
+ node = node .Parent
1569
+ }
1570
+ return IsTypeQueryNode (node .Parent ) || isJSDocLinkLike (node .Parent ) || isJSXTagName (node )
1571
+ case KindJSDocMemberName :
1572
+ return IsTypeQueryNode (node .Parent ) || isJSDocLinkLike (node .Parent ) || isJSXTagName (node )
1573
+ case KindPrivateIdentifier :
1574
+ return IsBinaryExpression (node .Parent ) && node .Parent .AsBinaryExpression ().Left == node && node .Parent .AsBinaryExpression ().OperatorToken .Kind == KindInKeyword
1575
+ case KindIdentifier :
1576
+ if IsTypeQueryNode (node .Parent ) || isJSDocLinkLike (node .Parent ) || isJSXTagName (node ) {
1577
+ return true
1578
+ }
1579
+ fallthrough
1580
+ case KindNumericLiteral , KindBigIntLiteral , KindStringLiteral , KindNoSubstitutionTemplateLiteral , KindThisKeyword :
1581
+ return IsInExpressionContext (node )
1582
+ default :
1583
+ return false
1584
+ }
1585
+ }
1586
+
1587
+ func IsInExpressionContext (node * Node ) bool {
1588
+ parent := node .Parent
1589
+ switch parent .Kind {
1590
+ case KindVariableDeclaration :
1591
+ return parent .AsVariableDeclaration ().Initializer == node
1592
+ case KindParameter :
1593
+ return parent .AsParameterDeclaration ().Initializer == node
1594
+ case KindPropertyDeclaration :
1595
+ return parent .AsPropertyDeclaration ().Initializer == node
1596
+ case KindPropertySignature :
1597
+ return parent .AsPropertySignatureDeclaration ().Initializer == node
1598
+ case KindEnumMember :
1599
+ return parent .AsEnumMember ().Initializer == node
1600
+ case KindPropertyAssignment :
1601
+ return parent .AsPropertyAssignment ().Initializer == node
1602
+ case KindBindingElement :
1603
+ return parent .AsBindingElement ().Initializer == node
1604
+ case KindExpressionStatement :
1605
+ return parent .AsExpressionStatement ().Expression == node
1606
+ case KindIfStatement :
1607
+ return parent .AsIfStatement ().Expression == node
1608
+ case KindDoStatement :
1609
+ return parent .AsDoStatement ().Expression == node
1610
+ case KindWhileStatement :
1611
+ return parent .AsWhileStatement ().Expression == node
1612
+ case KindReturnStatement :
1613
+ return parent .AsReturnStatement ().Expression == node
1614
+ case KindWithStatement :
1615
+ return parent .AsWithStatement ().Expression == node
1616
+ case KindSwitchStatement :
1617
+ return parent .AsSwitchStatement ().Expression == node
1618
+ case KindCaseClause , KindDefaultClause :
1619
+ return parent .AsCaseOrDefaultClause ().Expression == node
1620
+ case KindThrowStatement :
1621
+ return parent .AsThrowStatement ().Expression == node
1622
+ case KindForStatement :
1623
+ s := parent .AsForStatement ()
1624
+ return s .Initializer == node && s .Initializer .Kind != KindVariableDeclarationList || s .Condition == node || s .Incrementor == node
1625
+ case KindForInStatement , KindForOfStatement :
1626
+ s := parent .AsForInOrOfStatement ()
1627
+ return s .Initializer == node && s .Initializer .Kind != KindVariableDeclarationList || s .Expression == node
1628
+ case KindTypeAssertionExpression :
1629
+ return parent .AsTypeAssertion ().Expression == node
1630
+ case KindAsExpression :
1631
+ return parent .AsAsExpression ().Expression == node
1632
+ case KindTemplateSpan :
1633
+ return parent .AsTemplateSpan ().Expression == node
1634
+ case KindComputedPropertyName :
1635
+ return parent .AsComputedPropertyName ().Expression == node
1636
+ case KindDecorator , KindJsxExpression , KindJsxSpreadAttribute , KindSpreadAssignment :
1637
+ return true
1638
+ case KindExpressionWithTypeArguments :
1639
+ return parent .AsExpressionWithTypeArguments ().Expression == node && ! IsPartOfTypeNode (parent )
1640
+ case KindShorthandPropertyAssignment :
1641
+ return parent .AsShorthandPropertyAssignment ().ObjectAssignmentInitializer == node
1642
+ case KindSatisfiesExpression :
1643
+ return parent .AsSatisfiesExpression ().Expression == node
1644
+ default :
1645
+ return IsExpressionNode (parent )
1646
+ }
1647
+ }
1648
+
1649
+ func IsPartOfTypeNode (node * Node ) bool {
1650
+ kind := node .Kind
1651
+ if kind >= KindFirstTypeNode && kind <= KindLastTypeNode {
1652
+ return true
1653
+ }
1654
+ switch node .Kind {
1655
+ case KindAnyKeyword , KindUnknownKeyword , KindNumberKeyword , KindBigIntKeyword , KindStringKeyword ,
1656
+ KindBooleanKeyword , KindSymbolKeyword , KindObjectKeyword , KindUndefinedKeyword , KindNullKeyword ,
1657
+ KindNeverKeyword :
1658
+ return true
1659
+ case KindExpressionWithTypeArguments :
1660
+ return isPartOfTypeExpressionWithTypeArguments (node )
1661
+ case KindTypeParameter :
1662
+ return node .Parent .Kind == KindMappedType || node .Parent .Kind == KindInferType
1663
+ case KindIdentifier :
1664
+ parent := node .Parent
1665
+ if IsQualifiedName (parent ) && parent .AsQualifiedName ().Right == node {
1666
+ return isPartOfTypeNodeInParent (parent )
1667
+ }
1668
+ if IsPropertyAccessExpression (parent ) && parent .AsPropertyAccessExpression ().Name () == node {
1669
+ return isPartOfTypeNodeInParent (parent )
1670
+ }
1671
+ return isPartOfTypeNodeInParent (node )
1672
+ case KindQualifiedName , KindPropertyAccessExpression , KindThisKeyword :
1673
+ return isPartOfTypeNodeInParent (node )
1674
+ }
1675
+ return false
1676
+ }
1677
+
1678
+ func isPartOfTypeNodeInParent (node * Node ) bool {
1679
+ parent := node .Parent
1680
+ // Do not recursively call isPartOfTypeNode on the parent. In the example:
1681
+ //
1682
+ // let a: A.B.C;
1683
+ //
1684
+ // Calling isPartOfTypeNode would consider the qualified name A.B a type node.
1685
+ // Only C and A.B.C are type nodes.
1686
+ if parent .Kind >= KindFirstTypeNode && parent .Kind <= KindLastTypeNode {
1687
+ return true
1688
+ }
1689
+ switch parent .Kind {
1690
+ case KindTypeQuery :
1691
+ return false
1692
+ case KindImportType :
1693
+ return ! parent .AsImportTypeNode ().IsTypeOf
1694
+ case KindExpressionWithTypeArguments :
1695
+ return isPartOfTypeExpressionWithTypeArguments (parent )
1696
+ case KindTypeParameter :
1697
+ return node == parent .AsTypeParameter ().Constraint
1698
+ case KindVariableDeclaration , KindParameter , KindPropertyDeclaration , KindPropertySignature , KindFunctionDeclaration ,
1699
+ KindFunctionExpression , KindArrowFunction , KindConstructor , KindMethodDeclaration , KindMethodSignature ,
1700
+ KindGetAccessor , KindSetAccessor , KindCallSignature , KindConstructSignature , KindIndexSignature ,
1701
+ KindTypeAssertionExpression :
1702
+ return node == parent .Type ()
1703
+ case KindCallExpression , KindNewExpression , KindTaggedTemplateExpression :
1704
+ return slices .Contains (parent .TypeArguments (), node )
1705
+ }
1706
+ return false
1707
+ }
1708
+
1709
+ func isPartOfTypeExpressionWithTypeArguments (node * Node ) bool {
1710
+ parent := node .Parent
1711
+ return IsHeritageClause (parent ) && (! IsClassLike (parent .Parent ) || parent .AsHeritageClause ().Token == KindImplementsKeyword )
1712
+ }
1713
+
1714
+ func isJSDocLinkLike (node * Node ) bool {
1715
+ return NodeKindIs (node , KindJSDocLink , KindJSDocLinkCode , KindJSDocLinkPlain )
1716
+ }
1717
+
1718
+ func isJSXTagName (node * Node ) bool {
1719
+ parent := node .Parent
1720
+ switch parent .Kind {
1721
+ case KindJsxOpeningElement :
1722
+ return parent .AsJsxOpeningElement ().TagName == node
1723
+ case KindJsxSelfClosingElement :
1724
+ return parent .AsJsxSelfClosingElement ().TagName == node
1725
+ case KindJsxClosingElement :
1726
+ return parent .AsJsxClosingElement ().TagName == node
1727
+ }
1728
+ return false
1729
+ }
0 commit comments