Skip to content

Commit c8fdf27

Browse files
committed
Add ESNext transform
1 parent a9136a3 commit c8fdf27

File tree

455 files changed

+14191
-9681
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

455 files changed

+14191
-9681
lines changed

internal/ast/utilities.go

+4
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ func NodeIsSynthesized(node *Node) bool {
7575
return PositionIsSynthesized(node.Loc.Pos()) || PositionIsSynthesized(node.Loc.End())
7676
}
7777

78+
func RangeIsSynthesized(loc core.TextRange) bool {
79+
return PositionIsSynthesized(loc.Pos()) || PositionIsSynthesized(loc.End())
80+
}
81+
7882
// Determines whether a position is synthetic
7983
func PositionIsSynthesized(pos int) bool {
8084
return pos < 0

internal/compiler/emitHost.go

+2-23
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,7 @@ import (
77
"github.com/microsoft/typescript-go/internal/tspath"
88
)
99

10-
type WriteFileData struct {
11-
SourceMapUrlPos int
12-
// BuildInfo BuildInfo
13-
Diagnostics []*ast.Diagnostic
14-
DiffersOnlyInMap bool
15-
SkippedDtsWrite bool
16-
}
17-
18-
// NOTE: EmitHost operations must be thread-safe
19-
type EmitHost interface {
20-
Options() *core.CompilerOptions
21-
SourceFiles() []*ast.SourceFile
22-
UseCaseSensitiveFileNames() bool
23-
GetCurrentDirectory() string
24-
CommonSourceDirectory() string
25-
IsEmitBlocked(file string) bool
26-
WriteFile(fileName string, text string, writeByteOrderMark bool, relatedSourceFiles []*ast.SourceFile, data *WriteFileData) error
27-
GetSourceFileMetaData(path tspath.Path) *ast.SourceFileMetaData
28-
GetEmitResolver(file *ast.SourceFile, skipDiagnostics bool) printer.EmitResolver
29-
}
30-
31-
var _ EmitHost = (*emitHost)(nil)
10+
var _ printer.EmitHost = (*emitHost)(nil)
3211

3312
// NOTE: emitHost operations must be thread-safe
3413
type emitHost struct {
@@ -48,7 +27,7 @@ func (host *emitHost) IsEmitBlocked(file string) bool {
4827
return false
4928
}
5029

51-
func (host *emitHost) WriteFile(fileName string, text string, writeByteOrderMark bool, _ []*ast.SourceFile, _ *WriteFileData) error {
30+
func (host *emitHost) WriteFile(fileName string, text string, writeByteOrderMark bool, _ []*ast.SourceFile, _ *printer.WriteFileData) error {
5231
return host.program.host.FS().WriteFile(fileName, text, writeByteOrderMark)
5332
}
5433

internal/compiler/emitter.go

+11-67
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"strings"
66

77
"github.com/microsoft/typescript-go/internal/ast"
8-
"github.com/microsoft/typescript-go/internal/binder"
98
"github.com/microsoft/typescript-go/internal/core"
109
"github.com/microsoft/typescript-go/internal/diagnostics"
1110
"github.com/microsoft/typescript-go/internal/printer"
@@ -25,7 +24,7 @@ const (
2524
)
2625

2726
type emitter struct {
28-
host EmitHost
27+
host printer.EmitHost
2928
emitOnly emitOnly
3029
emittedFilesList []string
3130
emitterDiagnostics ast.DiagnosticsCollection
@@ -43,61 +42,6 @@ func (e *emitter) emit() {
4342
e.emitBuildInfo(e.paths.buildInfoPath)
4443
}
4544

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-
10145
func (e *emitter) emitJSFile(sourceFile *ast.SourceFile, jsFilePath string, sourceMapFilePath string) {
10246
options := e.host.Options()
10347

@@ -110,7 +54,7 @@ func (e *emitter) emitJSFile(sourceFile *ast.SourceFile, jsFilePath string, sour
11054
}
11155

11256
emitContext := printer.NewEmitContext()
113-
for _, transformer := range e.getScriptTransformers(emitContext, sourceFile) {
57+
for _, transformer := range transformers.GetScriptTransformers(emitContext, e.host, sourceFile) {
11458
sourceFile = transformer.TransformSourceFile(sourceFile)
11559
}
11660

@@ -147,7 +91,7 @@ func (e *emitter) emitBuildInfo(buildInfoPath string) {
14791
// !!!
14892
}
14993

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 {
15195
// !!! sourceMapGenerator
15296
options := e.host.Options()
15397
var sourceMapGenerator *sourcemap.Generator
@@ -166,7 +110,7 @@ func (e *emitter) printSourceFile(jsFilePath string, sourceMapFilePath string, s
166110
// !!! bundles not implemented, may be deprecated
167111
sourceFiles := []*ast.SourceFile{sourceFile}
168112

169-
printer.Write(sourceFile.AsNode(), sourceFile, e.writer, sourceMapGenerator)
113+
printer_.Write(sourceFile.AsNode(), sourceFile, e.writer, sourceMapGenerator)
170114

171115
sourceMapUrlPos := -1
172116
if sourceMapGenerator != nil {
@@ -208,7 +152,7 @@ func (e *emitter) printSourceFile(jsFilePath string, sourceMapFilePath string, s
208152

209153
// Write the output file
210154
text := e.writer.String()
211-
data := &WriteFileData{SourceMapUrlPos: sourceMapUrlPos} // !!! transform diagnostics
155+
data := &printer.WriteFileData{SourceMapUrlPos: sourceMapUrlPos} // !!! transform diagnostics
212156
err := e.host.WriteFile(jsFilePath, text, e.host.Options().EmitBOM.IsTrue(), sourceFiles, data)
213157
if err != nil {
214158
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
232176
return tspath.CombinePaths(newDirPath, sourceFilePath)
233177
}
234178

235-
func getOwnEmitOutputFilePath(fileName string, host EmitHost, extension string) string {
179+
func getOwnEmitOutputFilePath(fileName string, host printer.EmitHost, extension string) string {
236180
compilerOptions := host.Options()
237181
var emitOutputFilePathWithoutExtension string
238182
if len(compilerOptions.OutDir) > 0 {
@@ -341,7 +285,7 @@ func (e *emitter) getSourceMappingURL(mapOptions *core.CompilerOptions, sourceMa
341285
return stringutil.EncodeURI(sourceMapFile)
342286
}
343287

344-
func getDeclarationEmitOutputFilePath(file string, host EmitHost) string {
288+
func getDeclarationEmitOutputFilePath(file string, host printer.EmitHost) string {
345289
// !!!
346290
return ""
347291
}
@@ -354,7 +298,7 @@ type outputPaths struct {
354298
buildInfoPath string
355299
}
356300

357-
func getOutputPathsFor(sourceFile *ast.SourceFile, host EmitHost, forceDtsEmit bool) *outputPaths {
301+
func getOutputPathsFor(sourceFile *ast.SourceFile, host printer.EmitHost, forceDtsEmit bool) *outputPaths {
358302
options := host.Options()
359303
// !!! bundle not implemented, may be deprecated
360304
ownOutputFilePath := getOwnEmitOutputFilePath(sourceFile.FileName(), host, core.GetOutputExtension(sourceFile.FileName(), options.Jsx))
@@ -381,7 +325,7 @@ func getOutputPathsFor(sourceFile *ast.SourceFile, host EmitHost, forceDtsEmit b
381325
return paths
382326
}
383327

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 {
385329
// !!! outFile not yet implemented, may be deprecated
386330
for _, sourceFile := range sourceFiles {
387331
if action(getOutputPathsFor(sourceFile, host, options.forceDtsEmit), sourceFile) {
@@ -391,7 +335,7 @@ func forEachEmittedFile(host EmitHost, action func(emitFileNames *outputPaths, s
391335
return false
392336
}
393337

394-
func sourceFileMayBeEmitted(sourceFile *ast.SourceFile, host EmitHost, forceDtsEmit bool) bool {
338+
func sourceFileMayBeEmitted(sourceFile *ast.SourceFile, host printer.EmitHost, forceDtsEmit bool) bool {
395339
// !!! Js files are emitted only if option is enabled
396340

397341
// Declaration files are not emitted
@@ -422,7 +366,7 @@ func sourceFileMayBeEmitted(sourceFile *ast.SourceFile, host EmitHost, forceDtsE
422366
return false
423367
}
424368

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 {
426370
// !!! outFile not yet implemented, may be deprecated
427371
var sourceFiles []*ast.SourceFile
428372
if targetSourceFile != nil {

internal/printer/emitcontext.go

+31
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ type EmitContext struct {
1919
textSource map[*ast.StringLiteralNode]*ast.Node
2020
original map[*ast.Node]*ast.Node
2121
emitNodes core.LinkStore[*ast.Node, emitNode]
22+
assignedName map[*ast.Node]*ast.Expression
23+
classThis map[*ast.Node]*ast.IdentifierNode
2224
varScopeStack core.Stack[*varScope]
2325
letScopeStack core.Stack[*varScope]
2426
emitHelpers collections.OrderedSet[*EmitHelper]
@@ -575,6 +577,28 @@ func (c *EmitContext) SetTokenSourceMapRange(node *ast.Node, kind ast.Kind, loc
575577
emitNode.tokenSourceMapRanges[kind] = loc
576578
}
577579

580+
func (c *EmitContext) AssignedName(node *ast.Node) *ast.Expression {
581+
return c.assignedName[node]
582+
}
583+
584+
func (c *EmitContext) SetAssignedName(node *ast.Node, name *ast.Expression) {
585+
if c.assignedName == nil {
586+
c.assignedName = make(map[*ast.Node]*ast.Expression)
587+
}
588+
c.assignedName[node] = name
589+
}
590+
591+
func (c *EmitContext) ClassThis(node *ast.Node) *ast.Expression {
592+
return c.classThis[node]
593+
}
594+
595+
func (c *EmitContext) SetClassThis(node *ast.Node, classThis *ast.IdentifierNode) {
596+
if c.classThis == nil {
597+
c.classThis = make(map[*ast.Node]*ast.Expression)
598+
}
599+
c.classThis[node] = classThis
600+
}
601+
578602
func (c *EmitContext) RequestEmitHelper(helper *EmitHelper) {
579603
if helper.Scoped {
580604
panic("Cannot request a scoped emit helper")
@@ -659,6 +683,13 @@ func (c *EmitContext) HasRecordedExternalHelpers(node *ast.SourceFile) bool {
659683
return false
660684
}
661685

686+
func (c *EmitContext) IsCallToHelper(firstSegment *ast.Expression, helperName string) bool {
687+
return ast.IsCallExpression(firstSegment) &&
688+
ast.IsIdentifier(firstSegment.Expression()) &&
689+
(c.EmitFlags(firstSegment.Expression())&EFHelperName) != 0 &&
690+
firstSegment.Expression().Text() == helperName
691+
}
692+
662693
//
663694
// Visitor Hooks
664695
//

internal/printer/emithost.go

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package printer
2+
3+
import (
4+
"github.com/microsoft/typescript-go/internal/ast"
5+
"github.com/microsoft/typescript-go/internal/core"
6+
"github.com/microsoft/typescript-go/internal/tspath"
7+
)
8+
9+
type WriteFileData struct {
10+
SourceMapUrlPos int
11+
// BuildInfo BuildInfo
12+
Diagnostics []*ast.Diagnostic
13+
DiffersOnlyInMap bool
14+
SkippedDtsWrite bool
15+
}
16+
17+
// NOTE: EmitHost operations must be thread-safe
18+
type EmitHost interface {
19+
SourceFileMetaDataProvider
20+
Options() *core.CompilerOptions
21+
SourceFiles() []*ast.SourceFile
22+
UseCaseSensitiveFileNames() bool
23+
GetCurrentDirectory() string
24+
CommonSourceDirectory() string
25+
IsEmitBlocked(file string) bool
26+
WriteFile(fileName string, text string, writeByteOrderMark bool, relatedSourceFiles []*ast.SourceFile, data *WriteFileData) error
27+
GetSourceFileMetaData(path tspath.Path) *ast.SourceFileMetaData
28+
GetEmitResolver(file *ast.SourceFile, skipDiagnostics bool) EmitResolver
29+
}

0 commit comments

Comments
 (0)