diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 6139dda0013..b73558cfc6b 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -661,19 +661,17 @@ namespace ts { } function getSemanticDiagnosticsForFile(sourceFile: SourceFile, cancellationToken: CancellationToken): Diagnostic[] { - // For JavaScript files, we don't want to report the normal typescript semantic errors. - // Instead, we just report errors for using TypeScript-only constructs from within a - // JavaScript file. - if (isSourceFileJavaScript(sourceFile)) { - return getJavaScriptSemanticDiagnosticsForFile(sourceFile, cancellationToken); - } - return runWithCancellationToken(() => { const typeChecker = getDiagnosticsProducingTypeChecker(); Debug.assert(!!sourceFile.bindDiagnostics); const bindDiagnostics = sourceFile.bindDiagnostics; - const checkDiagnostics = typeChecker.getDiagnostics(sourceFile, cancellationToken); + // For JavaScript files, we don't want to report the normal typescript semantic errors. + // Instead, we just report errors for using TypeScript-only constructs from within a + // JavaScript file. + const checkDiagnostics = isSourceFileJavaScript(sourceFile) ? + getJavaScriptSemanticDiagnosticsForFile(sourceFile, cancellationToken) : + typeChecker.getDiagnostics(sourceFile, cancellationToken); const fileProcessingDiagnosticsInFile = fileProcessingDiagnostics.getDiagnostics(sourceFile.fileName); const programDiagnosticsInFile = programDiagnostics.getDiagnostics(sourceFile.fileName); diff --git a/tests/baselines/reference/jsFileCompilationBindErrors.errors.txt b/tests/baselines/reference/jsFileCompilationBindErrors.errors.txt new file mode 100644 index 00000000000..eeae7053333 --- /dev/null +++ b/tests/baselines/reference/jsFileCompilationBindErrors.errors.txt @@ -0,0 +1,27 @@ +tests/cases/compiler/a.js(1,5): error TS2451: Cannot redeclare block-scoped variable 'C'. +tests/cases/compiler/a.js(2,5): error TS2451: Cannot redeclare block-scoped variable 'C'. +tests/cases/compiler/a.js(6,5): error TS7027: Unreachable code detected. +tests/cases/compiler/a.js(11,9): error TS1100: Invalid use of 'arguments' in strict mode. + + +==== tests/cases/compiler/a.js (4 errors) ==== + let C = "sss"; + ~ +!!! error TS2451: Cannot redeclare block-scoped variable 'C'. + let C = 0; // Error: Cannot redeclare block-scoped variable 'C'. + ~ +!!! error TS2451: Cannot redeclare block-scoped variable 'C'. + + function f() { + return; + return; // Error: Unreachable code detected. + ~~~~~~ +!!! error TS7027: Unreachable code detected. + } + + function b() { + "use strict"; + var arguments = 0; // Error: Invalid use of 'arguments' in strict mode. + ~~~~~~~~~ +!!! error TS1100: Invalid use of 'arguments' in strict mode. + } \ No newline at end of file diff --git a/tests/baselines/reference/jsFileCompilationBindErrors.symbols b/tests/baselines/reference/jsFileCompilationBindErrors.symbols deleted file mode 100644 index 6ad06da1df6..00000000000 --- a/tests/baselines/reference/jsFileCompilationBindErrors.symbols +++ /dev/null @@ -1,21 +0,0 @@ -=== tests/cases/compiler/a.js === -let C = "sss"; ->C : Symbol(C, Decl(a.js, 0, 3)) - -let C = 0; // Error: Cannot redeclare block-scoped variable 'C'. ->C : Symbol(C, Decl(a.js, 1, 3)) - -function f() { ->f : Symbol(f, Decl(a.js, 1, 10)) - - return; - return; // Error: Unreachable code detected. -} - -function b() { ->b : Symbol(b, Decl(a.js, 6, 1)) - - "use strict"; - var arguments = 0; // Error: Invalid use of 'arguments' in strict mode. ->arguments : Symbol(arguments, Decl(a.js, 10, 7)) -} diff --git a/tests/baselines/reference/jsFileCompilationBindErrors.types b/tests/baselines/reference/jsFileCompilationBindErrors.types deleted file mode 100644 index 113bcd08bc9..00000000000 --- a/tests/baselines/reference/jsFileCompilationBindErrors.types +++ /dev/null @@ -1,26 +0,0 @@ -=== tests/cases/compiler/a.js === -let C = "sss"; ->C : string ->"sss" : string - -let C = 0; // Error: Cannot redeclare block-scoped variable 'C'. ->C : number ->0 : number - -function f() { ->f : () => void - - return; - return; // Error: Unreachable code detected. -} - -function b() { ->b : () => void - - "use strict"; ->"use strict" : string - - var arguments = 0; // Error: Invalid use of 'arguments' in strict mode. ->arguments : number ->0 : number -} diff --git a/tests/baselines/reference/jsFileCompilationExportAssignmentSyntax.errors.txt b/tests/baselines/reference/jsFileCompilationExportAssignmentSyntax.errors.txt index f537941c55c..3be5f99823a 100644 --- a/tests/baselines/reference/jsFileCompilationExportAssignmentSyntax.errors.txt +++ b/tests/baselines/reference/jsFileCompilationExportAssignmentSyntax.errors.txt @@ -1,9 +1,12 @@ error TS5055: Cannot write file 'tests/cases/compiler/a.js' because it would overwrite input file. +tests/cases/compiler/a.js(1,1): error TS1148: Cannot compile modules unless the '--module' flag is provided. tests/cases/compiler/a.js(1,1): error TS8003: 'export=' can only be used in a .ts file. !!! error TS5055: Cannot write file 'tests/cases/compiler/a.js' because it would overwrite input file. -==== tests/cases/compiler/a.js (1 errors) ==== +==== tests/cases/compiler/a.js (2 errors) ==== export = b; ~~~~~~~~~~~ +!!! error TS1148: Cannot compile modules unless the '--module' flag is provided. + ~~~~~~~~~~~ !!! error TS8003: 'export=' can only be used in a .ts file. \ No newline at end of file diff --git a/tests/cases/fourslash/getJavaScriptSemanticDiagnostics2.ts b/tests/cases/fourslash/getJavaScriptSemanticDiagnostics2.ts index 9ab29b41798..58c02dae183 100644 --- a/tests/cases/fourslash/getJavaScriptSemanticDiagnostics2.ts +++ b/tests/cases/fourslash/getJavaScriptSemanticDiagnostics2.ts @@ -11,5 +11,12 @@ verify.getSemanticDiagnostics(`[ "length": 11, "category": "error", "code": 8003 + }, + { + "message": "Cannot compile modules unless the '--module' flag is provided.", + "start": 0, + "length": 11, + "category": "error", + "code": 1148 } ]`); \ No newline at end of file