diff --git a/src/compiler/declarationEmitter.ts b/src/compiler/declarationEmitter.ts
index 03d2e0453b1..9ebca239758 100644
--- a/src/compiler/declarationEmitter.ts
+++ b/src/compiler/declarationEmitter.ts
@@ -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);
+ }
}
});
}
diff --git a/src/compiler/diagnosticInformationMap.generated.ts b/src/compiler/diagnosticInformationMap.generated.ts
index ced0dd6d87c..610ee7e1125 100644
--- a/src/compiler/diagnosticInformationMap.generated.ts
+++ b/src/compiler/diagnosticInformationMap.generated.ts
@@ -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'." },
diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json
index aaac6284f7d..5d40a1e5ab2 100644
--- a/src/compiler/diagnosticMessages.json
+++ b/src/compiler/diagnosticMessages.json
@@ -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",
diff --git a/tests/baselines/reference/jsFileCompilationErrorOnDeclarationsWithJsFileReferenceWithNoOut.errors.txt b/tests/baselines/reference/jsFileCompilationErrorOnDeclarationsWithJsFileReferenceWithNoOut.errors.txt
new file mode 100644
index 00000000000..1d6ddb3ed30
--- /dev/null
+++ b/tests/baselines/reference/jsFileCompilationErrorOnDeclarationsWithJsFileReferenceWithNoOut.errors.txt
@@ -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) ====
+ ///
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+!!! 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() {
+ }
\ No newline at end of file
diff --git a/tests/baselines/reference/jsFileCompilationErrorOnDeclarationsWithJsFileReferenceWithNoOut.js b/tests/baselines/reference/jsFileCompilationErrorOnDeclarationsWithJsFileReferenceWithNoOut.js
new file mode 100644
index 00000000000..6bd76de881b
--- /dev/null
+++ b/tests/baselines/reference/jsFileCompilationErrorOnDeclarationsWithJsFileReferenceWithNoOut.js
@@ -0,0 +1,32 @@
+//// [tests/cases/compiler/jsFileCompilationErrorOnDeclarationsWithJsFileReferenceWithNoOut.ts] ////
+
+//// [a.ts]
+class c {
+}
+
+//// [b.ts]
+///
+// 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]
+///
+// error on above reference path when emitting declarations
+function foo() {
+}
+
+
+//// [a.d.ts]
+declare class c {
+}
diff --git a/tests/baselines/reference/jsFileCompilationErrorOnDeclarationsWithJsFileReferenceWithOut.errors.txt b/tests/baselines/reference/jsFileCompilationErrorOnDeclarationsWithJsFileReferenceWithOut.errors.txt
new file mode 100644
index 00000000000..a658130e10a
--- /dev/null
+++ b/tests/baselines/reference/jsFileCompilationErrorOnDeclarationsWithJsFileReferenceWithOut.errors.txt
@@ -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) ====
+ ///
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+!!! 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() {
+ }
\ No newline at end of file
diff --git a/tests/baselines/reference/jsFileCompilationErrorOnDeclarationsWithJsFileReferenceWithOut.js b/tests/baselines/reference/jsFileCompilationErrorOnDeclarationsWithJsFileReferenceWithOut.js
new file mode 100644
index 00000000000..99dd961db4c
--- /dev/null
+++ b/tests/baselines/reference/jsFileCompilationErrorOnDeclarationsWithJsFileReferenceWithOut.js
@@ -0,0 +1,26 @@
+//// [tests/cases/compiler/jsFileCompilationErrorOnDeclarationsWithJsFileReferenceWithOut.ts] ////
+
+//// [a.ts]
+class c {
+}
+
+//// [b.ts]
+///
+// error on above reference when emitting declarations
+function foo() {
+}
+
+//// [c.js]
+function bar() {
+}
+
+//// [out.js]
+var c = (function () {
+ function c() {
+ }
+ return c;
+})();
+///
+// error on above reference when emitting declarations
+function foo() {
+}
diff --git a/tests/baselines/reference/jsFileCompilationNoErrorWithoutDeclarationsWithJsFileReferenceWithNoOut.js b/tests/baselines/reference/jsFileCompilationNoErrorWithoutDeclarationsWithJsFileReferenceWithNoOut.js
new file mode 100644
index 00000000000..2d844207986
--- /dev/null
+++ b/tests/baselines/reference/jsFileCompilationNoErrorWithoutDeclarationsWithJsFileReferenceWithNoOut.js
@@ -0,0 +1,27 @@
+//// [tests/cases/compiler/jsFileCompilationNoErrorWithoutDeclarationsWithJsFileReferenceWithNoOut.ts] ////
+
+//// [a.ts]
+class c {
+}
+
+//// [b.ts]
+///
+// 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]
+///
+// no error on above reference path since not emitting declarations
+function foo() {
+}
diff --git a/tests/baselines/reference/jsFileCompilationNoErrorWithoutDeclarationsWithJsFileReferenceWithNoOut.symbols b/tests/baselines/reference/jsFileCompilationNoErrorWithoutDeclarationsWithJsFileReferenceWithNoOut.symbols
new file mode 100644
index 00000000000..06b80b144a4
--- /dev/null
+++ b/tests/baselines/reference/jsFileCompilationNoErrorWithoutDeclarationsWithJsFileReferenceWithNoOut.symbols
@@ -0,0 +1,16 @@
+=== tests/cases/compiler/a.ts ===
+class c {
+>c : Symbol(c, Decl(a.ts, 0, 0))
+}
+
+=== tests/cases/compiler/b.ts ===
+///
+// 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))
+}
diff --git a/tests/baselines/reference/jsFileCompilationNoErrorWithoutDeclarationsWithJsFileReferenceWithNoOut.types b/tests/baselines/reference/jsFileCompilationNoErrorWithoutDeclarationsWithJsFileReferenceWithNoOut.types
new file mode 100644
index 00000000000..ea9b48061c3
--- /dev/null
+++ b/tests/baselines/reference/jsFileCompilationNoErrorWithoutDeclarationsWithJsFileReferenceWithNoOut.types
@@ -0,0 +1,16 @@
+=== tests/cases/compiler/a.ts ===
+class c {
+>c : c
+}
+
+=== tests/cases/compiler/b.ts ===
+///
+// no error on above reference path since not emitting declarations
+function foo() {
+>foo : () => void
+}
+
+=== tests/cases/compiler/c.js ===
+function bar() {
+>bar : () => void
+}
diff --git a/tests/baselines/reference/jsFileCompilationNoErrorWithoutDeclarationsWithJsFileReferenceWithOut.js b/tests/baselines/reference/jsFileCompilationNoErrorWithoutDeclarationsWithJsFileReferenceWithOut.js
new file mode 100644
index 00000000000..55412253441
--- /dev/null
+++ b/tests/baselines/reference/jsFileCompilationNoErrorWithoutDeclarationsWithJsFileReferenceWithOut.js
@@ -0,0 +1,26 @@
+//// [tests/cases/compiler/jsFileCompilationNoErrorWithoutDeclarationsWithJsFileReferenceWithOut.ts] ////
+
+//// [a.ts]
+class c {
+}
+
+//// [b.ts]
+///
+//no error on above reference since not emitting declarations
+function foo() {
+}
+
+//// [c.js]
+function bar() {
+}
+
+//// [out.js]
+var c = (function () {
+ function c() {
+ }
+ return c;
+})();
+///
+//no error on above reference since not emitting declarations
+function foo() {
+}
diff --git a/tests/baselines/reference/jsFileCompilationNoErrorWithoutDeclarationsWithJsFileReferenceWithOut.symbols b/tests/baselines/reference/jsFileCompilationNoErrorWithoutDeclarationsWithJsFileReferenceWithOut.symbols
new file mode 100644
index 00000000000..2f3cf3e0785
--- /dev/null
+++ b/tests/baselines/reference/jsFileCompilationNoErrorWithoutDeclarationsWithJsFileReferenceWithOut.symbols
@@ -0,0 +1,16 @@
+=== tests/cases/compiler/a.ts ===
+class c {
+>c : Symbol(c, Decl(a.ts, 0, 0))
+}
+
+=== tests/cases/compiler/b.ts ===
+///
+//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))
+}
diff --git a/tests/baselines/reference/jsFileCompilationNoErrorWithoutDeclarationsWithJsFileReferenceWithOut.types b/tests/baselines/reference/jsFileCompilationNoErrorWithoutDeclarationsWithJsFileReferenceWithOut.types
new file mode 100644
index 00000000000..cd9a6dfafba
--- /dev/null
+++ b/tests/baselines/reference/jsFileCompilationNoErrorWithoutDeclarationsWithJsFileReferenceWithOut.types
@@ -0,0 +1,16 @@
+=== tests/cases/compiler/a.ts ===
+class c {
+>c : c
+}
+
+=== tests/cases/compiler/b.ts ===
+///
+//no error on above reference since not emitting declarations
+function foo() {
+>foo : () => void
+}
+
+=== tests/cases/compiler/c.js ===
+function bar() {
+>bar : () => void
+}
diff --git a/tests/cases/compiler/jsFileCompilationErrorOnDeclarationsWithJsFileReferenceWithNoOut.ts b/tests/cases/compiler/jsFileCompilationErrorOnDeclarationsWithJsFileReferenceWithNoOut.ts
new file mode 100644
index 00000000000..f02035c3f65
--- /dev/null
+++ b/tests/cases/compiler/jsFileCompilationErrorOnDeclarationsWithJsFileReferenceWithNoOut.ts
@@ -0,0 +1,14 @@
+// @declaration: true
+// @filename: a.ts
+class c {
+}
+
+// @filename: b.ts
+///
+// error on above reference path when emitting declarations
+function foo() {
+}
+
+// @filename: c.js
+function bar() {
+}
\ No newline at end of file
diff --git a/tests/cases/compiler/jsFileCompilationErrorOnDeclarationsWithJsFileReferenceWithOut.ts b/tests/cases/compiler/jsFileCompilationErrorOnDeclarationsWithJsFileReferenceWithOut.ts
new file mode 100644
index 00000000000..04945af8205
--- /dev/null
+++ b/tests/cases/compiler/jsFileCompilationErrorOnDeclarationsWithJsFileReferenceWithOut.ts
@@ -0,0 +1,15 @@
+// @out: out.js
+// @declaration: true
+// @filename: a.ts
+class c {
+}
+
+// @filename: b.ts
+///
+// error on above reference when emitting declarations
+function foo() {
+}
+
+// @filename: c.js
+function bar() {
+}
\ No newline at end of file
diff --git a/tests/cases/compiler/jsFileCompilationNoErrorWithoutDeclarationsWithJsFileReferenceWithNoOut.ts b/tests/cases/compiler/jsFileCompilationNoErrorWithoutDeclarationsWithJsFileReferenceWithNoOut.ts
new file mode 100644
index 00000000000..fc6acef20ee
--- /dev/null
+++ b/tests/cases/compiler/jsFileCompilationNoErrorWithoutDeclarationsWithJsFileReferenceWithNoOut.ts
@@ -0,0 +1,13 @@
+// @filename: a.ts
+class c {
+}
+
+// @filename: b.ts
+///
+// no error on above reference path since not emitting declarations
+function foo() {
+}
+
+// @filename: c.js
+function bar() {
+}
\ No newline at end of file
diff --git a/tests/cases/compiler/jsFileCompilationNoErrorWithoutDeclarationsWithJsFileReferenceWithOut.ts b/tests/cases/compiler/jsFileCompilationNoErrorWithoutDeclarationsWithJsFileReferenceWithOut.ts
new file mode 100644
index 00000000000..3ed1ca1039d
--- /dev/null
+++ b/tests/cases/compiler/jsFileCompilationNoErrorWithoutDeclarationsWithJsFileReferenceWithOut.ts
@@ -0,0 +1,14 @@
+// @out: out.js
+// @filename: a.ts
+class c {
+}
+
+// @filename: b.ts
+///
+//no error on above reference since not emitting declarations
+function foo() {
+}
+
+// @filename: c.js
+function bar() {
+}
\ No newline at end of file