@@ -1527,14 +1527,14 @@ func GetExtendsHeritageClauseElement(node *Node) *ExpressionWithTypeArgumentsNod
1527
1527
}
1528
1528
1529
1529
func GetExtendsHeritageClauseElements (node * Node ) []* ExpressionWithTypeArgumentsNode {
1530
- return getHeritageElements (node , KindExtendsKeyword )
1530
+ return GetHeritageElements (node , KindExtendsKeyword )
1531
1531
}
1532
1532
1533
1533
func GetImplementsHeritageClauseElements (node * Node ) []* ExpressionWithTypeArgumentsNode {
1534
- return getHeritageElements (node , KindImplementsKeyword )
1534
+ return GetHeritageElements (node , KindImplementsKeyword )
1535
1535
}
1536
1536
1537
- func getHeritageElements (node * Node , kind Kind ) []* Node {
1537
+ func GetHeritageElements (node * Node , kind Kind ) []* Node {
1538
1538
clause := getHeritageClause (node , kind )
1539
1539
if clause != nil {
1540
1540
return clause .AsHeritageClause ().Types .Nodes
@@ -2793,3 +2793,135 @@ func ForEachChildAndJSDoc(node *Node, sourceFile *SourceFile, v Visitor) bool {
2793
2793
}
2794
2794
return node .ForEachChild (v )
2795
2795
}
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