diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index ab6279f99d6..1e1c57f5700 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -3231,21 +3231,27 @@ module ts { } if (targetSourceFile === undefined) { + // No targetSourceFile is specified (e.g. 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 (e.g calling emitter from language service) + if (shouldEmitToOwnFile(targetSourceFile, compilerOptions)) { + // If shouldEmitToOwnFile is true or targetSourceFile 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 e4d28f4700f..4709f109f67 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -3882,7 +3882,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: [], @@ -3903,7 +3904,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 { @@ -3930,7 +3931,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 diff --git a/tests/baselines/reference/getEmitOutputExternalModule.baseline b/tests/baselines/reference/getEmitOutputExternalModule.baseline new file mode 100644 index 00000000000..b5e670c5fd7 --- /dev/null +++ b/tests/baselines/reference/getEmitOutputExternalModule.baseline @@ -0,0 +1,9 @@ +EmitOutputStatus : Succeeded +Filename : declSingleFile.js +var x = 5; +var Bar = (function () { + function Bar() { + } + return Bar; +})(); + diff --git a/tests/baselines/reference/getEmitOutputExternalModule2.baseline b/tests/baselines/reference/getEmitOutputExternalModule2.baseline new file mode 100644 index 00000000000..5c03a092257 --- /dev/null +++ b/tests/baselines/reference/getEmitOutputExternalModule2.baseline @@ -0,0 +1,15 @@ +EmitOutputStatus : JSGeneratedWithSemanticErrors +Filename : declSingleFile.js +var x = 5; +var Bar = (function () { + function Bar() { + } + return Bar; +})(); +var x = "world"; +var Bar2 = (function () { + function Bar2() { + } + return Bar2; +})(); + diff --git a/tests/baselines/reference/getEmitOutputSingleFile2.baseline b/tests/baselines/reference/getEmitOutputSingleFile2.baseline index 77fa6b9acdd..2f5dbe3daf8 100644 --- a/tests/baselines/reference/getEmitOutputSingleFile2.baseline +++ b/tests/baselines/reference/getEmitOutputSingleFile2.baseline @@ -5,28 +5,4 @@ exports.bar = "hello world"; Filename : tests/cases/fourslash/inputFile3.d.ts export declare var foo: number; export declare var bar: string; -Filename : declSingleFile.js -var x = 5; -var Bar = (function () { - function Bar() { - } - return Bar; -})(); -var x1 = "hello world"; -var Foo = (function () { - function Foo() { - } - return Foo; -})(); -Filename : declSingleFile.d.ts -declare var x: number; -declare class Bar { - x: string; - y: number; -} -declare var x1: string; -declare class Foo { - x: string; - y: number; -} diff --git a/tests/cases/fourslash/getEmitOutputExternalModule.ts b/tests/cases/fourslash/getEmitOutputExternalModule.ts new file mode 100644 index 00000000000..ef9fb348c57 --- /dev/null +++ b/tests/cases/fourslash/getEmitOutputExternalModule.ts @@ -0,0 +1,19 @@ +/// + +// @BaselineFile: getEmitOutputExternalModule.baseline +// @out: declSingleFile.js + +// @Filename: inputFile1.ts +// @emitThisFile: true +//// var x: number = 5; +//// class Bar { +//// x : string; +//// y : number +//// } + +// @Filename: inputFile2.ts +//// export module M { +//// class C {c} +//// } + +verify.baselineGetEmitOutput(); \ No newline at end of file diff --git a/tests/cases/fourslash/getEmitOutputExternalModule2.ts b/tests/cases/fourslash/getEmitOutputExternalModule2.ts new file mode 100644 index 00000000000..abecc219698 --- /dev/null +++ b/tests/cases/fourslash/getEmitOutputExternalModule2.ts @@ -0,0 +1,26 @@ +/// + +// @BaselineFile: getEmitOutputExternalModule2.baseline +// @out: declSingleFile.js + +// @Filename: inputFile1.ts +//// var x: number = 5; +//// class Bar { +//// x : string; +//// y : number +//// } + +// @Filename: inputFile2.ts +// @emitThisFile: true +//// var x: string = "world"; +//// class Bar2 { +//// x : string; +//// y : number +//// } + +// @Filename: inputFile3.ts +//// export module M { +//// class C {c} +//// } + +verify.baselineGetEmitOutput(); \ No newline at end of file