Skip to content

Commit 6b42712

Browse files
committed
Report bind diagnostics, program diagnostics and file pre processing diagnostics in javascript file
Handles #5785
1 parent acedf3c commit 6b42712

6 files changed

+44
-56
lines changed

src/compiler/program.ts

+6-8
Original file line numberDiff line numberDiff line change
@@ -661,19 +661,17 @@ namespace ts {
661661
}
662662

663663
function getSemanticDiagnosticsForFile(sourceFile: SourceFile, cancellationToken: CancellationToken): Diagnostic[] {
664-
// For JavaScript files, we don't want to report the normal typescript semantic errors.
665-
// Instead, we just report errors for using TypeScript-only constructs from within a
666-
// JavaScript file.
667-
if (isSourceFileJavaScript(sourceFile)) {
668-
return getJavaScriptSemanticDiagnosticsForFile(sourceFile, cancellationToken);
669-
}
670-
671664
return runWithCancellationToken(() => {
672665
const typeChecker = getDiagnosticsProducingTypeChecker();
673666

674667
Debug.assert(!!sourceFile.bindDiagnostics);
675668
const bindDiagnostics = sourceFile.bindDiagnostics;
676-
const checkDiagnostics = typeChecker.getDiagnostics(sourceFile, cancellationToken);
669+
// For JavaScript files, we don't want to report the normal typescript semantic errors.
670+
// Instead, we just report errors for using TypeScript-only constructs from within a
671+
// JavaScript file.
672+
const checkDiagnostics = isSourceFileJavaScript(sourceFile) ?
673+
getJavaScriptSemanticDiagnosticsForFile(sourceFile, cancellationToken) :
674+
typeChecker.getDiagnostics(sourceFile, cancellationToken);
677675
const fileProcessingDiagnosticsInFile = fileProcessingDiagnostics.getDiagnostics(sourceFile.fileName);
678676
const programDiagnosticsInFile = programDiagnostics.getDiagnostics(sourceFile.fileName);
679677

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
tests/cases/compiler/a.js(1,5): error TS2451: Cannot redeclare block-scoped variable 'C'.
2+
tests/cases/compiler/a.js(2,5): error TS2451: Cannot redeclare block-scoped variable 'C'.
3+
tests/cases/compiler/a.js(6,5): error TS7027: Unreachable code detected.
4+
tests/cases/compiler/a.js(11,9): error TS1100: Invalid use of 'arguments' in strict mode.
5+
6+
7+
==== tests/cases/compiler/a.js (4 errors) ====
8+
let C = "sss";
9+
~
10+
!!! error TS2451: Cannot redeclare block-scoped variable 'C'.
11+
let C = 0; // Error: Cannot redeclare block-scoped variable 'C'.
12+
~
13+
!!! error TS2451: Cannot redeclare block-scoped variable 'C'.
14+
15+
function f() {
16+
return;
17+
return; // Error: Unreachable code detected.
18+
~~~~~~
19+
!!! error TS7027: Unreachable code detected.
20+
}
21+
22+
function b() {
23+
"use strict";
24+
var arguments = 0; // Error: Invalid use of 'arguments' in strict mode.
25+
~~~~~~~~~
26+
!!! error TS1100: Invalid use of 'arguments' in strict mode.
27+
}

tests/baselines/reference/jsFileCompilationBindErrors.symbols

-21
This file was deleted.

tests/baselines/reference/jsFileCompilationBindErrors.types

-26
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
error TS5055: Cannot write file 'tests/cases/compiler/a.js' because it would overwrite input file.
2+
tests/cases/compiler/a.js(1,1): error TS1148: Cannot compile modules unless the '--module' flag is provided.
23
tests/cases/compiler/a.js(1,1): error TS8003: 'export=' can only be used in a .ts file.
34

45

56
!!! error TS5055: Cannot write file 'tests/cases/compiler/a.js' because it would overwrite input file.
6-
==== tests/cases/compiler/a.js (1 errors) ====
7+
==== tests/cases/compiler/a.js (2 errors) ====
78
export = b;
89
~~~~~~~~~~~
10+
!!! error TS1148: Cannot compile modules unless the '--module' flag is provided.
11+
~~~~~~~~~~~
912
!!! error TS8003: 'export=' can only be used in a .ts file.

tests/cases/fourslash/getJavaScriptSemanticDiagnostics2.ts

+7
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,12 @@ verify.getSemanticDiagnostics(`[
1111
"length": 11,
1212
"category": "error",
1313
"code": 8003
14+
},
15+
{
16+
"message": "Cannot compile modules unless the '--module' flag is provided.",
17+
"start": 0,
18+
"length": 11,
19+
"category": "error",
20+
"code": 1148
1421
}
1522
]`);

0 commit comments

Comments
 (0)