Skip to content

Commit b379840

Browse files
author
Yui
committed
Merge pull request #840 from Microsoft/getEmitOutputExternalModule
Get emit output external module
2 parents 0625cc4 + 530a5c0 commit b379840

File tree

7 files changed

+87
-35
lines changed

7 files changed

+87
-35
lines changed

src/compiler/emitter.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3231,21 +3231,27 @@ module ts {
32313231
}
32323232

32333233
if (targetSourceFile === undefined) {
3234+
// No targetSourceFile is specified (e.g. calling emitter from batch compiler)
32343235
forEach(program.getSourceFiles(), sourceFile => {
32353236
if (shouldEmitToOwnFile(sourceFile, compilerOptions)) {
32363237
var jsFilePath = getOwnEmitOutputFilePath(sourceFile, ".js");
32373238
emitFile(jsFilePath, sourceFile);
32383239
}
32393240
});
3240-
}
3241-
else {
3242-
// Emit only one file specified in targetFilename. This is mainly used in compilerOnSave feature
3243-
var jsFilePath = getOwnEmitOutputFilePath(targetSourceFile, ".js");
3244-
emitFile(jsFilePath, targetSourceFile);
3245-
}
32463241

3247-
if (compilerOptions.out) {
3248-
emitFile(compilerOptions.out);
3242+
if (compilerOptions.out) {
3243+
emitFile(compilerOptions.out);
3244+
}
3245+
} else {
3246+
// targetSourceFile is specified (e.g calling emitter from language service)
3247+
if (shouldEmitToOwnFile(targetSourceFile, compilerOptions)) {
3248+
// If shouldEmitToOwnFile is true or targetSourceFile is an external module file, then emit targetSourceFile in its own output file
3249+
var jsFilePath = getOwnEmitOutputFilePath(targetSourceFile, ".js");
3250+
emitFile(jsFilePath, targetSourceFile);
3251+
} else {
3252+
// If shouldEmitToOwnFile is false, then emit all, non-external-module file, into one single output file
3253+
emitFile(compilerOptions.out);
3254+
}
32493255
}
32503256

32513257
// Sort and make the unique list of diagnostics

src/services/services.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3882,7 +3882,8 @@ module ts {
38823882
filename = TypeScript.switchToForwardSlashes(filename);
38833883
var compilerOptions = program.getCompilerOptions();
38843884
var targetSourceFile = program.getSourceFile(filename); // Current selected file to be output
3885-
var emitToSingleFile = ts.shouldEmitToOwnFile(targetSourceFile, compilerOptions);
3885+
// If --out flag is not specified, shouldEmitToOwnFile is true. Otherwise shouldEmitToOwnFile is false.
3886+
var shouldEmitToOwnFile = ts.shouldEmitToOwnFile(targetSourceFile, compilerOptions);
38863887
var emitDeclaration = compilerOptions.declaration;
38873888
var emitOutput: EmitOutput = {
38883889
outputFiles: [],
@@ -3903,7 +3904,7 @@ module ts {
39033904
var syntacticDiagnostics: Diagnostic[] = [];
39043905
var containSyntacticErrors = false;
39053906

3906-
if (emitToSingleFile) {
3907+
if (shouldEmitToOwnFile) {
39073908
// Check only the file we want to emit
39083909
containSyntacticErrors = containErrors(program.getDiagnostics(targetSourceFile));
39093910
} else {
@@ -3930,7 +3931,7 @@ module ts {
39303931
// Perform semantic and force a type check before emit to ensure that all symbols are updated
39313932
// EmitFiles will report if there is an error from TypeChecker and Emitter
39323933
// 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
3933-
var emitFilesResult = emitToSingleFile ? getFullTypeCheckChecker().emitFiles(targetSourceFile) : getFullTypeCheckChecker().emitFiles();
3934+
var emitFilesResult = getFullTypeCheckChecker().emitFiles(targetSourceFile);
39343935
emitOutput.emitOutputStatus = emitFilesResult.emitResultStatus;
39353936

39363937
// 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
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
EmitOutputStatus : Succeeded
2+
Filename : declSingleFile.js
3+
var x = 5;
4+
var Bar = (function () {
5+
function Bar() {
6+
}
7+
return Bar;
8+
})();
9+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
EmitOutputStatus : JSGeneratedWithSemanticErrors
2+
Filename : declSingleFile.js
3+
var x = 5;
4+
var Bar = (function () {
5+
function Bar() {
6+
}
7+
return Bar;
8+
})();
9+
var x = "world";
10+
var Bar2 = (function () {
11+
function Bar2() {
12+
}
13+
return Bar2;
14+
})();
15+

tests/baselines/reference/getEmitOutputSingleFile2.baseline

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,4 @@ exports.bar = "hello world";
55
Filename : tests/cases/fourslash/inputFile3.d.ts
66
export declare var foo: number;
77
export declare var bar: string;
8-
Filename : declSingleFile.js
9-
var x = 5;
10-
var Bar = (function () {
11-
function Bar() {
12-
}
13-
return Bar;
14-
})();
15-
var x1 = "hello world";
16-
var Foo = (function () {
17-
function Foo() {
18-
}
19-
return Foo;
20-
})();
21-
Filename : declSingleFile.d.ts
22-
declare var x: number;
23-
declare class Bar {
24-
x: string;
25-
y: number;
26-
}
27-
declare var x1: string;
28-
declare class Foo {
29-
x: string;
30-
y: number;
31-
}
328

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @BaselineFile: getEmitOutputExternalModule.baseline
4+
// @out: declSingleFile.js
5+
6+
// @Filename: inputFile1.ts
7+
// @emitThisFile: true
8+
//// var x: number = 5;
9+
//// class Bar {
10+
//// x : string;
11+
//// y : number
12+
//// }
13+
14+
// @Filename: inputFile2.ts
15+
//// export module M {
16+
//// class C {c}
17+
//// }
18+
19+
verify.baselineGetEmitOutput();
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @BaselineFile: getEmitOutputExternalModule2.baseline
4+
// @out: declSingleFile.js
5+
6+
// @Filename: inputFile1.ts
7+
//// var x: number = 5;
8+
//// class Bar {
9+
//// x : string;
10+
//// y : number
11+
//// }
12+
13+
// @Filename: inputFile2.ts
14+
// @emitThisFile: true
15+
//// var x: string = "world";
16+
//// class Bar2 {
17+
//// x : string;
18+
//// y : number
19+
//// }
20+
21+
// @Filename: inputFile3.ts
22+
//// export module M {
23+
//// class C {c}
24+
//// }
25+
26+
verify.baselineGetEmitOutput();

0 commit comments

Comments
 (0)