5
5
"strings"
6
6
7
7
"github.com/microsoft/typescript-go/internal/ast"
8
- "github.com/microsoft/typescript-go/internal/binder"
9
8
"github.com/microsoft/typescript-go/internal/core"
10
9
"github.com/microsoft/typescript-go/internal/diagnostics"
11
10
"github.com/microsoft/typescript-go/internal/printer"
@@ -25,7 +24,7 @@ const (
25
24
)
26
25
27
26
type emitter struct {
28
- host EmitHost
27
+ host printer. EmitHost
29
28
emitOnly emitOnly
30
29
emittedFilesList []string
31
30
emitterDiagnostics ast.DiagnosticsCollection
@@ -43,61 +42,6 @@ func (e *emitter) emit() {
43
42
e .emitBuildInfo (e .paths .buildInfoPath )
44
43
}
45
44
46
- func (e * emitter ) getModuleTransformer (emitContext * printer.EmitContext , resolver binder.ReferenceResolver , sourceFileMetaDataProvider printer.SourceFileMetaDataProvider ) * transformers.Transformer {
47
- options := e .host .Options ()
48
-
49
- switch options .GetEmitModuleKind () {
50
- case core .ModuleKindPreserve :
51
- // `ESModuleTransformer` contains logic for preserving CJS input syntax in `--module preserve`
52
- return transformers .NewESModuleTransformer (emitContext , options , resolver , sourceFileMetaDataProvider )
53
-
54
- case core .ModuleKindESNext ,
55
- core .ModuleKindES2022 ,
56
- core .ModuleKindES2020 ,
57
- core .ModuleKindES2015 ,
58
- core .ModuleKindNode16 ,
59
- core .ModuleKindNodeNext ,
60
- core .ModuleKindCommonJS :
61
- return transformers .NewImpliedModuleTransformer (emitContext , options , resolver , sourceFileMetaDataProvider )
62
-
63
- default :
64
- return transformers .NewCommonJSModuleTransformer (emitContext , options , resolver , sourceFileMetaDataProvider )
65
- }
66
- }
67
-
68
- func (e * emitter ) getScriptTransformers (emitContext * printer.EmitContext , sourceFile * ast.SourceFile ) []* transformers.Transformer {
69
- var tx []* transformers.Transformer
70
- options := e .host .Options ()
71
-
72
- // JS files don't use reference calculations as they don't do import elision, no need to calculate it
73
- importElisionEnabled := ! options .VerbatimModuleSyntax .IsTrue () && ! ast .IsInJSFile (sourceFile .AsNode ())
74
-
75
- var emitResolver printer.EmitResolver
76
- var referenceResolver binder.ReferenceResolver
77
- if importElisionEnabled {
78
- emitResolver = e .host .GetEmitResolver (sourceFile , false /*skipDiagnostics*/ ) // !!! conditionally skip diagnostics
79
- emitResolver .MarkLinkedReferencesRecursively (sourceFile )
80
- referenceResolver = emitResolver
81
- } else {
82
- referenceResolver = binder .NewReferenceResolver (options , binder.ReferenceResolverHooks {})
83
- }
84
-
85
- // erase types
86
- tx = append (tx , transformers .NewTypeEraserTransformer (emitContext , options ))
87
-
88
- // elide imports
89
- if importElisionEnabled {
90
- tx = append (tx , transformers .NewImportElisionTransformer (emitContext , options , emitResolver ))
91
- }
92
-
93
- // transform `enum`, `namespace`, and parameter properties
94
- tx = append (tx , transformers .NewRuntimeSyntaxTransformer (emitContext , options , referenceResolver ))
95
-
96
- // transform module syntax
97
- tx = append (tx , e .getModuleTransformer (emitContext , referenceResolver , e .host ))
98
- return tx
99
- }
100
-
101
45
func (e * emitter ) emitJSFile (sourceFile * ast.SourceFile , jsFilePath string , sourceMapFilePath string ) {
102
46
options := e .host .Options ()
103
47
@@ -110,7 +54,7 @@ func (e *emitter) emitJSFile(sourceFile *ast.SourceFile, jsFilePath string, sour
110
54
}
111
55
112
56
emitContext := printer .NewEmitContext ()
113
- for _ , transformer := range e . getScriptTransformers (emitContext , sourceFile ) {
57
+ for _ , transformer := range transformers . GetScriptTransformers (emitContext , e . host , sourceFile ) {
114
58
sourceFile = transformer .TransformSourceFile (sourceFile )
115
59
}
116
60
@@ -147,7 +91,7 @@ func (e *emitter) emitBuildInfo(buildInfoPath string) {
147
91
// !!!
148
92
}
149
93
150
- func (e * emitter ) printSourceFile (jsFilePath string , sourceMapFilePath string , sourceFile * ast.SourceFile , printer * printer.Printer ) bool {
94
+ func (e * emitter ) printSourceFile (jsFilePath string , sourceMapFilePath string , sourceFile * ast.SourceFile , printer_ * printer.Printer ) bool {
151
95
// !!! sourceMapGenerator
152
96
options := e .host .Options ()
153
97
var sourceMapGenerator * sourcemap.Generator
@@ -166,7 +110,7 @@ func (e *emitter) printSourceFile(jsFilePath string, sourceMapFilePath string, s
166
110
// !!! bundles not implemented, may be deprecated
167
111
sourceFiles := []* ast.SourceFile {sourceFile }
168
112
169
- printer .Write (sourceFile .AsNode (), sourceFile , e .writer , sourceMapGenerator )
113
+ printer_ .Write (sourceFile .AsNode (), sourceFile , e .writer , sourceMapGenerator )
170
114
171
115
sourceMapUrlPos := - 1
172
116
if sourceMapGenerator != nil {
@@ -208,7 +152,7 @@ func (e *emitter) printSourceFile(jsFilePath string, sourceMapFilePath string, s
208
152
209
153
// Write the output file
210
154
text := e .writer .String ()
211
- data := & WriteFileData {SourceMapUrlPos : sourceMapUrlPos } // !!! transform diagnostics
155
+ data := & printer. WriteFileData {SourceMapUrlPos : sourceMapUrlPos } // !!! transform diagnostics
212
156
err := e .host .WriteFile (jsFilePath , text , e .host .Options ().EmitBOM .IsTrue (), sourceFiles , data )
213
157
if err != nil {
214
158
e .emitterDiagnostics .Add (ast .NewCompilerDiagnostic (diagnostics .Could_not_write_file_0_Colon_1 , jsFilePath , err .Error ()))
@@ -232,7 +176,7 @@ func getSourceFilePathInNewDir(fileName string, newDirPath string, currentDirect
232
176
return tspath .CombinePaths (newDirPath , sourceFilePath )
233
177
}
234
178
235
- func getOwnEmitOutputFilePath (fileName string , host EmitHost , extension string ) string {
179
+ func getOwnEmitOutputFilePath (fileName string , host printer. EmitHost , extension string ) string {
236
180
compilerOptions := host .Options ()
237
181
var emitOutputFilePathWithoutExtension string
238
182
if len (compilerOptions .OutDir ) > 0 {
@@ -341,7 +285,7 @@ func (e *emitter) getSourceMappingURL(mapOptions *core.CompilerOptions, sourceMa
341
285
return stringutil .EncodeURI (sourceMapFile )
342
286
}
343
287
344
- func getDeclarationEmitOutputFilePath (file string , host EmitHost ) string {
288
+ func getDeclarationEmitOutputFilePath (file string , host printer. EmitHost ) string {
345
289
// !!!
346
290
return ""
347
291
}
@@ -354,7 +298,7 @@ type outputPaths struct {
354
298
buildInfoPath string
355
299
}
356
300
357
- func getOutputPathsFor (sourceFile * ast.SourceFile , host EmitHost , forceDtsEmit bool ) * outputPaths {
301
+ func getOutputPathsFor (sourceFile * ast.SourceFile , host printer. EmitHost , forceDtsEmit bool ) * outputPaths {
358
302
options := host .Options ()
359
303
// !!! bundle not implemented, may be deprecated
360
304
ownOutputFilePath := getOwnEmitOutputFilePath (sourceFile .FileName (), host , core .GetOutputExtension (sourceFile .FileName (), options .Jsx ))
@@ -381,7 +325,7 @@ func getOutputPathsFor(sourceFile *ast.SourceFile, host EmitHost, forceDtsEmit b
381
325
return paths
382
326
}
383
327
384
- func forEachEmittedFile (host EmitHost , action func (emitFileNames * outputPaths , sourceFile * ast.SourceFile ) bool , sourceFiles []* ast.SourceFile , options * EmitOptions ) bool {
328
+ func forEachEmittedFile (host printer. EmitHost , action func (emitFileNames * outputPaths , sourceFile * ast.SourceFile ) bool , sourceFiles []* ast.SourceFile , options * EmitOptions ) bool {
385
329
// !!! outFile not yet implemented, may be deprecated
386
330
for _ , sourceFile := range sourceFiles {
387
331
if action (getOutputPathsFor (sourceFile , host , options .forceDtsEmit ), sourceFile ) {
@@ -391,7 +335,7 @@ func forEachEmittedFile(host EmitHost, action func(emitFileNames *outputPaths, s
391
335
return false
392
336
}
393
337
394
- func sourceFileMayBeEmitted (sourceFile * ast.SourceFile , host EmitHost , forceDtsEmit bool ) bool {
338
+ func sourceFileMayBeEmitted (sourceFile * ast.SourceFile , host printer. EmitHost , forceDtsEmit bool ) bool {
395
339
// !!! Js files are emitted only if option is enabled
396
340
397
341
// Declaration files are not emitted
@@ -422,7 +366,7 @@ func sourceFileMayBeEmitted(sourceFile *ast.SourceFile, host EmitHost, forceDtsE
422
366
return false
423
367
}
424
368
425
- func getSourceFilesToEmit (host EmitHost , targetSourceFile * ast.SourceFile , forceDtsEmit bool ) []* ast.SourceFile {
369
+ func getSourceFilesToEmit (host printer. EmitHost , targetSourceFile * ast.SourceFile , forceDtsEmit bool ) []* ast.SourceFile {
426
370
// !!! outFile not yet implemented, may be deprecated
427
371
var sourceFiles []* ast.SourceFile
428
372
if targetSourceFile != nil {
0 commit comments