Skip to content

Commit 9ee248b

Browse files
committed
Move Program.emit to program.go
1 parent aee068c commit 9ee248b

File tree

2 files changed

+78
-79
lines changed

2 files changed

+78
-79
lines changed

internal/compiler/emitter.go

-79
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,13 @@
11
package compiler
22

33
import (
4-
"sync"
5-
64
"github.com/microsoft/typescript-go/internal/ast"
75
"github.com/microsoft/typescript-go/internal/compiler/diagnostics"
86
"github.com/microsoft/typescript-go/internal/core"
97
"github.com/microsoft/typescript-go/internal/printer"
10-
"github.com/microsoft/typescript-go/internal/sourcemap"
118
"github.com/microsoft/typescript-go/internal/tspath"
129
)
1310

14-
type EmitOptions struct {
15-
TargetSourceFile *ast.SourceFile // Single file to emit. If `nil`, emits all files
16-
forceDtsEmit bool
17-
}
18-
19-
type EmitResult struct {
20-
EmitSkipped bool
21-
Diagnostics []*ast.Diagnostic // Contains declaration emit diagnostics
22-
EmittedFiles []string // Array of files the compiler wrote to disk
23-
sourceMaps []*sourceMapEmitResult // Array of sourceMapData if compiler emitted sourcemaps
24-
}
25-
26-
type sourceMapEmitResult struct {
27-
inputSourceFileNames []string // Input source file (which one can use on program to get the file), 1:1 mapping with the sourceMap.sources list
28-
sourceMap *sourcemap.RawSourceMap
29-
}
30-
31-
func (p *Program) Emit(options *EmitOptions) *EmitResult {
32-
// !!! performance measurement
33-
34-
host := &emitHost{program: p}
35-
36-
writerPool := &sync.Pool{
37-
New: func() any {
38-
return printer.NewTextWriter(host.Options().NewLine.GetNewLineCharacter())
39-
},
40-
}
41-
wg := core.NewWorkGroup(p.programOptions.SingleThreaded)
42-
43-
var emitters []*emitter
44-
sourceFiles := getSourceFilesToEmit(host, options.TargetSourceFile, options.forceDtsEmit)
45-
for _, sourceFile := range sourceFiles {
46-
emitter := &emitter{
47-
host: host,
48-
emittedFilesList: nil,
49-
sourceMapDataList: nil,
50-
writer: nil,
51-
sourceFile: sourceFile,
52-
}
53-
emitters = append(emitters, emitter)
54-
wg.Run(func() {
55-
// take an unused writer
56-
writer := writerPool.Get().(printer.EmitTextWriter)
57-
writer.Clear()
58-
59-
// attach writer and perform emit
60-
emitter.writer = writer
61-
emitter.paths = getOutputPathsFor(sourceFile, host, options.forceDtsEmit)
62-
emitter.emit()
63-
emitter.writer = nil
64-
65-
// put the writer back in the pool
66-
writerPool.Put(writer)
67-
})
68-
}
69-
70-
// wait for emit to complete
71-
wg.Wait()
72-
73-
// collect results from emit, preserving input order
74-
result := &EmitResult{}
75-
for _, emitter := range emitters {
76-
if emitter.emitSkipped {
77-
result.EmitSkipped = true
78-
}
79-
result.Diagnostics = append(result.Diagnostics, emitter.emitterDiagnostics.GetDiagnostics()...)
80-
if emitter.emittedFilesList != nil {
81-
result.EmittedFiles = append(result.EmittedFiles, emitter.emittedFilesList...)
82-
}
83-
if emitter.sourceMapDataList != nil {
84-
result.sourceMaps = append(result.sourceMaps, emitter.sourceMapDataList...)
85-
}
86-
}
87-
return result
88-
}
89-
9011
type emitOnly byte
9112

9213
const (

internal/compiler/program.go

+78
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"github.com/microsoft/typescript-go/internal/compiler/module"
1212
"github.com/microsoft/typescript-go/internal/core"
1313
"github.com/microsoft/typescript-go/internal/parser"
14+
"github.com/microsoft/typescript-go/internal/printer"
15+
"github.com/microsoft/typescript-go/internal/sourcemap"
1416
"github.com/microsoft/typescript-go/internal/tspath"
1517
"github.com/microsoft/typescript-go/internal/vfs"
1618
)
@@ -660,3 +662,79 @@ func getCommonSourceDirectory(options *core.CompilerOptions, files []string, cur
660662

661663
return commonSourceDirectory
662664
}
665+
666+
type EmitOptions struct {
667+
TargetSourceFile *ast.SourceFile // Single file to emit. If `nil`, emits all files
668+
forceDtsEmit bool
669+
}
670+
671+
type EmitResult struct {
672+
EmitSkipped bool
673+
Diagnostics []*ast.Diagnostic // Contains declaration emit diagnostics
674+
EmittedFiles []string // Array of files the compiler wrote to disk
675+
sourceMaps []*sourceMapEmitResult // Array of sourceMapData if compiler emitted sourcemaps
676+
}
677+
678+
type sourceMapEmitResult struct {
679+
inputSourceFileNames []string // Input source file (which one can use on program to get the file), 1:1 mapping with the sourceMap.sources list
680+
sourceMap *sourcemap.RawSourceMap
681+
}
682+
683+
func (p *Program) Emit(options *EmitOptions) *EmitResult {
684+
// !!! performance measurement
685+
686+
host := &emitHost{program: p}
687+
688+
writerPool := &sync.Pool{
689+
New: func() any {
690+
return printer.NewTextWriter(host.Options().NewLine.GetNewLineCharacter())
691+
},
692+
}
693+
wg := core.NewWorkGroup(p.programOptions.SingleThreaded)
694+
695+
var emitters []*emitter
696+
sourceFiles := getSourceFilesToEmit(host, options.TargetSourceFile, options.forceDtsEmit)
697+
for _, sourceFile := range sourceFiles {
698+
emitter := &emitter{
699+
host: host,
700+
emittedFilesList: nil,
701+
sourceMapDataList: nil,
702+
writer: nil,
703+
sourceFile: sourceFile,
704+
}
705+
emitters = append(emitters, emitter)
706+
wg.Run(func() {
707+
// take an unused writer
708+
writer := writerPool.Get().(printer.EmitTextWriter)
709+
writer.Clear()
710+
711+
// attach writer and perform emit
712+
emitter.writer = writer
713+
emitter.paths = getOutputPathsFor(sourceFile, host, options.forceDtsEmit)
714+
emitter.emit()
715+
emitter.writer = nil
716+
717+
// put the writer back in the pool
718+
writerPool.Put(writer)
719+
})
720+
}
721+
722+
// wait for emit to complete
723+
wg.Wait()
724+
725+
// collect results from emit, preserving input order
726+
result := &EmitResult{}
727+
for _, emitter := range emitters {
728+
if emitter.emitSkipped {
729+
result.EmitSkipped = true
730+
}
731+
result.Diagnostics = append(result.Diagnostics, emitter.emitterDiagnostics.GetDiagnostics()...)
732+
if emitter.emittedFilesList != nil {
733+
result.EmittedFiles = append(result.EmittedFiles, emitter.emittedFilesList...)
734+
}
735+
if emitter.sourceMapDataList != nil {
736+
result.sourceMaps = append(result.sourceMaps, emitter.sourceMapDataList...)
737+
}
738+
}
739+
return result
740+
}

0 commit comments

Comments
 (0)