7
7
"github.com/microsoft/typescript-go/internal/ast"
8
8
"github.com/microsoft/typescript-go/internal/core"
9
9
"github.com/microsoft/typescript-go/internal/diagnostics"
10
+ "github.com/microsoft/typescript-go/internal/outputpaths"
10
11
"github.com/microsoft/typescript-go/internal/printer"
11
12
"github.com/microsoft/typescript-go/internal/sourcemap"
12
13
"github.com/microsoft/typescript-go/internal/stringutil"
@@ -32,15 +33,15 @@ type emitter struct {
32
33
emitSkipped bool
33
34
sourceMapDataList []* SourceMapEmitResult
34
35
writer printer.EmitTextWriter
35
- paths * outputPaths
36
+ paths * outputpaths. OutputPaths
36
37
sourceFile * ast.SourceFile
37
38
}
38
39
39
40
func (e * emitter ) emit () {
40
41
// !!! tracing
41
- e .emitJSFile (e .sourceFile , e .paths .jsFilePath , e .paths .sourceMapFilePath )
42
- e .emitDeclarationFile (e .sourceFile , e .paths .declarationFilePath , e .paths .declarationMapPath )
43
- e .emitBuildInfo (e .paths .buildInfoPath )
42
+ e .emitJSFile (e .sourceFile , e .paths .JsFilePath () , e .paths .SourceMapFilePath () )
43
+ e .emitDeclarationFile (e .sourceFile , e .paths .DeclarationFilePath () , e .paths .DeclarationMapPath () )
44
+ e .emitBuildInfo (e .paths .BuildInfoPath () )
44
45
}
45
46
46
47
func (e * emitter ) getDeclarationTransformers (emitContext * printer.EmitContext , sourceFile * ast.SourceFile , declarationFilePath string , declarationMapPath string ) []* declarations.DeclarationTransformer {
@@ -207,44 +208,6 @@ func (e *emitter) printSourceFile(jsFilePath string, sourceMapFilePath string, s
207
208
return ! data .SkippedDtsWrite
208
209
}
209
210
210
- func getSourceFilePathInNewDir (fileName string , newDirPath string , currentDirectory string , commonSourceDirectory string , useCaseSensitiveFileNames bool ) string {
211
- sourceFilePath := tspath .GetNormalizedAbsolutePath (fileName , currentDirectory )
212
- commonSourceDirectory = tspath .EnsureTrailingDirectorySeparator (commonSourceDirectory )
213
- isSourceFileInCommonSourceDirectory := tspath .ContainsPath (commonSourceDirectory , sourceFilePath , tspath.ComparePathsOptions {
214
- UseCaseSensitiveFileNames : useCaseSensitiveFileNames ,
215
- CurrentDirectory : currentDirectory ,
216
- })
217
- if isSourceFileInCommonSourceDirectory {
218
- sourceFilePath = sourceFilePath [len (commonSourceDirectory ):]
219
- }
220
- return tspath .CombinePaths (newDirPath , sourceFilePath )
221
- }
222
-
223
- func getOwnEmitOutputFilePath (fileName string , host printer.EmitHost , extension string ) string {
224
- compilerOptions := host .Options ()
225
- var emitOutputFilePathWithoutExtension string
226
- if len (compilerOptions .OutDir ) > 0 {
227
- currentDirectory := host .GetCurrentDirectory ()
228
- emitOutputFilePathWithoutExtension = tspath .RemoveFileExtension (getSourceFilePathInNewDir (
229
- fileName ,
230
- compilerOptions .OutDir ,
231
- currentDirectory ,
232
- host .CommonSourceDirectory (),
233
- host .UseCaseSensitiveFileNames (),
234
- ))
235
- } else {
236
- emitOutputFilePathWithoutExtension = tspath .RemoveFileExtension (fileName )
237
- }
238
- return emitOutputFilePathWithoutExtension + extension
239
- }
240
-
241
- func getSourceMapFilePath (jsFilePath string , options * core.CompilerOptions ) string {
242
- if options .SourceMap .IsTrue () && ! options .InlineSourceMap .IsTrue () {
243
- return jsFilePath + ".map"
244
- }
245
- return ""
246
- }
247
-
248
211
func shouldEmitSourceMaps (mapOptions * core.CompilerOptions , sourceFile * ast.SourceFile ) bool {
249
212
return (mapOptions .SourceMap .IsTrue () || mapOptions .InlineSourceMap .IsTrue ()) &&
250
213
! tspath .FileExtensionIs (sourceFile .FileName (), tspath .ExtensionJson )
@@ -274,7 +237,7 @@ func (e *emitter) getSourceMapDirectory(mapOptions *core.CompilerOptions, filePa
274
237
if sourceFile != nil {
275
238
// For modules or multiple emit files the mapRoot will have directory structure like the sources
276
239
// So if src\a.ts and src\lib\b.ts are compiled together user would be moving the maps into mapRoot\a.js.map and mapRoot\lib\b.js.map
277
- sourceMapDir = tspath .GetDirectoryPath (getSourceFilePathInNewDir (
240
+ sourceMapDir = tspath .GetDirectoryPath (outputpaths . GetSourceFilePathInNewDir (
278
241
sourceFile .FileName (),
279
242
sourceMapDir ,
280
243
e .host .GetCurrentDirectory (),
@@ -305,7 +268,7 @@ func (e *emitter) getSourceMappingURL(mapOptions *core.CompilerOptions, sourceMa
305
268
if sourceFile != nil {
306
269
// For modules or multiple emit files the mapRoot will have directory structure like the sources
307
270
// So if src\a.ts and src\lib\b.ts are compiled together user would be moving the maps into mapRoot\a.js.map and mapRoot\lib\b.js.map
308
- sourceMapDir = tspath .GetDirectoryPath (getSourceFilePathInNewDir (
271
+ sourceMapDir = tspath .GetDirectoryPath (outputpaths . GetSourceFilePathInNewDir (
309
272
sourceFile .FileName (),
310
273
sourceMapDir ,
311
274
e .host .GetCurrentDirectory (),
@@ -334,91 +297,6 @@ func (e *emitter) getSourceMappingURL(mapOptions *core.CompilerOptions, sourceMa
334
297
return stringutil .EncodeURI (sourceMapFile )
335
298
}
336
299
337
- func getDeclarationEmitOutputFilePath (file string , host EmitHost ) string {
338
- options := host .Options ()
339
- var outputDir * string
340
- if len (options .DeclarationDir ) > 0 {
341
- outputDir = & options .DeclarationDir
342
- } else if len (options .OutDir ) > 0 {
343
- outputDir = & options .OutDir
344
- }
345
-
346
- var path string
347
- if outputDir != nil {
348
- path = getSourceFilePathInNewDirWorker (file , * outputDir , host .GetCurrentDirectory (), host .CommonSourceDirectory (), host .UseCaseSensitiveFileNames ())
349
- } else {
350
- path = file
351
- }
352
- declarationExtension := tspath .GetDeclarationEmitExtensionForPath (path )
353
- return tspath .RemoveFileExtension (path ) + declarationExtension
354
- }
355
-
356
- func getSourceFilePathInNewDirWorker (fileName string , newDirPath string , currentDirectory string , commonSourceDirectory string , useCaseSensitiveFileNames bool ) string {
357
- sourceFilePath := tspath .GetNormalizedAbsolutePath (fileName , currentDirectory )
358
- commonDir := tspath .GetCanonicalFileName (commonSourceDirectory , useCaseSensitiveFileNames )
359
- canonFile := tspath .GetCanonicalFileName (sourceFilePath , useCaseSensitiveFileNames )
360
- isSourceFileInCommonSourceDirectory := strings .HasPrefix (canonFile , commonDir )
361
- if isSourceFileInCommonSourceDirectory {
362
- sourceFilePath = sourceFilePath [len (commonSourceDirectory ):]
363
- }
364
- return tspath .CombinePaths (newDirPath , sourceFilePath )
365
- }
366
-
367
- type outputPaths struct {
368
- jsFilePath string
369
- sourceMapFilePath string
370
- declarationFilePath string
371
- declarationMapPath string
372
- buildInfoPath string
373
- }
374
-
375
- // DeclarationFilePath implements declarations.OutputPaths.
376
- func (o * outputPaths ) DeclarationFilePath () string {
377
- return o .declarationFilePath
378
- }
379
-
380
- // JsFilePath implements declarations.OutputPaths.
381
- func (o * outputPaths ) JsFilePath () string {
382
- return o .jsFilePath
383
- }
384
-
385
- func getOutputPathsFor (sourceFile * ast.SourceFile , host EmitHost , forceDtsEmit bool ) * outputPaths {
386
- options := host .Options ()
387
- // !!! bundle not implemented, may be deprecated
388
- ownOutputFilePath := getOwnEmitOutputFilePath (sourceFile .FileName (), host , core .GetOutputExtension (sourceFile .FileName (), options .Jsx ))
389
- isJsonFile := ast .IsJsonSourceFile (sourceFile )
390
- // If json file emits to the same location skip writing it, if emitDeclarationOnly skip writing it
391
- isJsonEmittedToSameLocation := isJsonFile &&
392
- tspath .ComparePaths (sourceFile .FileName (), ownOutputFilePath , tspath.ComparePathsOptions {
393
- CurrentDirectory : host .GetCurrentDirectory (),
394
- UseCaseSensitiveFileNames : host .UseCaseSensitiveFileNames (),
395
- }) == 0
396
- paths := & outputPaths {}
397
- if options .EmitDeclarationOnly != core .TSTrue && ! isJsonEmittedToSameLocation {
398
- paths .jsFilePath = ownOutputFilePath
399
- if ! ast .IsJsonSourceFile (sourceFile ) {
400
- paths .sourceMapFilePath = getSourceMapFilePath (paths .jsFilePath , options )
401
- }
402
- }
403
- if forceDtsEmit || options .GetEmitDeclarations () && ! isJsonFile {
404
- paths .declarationFilePath = getDeclarationEmitOutputFilePath (sourceFile .FileName (), host )
405
- if options .GetAreDeclarationMapsEnabled () {
406
- paths .declarationMapPath = paths .declarationFilePath + ".map"
407
- }
408
- }
409
- return paths
410
- }
411
-
412
- func forEachEmittedFile (host EmitHost , action func (emitFileNames * outputPaths , sourceFile * ast.SourceFile ) bool , sourceFiles []* ast.SourceFile , options * EmitOptions ) bool {
413
- // !!! outFile not yet implemented, may be deprecated
414
- for _ , sourceFile := range sourceFiles {
415
- if action (getOutputPathsFor (sourceFile , host , options .forceDtsEmit ), sourceFile ) {
416
- return true
417
- }
418
- }
419
- return false
420
- }
421
-
422
300
func sourceFileMayBeEmitted (sourceFile * ast.SourceFile , host printer.EmitHost , forceDtsEmit bool ) bool {
423
301
// !!! Js files are emitted only if option is enabled
424
302
0 commit comments