@@ -1208,7 +1208,7 @@ namespace ts {
12081208 bind ( node . statement ) ;
12091209 popActiveLabel ( ) ;
12101210 if ( ! activeLabel . referenced && ! options . allowUnusedLabels ) {
1211- file . bindDiagnostics . push ( createDiagnosticForNode ( node . label , Diagnostics . Unused_label ) ) ;
1211+ errorOrSuggestionOnFirstToken ( unusedLabelIsError ( options ) , node , Diagnostics . Unused_label ) ;
12121212 }
12131213 if ( ! node . statement || node . statement . kind !== SyntaxKind . DoStatement ) {
12141214 // do statement sets current flow inside bindDoStatement
@@ -1914,6 +1914,17 @@ namespace ts {
19141914 file . bindDiagnostics . push ( createFileDiagnostic ( file , span . start , span . length , message , arg0 , arg1 , arg2 ) ) ;
19151915 }
19161916
1917+ function errorOrSuggestionOnFirstToken ( isError : boolean , node : Node , message : DiagnosticMessage , arg0 ?: any , arg1 ?: any , arg2 ?: any ) {
1918+ const span = getSpanOfTokenAtPosition ( file , node . pos ) ;
1919+ const diag = createFileDiagnostic ( file , span . start , span . length , message , arg0 , arg1 , arg2 ) ;
1920+ if ( isError ) {
1921+ file . bindDiagnostics . push ( diag ) ;
1922+ }
1923+ else {
1924+ file . bindSuggestionDiagnostics = append ( file . bindSuggestionDiagnostics , { ...diag , category : DiagnosticCategory . Suggestion } ) ;
1925+ }
1926+ }
1927+
19171928 function bind ( node : Node ) : void {
19181929 if ( ! node ) {
19191930 return ;
@@ -2730,26 +2741,26 @@ namespace ts {
27302741 if ( reportError ) {
27312742 currentFlow = reportedUnreachableFlow ;
27322743
2733- // unreachable code is reported if
2734- // - user has explicitly asked about it AND
2735- // - statement is in not ambient context (statements in ambient context is already an error
2736- // so we should not report extras) AND
2737- // - node is not variable statement OR
2738- // - node is block scoped variable statement OR
2739- // - node is not block scoped variable statement and at least one variable declaration has initializer
2740- // Rationale: we don't want to report errors on non-initialized var's since they are hoisted
2741- // On the other side we do want to report errors on non-initialized 'lets' because of TDZ
2742- const reportUnreachableCode =
2743- ! options . allowUnreachableCode &&
2744- ! ( node . flags & NodeFlags . Ambient ) &&
2745- (
2746- node . kind !== SyntaxKind . VariableStatement ||
2747- getCombinedNodeFlags ( ( < VariableStatement > node ) . declarationList ) & NodeFlags . BlockScoped ||
2748- forEach ( ( < VariableStatement > node ) . declarationList . declarations , d => d . initializer )
2749- ) ;
2750-
2751- if ( reportUnreachableCode ) {
2752- errorOnFirstToken ( node , Diagnostics . Unreachable_code_detected ) ;
2744+ if ( ! options . allowUnreachableCode ) {
2745+ // unreachable code is reported if
2746+ // - user has explicitly asked about it AND
2747+ // - statement is in not ambient context (statements in ambient context is already an error
2748+ // so we should not report extras) AND
2749+ // - node is not variable statement OR
2750+ // - node is block scoped variable statement OR
2751+ // - node is not block scoped variable statement and at least one variable declaration has initializer
2752+ // Rationale: we don't want to report errors on non-initialized var's since they are hoisted
2753+ // On the other side we do want to report errors on non-initialized 'lets' because of TDZ
2754+ const isError =
2755+ unreachableCodeIsError ( options ) &&
2756+ ! ( node . flags & NodeFlags . Ambient ) &&
2757+ (
2758+ ! isVariableStatement ( node ) ||
2759+ ! ! ( getCombinedNodeFlags ( node . declarationList ) & NodeFlags . BlockScoped ) ||
2760+ node . declarationList . declarations . some ( d => ! ! d . initializer )
2761+ ) ;
2762+
2763+ errorOrSuggestionOnFirstToken ( isError , node , Diagnostics . Unreachable_code_detected ) ;
27532764 }
27542765 }
27552766 }
0 commit comments