Skip to content

Commit 94a647b

Browse files
committed
Do not emit declarations for javascript files
1 parent b217b8b commit 94a647b

21 files changed

Lines changed: 71 additions & 27 deletions

src/compiler/declarationEmitter.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ namespace ts {
3333
export function getDeclarationDiagnostics(host: EmitHost, resolver: EmitResolver, targetSourceFile: SourceFile): Diagnostic[] {
3434
let diagnostics: Diagnostic[] = [];
3535
let { declarationFilePath } = getEmitFileNames(targetSourceFile, host);
36-
emitDeclarations(host, resolver, diagnostics, declarationFilePath, targetSourceFile);
36+
if (declarationFilePath) {
37+
emitDeclarations(host, resolver, diagnostics, declarationFilePath, targetSourceFile);
38+
}
3739
return diagnostics;
3840
}
3941

@@ -105,7 +107,7 @@ namespace ts {
105107
// Emit references corresponding to this file
106108
let emittedReferencedFiles: SourceFile[] = [];
107109
forEach(host.getSourceFiles(), sourceFile => {
108-
if (!isExternalModuleOrDeclarationFile(sourceFile)) {
110+
if (!isExternalModuleOrDeclarationFile(sourceFile) && !isJavaScript(sourceFile.fileName)) {
109111
// Check what references need to be added
110112
if (!compilerOptions.noResolve) {
111113
forEach(sourceFile.referencedFiles, fileReference => {
@@ -1590,9 +1592,17 @@ namespace ts {
15901592
}
15911593

15921594
function writeReferencePath(referencedFile: SourceFile) {
1593-
let declFileName = referencedFile.flags & NodeFlags.DeclarationFile
1594-
? referencedFile.fileName // Declaration file, use declaration file name
1595-
: getEmitFileNames(referencedFile, host).declarationFilePath; // declaration file name
1595+
let declFileName: string;
1596+
if (referencedFile.flags & NodeFlags.DeclarationFile) {
1597+
// Declaration file, use declaration file name
1598+
declFileName = referencedFile.fileName;
1599+
}
1600+
else {
1601+
// declaration file name
1602+
let { declarationFilePath, jsFilePath } = getEmitFileNames(referencedFile, host);
1603+
Debug.assert(!!declarationFilePath || isJavaScript(referencedFile.fileName), "Declaration file is not present only for javascript files");
1604+
declFileName = declarationFilePath || jsFilePath;
1605+
}
15961606

15971607
declFileName = getRelativePathToDirectoryOrUrl(
15981608
getDirectoryPath(normalizeSlashes(declarationFilePath)),

src/compiler/emitter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8158,7 +8158,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
81588158
emitJavaScript(jsFilePath, sourceMapFilePath, sourceFile);
81598159
}
81608160

8161-
if (compilerOptions.declaration) {
8161+
if (declarationFilePath) {
81628162
emitSkipped = writeDeclarationFile(declarationFilePath, sourceFile, host, resolver, diagnostics) || emitSkipped;
81638163
}
81648164

src/compiler/program.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1272,7 +1272,7 @@ namespace ts {
12721272
if (sourceMapFilePath) {
12731273
emitFilesSeen[sourceMapFilePath] = [file];
12741274
}
1275-
if (options.declaration) {
1275+
if (declarationFilePath) {
12761276
emitFilesSeen[declarationFilePath] = [file];
12771277
}
12781278
}

src/compiler/utilities.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1797,7 +1797,7 @@ namespace ts {
17971797
return {
17981798
jsFilePath,
17991799
sourceMapFilePath: getSourceMapFilePath(jsFilePath, options),
1800-
declarationFilePath: getDeclarationEmitFilePath(jsFilePath, options)
1800+
declarationFilePath: !isJavaScript(sourceFile.fileName) ? getDeclarationEmitFilePath(jsFilePath, options) : undefined
18011801
};
18021802
}
18031803
else if (options.outFile || options.out) {

tests/baselines/reference/jsFileCompilationDuplicateVariable.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,3 @@ var x = "hello"; // No error is recorded here and declaration file will show thi
1313

1414
//// [out.d.ts]
1515
declare var x: number;
16-
declare var x: number;

tests/baselines/reference/jsFileCompilationDuplicateVariableErrorReported.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,3 @@ var x = 10; // Error reported so no declaration file generated?
1313

1414
//// [out.d.ts]
1515
declare var x: string;
16-
declare var x: string;

tests/baselines/reference/jsFileCompilationEmitDeclarations.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,3 @@ function foo() {
2222
//// [out.d.ts]
2323
declare class c {
2424
}
25-
declare function foo(): void;

tests/baselines/reference/jsFileCompilationEmitTrippleSlashReference.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,3 @@ function foo() {
2929
//// [out.d.ts]
3030
declare class c {
3131
}
32-
declare function bar(): void;
33-
declare function foo(): void;

tests/baselines/reference/jsFileCompilationErrorOnDeclarationsWithJsFileReferenceWithNoOut.errors.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ error TS5055: Cannot write file 'tests/cases/compiler/c.js' which is one of the
88

99
==== tests/cases/compiler/b.ts (0 errors) ====
1010
/// <reference path="c.js"/>
11-
// error on above reference path when emitting declarations
11+
// b.d.ts should have c.js as the reference path since we dont emit declarations for js files
1212
function foo() {
1313
}
1414

tests/baselines/reference/jsFileCompilationErrorOnDeclarationsWithJsFileReferenceWithNoOut.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class c {
66

77
//// [b.ts]
88
/// <reference path="c.js"/>
9-
// error on above reference path when emitting declarations
9+
// b.d.ts should have c.js as the reference path since we dont emit declarations for js files
1010
function foo() {
1111
}
1212

@@ -22,16 +22,14 @@ var c = (function () {
2222
})();
2323
//// [b.js]
2424
/// <reference path="c.js"/>
25-
// error on above reference path when emitting declarations
25+
// b.d.ts should have c.js as the reference path since we dont emit declarations for js files
2626
function foo() {
2727
}
2828

2929

3030
//// [a.d.ts]
3131
declare class c {
3232
}
33-
//// [c.d.ts]
34-
declare function bar(): void;
3533
//// [b.d.ts]
36-
/// <reference path="c.d.ts" />
34+
/// <reference path="c.js" />
3735
declare function foo(): void;

0 commit comments

Comments
 (0)