Skip to content

Commit 29b98d2

Browse files
authored
Port global completions (#858)
1 parent d1b4a91 commit 29b98d2

13 files changed

+3449
-727
lines changed

internal/ast/ast.go

+64
Original file line numberDiff line numberDiff line change
@@ -779,6 +779,57 @@ func (n *Node) ModuleSpecifier() *Expression {
779779
panic("Unhandled case in Node.ModuleSpecifier: " + n.Kind.String())
780780
}
781781

782+
func (n *Node) Statement() *Statement {
783+
switch n.Kind {
784+
case KindDoStatement:
785+
return n.AsDoStatement().Statement
786+
case KindWhileStatement:
787+
return n.AsWhileStatement().Statement
788+
case KindForStatement:
789+
return n.AsForStatement().Statement
790+
case KindForInStatement, KindForOfStatement:
791+
return n.AsForInOrOfStatement().Statement
792+
}
793+
panic("Unhandled case in Node.Statement: " + n.Kind.String())
794+
}
795+
796+
func (n *Node) PropertyList() *NodeList {
797+
switch n.Kind {
798+
case KindObjectLiteralExpression:
799+
return n.AsObjectLiteralExpression().Properties
800+
case KindJsxAttributes:
801+
return n.AsJsxAttributes().Properties
802+
}
803+
panic("Unhandled case in Node.PropertyList: " + n.Kind.String())
804+
}
805+
806+
func (n *Node) Properties() []*Node {
807+
list := n.PropertyList()
808+
if list != nil {
809+
return list.Nodes
810+
}
811+
return nil
812+
}
813+
814+
func (n *Node) ElementList() *NodeList {
815+
switch n.Kind {
816+
case KindNamedImports:
817+
return n.AsNamedImports().Elements
818+
case KindNamedExports:
819+
return n.AsNamedExports().Elements
820+
}
821+
822+
panic("Unhandled case in Node.ElementList: " + n.Kind.String())
823+
}
824+
825+
func (n *Node) Elements() []*Node {
826+
list := n.ElementList()
827+
if list != nil {
828+
return list.Nodes
829+
}
830+
return nil
831+
}
832+
782833
// Determines if `n` contains `descendant` by walking up the `Parent` pointers from `descendant`. This method panics if
783834
// `descendant` or one of its ancestors is not parented except when that node is a `SourceFile`.
784835
func (n *Node) Contains(descendant *Node) bool {
@@ -1693,6 +1744,11 @@ type (
16931744
AnyValidImportOrReExport = Node // (ImportDeclaration | ExportDeclaration | JSDocImportTag) & { moduleSpecifier: StringLiteral } | ImportEqualsDeclaration & { moduleReference: ExternalModuleReference & { expression: StringLiteral }} | RequireOrImportCall | ValidImportTypeNode
16941745
ValidImportTypeNode = Node // ImportTypeNode & { argument: LiteralTypeNode & { literal: StringLiteral } }
16951746
NumericOrStringLikeLiteral = Node // StringLiteralLike | NumericLiteral
1747+
TypeOnlyImportDeclaration = Node // ImportClause | ImportEqualsDeclaration | ImportSpecifier | NamespaceImport with isTypeOnly: true
1748+
ObjectLiteralLike = Node // ObjectLiteralExpression | ObjectBindingPattern
1749+
ObjectTypeDeclaration = Node // ClassLikeDeclaration | InterfaceDeclaration | TypeLiteralNode
1750+
JsxOpeningLikeElement = Node // JsxOpeningElement | JsxSelfClosingElement
1751+
NamedImportsOrExports = Node // NamedImports | NamedExports
16961752
)
16971753

16981754
// Aliases for node singletons
@@ -1735,6 +1791,10 @@ type (
17351791
JsxClosingFragmentNode = Node
17361792
SourceFileNode = Node
17371793
PropertyAccessExpressionNode = Node
1794+
TypeLiteral = Node
1795+
ObjectLiteralExpressionNode = Node
1796+
ConstructorDeclarationNode = Node
1797+
NamedExportsNode = Node
17381798
)
17391799

17401800
type (
@@ -6939,6 +6999,10 @@ func (node *IntersectionTypeNode) Clone(f NodeFactoryCoercible) *Node {
69396999
return cloneNode(f.AsNodeFactory().NewIntersectionTypeNode(node.Types), node.AsNode(), f.AsNodeFactory().hooks)
69407000
}
69417001

7002+
func IsIntersectionTypeNode(node *Node) bool {
7003+
return node.Kind == KindIntersectionType
7004+
}
7005+
69427006
// ConditionalTypeNode
69437007

69447008
type ConditionalTypeNode struct {

internal/ast/utilities.go

+135-3
Original file line numberDiff line numberDiff line change
@@ -1527,14 +1527,14 @@ func GetExtendsHeritageClauseElement(node *Node) *ExpressionWithTypeArgumentsNod
15271527
}
15281528

15291529
func GetExtendsHeritageClauseElements(node *Node) []*ExpressionWithTypeArgumentsNode {
1530-
return getHeritageElements(node, KindExtendsKeyword)
1530+
return GetHeritageElements(node, KindExtendsKeyword)
15311531
}
15321532

15331533
func GetImplementsHeritageClauseElements(node *Node) []*ExpressionWithTypeArgumentsNode {
1534-
return getHeritageElements(node, KindImplementsKeyword)
1534+
return GetHeritageElements(node, KindImplementsKeyword)
15351535
}
15361536

1537-
func getHeritageElements(node *Node, kind Kind) []*Node {
1537+
func GetHeritageElements(node *Node, kind Kind) []*Node {
15381538
clause := getHeritageClause(node, kind)
15391539
if clause != nil {
15401540
return clause.AsHeritageClause().Types.Nodes
@@ -2793,3 +2793,135 @@ func ForEachChildAndJSDoc(node *Node, sourceFile *SourceFile, v Visitor) bool {
27932793
}
27942794
return node.ForEachChild(v)
27952795
}
2796+
2797+
func IsTypeReferenceType(node *Node) bool {
2798+
return node.Kind == KindTypeReference || node.Kind == KindExpressionWithTypeArguments
2799+
}
2800+
2801+
func IsVariableLike(node *Node) bool {
2802+
switch node.Kind {
2803+
case KindBindingElement, KindEnumMember, KindParameter, KindPropertyAssignment, KindPropertyDeclaration,
2804+
KindPropertySignature, KindShorthandPropertyAssignment, KindVariableDeclaration:
2805+
return true
2806+
}
2807+
return false
2808+
}
2809+
2810+
func HasInitializer(node *Node) bool {
2811+
switch node.Kind {
2812+
case KindVariableDeclaration, KindParameter, KindBindingElement, KindPropertyDeclaration,
2813+
KindPropertyAssignment, KindEnumMember, KindForStatement, KindForInStatement, KindForOfStatement,
2814+
KindJsxAttribute:
2815+
return node.Initializer() != nil
2816+
default:
2817+
return false
2818+
}
2819+
}
2820+
2821+
func GetTypeAnnotationNode(node *Node) *TypeNode {
2822+
switch node.Kind {
2823+
case KindVariableDeclaration, KindParameter, KindPropertySignature, KindPropertyDeclaration,
2824+
KindTypePredicate, KindParenthesizedType, KindTypeOperator, KindMappedType, KindTypeAssertionExpression,
2825+
KindAsExpression, KindSatisfiesExpression, KindTypeAliasDeclaration, KindJSTypeAliasDeclaration,
2826+
KindNamedTupleMember, KindOptionalType, KindRestType, KindTemplateLiteralTypeSpan, KindJSDocTypeExpression,
2827+
KindJSDocPropertyTag, KindJSDocNullableType, KindJSDocNonNullableType, KindJSDocOptionalType:
2828+
return node.Type()
2829+
default:
2830+
funcLike := node.FunctionLikeData()
2831+
if funcLike != nil {
2832+
return funcLike.Type
2833+
}
2834+
return nil
2835+
}
2836+
}
2837+
2838+
func IsObjectTypeDeclaration(node *Node) bool {
2839+
return IsClassLike(node) || IsInterfaceDeclaration(node) || IsTypeLiteralNode(node)
2840+
}
2841+
2842+
func IsClassOrTypeElement(node *Node) bool {
2843+
return IsClassElement(node) || IsTypeElement(node)
2844+
}
2845+
2846+
func GetClassExtendsHeritageElement(node *Node) *ExpressionWithTypeArgumentsNode {
2847+
heritageElements := GetHeritageElements(node, KindExtendsKeyword)
2848+
if len(heritageElements) > 0 {
2849+
return heritageElements[0]
2850+
}
2851+
return nil
2852+
}
2853+
2854+
func GetImplementsTypeNodes(node *Node) []*ExpressionWithTypeArgumentsNode {
2855+
return GetHeritageElements(node, KindImplementsKeyword)
2856+
}
2857+
2858+
func IsTypeKeywordToken(node *Node) bool {
2859+
return node.Kind == KindTypeKeyword
2860+
}
2861+
2862+
// If node is a single comment JSDoc, we do not visit the comment node list.
2863+
func IsJSDocSingleCommentNodeList(parent *Node, nodeList *NodeList) bool {
2864+
return IsJSDocSingleCommentNode(parent) && nodeList == parent.AsJSDoc().Comment
2865+
}
2866+
2867+
func IsJSDocSingleCommentNode(node *Node) bool {
2868+
return node.Kind == KindJSDoc && node.AsJSDoc().Comment != nil && len(node.AsJSDoc().Comment.Nodes) == 1
2869+
}
2870+
2871+
func IsValidTypeOnlyAliasUseSite(useSite *Node) bool {
2872+
return useSite.Flags&NodeFlagsAmbient != 0 ||
2873+
IsPartOfTypeQuery(useSite) ||
2874+
isIdentifierInNonEmittingHeritageClause(useSite) ||
2875+
isPartOfPossiblyValidTypeOrAbstractComputedPropertyName(useSite) ||
2876+
!(IsExpressionNode(useSite) || isShorthandPropertyNameUseSite(useSite))
2877+
}
2878+
2879+
func isIdentifierInNonEmittingHeritageClause(node *Node) bool {
2880+
if !IsIdentifier(node) {
2881+
return false
2882+
}
2883+
parent := node.Parent
2884+
for IsPropertyAccessExpression(parent) || IsExpressionWithTypeArguments(parent) {
2885+
parent = parent.Parent
2886+
}
2887+
return IsHeritageClause(parent) && (parent.AsHeritageClause().Token == KindImplementsKeyword || IsInterfaceDeclaration(parent.Parent))
2888+
}
2889+
2890+
func isPartOfPossiblyValidTypeOrAbstractComputedPropertyName(node *Node) bool {
2891+
for NodeKindIs(node, KindIdentifier, KindPropertyAccessExpression) {
2892+
node = node.Parent
2893+
}
2894+
if node.Kind != KindComputedPropertyName {
2895+
return false
2896+
}
2897+
if HasSyntacticModifier(node.Parent, ModifierFlagsAbstract) {
2898+
return true
2899+
}
2900+
return NodeKindIs(node.Parent.Parent, KindInterfaceDeclaration, KindTypeLiteral)
2901+
}
2902+
2903+
func isShorthandPropertyNameUseSite(useSite *Node) bool {
2904+
return IsIdentifier(useSite) && IsShorthandPropertyAssignment(useSite.Parent) && useSite.Parent.AsShorthandPropertyAssignment().Name() == useSite
2905+
}
2906+
2907+
func GetPropertyNameForPropertyNameNode(name *Node) string {
2908+
switch name.Kind {
2909+
case KindIdentifier, KindPrivateIdentifier, KindStringLiteral, KindNoSubstitutionTemplateLiteral,
2910+
KindNumericLiteral, KindBigIntLiteral, KindJsxNamespacedName:
2911+
return name.Text()
2912+
case KindComputedPropertyName:
2913+
nameExpression := name.AsComputedPropertyName().Expression
2914+
if IsStringOrNumericLiteralLike(nameExpression) {
2915+
return nameExpression.Text()
2916+
}
2917+
if IsSignedNumericLiteral(nameExpression) {
2918+
text := nameExpression.AsPrefixUnaryExpression().Operand.Text()
2919+
if nameExpression.AsPrefixUnaryExpression().Operator == KindMinusToken {
2920+
text = "-" + text
2921+
}
2922+
return text
2923+
}
2924+
return InternalSymbolNameMissing
2925+
}
2926+
panic("Unhandled case in getPropertyNameForPropertyNameNode")
2927+
}

0 commit comments

Comments
 (0)