@@ -19,12 +19,14 @@ import {
19
19
DiagnosticWithLocation ,
20
20
ElementAccessExpression ,
21
21
EmitResolver ,
22
+ EntityNameOrEntityNameExpression ,
22
23
ExportAssignment ,
23
24
Expression ,
24
25
ExpressionWithTypeArguments ,
25
26
findAncestor ,
26
27
FunctionDeclaration ,
27
28
FunctionExpression ,
29
+ FunctionLikeDeclaration ,
28
30
GetAccessorDeclaration ,
29
31
getAllAccessorDeclarations ,
30
32
getNameOfDeclaration ,
@@ -40,9 +42,12 @@ import {
40
42
isConstructorDeclaration ,
41
43
isConstructSignatureDeclaration ,
42
44
isElementAccessExpression ,
45
+ isEntityName ,
46
+ isEntityNameExpression ,
43
47
isExportAssignment ,
44
48
isExpressionWithTypeArguments ,
45
49
isFunctionDeclaration ,
50
+ isFunctionLikeDeclaration ,
46
51
isGetAccessor ,
47
52
isHeritageClause ,
48
53
isImportEqualsDeclaration ,
@@ -53,15 +58,18 @@ import {
53
58
isParameter ,
54
59
isParameterPropertyDeclaration ,
55
60
isParenthesizedExpression ,
61
+ isPartOfTypeNode ,
56
62
isPropertyAccessExpression ,
57
63
isPropertyDeclaration ,
58
64
isPropertySignature ,
65
+ isReturnStatement ,
59
66
isSetAccessor ,
60
67
isStatement ,
61
68
isStatic ,
62
69
isTypeAliasDeclaration ,
63
70
isTypeAssertionExpression ,
64
71
isTypeParameterDeclaration ,
72
+ isTypeQueryNode ,
65
73
isVariableDeclaration ,
66
74
JSDocCallbackTag ,
67
75
JSDocEnumTag ,
@@ -658,6 +666,9 @@ export function createGetIsolatedDeclarationErrors(resolver: EmitResolver) {
658
666
if ( heritageClause ) {
659
667
return createDiagnosticForNode ( node , Diagnostics . Extends_clause_can_t_contain_an_expression_with_isolatedDeclarations ) ;
660
668
}
669
+ if ( ( isPartOfTypeNode ( node ) || isTypeQueryNode ( node . parent ) ) && ( isEntityName ( node ) || isEntityNameExpression ( node ) ) ) {
670
+ return createEntityInTypeNodeError ( node ) ;
671
+ }
661
672
Debug . type < WithIsolatedDeclarationDiagnostic > ( node ) ;
662
673
switch ( node . kind ) {
663
674
case SyntaxKind . GetAccessor :
@@ -694,8 +705,15 @@ export function createGetIsolatedDeclarationErrors(resolver: EmitResolver) {
694
705
}
695
706
696
707
function findNearestDeclaration ( node : Node ) {
697
- const result = findAncestor ( node , n => isExportAssignment ( n ) || ( isStatement ( n ) ? "quit" : isVariableDeclaration ( n ) || isPropertyDeclaration ( n ) || isParameter ( n ) ) ) ;
698
- return result as VariableDeclaration | PropertyDeclaration | ParameterDeclaration | ExportAssignment | undefined ;
708
+ const result = findAncestor ( node , n => isExportAssignment ( n ) || isStatement ( n ) || isVariableDeclaration ( n ) || isPropertyDeclaration ( n ) || isParameter ( n ) ) ;
709
+ if ( ! result ) return undefined ;
710
+
711
+ if ( isExportAssignment ( result ) ) return result ;
712
+
713
+ if ( isReturnStatement ( result ) ) {
714
+ return findAncestor ( result , ( n ) : n is Exclude < FunctionLikeDeclaration , ConstructorDeclaration > => isFunctionLikeDeclaration ( n ) && ! isConstructorDeclaration ( n ) ) ;
715
+ }
716
+ return ( isStatement ( result ) ? undefined : result ) as VariableDeclaration | PropertyDeclaration | ParameterDeclaration | ExportAssignment | undefined ;
699
717
}
700
718
701
719
function createAccessorTypeError ( node : GetAccessorDeclaration | SetAccessorDeclaration ) {
@@ -712,31 +730,27 @@ export function createGetIsolatedDeclarationErrors(resolver: EmitResolver) {
712
730
}
713
731
return diag ;
714
732
}
715
- function createObjectLiteralError ( node : ShorthandPropertyAssignment | SpreadAssignment | ComputedPropertyName ) {
716
- const diag = createDiagnosticForNode ( node , errorByDeclarationKind [ node . kind ] ) ;
733
+ function addParentDeclarationRelatedInfo ( node : Node , diag : DiagnosticWithLocation ) {
717
734
const parentDeclaration = findNearestDeclaration ( node ) ;
718
735
if ( parentDeclaration ) {
719
- const targetStr = isExportAssignment ( parentDeclaration ) ? "" : getTextOfNode ( parentDeclaration . name , /*includeTrivia*/ false ) ;
736
+ const targetStr = isExportAssignment ( parentDeclaration ) || ! parentDeclaration . name ? "" : getTextOfNode ( parentDeclaration . name , /*includeTrivia*/ false ) ;
720
737
addRelatedInfo ( diag , createDiagnosticForNode ( parentDeclaration , relatedSuggestionByDeclarationKind [ parentDeclaration . kind ] , targetStr ) ) ;
721
738
}
722
739
return diag ;
723
740
}
741
+ function createObjectLiteralError ( node : ShorthandPropertyAssignment | SpreadAssignment | ComputedPropertyName ) {
742
+ const diag = createDiagnosticForNode ( node , errorByDeclarationKind [ node . kind ] ) ;
743
+ addParentDeclarationRelatedInfo ( node , diag ) ;
744
+ return diag ;
745
+ }
724
746
function createArrayLiteralError ( node : ArrayLiteralExpression | SpreadElement ) {
725
747
const diag = createDiagnosticForNode ( node , errorByDeclarationKind [ node . kind ] ) ;
726
- const parentDeclaration = findNearestDeclaration ( node ) ;
727
- if ( parentDeclaration ) {
728
- const targetStr = isExportAssignment ( parentDeclaration ) ? "" : getTextOfNode ( parentDeclaration . name , /*includeTrivia*/ false ) ;
729
- addRelatedInfo ( diag , createDiagnosticForNode ( parentDeclaration , relatedSuggestionByDeclarationKind [ parentDeclaration . kind ] , targetStr ) ) ;
730
- }
748
+ addParentDeclarationRelatedInfo ( node , diag ) ;
731
749
return diag ;
732
750
}
733
751
function createReturnTypeError ( node : FunctionDeclaration | FunctionExpression | ArrowFunction | MethodDeclaration | ConstructSignatureDeclaration ) {
734
752
const diag = createDiagnosticForNode ( node , errorByDeclarationKind [ node . kind ] ) ;
735
- const parentDeclaration = findNearestDeclaration ( node ) ;
736
- if ( parentDeclaration ) {
737
- const targetStr = isExportAssignment ( parentDeclaration ) ? "" : getTextOfNode ( parentDeclaration . name , /*includeTrivia*/ false ) ;
738
- addRelatedInfo ( diag , createDiagnosticForNode ( parentDeclaration , relatedSuggestionByDeclarationKind [ parentDeclaration . kind ] , targetStr ) ) ;
739
- }
753
+ addParentDeclarationRelatedInfo ( node , diag ) ;
740
754
addRelatedInfo ( diag , createDiagnosticForNode ( node , relatedSuggestionByDeclarationKind [ node . kind ] ) ) ;
741
755
return diag ;
742
756
}
@@ -768,12 +782,18 @@ export function createGetIsolatedDeclarationErrors(resolver: EmitResolver) {
768
782
function createClassExpressionError ( node : Expression ) {
769
783
return createExpressionError ( node , Diagnostics . Inference_from_class_expressions_is_not_supported_with_isolatedDeclarations ) ;
770
784
}
785
+ function createEntityInTypeNodeError ( node : EntityNameOrEntityNameExpression ) {
786
+ const diag = createDiagnosticForNode ( node , Diagnostics . Type_containing_private_name_0_can_t_be_used_with_isolatedDeclarations , getTextOfNode ( node , /*includeTrivia*/ false ) ) ;
787
+ addParentDeclarationRelatedInfo ( node , diag ) ;
788
+ return diag ;
789
+ }
771
790
function createExpressionError ( node : Expression , diagnosticMessage ?: DiagnosticMessage ) {
772
791
const parentDeclaration = findNearestDeclaration ( node ) ;
773
792
let diag : DiagnosticWithLocation ;
774
793
if ( parentDeclaration ) {
775
- const targetStr = isExportAssignment ( parentDeclaration ) ? "" : getTextOfNode ( parentDeclaration . name , /*includeTrivia*/ false ) ;
794
+ const targetStr = isExportAssignment ( parentDeclaration ) || ! parentDeclaration . name ? "" : getTextOfNode ( parentDeclaration . name , /*includeTrivia*/ false ) ;
776
795
const parent = findAncestor ( node . parent , n => isExportAssignment ( n ) || ( isStatement ( n ) ? "quit" : ! isParenthesizedExpression ( n ) && ! isTypeAssertionExpression ( n ) && ! isAsExpression ( n ) ) ) ;
796
+
777
797
if ( parentDeclaration === parent ) {
778
798
diag = createDiagnosticForNode ( node , diagnosticMessage ?? errorByDeclarationKind [ parentDeclaration . kind ] ) ;
779
799
addRelatedInfo ( diag , createDiagnosticForNode ( parentDeclaration , relatedSuggestionByDeclarationKind [ parentDeclaration . kind ] , targetStr ) ) ;
0 commit comments