Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions internal/ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -2496,6 +2496,10 @@ func (node *TryStatement) Clone(f *NodeFactory) *Node {
return cloneNode(f.NewTryStatement(node.TryBlock, node.CatchClause, node.FinallyBlock), node.AsNode(), f.hooks)
}

func IsTryStatement(node *Node) bool {
return node.Kind == KindTryStatement
}

// CatchClause

type CatchClause struct {
Expand Down
48 changes: 48 additions & 0 deletions internal/ast/utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -1495,6 +1495,50 @@ func GetContainingClass(node *Node) *Node {
return FindAncestor(node.Parent, IsClassLike)
}

func GetExtendsHeritageClauseElement(node *Node) *ExpressionWithTypeArgumentsNode {
return core.FirstOrNil(GetExtendsHeritageClauseElements(node))
}

func GetExtendsHeritageClauseElements(node *Node) []*ExpressionWithTypeArgumentsNode {
return getHeritageElements(node, KindExtendsKeyword)
}

func GetImplementsHeritageClauseElements(node *Node) []*ExpressionWithTypeArgumentsNode {
return getHeritageElements(node, KindImplementsKeyword)
}

func getHeritageElements(node *Node, kind Kind) []*Node {
clause := getHeritageClause(node, kind)
if clause != nil {
return clause.AsHeritageClause().Types.Nodes
}
return nil
}

func getHeritageClause(node *Node, kind Kind) *Node {
clauses := getHeritageClauses(node)
if clauses != nil {
for _, clause := range clauses.Nodes {
if clause.AsHeritageClause().Token == kind {
return clause
}
}
}
return nil
}

func getHeritageClauses(node *Node) *NodeList {
switch node.Kind {
case KindClassDeclaration:
return node.AsClassDeclaration().HeritageClauses
case KindClassExpression:
return node.AsClassExpression().HeritageClauses
case KindInterfaceDeclaration:
return node.AsInterfaceDeclaration().HeritageClauses
}
return nil
}

func IsPartOfTypeQuery(node *Node) bool {
for node.Kind == KindQualifiedName || node.Kind == KindIdentifier {
node = node.Parent
Expand Down Expand Up @@ -1844,6 +1888,10 @@ func isJSXTagName(node *Node) bool {
return false
}

func IsSuperCall(node *Node) bool {
return IsCallExpression(node) && node.AsCallExpression().Expression.Kind == KindSuperKeyword
}

func IsImportCall(node *Node) bool {
return IsCallExpression(node) && node.AsCallExpression().Expression.Kind == KindImportKeyword
}
Expand Down
22 changes: 11 additions & 11 deletions internal/checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -2526,7 +2526,7 @@ func (c *Checker) checkConstructorDeclaration(node *ast.Node) {
// Constructors of classes with no extends clause may not contain super calls, whereas
// constructors of derived classes must contain at least one super call somewhere in their function body.
containingClassDecl := node.Parent
if getExtendsTypeNode(containingClassDecl) == nil {
if ast.GetExtendsHeritageClauseElement(containingClassDecl) == nil {
return
}
classExtendsNull := c.classDeclarationExtendsNull(containingClassDecl)
Expand Down Expand Up @@ -3937,7 +3937,7 @@ func (c *Checker) checkClassLikeDeclaration(node *ast.Node) {
c.checkTypeParameterListsIdentical(symbol)
c.checkFunctionOrConstructorSymbol(symbol)
c.checkObjectTypeForDuplicateDeclarations(node, true /*checkPrivateNames*/)
baseTypeNode := getExtendsTypeNode(node)
baseTypeNode := ast.GetExtendsHeritageClauseElement(node)
if baseTypeNode != nil {
c.checkSourceElements(baseTypeNode.TypeArguments())
baseTypes := c.getBaseTypes(classType)
Expand Down Expand Up @@ -3988,7 +3988,7 @@ func (c *Checker) checkClassLikeDeclaration(node *ast.Node) {
}
}
c.checkMembersForOverrideModifier(node, classType, typeWithThis, staticType)
implementedTypeNodes := getImplementsTypeNodes(node)
implementedTypeNodes := ast.GetImplementsHeritageClauseElements(node)
for _, typeRefNode := range implementedTypeNodes {
expr := typeRefNode.Expression()
if !ast.IsEntityNameExpression(expr) || ast.IsOptionalChain(expr) {
Expand Down Expand Up @@ -4308,7 +4308,7 @@ func (c *Checker) isPropertyAbstractOrInterface(declaration *ast.Node, baseDecla

func (c *Checker) checkMembersForOverrideModifier(node *ast.Node, t *Type, typeWithThis *Type, staticType *Type) {
var baseWithThis *Type
baseTypeNode := getExtendsTypeNode(node)
baseTypeNode := ast.GetExtendsHeritageClauseElement(node)
if baseTypeNode != nil {
baseTypes := c.getBaseTypes(t)
if len(baseTypes) > 0 {
Expand Down Expand Up @@ -4611,7 +4611,7 @@ func (c *Checker) checkInterfaceDeclaration(node *ast.Node) {
}
}
c.checkObjectTypeForDuplicateDeclarations(node, false /*checkPrivateNames*/)
for _, heritageElement := range getExtendsTypeNodes(node) {
for _, heritageElement := range ast.GetExtendsHeritageClauseElements(node) {
expr := heritageElement.Expression()
if !ast.IsEntityNameExpression(expr) || ast.IsOptionalChain(expr) {
c.error(expr, diagnostics.An_interface_can_only_extend_an_identifier_Slashqualified_name_with_optional_type_arguments)
Expand Down Expand Up @@ -7336,7 +7336,7 @@ func (c *Checker) checkSuperExpression(node *ast.Node) *Type {
}
// at this point the only legal case for parent is ClassLikeDeclaration
classLikeDeclaration := container.Parent
if getExtendsTypeNode(classLikeDeclaration) == nil {
if ast.GetExtendsHeritageClauseElement(classLikeDeclaration) == nil {
c.error(node, diagnostics.X_super_can_only_be_referenced_in_a_derived_class)
return c.errorType
}
Expand Down Expand Up @@ -7871,7 +7871,7 @@ func (c *Checker) resolveCallExpression(node *ast.Node, candidatesOutArray *[]*S
if !c.isErrorType(superType) {
// In super call, the candidate signatures are the matching arity signatures of the base constructor function instantiated
// with the type arguments specified in the extends clause.
baseTypeNode := getExtendsTypeNode(ast.GetContainingClass(node))
baseTypeNode := ast.GetExtendsHeritageClauseElement(ast.GetContainingClass(node))
if baseTypeNode != nil {
baseConstructors := c.getInstantiatedConstructorsForTypeArguments(superType, baseTypeNode.TypeArguments(), baseTypeNode)
return c.resolveCall(node, baseConstructors, candidatesOutArray, checkMode, SignatureFlagsNone, nil)
Expand Down Expand Up @@ -11422,7 +11422,7 @@ func (c *Checker) checkThisInStaticClassFieldInitializerInDecoratedClass(thisExp

func (c *Checker) checkThisBeforeSuper(node *ast.Node, container *ast.Node, diagnosticMessage *diagnostics.Message) {
containingClassDecl := container.Parent
baseTypeNode := getExtendsTypeNode(containingClassDecl)
baseTypeNode := ast.GetExtendsHeritageClauseElement(containingClassDecl)
// If a containing class does not have extends clause or the class extends null
// skip checking whether super statement is called before "this" accessing.
if baseTypeNode != nil && !c.classDeclarationExtendsNull(containingClassDecl) {
Expand Down Expand Up @@ -15740,7 +15740,7 @@ func (c *Checker) isThislessInterface(symbol *ast.Symbol) bool {
if declaration.Flags&ast.NodeFlagsContainsThis != 0 {
return false
}
baseTypeNodes := getExtendsTypeNodes(declaration)
baseTypeNodes := ast.GetExtendsHeritageClauseElements(declaration)
for _, node := range baseTypeNodes {
if ast.IsEntityNameExpression(node.Expression()) {
baseSymbol := c.resolveEntityName(node.Expression(), ast.SymbolFlagsType, true /*ignoreErrors*/, false, nil)
Expand Down Expand Up @@ -17437,7 +17437,7 @@ func (c *Checker) resolveBaseTypesOfClass(t *Type) {
func getBaseTypeNodeOfClass(t *Type) *ast.Node {
decl := getClassLikeDeclarationOfSymbol(t.symbol)
if decl != nil {
return getExtendsTypeNode(decl)
return ast.GetExtendsHeritageClauseElement(decl)
}
return nil
}
Expand Down Expand Up @@ -17671,7 +17671,7 @@ func (c *Checker) resolveBaseTypesOfInterface(t *Type) {
data := t.AsInterfaceType()
for _, declaration := range t.symbol.Declarations {
if ast.IsInterfaceDeclaration(declaration) {
for _, node := range getExtendsTypeNodes(declaration) {
for _, node := range ast.GetExtendsHeritageClauseElements(declaration) {
baseType := c.getReducedType(c.getTypeFromTypeNode(node))
if !c.isErrorType(baseType) {
if c.isValidBaseType(baseType) {
Expand Down
44 changes: 0 additions & 44 deletions internal/checker/utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -1273,50 +1273,6 @@ func parameterIsThisKeyword(parameter *ast.Node) bool {
return ast.IsThisParameter(parameter)
}

func getExtendsTypeNode(node *ast.Node) *ast.Node {
return core.FirstOrNil(getExtendsTypeNodes(node))
}

func getExtendsTypeNodes(node *ast.Node) []*ast.Node {
return getHeritageTypeNodes(node, ast.KindExtendsKeyword)
}

func getImplementsTypeNodes(node *ast.Node) []*ast.Node {
return getHeritageTypeNodes(node, ast.KindImplementsKeyword)
}

func getHeritageTypeNodes(node *ast.Node, kind ast.Kind) []*ast.Node {
clause := getHeritageClause(node, kind)
if clause != nil {
return clause.AsHeritageClause().Types.Nodes
}
return nil
}

func getHeritageClause(node *ast.Node, kind ast.Kind) *ast.Node {
clauses := getHeritageClauses(node)
if clauses != nil {
for _, clause := range clauses.Nodes {
if clause.AsHeritageClause().Token == kind {
return clause
}
}
}
return nil
}

func getHeritageClauses(node *ast.Node) *ast.NodeList {
switch node.Kind {
case ast.KindClassDeclaration:
return node.AsClassDeclaration().HeritageClauses
case ast.KindClassExpression:
return node.AsClassExpression().HeritageClauses
case ast.KindInterfaceDeclaration:
return node.AsInterfaceDeclaration().HeritageClauses
}
return nil
}

func isObjectOrArrayLiteralType(t *Type) bool {
return t.objectFlags&(ObjectFlagsObjectLiteral|ObjectFlagsArrayLiteral) != 0
}
Expand Down
Loading