@@ -25,15 +25,21 @@ module ts {
25
25
return indentStrings [ 1 ] . length ;
26
26
}
27
27
28
- export function shouldEmitToOwnFile ( sourceFile : SourceFile , compilerOptions : CompilerOptions ) {
28
+ export function shouldEmitToOwnFile ( sourceFile : SourceFile , compilerOptions : CompilerOptions ) : boolean {
29
29
if ( ! ( sourceFile . flags & NodeFlags . DeclarationFile ) ) {
30
30
if ( ( isExternalModule ( sourceFile ) || ! compilerOptions . out ) && ! fileExtensionIs ( sourceFile . filename , ".js" ) ) {
31
31
return true ;
32
32
}
33
+ return false ;
33
34
}
35
+ return false ;
36
+ }
37
+ export function isExternalModuleOrDeclarationFile ( sourceFile : SourceFile ) {
38
+ return isExternalModule ( sourceFile ) || ( sourceFile . flags & NodeFlags . DeclarationFile ) !== 0 ;
34
39
}
35
40
36
- export function emitFiles ( resolver : EmitResolver ) : EmitResult {
41
+ // targetSourceFile is when users only want one file in entire project to be emitted. This is used in compilerOnSave feature
42
+ export function emitFiles ( resolver : EmitResolver , targetSourceFile ?: SourceFile ) : EmitResult {
37
43
var program = resolver . getProgram ( ) ;
38
44
var compilerHost = program . getCompilerHost ( ) ;
39
45
var compilerOptions = program . getCompilerOptions ( ) ;
@@ -58,10 +64,6 @@ module ts {
58
64
return emitOutputFilePathWithoutExtension + extension ;
59
65
}
60
66
61
- function isExternalModuleOrDeclarationFile ( sourceFile : SourceFile ) {
62
- return isExternalModule ( sourceFile ) || ( sourceFile . flags & NodeFlags . DeclarationFile ) !== 0 ;
63
- }
64
-
65
67
function getFirstConstructorWithBody ( node : ClassDeclaration ) : ConstructorDeclaration {
66
68
return forEach ( node . members , member => {
67
69
if ( member . kind === SyntaxKind . Constructor && ( < ConstructorDeclaration > member ) . body ) {
@@ -3161,44 +3163,50 @@ module ts {
3161
3163
}
3162
3164
}
3163
3165
3164
- var shouldEmitDeclarations = resolver . shouldEmitDeclarations ( ) ;
3166
+ var hasSemanticErros = resolver . hasSemanticErrors ( ) ;
3167
+ var returnCode = EmitReturnStatus . Succeeded ;
3168
+
3165
3169
function emitFile ( jsFilePath : string , sourceFile ?: SourceFile ) {
3166
3170
emitJavaScript ( jsFilePath , sourceFile ) ;
3167
- if ( shouldEmitDeclarations ) {
3171
+ // Update the returnCode with appropriate value depended on whether we have semantic errors
3172
+ if ( ! hasSemanticErros && compilerOptions . declaration ) {
3173
+ returnCode = EmitReturnStatus . Succeeded ;
3168
3174
emitDeclarations ( jsFilePath , sourceFile ) ;
3169
3175
}
3176
+ else if ( hasSemanticErros && compilerOptions . declaration ) {
3177
+ returnCode = EmitReturnStatus . DeclarationGenerationSkipped ;
3178
+ }
3179
+ else if ( hasSemanticErros && ! compilerOptions . declaration ) {
3180
+ returnCode = EmitReturnStatus . JSGeneratedWithSemanticErrors ;
3181
+ }
3182
+ }
3183
+
3184
+ if ( targetSourceFile === undefined ) {
3185
+ forEach ( program . getSourceFiles ( ) , sourceFile => {
3186
+ if ( shouldEmitToOwnFile ( sourceFile , compilerOptions ) ) {
3187
+ var jsFilePath = getOwnEmitOutputFilePath ( sourceFile , ".js" ) ;
3188
+ emitFile ( jsFilePath , sourceFile ) ;
3189
+ }
3190
+ } ) ;
3191
+ }
3192
+ else {
3193
+ // Emit only one file specified in targetFilename. This is mainly used in compilerOnSave feature
3194
+ var jsFilePath = getOwnEmitOutputFilePath ( targetSourceFile , ".js" ) ;
3195
+ emitFile ( jsFilePath , targetSourceFile ) ;
3170
3196
}
3171
3197
3172
- forEach ( program . getSourceFiles ( ) , sourceFile => {
3173
- if ( shouldEmitToOwnFile ( sourceFile , compilerOptions ) ) {
3174
- var jsFilePath = getOwnEmitOutputFilePath ( sourceFile , ".js" ) ;
3175
- emitFile ( jsFilePath , sourceFile ) ;
3176
- }
3177
- } ) ;
3178
3198
if ( compilerOptions . out ) {
3179
3199
emitFile ( compilerOptions . out ) ;
3180
3200
}
3181
-
3201
+
3182
3202
// Sort and make the unique list of diagnostics
3183
3203
diagnostics . sort ( compareDiagnostics ) ;
3184
3204
diagnostics = deduplicateSortedDiagnostics ( diagnostics ) ;
3185
3205
3186
- var returnCode = EmitReturnStatus . Succeeded ;
3187
-
3188
- // Check if there is any diagnostic in an error category; if so, there is an emitter error
3206
+ // Update returnCode if there is any EmitterError
3189
3207
var hasEmitterError = forEach ( diagnostics , diagnostic => diagnostic . category === DiagnosticCategory . Error ) ;
3190
3208
3191
- if ( resolver . hasSemanticErrors ( ) && ! compilerOptions . declaration ) {
3192
- // There is an semantic errror when output javascript file
3193
- // Output JS file with semantic error
3194
- returnCode = EmitReturnStatus . JSGeneratedWithSemanticErrors ;
3195
- }
3196
- else if ( resolver . hasSemanticErrors ( ) && compilerOptions . declaration ) {
3197
- // There is an semantic errror when output javascript and declaration file
3198
- // Output JS file with semantic error, not output declaration file
3199
- returnCode = EmitReturnStatus . DeclarationGenerationSkipped ;
3200
- }
3201
- else if ( hasEmitterError ) {
3209
+ if ( hasEmitterError ) {
3202
3210
returnCode = EmitReturnStatus . EmitErrorsEncountered ;
3203
3211
}
3204
3212
0 commit comments