From be02f962c7f4324f608064b69ce80bb262cb830a Mon Sep 17 00:00:00 2001 From: Yui T Date: Mon, 6 Oct 2014 18:34:01 -0700 Subject: [PATCH 1/4] 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 From 747ae1b1f50fb966e4d4c24706fc4304516d5b94 Mon Sep 17 00:00:00 2001 From: Yui T Date: Mon, 6 Oct 2014 18:37:01 -0700 Subject: [PATCH 2/4] Add fourslash tests to cover external module case --- .../getEmitOutputExternalModule.baseline | 9 +++++++ .../getEmitOutputExternalModule2.baseline | 15 +++++++++++ .../getEmitOutputSingleFile2.baseline | 24 ----------------- .../fourslash/getEmitOutputExternalModule.ts | 19 ++++++++++++++ .../fourslash/getEmitOutputExternalModule2.ts | 26 +++++++++++++++++++ 5 files changed, 69 insertions(+), 24 deletions(-) create mode 100644 tests/baselines/reference/getEmitOutputExternalModule.baseline create mode 100644 tests/baselines/reference/getEmitOutputExternalModule2.baseline create mode 100644 tests/cases/fourslash/getEmitOutputExternalModule.ts create mode 100644 tests/cases/fourslash/getEmitOutputExternalModule2.ts 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 From d345d228a7fb153d4f1bab3ae75bfa05b308560f Mon Sep 17 00:00:00 2001 From: Yui T Date: Tue, 7 Oct 2014 10:17:29 -0700 Subject: [PATCH 3/4] Fix spelling --- src/compiler/emitter.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index 74520b3fc6c..f7c9fa300a9 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -3242,7 +3242,7 @@ module ts { } 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 + // 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 { From 7d725037b38fc4bd05a9639a84b6245198462989 Mon Sep 17 00:00:00 2001 From: Yui T Date: Tue, 7 Oct 2014 13:20:06 -0700 Subject: [PATCH 4/4] Address code review --- src/compiler/emitter.ts | 4 ++-- src/services/services.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index f7c9fa300a9..37442772078 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -3228,7 +3228,7 @@ module ts { } if (targetSourceFile === undefined) { - // No targetSourceFile is specified (i.e. calling emitter from batch compiler) + // No targetSourceFile is specified (e.g. calling emitter from batch compiler) forEach(program.getSourceFiles(), sourceFile => { if (shouldEmitToOwnFile(sourceFile, compilerOptions)) { var jsFilePath = getOwnEmitOutputFilePath(sourceFile, ".js"); @@ -3240,7 +3240,7 @@ module ts { emitFile(compilerOptions.out); } } else { - // targetSourceFile is specified (i.e. calling emitter from language service) + // 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"); diff --git a/src/services/services.ts b/src/services/services.ts index bd222cb8240..b00c2b703e8 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -3930,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 = getFullTypeCheckChecker().emitFiles(targetSourceFile);; + 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