Report error when emitting declarations if the reference is to .js file

This commit is contained in:
Sheetal Nandi 2015-09-16 15:11:11 -07:00
parent e32c920cc2
commit 60e15b267d
17 changed files with 298 additions and 14 deletions

View File

@ -68,16 +68,22 @@ namespace ts {
if (!compilerOptions.noResolve) {
let addedGlobalFileReference = false;
forEach(root.referencedFiles, fileReference => {
let referencedFile = tryResolveScriptReference(host, root, fileReference);
if (isJavaScript(fileReference.fileName)) {
reportedDeclarationError = true;
diagnostics.push(createFileDiagnostic(root, fileReference.pos, fileReference.end - fileReference.pos, Diagnostics.js_file_cannot_be_referenced_in_ts_file_when_emitting_declarations));
}
else {
let referencedFile = tryResolveScriptReference(host, root, fileReference);
// All the references that are not going to be part of same file
if (referencedFile && ((referencedFile.flags & NodeFlags.DeclarationFile) || // This is a declare file reference
shouldEmitToOwnFile(referencedFile, compilerOptions) || // This is referenced file is emitting its own js file
!addedGlobalFileReference)) { // Or the global out file corresponding to this reference was not added
// All the references that are not going to be part of same file
if (referencedFile && ((referencedFile.flags & NodeFlags.DeclarationFile) || // This is a declare file reference
shouldEmitToOwnFile(referencedFile, compilerOptions) || // This is referenced file is emitting its own js file
!addedGlobalFileReference)) { // Or the global out file corresponding to this reference was not added
writeReferencePath(referencedFile);
if (!isExternalModuleOrDeclarationFile(referencedFile)) {
addedGlobalFileReference = true;
writeReferencePath(referencedFile);
if (!isExternalModuleOrDeclarationFile(referencedFile)) {
addedGlobalFileReference = true;
}
}
}
});
@ -108,14 +114,20 @@ namespace ts {
// Check what references need to be added
if (!compilerOptions.noResolve) {
forEach(sourceFile.referencedFiles, fileReference => {
let referencedFile = tryResolveScriptReference(host, sourceFile, fileReference);
if (isJavaScript(fileReference.fileName)) {
reportedDeclarationError = true;
diagnostics.push(createFileDiagnostic(sourceFile, fileReference.pos, fileReference.end - fileReference.pos, Diagnostics.js_file_cannot_be_referenced_in_ts_file_when_emitting_declarations));
}
else {
let referencedFile = tryResolveScriptReference(host, sourceFile, fileReference);
// If the reference file is a declaration file or an external module, emit that reference
if (referencedFile && (isExternalModuleOrDeclarationFile(referencedFile) &&
!contains(emittedReferencedFiles, referencedFile))) { // If the file reference was not already emitted
// If the reference file is a declaration file or an external module, emit that reference
if (referencedFile && (isExternalModuleOrDeclarationFile(referencedFile) &&
!contains(emittedReferencedFiles, referencedFile))) { // If the file reference was not already emitted
writeReferencePath(referencedFile);
emittedReferencedFiles.push(referencedFile);
writeReferencePath(referencedFile);
emittedReferencedFiles.push(referencedFile);
}
}
});
}

View File

@ -611,6 +611,7 @@ namespace ts {
enum_declarations_can_only_be_used_in_a_ts_file: { code: 8015, category: DiagnosticCategory.Error, key: "'enum declarations' can only be used in a .ts file." },
type_assertion_expressions_can_only_be_used_in_a_ts_file: { code: 8016, category: DiagnosticCategory.Error, key: "'type assertion expressions' can only be used in a .ts file." },
decorators_can_only_be_used_in_a_ts_file: { code: 8017, category: DiagnosticCategory.Error, key: "'decorators' can only be used in a .ts file." },
js_file_cannot_be_referenced_in_ts_file_when_emitting_declarations: { code: 8018, category: DiagnosticCategory.Error, key: ".js file cannot be referenced in .ts file when emitting declarations." },
Only_identifiers_Slashqualified_names_with_optional_type_arguments_are_currently_supported_in_a_class_extends_clauses: { code: 9002, category: DiagnosticCategory.Error, key: "Only identifiers/qualified-names with optional type arguments are currently supported in a class 'extends' clauses." },
class_expressions_are_not_currently_supported: { code: 9003, category: DiagnosticCategory.Error, key: "'class' expressions are not currently supported." },
JSX_attributes_must_only_be_assigned_a_non_empty_expression: { code: 17000, category: DiagnosticCategory.Error, key: "JSX attributes must only be assigned a non-empty 'expression'." },

View File

@ -2438,6 +2438,10 @@
"category": "Error",
"code": 8017
},
".js file cannot be referenced in .ts file when emitting declarations.": {
"category": "Error",
"code": 8018
},
"Only identifiers/qualified-names with optional type arguments are currently supported in a class 'extends' clauses.": {
"category": "Error",

View File

@ -0,0 +1,18 @@
tests/cases/compiler/b.ts(1,1): error TS8018: .js file cannot be referenced in .ts file when emitting declarations.
==== tests/cases/compiler/a.ts (0 errors) ====
class c {
}
==== tests/cases/compiler/b.ts (1 errors) ====
/// <reference path="c.js"/>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS8018: .js file cannot be referenced in .ts file when emitting declarations.
// error on above reference path when emitting declarations
function foo() {
}
==== tests/cases/compiler/c.js (0 errors) ====
function bar() {
}

View File

@ -0,0 +1,32 @@
//// [tests/cases/compiler/jsFileCompilationErrorOnDeclarationsWithJsFileReferenceWithNoOut.ts] ////
//// [a.ts]
class c {
}
//// [b.ts]
/// <reference path="c.js"/>
// error on above reference path when emitting declarations
function foo() {
}
//// [c.js]
function bar() {
}
//// [a.js]
var c = (function () {
function c() {
}
return c;
})();
//// [b.js]
/// <reference path="c.js"/>
// error on above reference path when emitting declarations
function foo() {
}
//// [a.d.ts]
declare class c {
}

View File

@ -0,0 +1,18 @@
tests/cases/compiler/b.ts(1,1): error TS8018: .js file cannot be referenced in .ts file when emitting declarations.
==== tests/cases/compiler/a.ts (0 errors) ====
class c {
}
==== tests/cases/compiler/b.ts (1 errors) ====
/// <reference path="c.js"/>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS8018: .js file cannot be referenced in .ts file when emitting declarations.
// error on above reference when emitting declarations
function foo() {
}
==== tests/cases/compiler/c.js (0 errors) ====
function bar() {
}

View File

@ -0,0 +1,26 @@
//// [tests/cases/compiler/jsFileCompilationErrorOnDeclarationsWithJsFileReferenceWithOut.ts] ////
//// [a.ts]
class c {
}
//// [b.ts]
/// <reference path="c.js"/>
// error on above reference when emitting declarations
function foo() {
}
//// [c.js]
function bar() {
}
//// [out.js]
var c = (function () {
function c() {
}
return c;
})();
/// <reference path="c.js"/>
// error on above reference when emitting declarations
function foo() {
}

View File

@ -0,0 +1,27 @@
//// [tests/cases/compiler/jsFileCompilationNoErrorWithoutDeclarationsWithJsFileReferenceWithNoOut.ts] ////
//// [a.ts]
class c {
}
//// [b.ts]
/// <reference path="c.js"/>
// no error on above reference path since not emitting declarations
function foo() {
}
//// [c.js]
function bar() {
}
//// [a.js]
var c = (function () {
function c() {
}
return c;
})();
//// [b.js]
/// <reference path="c.js"/>
// no error on above reference path since not emitting declarations
function foo() {
}

View File

@ -0,0 +1,16 @@
=== tests/cases/compiler/a.ts ===
class c {
>c : Symbol(c, Decl(a.ts, 0, 0))
}
=== tests/cases/compiler/b.ts ===
/// <reference path="c.js"/>
// no error on above reference path since not emitting declarations
function foo() {
>foo : Symbol(foo, Decl(b.ts, 0, 0))
}
=== tests/cases/compiler/c.js ===
function bar() {
>bar : Symbol(bar, Decl(c.js, 0, 0))
}

View File

@ -0,0 +1,16 @@
=== tests/cases/compiler/a.ts ===
class c {
>c : c
}
=== tests/cases/compiler/b.ts ===
/// <reference path="c.js"/>
// no error on above reference path since not emitting declarations
function foo() {
>foo : () => void
}
=== tests/cases/compiler/c.js ===
function bar() {
>bar : () => void
}

View File

@ -0,0 +1,26 @@
//// [tests/cases/compiler/jsFileCompilationNoErrorWithoutDeclarationsWithJsFileReferenceWithOut.ts] ////
//// [a.ts]
class c {
}
//// [b.ts]
/// <reference path="c.js"/>
//no error on above reference since not emitting declarations
function foo() {
}
//// [c.js]
function bar() {
}
//// [out.js]
var c = (function () {
function c() {
}
return c;
})();
/// <reference path="c.js"/>
//no error on above reference since not emitting declarations
function foo() {
}

View File

@ -0,0 +1,16 @@
=== tests/cases/compiler/a.ts ===
class c {
>c : Symbol(c, Decl(a.ts, 0, 0))
}
=== tests/cases/compiler/b.ts ===
/// <reference path="c.js"/>
//no error on above reference since not emitting declarations
function foo() {
>foo : Symbol(foo, Decl(b.ts, 0, 0))
}
=== tests/cases/compiler/c.js ===
function bar() {
>bar : Symbol(bar, Decl(c.js, 0, 0))
}

View File

@ -0,0 +1,16 @@
=== tests/cases/compiler/a.ts ===
class c {
>c : c
}
=== tests/cases/compiler/b.ts ===
/// <reference path="c.js"/>
//no error on above reference since not emitting declarations
function foo() {
>foo : () => void
}
=== tests/cases/compiler/c.js ===
function bar() {
>bar : () => void
}

View File

@ -0,0 +1,14 @@
// @declaration: true
// @filename: a.ts
class c {
}
// @filename: b.ts
/// <reference path="c.js"/>
// error on above reference path when emitting declarations
function foo() {
}
// @filename: c.js
function bar() {
}

View File

@ -0,0 +1,15 @@
// @out: out.js
// @declaration: true
// @filename: a.ts
class c {
}
// @filename: b.ts
/// <reference path="c.js"/>
// error on above reference when emitting declarations
function foo() {
}
// @filename: c.js
function bar() {
}

View File

@ -0,0 +1,13 @@
// @filename: a.ts
class c {
}
// @filename: b.ts
/// <reference path="c.js"/>
// no error on above reference path since not emitting declarations
function foo() {
}
// @filename: c.js
function bar() {
}

View File

@ -0,0 +1,14 @@
// @out: out.js
// @filename: a.ts
class c {
}
// @filename: b.ts
/// <reference path="c.js"/>
//no error on above reference since not emitting declarations
function foo() {
}
// @filename: c.js
function bar() {
}