Skip to content

Extract Evaluator and NameResolver out of checker #229

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
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
82 changes: 78 additions & 4 deletions internal/ast/utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -1770,10 +1770,6 @@ func IsImportCall(node *Node) bool {
return IsCallExpression(node) && node.AsCallExpression().Expression.Kind == KindImportKeyword
}

func IsComputedNonLiteralName(name *Node) bool {
return IsComputedPropertyName(name) && !IsStringOrNumericLiteralLike(name.Expression())
}

func IsQuestionToken(node *Node) bool {
return node != nil && node.Kind == KindQuestionToken
}
Expand All @@ -1792,3 +1788,81 @@ func TryGetTextOfPropertyName(name *Node) (string, bool) {
}
return "", false
}

func GetAssertedTypeNode(node *Node) *Node {
switch node.Kind {
case KindAsExpression:
return node.AsAsExpression().Type
case KindSatisfiesExpression:
return node.AsSatisfiesExpression().Type
case KindTypeAssertionExpression:
return node.AsTypeAssertion().Type
}
panic("Unhandled case in getAssertedTypeNode")
}

func IsConstAssertion(node *Node) bool {
switch node.Kind {
case KindAsExpression, KindTypeAssertionExpression:
return IsConstTypeReference(GetAssertedTypeNode(node))
}
return false
}

func IsConstTypeReference(node *Node) bool {
return IsTypeReferenceNode(node) && len(node.TypeArguments()) == 0 && IsIdentifier(node.AsTypeReferenceNode().TypeName) && node.AsTypeReferenceNode().TypeName.Text() == "const"
}

func IsGlobalSourceFile(node *Node) bool {
return node.Kind == KindSourceFile && !IsExternalOrCommonJsModule(node.AsSourceFile())
}

func GetBodyOfNode(node *Node) *Node {
bodyData := node.BodyData()
if bodyData != nil {
return bodyData.Body
}
return nil
}

func IsParameterLikeOrReturnTag(node *Node) bool {
switch node.Kind {
case KindParameter, KindTypeParameter, KindJSDocParameterTag, KindJSDocReturnTag:
return true
}
return false
}

func GetDeclarationOfKind(symbol *Symbol, kind Kind) *Node {
for _, declaration := range symbol.Declarations {
if declaration.Kind == kind {
return declaration
}
}
return nil
}

func FindConstructorDeclaration(node *ClassLikeDeclaration) *Node {
for _, member := range node.ClassLikeData().Members.Nodes {
if IsConstructorDeclaration(member) && NodeIsPresent(member.AsConstructorDeclaration().Body) {
return member
}
}
return nil
}

func IsComputedNonLiteralName(name *Node) bool {
return IsComputedPropertyName(name) && !IsStringOrNumericLiteralLike(name.Expression())
}

func GetFirstIdentifier(node *Node) *Node {
switch node.Kind {
case KindIdentifier:
return node
case KindQualifiedName:
return GetFirstIdentifier(node.AsQualifiedName().Left)
case KindPropertyAccessExpression:
return GetFirstIdentifier(node.AsPropertyAccessExpression().Expression)
}
panic("Unhandled case in GetFirstIdentifier")
}
Loading
Loading