Skip to content

Commit 980f9fd

Browse files
authored
Merge pull request #11848 from Microsoft/AddJavaScriptSemanticErrorsToSyntacticErrors
Fix for #11719 - TSServer: JS files should display syntactic errors for TS syntax
2 parents fcdeecf + 05c2c9b commit 980f9fd

26 files changed

+76
-27
lines changed

src/compiler/program.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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);

src/compiler/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2073,6 +2073,9 @@ namespace ts {
20732073
// as well as code diagnostics).
20742074
/* @internal */ parseDiagnostics: Diagnostic[];
20752075

2076+
// Stores additional file level diagnostics reported by the program
2077+
/* @internal */ additionalSyntacticDiagnostics?: Diagnostic[];
2078+
20762079
// File level diagnostics reported by the binder.
20772080
/* @internal */ bindDiagnostics: Diagnostic[];
20782081

tests/cases/fourslash/getJavaScriptSemanticDiagnostics1.ts renamed to tests/cases/fourslash/getJavaScriptSyntacticDiagnostics1.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// @Filename: a.js
55
//// import a = b;
66

7-
verify.getSemanticDiagnostics(`[
7+
verify.getSyntacticDiagnostics(`[
88
{
99
"message": "'import ... =' can only be used in a .ts file.",
1010
"start": 0,

tests/cases/fourslash/getJavaScriptSemanticDiagnostics10.ts renamed to tests/cases/fourslash/getJavaScriptSyntacticDiagnostics10.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// @Filename: a.js
55
//// function F<T>() { }
66

7-
verify.getSemanticDiagnostics(`[
7+
verify.getSyntacticDiagnostics(`[
88
{
99
"message": "'type parameter declarations' can only be used in a .ts file.",
1010
"start": 11,

tests/cases/fourslash/getJavaScriptSemanticDiagnostics11.ts renamed to tests/cases/fourslash/getJavaScriptSyntacticDiagnostics11.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// @Filename: a.js
55
//// function F(): number { }
66

7-
verify.getSemanticDiagnostics(`[
7+
verify.getSyntacticDiagnostics(`[
88
{
99
"message": "'types' can only be used in a .ts file.",
1010
"start": 14,

tests/cases/fourslash/getJavaScriptSemanticDiagnostics12.ts renamed to tests/cases/fourslash/getJavaScriptSyntacticDiagnostics12.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// @Filename: a.js
55
//// declare var v;
66

7-
verify.getSemanticDiagnostics(`[
7+
verify.getSyntacticDiagnostics(`[
88
{
99
"message": "'declare' can only be used in a .ts file.",
1010
"start": 0,

tests/cases/fourslash/getJavaScriptSemanticDiagnostics13.ts renamed to tests/cases/fourslash/getJavaScriptSyntacticDiagnostics13.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// @Filename: a.js
55
//// var v: () => number;
66

7-
verify.getSemanticDiagnostics(`[
7+
verify.getSyntacticDiagnostics(`[
88
{
99
"message": "'types' can only be used in a .ts file.",
1010
"start": 7,

tests/cases/fourslash/getJavaScriptSemanticDiagnostics14.ts renamed to tests/cases/fourslash/getJavaScriptSyntacticDiagnostics14.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// @Filename: a.js
55
//// Foo<number>();
66

7-
verify.getSemanticDiagnostics(`[
7+
verify.getSyntacticDiagnostics(`[
88
{
99
"message": "'type arguments' can only be used in a .ts file.",
1010
"start": 4,

tests/cases/fourslash/getJavaScriptSemanticDiagnostics15.ts renamed to tests/cases/fourslash/getJavaScriptSyntacticDiagnostics15.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// @Filename: a.js
55
//// function F(public p) { }
66

7-
verify.getSemanticDiagnostics(`[
7+
verify.getSyntacticDiagnostics(`[
88
{
99
"message": "'parameter modifiers' can only be used in a .ts file.",
1010
"start": 11,

tests/cases/fourslash/getJavaScriptSemanticDiagnostics16.ts renamed to tests/cases/fourslash/getJavaScriptSyntacticDiagnostics16.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// @Filename: a.js
55
//// function F(p?) { }
66

7-
verify.getSemanticDiagnostics(`[
7+
verify.getSyntacticDiagnostics(`[
88
{
99
"message": "'?' can only be used in a .ts file.",
1010
"start": 12,

0 commit comments

Comments
 (0)