Skip to content

Commit 35c9ce1

Browse files
author
Yui
committed
Merge pull request #856 from Microsoft/fixThrowExceptionOnEmitFile
Fix throw exception on emit file
2 parents c5c0576 + 016c3a0 commit 35c9ce1

8 files changed

+105
-8
lines changed

src/compiler/emitter.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ module ts {
2828
}
2929

3030
export function shouldEmitToOwnFile(sourceFile: SourceFile, compilerOptions: CompilerOptions): boolean {
31-
if (!(sourceFile.flags & NodeFlags.DeclarationFile)) {
31+
if (!isDeclarationFile(sourceFile)) {
3232
if ((isExternalModule(sourceFile) || !compilerOptions.out) && !fileExtensionIs(sourceFile.filename, ".js")) {
3333
return true;
3434
}
@@ -38,7 +38,7 @@ module ts {
3838
}
3939

4040
export function isExternalModuleOrDeclarationFile(sourceFile: SourceFile) {
41-
return isExternalModule(sourceFile) || (sourceFile.flags & NodeFlags.DeclarationFile) !== 0;
41+
return isExternalModule(sourceFile) || isDeclarationFile(sourceFile);
4242
}
4343

4444
// targetSourceFile is when users only want one file in entire project to be emitted. This is used in compilerOnSave feature
@@ -3253,14 +3253,17 @@ module ts {
32533253
if (compilerOptions.out) {
32543254
emitFile(compilerOptions.out);
32553255
}
3256-
} else {
3257-
// targetSourceFile is specified (e.g calling emitter from language service)
3256+
}
3257+
else {
3258+
// targetSourceFile is specified (e.g calling emitter from language service or calling getSemanticDiagnostic from language service)
32583259
if (shouldEmitToOwnFile(targetSourceFile, compilerOptions)) {
3259-
// If shouldEmitToOwnFile is true or targetSourceFile is an external module file, then emit targetSourceFile in its own output file
3260+
// If shouldEmitToOwnFile returns true or targetSourceFile is an external module file, then emit targetSourceFile in its own output file
32603261
var jsFilePath = getOwnEmitOutputFilePath(targetSourceFile, ".js");
32613262
emitFile(jsFilePath, targetSourceFile);
3262-
} else {
3263-
// If shouldEmitToOwnFile is false, then emit all, non-external-module file, into one single output file
3263+
}
3264+
else if (!isDeclarationFile(targetSourceFile) && compilerOptions.out) {
3265+
// Otherwise, if --out is specified and targetSourceFile is not a declaration file,
3266+
// Emit all, non-external-module file, into one single output file
32643267
emitFile(compilerOptions.out);
32653268
}
32663269
}

src/compiler/parser.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,10 @@ module ts {
111111
return file.externalModuleIndicator !== undefined;
112112
}
113113

114+
export function isDeclarationFile(file: SourceFile): boolean {
115+
return (file.flags & NodeFlags.DeclarationFile) !== 0;
116+
}
117+
114118
export function isPrologueDirective(node: Node): boolean {
115119
return node.kind === SyntaxKind.ExpressionStatement && (<ExpressionStatement>node).expression.kind === SyntaxKind.StringLiteral;
116120
}
@@ -4031,7 +4035,7 @@ module ts {
40314035
}
40324036
}
40334037
}
4034-
else if (node.kind === SyntaxKind.ModuleDeclaration && (<ModuleDeclaration>node).name.kind === SyntaxKind.StringLiteral && (node.flags & NodeFlags.Ambient || file.flags & NodeFlags.DeclarationFile)) {
4038+
else if (node.kind === SyntaxKind.ModuleDeclaration && (<ModuleDeclaration>node).name.kind === SyntaxKind.StringLiteral && (node.flags & NodeFlags.Ambient || isDeclarationFile(file))) {
40354039
// TypeScript 1.0 spec (April 2014): 12.1.6
40364040
// An AmbientExternalModuleDeclaration declares an external module.
40374041
// This type of declaration is permitted only in the global module.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
EmitOutputStatus : Succeeded
2+
3+
EmitOutputStatus : Succeeded
4+
Filename : tests/cases/fourslash/inputFile2.js
5+
var x1 = "hello world";
6+
var Foo = (function () {
7+
function Foo() {
8+
}
9+
return Foo;
10+
})();
11+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
EmitOutputStatus : Succeeded
2+
3+
EmitOutputStatus : Succeeded
4+
Filename : tests/cases/fourslash/inputFile2.js
5+
var Foo = (function () {
6+
function Foo() {
7+
}
8+
return Foo;
9+
})();
10+
exports.Foo = Foo;
11+
12+
EmitOutputStatus : Succeeded
13+
Filename : tests/cases/fourslash/inputFile3.js
14+
var x = "hello";
15+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
EmitOutputStatus : Succeeded
2+
Filename : declSingle.js
3+
var x = "hello";
4+
var x1 = 1000;
5+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @BaselineFile: getEmitOutputWithDeclarationFile.baseline
4+
5+
// @Filename: decl.d.ts
6+
// @emitThisFile: true
7+
//// interface I { a: string; }
8+
9+
// @Filename: inputFile2.ts
10+
// @emitThisFile: true
11+
//// var x1: string = "hello world";
12+
//// class Foo{
13+
//// x : string;
14+
//// y : number;
15+
//// }
16+
17+
debugger;
18+
verify.baselineGetEmitOutput();
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @BaselineFile: getEmitOutputWithDeclarationFile2.baseline
4+
5+
// @Filename: decl.d.ts
6+
// @emitThisFile: true
7+
//// interface I { a: string; }
8+
9+
// @Filename: inputFile2.ts
10+
// @emitThisFile: true
11+
//// export class Foo { }
12+
13+
// @Filename: inputFile3.ts
14+
// @emitThisFile: true
15+
//// var x:string = "hello";
16+
17+
debugger;
18+
verify.baselineGetEmitOutput();
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @BaselineFile: getEmitOutputWithDeclarationFile3.baseline
4+
// @out: declSingle.js
5+
6+
// @Filename: decl.d.ts
7+
//// interface I { a: string; }
8+
9+
// @Filename: inputFile2.ts
10+
//// export class Foo { }
11+
12+
// @Filename: inputFile3.ts
13+
// @emitThisFile: true
14+
//// var x:string = "hello";
15+
16+
// @Filename: inputFile4.ts
17+
//// var x1:number = 1000;
18+
19+
// @Filename: inputFile5.js
20+
//// var x2 = 1000;
21+
22+
debugger;
23+
verify.baselineGetEmitOutput();

0 commit comments

Comments
 (0)