For JavaScript files, we report semantic errors for using TypeScript-only constructs

This commit is contained in:
Jason Ramsay 2016-10-24 16:57:43 -07:00
parent 1884c89620
commit 1b16c0b94b
26 changed files with 73 additions and 26 deletions

View File

@ -725,6 +725,12 @@ namespace ts {
}
function getSyntacticDiagnosticsForFile(sourceFile: SourceFile): Diagnostic[] {
// For JavaScript files, we report semantic errors for using TypeScript-only
// constructs from within a JavaScript file as syntactic errors.
if (isSourceFileJavaScript(sourceFile) && !sourceFile.parseJavaScriptDiagnostics) {
sourceFile.parseJavaScriptDiagnostics = getJavaScriptSemanticDiagnosticsForFile(sourceFile);
sourceFile.parseDiagnostics = sourceFile.parseDiagnostics.concat(sourceFile.parseJavaScriptDiagnostics);
}
return sourceFile.parseDiagnostics;
}
@ -757,12 +763,10 @@ namespace ts {
Debug.assert(!!sourceFile.bindDiagnostics);
const bindDiagnostics = sourceFile.bindDiagnostics;
// 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) :
typeChecker.getDiagnostics(sourceFile, cancellationToken);
// For JavaScript files, we don't want to report semantic errors.
// Instead, we'll report errors for using TypeScript-only constructs from within a
// JavaScript file when we get syntactic diagnostics for the file.
const checkDiagnostics = isSourceFileJavaScript(sourceFile) ? [] : typeChecker.getDiagnostics(sourceFile, cancellationToken);
const fileProcessingDiagnosticsInFile = fileProcessingDiagnostics.getDiagnostics(sourceFile.fileName);
const programDiagnosticsInFile = programDiagnostics.getDiagnostics(sourceFile.fileName);

View File

@ -2024,6 +2024,9 @@ namespace ts {
// as well as code diagnostics).
/* @internal */ parseDiagnostics: Diagnostic[];
// Stores file level JavaScript diagnostics reported by the program
/* @internal */ parseJavaScriptDiagnostics?: Diagnostic[];
// File level diagnostics reported by the binder.
/* @internal */ bindDiagnostics: Diagnostic[];

View File

@ -4,7 +4,7 @@
// @Filename: a.js
//// import a = b;
verify.getSemanticDiagnostics(`[
verify.getSyntacticDiagnostics(`[
{
"message": "'import ... =' can only be used in a .ts file.",
"start": 0,

View File

@ -4,7 +4,7 @@
// @Filename: a.js
//// function F<T>() { }
verify.getSemanticDiagnostics(`[
verify.getSyntacticDiagnostics(`[
{
"message": "'type parameter declarations' can only be used in a .ts file.",
"start": 11,

View File

@ -4,7 +4,7 @@
// @Filename: a.js
//// function F(): number { }
verify.getSemanticDiagnostics(`[
verify.getSyntacticDiagnostics(`[
{
"message": "'types' can only be used in a .ts file.",
"start": 14,

View File

@ -4,7 +4,7 @@
// @Filename: a.js
//// declare var v;
verify.getSemanticDiagnostics(`[
verify.getSyntacticDiagnostics(`[
{
"message": "'declare' can only be used in a .ts file.",
"start": 0,

View File

@ -4,7 +4,7 @@
// @Filename: a.js
//// var v: () => number;
verify.getSemanticDiagnostics(`[
verify.getSyntacticDiagnostics(`[
{
"message": "'types' can only be used in a .ts file.",
"start": 7,

View File

@ -4,7 +4,7 @@
// @Filename: a.js
//// Foo<number>();
verify.getSemanticDiagnostics(`[
verify.getSyntacticDiagnostics(`[
{
"message": "'type arguments' can only be used in a .ts file.",
"start": 4,

View File

@ -4,7 +4,7 @@
// @Filename: a.js
//// function F(public p) { }
verify.getSemanticDiagnostics(`[
verify.getSyntacticDiagnostics(`[
{
"message": "'parameter modifiers' can only be used in a .ts file.",
"start": 11,

View File

@ -4,7 +4,7 @@
// @Filename: a.js
//// function F(p?) { }
verify.getSemanticDiagnostics(`[
verify.getSyntacticDiagnostics(`[
{
"message": "'?' can only be used in a .ts file.",
"start": 12,

View File

@ -4,7 +4,7 @@
// @Filename: a.js
//// function F(a: number) { }
verify.getSemanticDiagnostics(`[
verify.getSyntacticDiagnostics(`[
{
"message": "'types' can only be used in a .ts file.",
"start": 14,

View File

@ -9,7 +9,7 @@
////}
goTo.file("a.js");
verify.getSemanticDiagnostics(`[
verify.getSyntacticDiagnostics(`[
{
"message": "\'public\' can only be used in a .ts file.",
"start": 93,
@ -25,7 +25,7 @@ verify.getSemanticDiagnostics(`[
////}
goTo.file("b.js");
verify.getSemanticDiagnostics(`[
verify.getSyntacticDiagnostics(`[
{
"message": "'types' can only be used in a .ts file.",
"start": 17,

View File

@ -4,7 +4,7 @@
// @Filename: a.js
//// enum E { }
verify.getSemanticDiagnostics(`[
verify.getSyntacticDiagnostics(`[
{
"message": "'enum declarations' can only be used in a .ts file.",
"start": 5,

View File

@ -4,7 +4,7 @@
// @Filename: a.js
//// export = b;
verify.getSemanticDiagnostics(`[
verify.getSyntacticDiagnostics(`[
{
"message": "'export=' can only be used in a .ts file.",
"start": 0,

View File

@ -4,7 +4,7 @@
// @Filename: a.js
//// class C<T> { }
verify.getSemanticDiagnostics(`[
verify.getSyntacticDiagnostics(`[
{
"message": "'type parameter declarations' can only be used in a .ts file.",
"start": 8,

View File

@ -4,7 +4,7 @@
// @Filename: a.js
//// public class C { }
verify.getSemanticDiagnostics(`[
verify.getSyntacticDiagnostics(`[
{
"message": "'public' can only be used in a .ts file.",
"start": 0,

View File

@ -4,7 +4,7 @@
// @Filename: a.js
//// class C implements D { }
verify.getSemanticDiagnostics(`[
verify.getSyntacticDiagnostics(`[
{
"message": "'implements clauses' can only be used in a .ts file.",
"start": 8,

View File

@ -4,7 +4,7 @@
// @Filename: a.js
//// interface I { }
verify.getSemanticDiagnostics(`[
verify.getSyntacticDiagnostics(`[
{
"message": "'interface declarations' can only be used in a .ts file.",
"start": 10,

View File

@ -4,7 +4,7 @@
// @Filename: a.js
//// module M { }
verify.getSemanticDiagnostics(`[
verify.getSyntacticDiagnostics(`[
{
"message": "'module declarations' can only be used in a .ts file.",
"start": 7,

View File

@ -4,7 +4,7 @@
// @Filename: a.js
//// type a = b;
verify.getSemanticDiagnostics(`[
verify.getSyntacticDiagnostics(`[
{
"message": "'type aliases' can only be used in a .ts file.",
"start": 5,

View File

@ -4,7 +4,7 @@
// @Filename: a.js
//// public function F() { }
verify.getSemanticDiagnostics(`[
verify.getSyntacticDiagnostics(`[
{
"message": "'public' can only be used in a .ts file.",
"start": 0,

View File

@ -0,0 +1,40 @@
/// <reference path="../fourslash.ts" />
// @allowJs: true
// @Filename: b.js
//// var a = "a";
//// var b: boolean = true;
//// function foo(): string { }
//// var var = "c";
verify.getSyntacticDiagnostics(`[
{
"message": "Variable declaration expected.",
"start": 67,
"length": 3,
"category": "error",
"code": 1134
},
{
"message": "Variable declaration expected.",
"start": 71,
"length": 1,
"category": "error",
"code": 1134
},
{
"message": "Variable declaration expected.",
"start": 73,
"length": 3,
"category": "error",
"code": 1134
},
{
"message": "\'types\' can only be used in a .ts file.",
"start": 20,
"length": 7,
"category": "error",
"code": 8010
}
]`);
verify.getSemanticDiagnostics(`[]`);