@@ -719,6 +719,14 @@ namespace ts {
719719 }
720720
721721 function getSyntacticDiagnosticsForFile ( sourceFile : SourceFile ) : Diagnostic [ ] {
722+ // For JavaScript files, we report semantic errors for using TypeScript-only
723+ // constructs from within a JavaScript file as syntactic errors.
724+ if ( isSourceFileJavaScript ( sourceFile ) ) {
725+ if ( ! sourceFile . additionalSyntacticDiagnostics ) {
726+ sourceFile . additionalSyntacticDiagnostics = getJavaScriptSyntacticDiagnosticsForFile ( sourceFile ) ;
727+ }
728+ return concatenate ( sourceFile . additionalSyntacticDiagnostics , sourceFile . parseDiagnostics ) ;
729+ }
722730 return sourceFile . parseDiagnostics ;
723731 }
724732
@@ -751,20 +759,18 @@ namespace ts {
751759
752760 Debug . assert ( ! ! sourceFile . bindDiagnostics ) ;
753761 const bindDiagnostics = sourceFile . bindDiagnostics ;
754- // For JavaScript files, we don't want to report the normal typescript semantic errors.
755- // Instead, we just report errors for using TypeScript-only constructs from within a
756- // JavaScript file.
757- const checkDiagnostics = isSourceFileJavaScript ( sourceFile ) ?
758- getJavaScriptSemanticDiagnosticsForFile ( sourceFile ) :
759- typeChecker . getDiagnostics ( sourceFile , cancellationToken ) ;
762+ // For JavaScript files, we don't want to report semantic errors.
763+ // Instead, we'll report errors for using TypeScript-only constructs from within a
764+ // JavaScript file when we get syntactic diagnostics for the file.
765+ const checkDiagnostics = isSourceFileJavaScript ( sourceFile ) ? [ ] : typeChecker . getDiagnostics ( sourceFile , cancellationToken ) ;
760766 const fileProcessingDiagnosticsInFile = fileProcessingDiagnostics . getDiagnostics ( sourceFile . fileName ) ;
761767 const programDiagnosticsInFile = programDiagnostics . getDiagnostics ( sourceFile . fileName ) ;
762768
763769 return bindDiagnostics . concat ( checkDiagnostics , fileProcessingDiagnosticsInFile , programDiagnosticsInFile ) ;
764770 } ) ;
765771 }
766772
767- function getJavaScriptSemanticDiagnosticsForFile ( sourceFile : SourceFile ) : Diagnostic [ ] {
773+ function getJavaScriptSyntacticDiagnosticsForFile ( sourceFile : SourceFile ) : Diagnostic [ ] {
768774 return runWithCancellationToken ( ( ) => {
769775 const diagnostics : Diagnostic [ ] = [ ] ;
770776 walk ( sourceFile ) ;
0 commit comments