Skip to content

Commit d926212

Browse files
committed
Move helper
1 parent 18faee1 commit d926212

File tree

6 files changed

+190
-188
lines changed

6 files changed

+190
-188
lines changed

internal/ast/utilities.go

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1549,3 +1549,181 @@ func getImportTypeNodeLiteral(node *Node) *Node {
15491549
}
15501550
return nil
15511551
}
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+
}

internal/checker/checker.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3508,7 +3508,7 @@ func (c *Checker) getSymbolForPrivateIdentifierExpression(node *ast.Node) *ast.S
35083508
// !!!
35093509
// Review
35103510
// func (c *Checker) getSymbolForPrivateIdentifierExpression(privId *ast.Node) *ast.Symbol {
3511-
// if !isExpressionNode(privId) {
3511+
// if !ast.IsExpressionNode(privId) {
35123512
// return nil
35133513
// }
35143514

@@ -8443,7 +8443,7 @@ func (c *Checker) isInPropertyInitializerOrClassStaticBlock(node *ast.Node) bool
84438443
}
84448444
return ast.FindAncestorQuit
84458445
default:
8446-
if IsExpressionNode(node) {
8446+
if ast.IsExpressionNode(node) {
84478447
return ast.FindAncestorFalse
84488448
}
84498449
return ast.FindAncestorQuit
@@ -16623,7 +16623,7 @@ func (n *TupleNormalizer) normalize(c *Checker, elementTypes []*Type, elementInf
1662316623
} else if isTupleType(t) {
1662416624
spreadTypes := c.getElementTypes(t)
1662516625
if len(spreadTypes)+len(n.types) >= 10_000 {
16626-
message := core.IfElse(isPartOfTypeNode(c.currentNode),
16626+
message := core.IfElse(ast.IsPartOfTypeNode(c.currentNode),
1662716627
diagnostics.Type_produces_a_tuple_type_that_is_too_large_to_represent,
1662816628
diagnostics.Expression_produces_a_tuple_type_that_is_too_large_to_represent)
1662916629
c.error(c.currentNode, message)
@@ -23368,7 +23368,7 @@ func (c *Checker) getSymbolAtLocation(node *ast.Node, ignoreErrors bool) *ast.Sy
2336823368
return sig.thisParameter
2336923369
}
2337023370
}
23371-
if isInExpressionContext(node) {
23371+
if ast.IsInExpressionContext(node) {
2337223372
return c.checkExpression(node).symbol
2337323373
}
2337423374
fallthrough
@@ -23505,7 +23505,7 @@ func (c *Checker) getSymbolOfNameOrPropertyAccessExpression(name *ast.Node) *ast
2350523505
if name.Parent.Kind == ast.KindExpressionWithTypeArguments {
2350623506
// An 'ExpressionWithTypeArguments' may appear in type space (interface Foo extends Bar<T>),
2350723507
// value space (return foo<T>), or both(class Foo extends Bar<T>); ensure the meaning matches.
23508-
meaning = core.IfElse(isPartOfTypeNode(name), ast.SymbolFlagsType, ast.SymbolFlagsValue)
23508+
meaning = core.IfElse(ast.IsPartOfTypeNode(name), ast.SymbolFlagsType, ast.SymbolFlagsValue)
2350923509

2351023510
// In a class 'extends' clause we are also looking for a value.
2351123511
if ast.IsExpressionWithTypeArgumentsInClassExtendsClause(name.Parent) {
@@ -23525,7 +23525,7 @@ func (c *Checker) getSymbolOfNameOrPropertyAccessExpression(name *ast.Node) *ast
2352523525
}
2352623526
}
2352723527

23528-
if IsExpressionNode(name) {
23528+
if ast.IsExpressionNode(name) {
2352923529
if ast.NodeIsMissing(name) {
2353023530
// Missing entity name.
2353123531
return nil
@@ -23617,7 +23617,7 @@ func (c *Checker) getTypeOfNode(node *ast.Node) *Type {
2361723617
classType = c.getDeclaredTypeOfClassOrInterface(c.getSymbolOfDeclaration(classDecl))
2361823618
}
2361923619

23620-
if isPartOfTypeNode(node) {
23620+
if ast.IsPartOfTypeNode(node) {
2362123621
typeFromTypeNode := c.getTypeFromTypeNode(node)
2362223622
if classType != nil {
2362323623
return c.getTypeWithThisArgument(
@@ -23628,7 +23628,7 @@ func (c *Checker) getTypeOfNode(node *ast.Node) *Type {
2362823628
return typeFromTypeNode
2362923629
}
2363023630

23631-
if IsExpressionNode(node) {
23631+
if ast.IsExpressionNode(node) {
2363223632
return c.getRegularTypeOfExpression(node)
2363323633
}
2363423634

internal/checker/grammarchecks.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ func (c *Checker) checkGrammarPrivateIdentifierExpression(privId *ast.PrivateIde
9696
}
9797

9898
if !ast.IsForInStatement(privId.Parent) {
99-
if !IsExpressionNode(privIdAsNode) {
99+
if !ast.IsExpressionNode(privIdAsNode) {
100100
return c.grammarErrorOnNode(privIdAsNode, diagnostics.Private_identifiers_are_only_allowed_in_class_bodies_and_may_only_be_used_as_part_of_a_class_member_declaration_property_access_or_on_the_left_hand_side_of_an_in_expression)
101101
}
102102

internal/checker/printer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,7 @@ func (c *Checker) getTextAndTypeOfNode(node *ast.Node) (string, *Type, bool) {
593593
}
594594
}
595595
}
596-
if IsExpressionNode(node) && !isRightSideOfQualifiedNameOrPropertyAccess(node) {
596+
if ast.IsExpressionNode(node) && !isRightSideOfQualifiedNameOrPropertyAccess(node) {
597597
return scanner.GetTextOfNode(node), c.getTypeOfExpression(node), false
598598
}
599599
return "", nil, false

0 commit comments

Comments
 (0)