Skip to content

Commit 0ca0304

Browse files
Move the code to actually emit higher up in the function.
Now it it precedes all the other function declarations, and is much easier to debug.
1 parent 22ed101 commit 0ca0304

File tree

1 file changed

+59
-58
lines changed

1 file changed

+59
-58
lines changed

src/compiler/emitter.ts

+59-58
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,65 @@ module ts {
358358

359359
var aliasDeclarationEmitInfo: AliasDeclarationEmitInfo[] = [];
360360

361+
// Contains the reference paths that needs to go in the declaration file.
362+
// Collecting this separately because reference paths need to be first thing in the declaration file
363+
// and we could be collecting these paths from multiple files into single one with --out option
364+
var referencePathsOutput = "";
365+
366+
if (root) {
367+
// Emitting just a single file, so emit references in this file only
368+
if (!compilerOptions.noResolve) {
369+
var addedGlobalFileReference = false;
370+
forEach(root.referencedFiles, fileReference => {
371+
var referencedFile = tryResolveScriptReference(host, root, fileReference);
372+
373+
// All the references that are not going to be part of same file
374+
if (referencedFile && ((referencedFile.flags & NodeFlags.DeclarationFile) || // This is a declare file reference
375+
shouldEmitToOwnFile(referencedFile, compilerOptions) || // This is referenced file is emitting its own js file
376+
!addedGlobalFileReference)) { // Or the global out file corresponding to this reference was not added
377+
378+
writeReferencePath(referencedFile);
379+
if (!isExternalModuleOrDeclarationFile(referencedFile)) {
380+
addedGlobalFileReference = true;
381+
}
382+
}
383+
});
384+
}
385+
386+
emitNode(root);
387+
}
388+
else {
389+
// Emit references corresponding to this file
390+
var emittedReferencedFiles: SourceFile[] = [];
391+
forEach(host.getSourceFiles(), sourceFile => {
392+
if (!isExternalModuleOrDeclarationFile(sourceFile)) {
393+
// Check what references need to be added
394+
if (!compilerOptions.noResolve) {
395+
forEach(sourceFile.referencedFiles, fileReference => {
396+
var referencedFile = tryResolveScriptReference(host, sourceFile, fileReference);
397+
398+
// If the reference file is a declaration file or an external module, emit that reference
399+
if (referencedFile && (isExternalModuleOrDeclarationFile(referencedFile) &&
400+
!contains(emittedReferencedFiles, referencedFile))) { // If the file reference was not already emitted
401+
402+
writeReferencePath(referencedFile);
403+
emittedReferencedFiles.push(referencedFile);
404+
}
405+
});
406+
}
407+
408+
emitNode(sourceFile);
409+
}
410+
});
411+
}
412+
413+
return {
414+
reportedDeclarationError,
415+
aliasDeclarationEmitInfo,
416+
synchronousDeclarationOutput: writer.getText(),
417+
referencePathsOutput,
418+
}
419+
361420
function createAndSetNewTextWriterWithSymbolWriter(): EmitTextWriterWithSymbolWriter {
362421
var writer = <EmitTextWriterWithSymbolWriter>createTextWriter(newLine);
363422
writer.trackSymbol = trackSymbol;
@@ -1402,10 +1461,6 @@ module ts {
14021461
}
14031462
}
14041463

1405-
// Contains the reference paths that needs to go in the declaration file.
1406-
// Collecting this separately because reference paths need to be first thing in the declaration file
1407-
// and we could be collecting these paths from multiple files into single one with --out option
1408-
var referencePathsOutput = "";
14091464
function writeReferencePath(referencedFile: SourceFile) {
14101465
var declFileName = referencedFile.flags & NodeFlags.DeclarationFile
14111466
? referencedFile.filename // Declaration file, use declaration file name
@@ -1422,60 +1477,6 @@ module ts {
14221477

14231478
referencePathsOutput += "/// <reference path=\"" + declFileName + "\" />" + newLine;
14241479
}
1425-
1426-
if (root) {
1427-
// Emitting just a single file, so emit references in this file only
1428-
if (!compilerOptions.noResolve) {
1429-
var addedGlobalFileReference = false;
1430-
forEach(root.referencedFiles, fileReference => {
1431-
var referencedFile = tryResolveScriptReference(host, root, fileReference);
1432-
1433-
// All the references that are not going to be part of same file
1434-
if (referencedFile && ((referencedFile.flags & NodeFlags.DeclarationFile) || // This is a declare file reference
1435-
shouldEmitToOwnFile(referencedFile, compilerOptions) || // This is referenced file is emitting its own js file
1436-
!addedGlobalFileReference)) { // Or the global out file corresponding to this reference was not added
1437-
1438-
writeReferencePath(referencedFile);
1439-
if (!isExternalModuleOrDeclarationFile(referencedFile)) {
1440-
addedGlobalFileReference = true;
1441-
}
1442-
}
1443-
});
1444-
}
1445-
1446-
emitNode(root);
1447-
}
1448-
else {
1449-
// Emit references corresponding to this file
1450-
var emittedReferencedFiles: SourceFile[] = [];
1451-
forEach(host.getSourceFiles(), sourceFile => {
1452-
if (!isExternalModuleOrDeclarationFile(sourceFile)) {
1453-
// Check what references need to be added
1454-
if (!compilerOptions.noResolve) {
1455-
forEach(sourceFile.referencedFiles, fileReference => {
1456-
var referencedFile = tryResolveScriptReference(host, sourceFile, fileReference);
1457-
1458-
// If the reference file is a declaration file or an external module, emit that reference
1459-
if (referencedFile && (isExternalModuleOrDeclarationFile(referencedFile) &&
1460-
!contains(emittedReferencedFiles, referencedFile))) { // If the file reference was not already emitted
1461-
1462-
writeReferencePath(referencedFile);
1463-
emittedReferencedFiles.push(referencedFile);
1464-
}
1465-
});
1466-
}
1467-
1468-
emitNode(sourceFile);
1469-
}
1470-
});
1471-
}
1472-
1473-
return {
1474-
reportedDeclarationError,
1475-
aliasDeclarationEmitInfo,
1476-
synchronousDeclarationOutput: writer.getText(),
1477-
referencePathsOutput,
1478-
}
14791480
}
14801481

14811482
export function getDeclarationDiagnostics(host: EmitHost, resolver: EmitResolver, targetSourceFile: SourceFile): Diagnostic[] {

0 commit comments

Comments
 (0)