Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,14 @@ 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)) {
if (!sourceFile.additionalSyntacticDiagnostics) {
sourceFile.additionalSyntacticDiagnostics = getJavaScriptSyntacticDiagnosticsForFile(sourceFile);
}
return concatenate(sourceFile.additionalSyntacticDiagnostics, sourceFile.parseDiagnostics);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consider changing it to:

   if (isSourceFileJavaScript(sourceFile))
            if (!sourceFile.additionalSyntacticDiagnostics) {
                sourceFile.additionalSyntacticDiagnostics = getJavaScriptAdditionalSyntacticDiagnosticsForFile(sourceFile);
            }
        return concatenate(sourceFile.additionalSyntacticDiagnostics, sourceFile.parseDiagnostics);
    }
    return sourceFile.parseDiagnostics;

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds good - done

return sourceFile.parseDiagnostics;
}

Expand Down Expand Up @@ -757,20 +765,18 @@ 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);

return bindDiagnostics.concat(checkDiagnostics, fileProcessingDiagnosticsInFile, programDiagnosticsInFile);
});
}

function getJavaScriptSemanticDiagnosticsForFile(sourceFile: SourceFile): Diagnostic[] {
function getJavaScriptSyntacticDiagnosticsForFile(sourceFile: SourceFile): Diagnostic[] {
return runWithCancellationToken(() => {
const diagnostics: Diagnostic[] = [];
walk(sourceFile);
Expand Down
3 changes: 3 additions & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2024,6 +2024,9 @@ namespace ts {
// as well as code diagnostics).
/* @internal */ parseDiagnostics: Diagnostic[];

// Stores additional file level diagnostics reported by the program
/* @internal */ additionalSyntacticDiagnostics?: Diagnostic[];

// File level diagnostics reported by the binder.
/* @internal */ bindDiagnostics: Diagnostic[];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
////}

goTo.file("a.js");
verify.getSemanticDiagnostics(`[
verify.getSyntacticDiagnostics(`[
{
"message": "\'public\' can only be used in a .ts file.",
"start": 93,
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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": "\'types\' can only be used in a .ts file.",
"start": 20,
"length": 7,
"category": "error",
"code": 8010
},
{
"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
}
]`);
verify.getSemanticDiagnostics(`[]`);