From be02f962c7f4324f608064b69ce80bb262cb830a Mon Sep 17 00:00:00 2001 From: Yui T Date: Mon, 6 Oct 2014 18:34:01 -0700 Subject: [PATCH] Fix compileOnSave with external module --- src/compiler/emitter.ts | 22 ++++++++++++++-------- src/services/services.ts | 7 ++++--- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index fb40117fb9b..74520b3fc6c 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -3228,21 +3228,27 @@ module ts { } if (targetSourceFile === undefined) { + // No targetSourceFile is specified (i.e. calling emitter from batch compiler) forEach(program.getSourceFiles(), sourceFile => { if (shouldEmitToOwnFile(sourceFile, compilerOptions)) { var jsFilePath = getOwnEmitOutputFilePath(sourceFile, ".js"); emitFile(jsFilePath, sourceFile); } }); - } - else { - // Emit only one file specified in targetFilename. This is mainly used in compilerOnSave feature - var jsFilePath = getOwnEmitOutputFilePath(targetSourceFile, ".js"); - emitFile(jsFilePath, targetSourceFile); - } - if (compilerOptions.out) { - emitFile(compilerOptions.out); + if (compilerOptions.out) { + emitFile(compilerOptions.out); + } + } else { + // targetSourceFile is specified (i.e. calling emitter from language service) + if (shouldEmitToOwnFile(targetSourceFile, compilerOptions)) { + // If shouldEmitToOwnFile is true or targetSouceFile is an external module file, then emit targetSourceFile in its own output file + var jsFilePath = getOwnEmitOutputFilePath(targetSourceFile, ".js"); + emitFile(jsFilePath, targetSourceFile); + } else { + // If shouldEmitToOwnFile is false, then emit all, non-external-module file, into one single output file + emitFile(compilerOptions.out); + } } // Sort and make the unique list of diagnostics diff --git a/src/services/services.ts b/src/services/services.ts index 6e49e8427e5..bd222cb8240 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -3881,7 +3881,8 @@ module ts { filename = TypeScript.switchToForwardSlashes(filename); var compilerOptions = program.getCompilerOptions(); var targetSourceFile = program.getSourceFile(filename); // Current selected file to be output - var emitToSingleFile = ts.shouldEmitToOwnFile(targetSourceFile, compilerOptions); + // If --out flag is not specified, shouldEmitToOwnFile is true. Otherwise shouldEmitToOwnFile is false. + var shouldEmitToOwnFile = ts.shouldEmitToOwnFile(targetSourceFile, compilerOptions); var emitDeclaration = compilerOptions.declaration; var emitOutput: EmitOutput = { outputFiles: [], @@ -3902,7 +3903,7 @@ module ts { var syntacticDiagnostics: Diagnostic[] = []; var containSyntacticErrors = false; - if (emitToSingleFile) { + if (shouldEmitToOwnFile) { // Check only the file we want to emit containSyntacticErrors = containErrors(program.getDiagnostics(targetSourceFile)); } else { @@ -3929,7 +3930,7 @@ module ts { // Perform semantic and force a type check before emit to ensure that all symbols are updated // EmitFiles will report if there is an error from TypeChecker and Emitter // Depend whether we will have to emit into a single file or not either emit only selected file in the project, emit all files into a single file - var emitFilesResult = emitToSingleFile ? getFullTypeCheckChecker().emitFiles(targetSourceFile) : getFullTypeCheckChecker().emitFiles(); + var emitFilesResult = getFullTypeCheckChecker().emitFiles(targetSourceFile);; emitOutput.emitOutputStatus = emitFilesResult.emitResultStatus; // Reset writer back to undefined to make sure that we produce an error message if CompilerHost.writeFile method is called when we are not in getEmitOutput