Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 980f9fd

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

26 files changed

+76-27Lines changed: 76 additions & 27 deletions
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/compiler/program.ts‎

Copy file name to clipboardExpand all lines: src/compiler/program.ts
+13-7Lines 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);
Collapse file

‎src/compiler/types.ts‎

Copy file name to clipboardExpand all lines: src/compiler/types.ts
+3Lines 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

Collapse file

‎…ash/getJavaScriptSemanticDiagnostics1.ts‎ ‎…sh/getJavaScriptSyntacticDiagnostics1.ts‎tests/cases/fourslash/getJavaScriptSemanticDiagnostics1.ts renamed to tests/cases/fourslash/getJavaScriptSyntacticDiagnostics1.ts tests/cases/fourslash/getJavaScriptSemanticDiagnostics1.ts renamed to tests/cases/fourslash/getJavaScriptSyntacticDiagnostics1.ts

Copy file name to clipboardExpand all lines: tests/cases/fourslash/getJavaScriptSyntacticDiagnostics1.ts
+1-1Lines 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,
Collapse file

‎…sh/getJavaScriptSemanticDiagnostics10.ts‎ ‎…h/getJavaScriptSyntacticDiagnostics10.ts‎tests/cases/fourslash/getJavaScriptSemanticDiagnostics10.ts renamed to tests/cases/fourslash/getJavaScriptSyntacticDiagnostics10.ts tests/cases/fourslash/getJavaScriptSemanticDiagnostics10.ts renamed to tests/cases/fourslash/getJavaScriptSyntacticDiagnostics10.ts

Copy file name to clipboardExpand all lines: tests/cases/fourslash/getJavaScriptSyntacticDiagnostics10.ts
+1-1Lines 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,
Collapse file

‎…sh/getJavaScriptSemanticDiagnostics11.ts‎ ‎…h/getJavaScriptSyntacticDiagnostics11.ts‎tests/cases/fourslash/getJavaScriptSemanticDiagnostics11.ts renamed to tests/cases/fourslash/getJavaScriptSyntacticDiagnostics11.ts tests/cases/fourslash/getJavaScriptSemanticDiagnostics11.ts renamed to tests/cases/fourslash/getJavaScriptSyntacticDiagnostics11.ts

Copy file name to clipboardExpand all lines: tests/cases/fourslash/getJavaScriptSyntacticDiagnostics11.ts
+1-1Lines 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,
Collapse file

‎…sh/getJavaScriptSemanticDiagnostics12.ts‎ ‎…h/getJavaScriptSyntacticDiagnostics12.ts‎tests/cases/fourslash/getJavaScriptSemanticDiagnostics12.ts renamed to tests/cases/fourslash/getJavaScriptSyntacticDiagnostics12.ts tests/cases/fourslash/getJavaScriptSemanticDiagnostics12.ts renamed to tests/cases/fourslash/getJavaScriptSyntacticDiagnostics12.ts

Copy file name to clipboardExpand all lines: tests/cases/fourslash/getJavaScriptSyntacticDiagnostics12.ts
+1-1Lines 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,
Collapse file

‎…sh/getJavaScriptSemanticDiagnostics13.ts‎ ‎…h/getJavaScriptSyntacticDiagnostics13.ts‎tests/cases/fourslash/getJavaScriptSemanticDiagnostics13.ts renamed to tests/cases/fourslash/getJavaScriptSyntacticDiagnostics13.ts tests/cases/fourslash/getJavaScriptSemanticDiagnostics13.ts renamed to tests/cases/fourslash/getJavaScriptSyntacticDiagnostics13.ts

Copy file name to clipboardExpand all lines: tests/cases/fourslash/getJavaScriptSyntacticDiagnostics13.ts
+1-1Lines 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,
Collapse file

‎…sh/getJavaScriptSemanticDiagnostics14.ts‎ ‎…h/getJavaScriptSyntacticDiagnostics14.ts‎tests/cases/fourslash/getJavaScriptSemanticDiagnostics14.ts renamed to tests/cases/fourslash/getJavaScriptSyntacticDiagnostics14.ts tests/cases/fourslash/getJavaScriptSemanticDiagnostics14.ts renamed to tests/cases/fourslash/getJavaScriptSyntacticDiagnostics14.ts

Copy file name to clipboardExpand all lines: tests/cases/fourslash/getJavaScriptSyntacticDiagnostics14.ts
+1-1Lines 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,
Collapse file

‎…sh/getJavaScriptSemanticDiagnostics15.ts‎ ‎…h/getJavaScriptSyntacticDiagnostics15.ts‎tests/cases/fourslash/getJavaScriptSemanticDiagnostics15.ts renamed to tests/cases/fourslash/getJavaScriptSyntacticDiagnostics15.ts tests/cases/fourslash/getJavaScriptSemanticDiagnostics15.ts renamed to tests/cases/fourslash/getJavaScriptSyntacticDiagnostics15.ts

Copy file name to clipboardExpand all lines: tests/cases/fourslash/getJavaScriptSyntacticDiagnostics15.ts
+1-1Lines 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,
Collapse file

‎…sh/getJavaScriptSemanticDiagnostics16.ts‎ ‎…h/getJavaScriptSyntacticDiagnostics16.ts‎tests/cases/fourslash/getJavaScriptSemanticDiagnostics16.ts renamed to tests/cases/fourslash/getJavaScriptSyntacticDiagnostics16.ts tests/cases/fourslash/getJavaScriptSemanticDiagnostics16.ts renamed to tests/cases/fourslash/getJavaScriptSyntacticDiagnostics16.ts

Copy file name to clipboardExpand all lines: tests/cases/fourslash/getJavaScriptSyntacticDiagnostics16.ts
+1-1Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.