Skip to content

Commit 1b16c0b

Browse files
committed
For JavaScript files, we report semantic errors for using TypeScript-only constructs from within a JavaScript file as syntactic errors.
1 parent 1884c89 commit 1b16c0b

26 files changed

+73
-26
lines changed

src/compiler/program.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,12 @@ namespace ts {
725725
}
726726

727727
function getSyntacticDiagnosticsForFile(sourceFile: SourceFile): Diagnostic[] {
728+
// For JavaScript files, we report semantic errors for using TypeScript-only
729+
// constructs from within a JavaScript file as syntactic errors.
730+
if (isSourceFileJavaScript(sourceFile) && !sourceFile.parseJavaScriptDiagnostics) {
731+
sourceFile.parseJavaScriptDiagnostics = getJavaScriptSemanticDiagnosticsForFile(sourceFile);
732+
sourceFile.parseDiagnostics = sourceFile.parseDiagnostics.concat(sourceFile.parseJavaScriptDiagnostics);
733+
}
728734
return sourceFile.parseDiagnostics;
729735
}
730736

@@ -757,12 +763,10 @@ namespace ts {
757763

758764
Debug.assert(!!sourceFile.bindDiagnostics);
759765
const bindDiagnostics = sourceFile.bindDiagnostics;
760-
// For JavaScript files, we don't want to report the normal typescript semantic errors.
761-
// Instead, we just report errors for using TypeScript-only constructs from within a
762-
// JavaScript file.
763-
const checkDiagnostics = isSourceFileJavaScript(sourceFile) ?
764-
getJavaScriptSemanticDiagnosticsForFile(sourceFile) :
765-
typeChecker.getDiagnostics(sourceFile, cancellationToken);
766+
// For JavaScript files, we don't want to report semantic errors.
767+
// Instead, we'll report errors for using TypeScript-only constructs from within a
768+
// JavaScript file when we get syntactic diagnostics for the file.
769+
const checkDiagnostics = isSourceFileJavaScript(sourceFile) ? [] : typeChecker.getDiagnostics(sourceFile, cancellationToken);
766770
const fileProcessingDiagnosticsInFile = fileProcessingDiagnostics.getDiagnostics(sourceFile.fileName);
767771
const programDiagnosticsInFile = programDiagnostics.getDiagnostics(sourceFile.fileName);
768772

src/compiler/types.ts

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

2027+
// Stores file level JavaScript diagnostics reported by the program
2028+
/* @internal */ parseJavaScriptDiagnostics?: Diagnostic[];
2029+
20272030
// File level diagnostics reported by the binder.
20282031
/* @internal */ bindDiagnostics: Diagnostic[];
20292032

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)