Skip to content

Add esnext transform #819

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
4 changes: 4 additions & 0 deletions internal/ast/utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ func NodeIsSynthesized(node *Node) bool {
return PositionIsSynthesized(node.Loc.Pos()) || PositionIsSynthesized(node.Loc.End())
}

func RangeIsSynthesized(loc core.TextRange) bool {
return PositionIsSynthesized(loc.Pos()) || PositionIsSynthesized(loc.End())
}

// Determines whether a position is synthetic
func PositionIsSynthesized(pos int) bool {
return pos < 0
Expand Down
25 changes: 2 additions & 23 deletions internal/compiler/emitHost.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,7 @@ import (
"github.com/microsoft/typescript-go/internal/tspath"
)

type WriteFileData struct {
SourceMapUrlPos int
// BuildInfo BuildInfo
Diagnostics []*ast.Diagnostic
DiffersOnlyInMap bool
SkippedDtsWrite bool
}

// NOTE: EmitHost operations must be thread-safe
type EmitHost interface {
Options() *core.CompilerOptions
SourceFiles() []*ast.SourceFile
UseCaseSensitiveFileNames() bool
GetCurrentDirectory() string
CommonSourceDirectory() string
IsEmitBlocked(file string) bool
WriteFile(fileName string, text string, writeByteOrderMark bool, relatedSourceFiles []*ast.SourceFile, data *WriteFileData) error
GetSourceFileMetaData(path tspath.Path) *ast.SourceFileMetaData
GetEmitResolver(file *ast.SourceFile, skipDiagnostics bool) printer.EmitResolver
}

var _ EmitHost = (*emitHost)(nil)
var _ printer.EmitHost = (*emitHost)(nil)

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

func (host *emitHost) WriteFile(fileName string, text string, writeByteOrderMark bool, _ []*ast.SourceFile, _ *WriteFileData) error {
func (host *emitHost) WriteFile(fileName string, text string, writeByteOrderMark bool, _ []*ast.SourceFile, _ *printer.WriteFileData) error {
return host.program.host.FS().WriteFile(fileName, text, writeByteOrderMark)
}

Expand Down
78 changes: 11 additions & 67 deletions internal/compiler/emitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"strings"

"github.com/microsoft/typescript-go/internal/ast"
"github.com/microsoft/typescript-go/internal/binder"
"github.com/microsoft/typescript-go/internal/core"
"github.com/microsoft/typescript-go/internal/diagnostics"
"github.com/microsoft/typescript-go/internal/printer"
Expand All @@ -25,7 +24,7 @@ const (
)

type emitter struct {
host EmitHost
host printer.EmitHost
emitOnly emitOnly
emittedFilesList []string
emitterDiagnostics ast.DiagnosticsCollection
Expand All @@ -43,61 +42,6 @@ func (e *emitter) emit() {
e.emitBuildInfo(e.paths.buildInfoPath)
}

func (e *emitter) getModuleTransformer(emitContext *printer.EmitContext, resolver binder.ReferenceResolver, sourceFileMetaDataProvider printer.SourceFileMetaDataProvider) *transformers.Transformer {
options := e.host.Options()

switch options.GetEmitModuleKind() {
case core.ModuleKindPreserve:
// `ESModuleTransformer` contains logic for preserving CJS input syntax in `--module preserve`
return transformers.NewESModuleTransformer(emitContext, options, resolver, sourceFileMetaDataProvider)

case core.ModuleKindESNext,
core.ModuleKindES2022,
core.ModuleKindES2020,
core.ModuleKindES2015,
core.ModuleKindNode16,
core.ModuleKindNodeNext,
core.ModuleKindCommonJS:
return transformers.NewImpliedModuleTransformer(emitContext, options, resolver, sourceFileMetaDataProvider)

default:
return transformers.NewCommonJSModuleTransformer(emitContext, options, resolver, sourceFileMetaDataProvider)
}
}

func (e *emitter) getScriptTransformers(emitContext *printer.EmitContext, sourceFile *ast.SourceFile) []*transformers.Transformer {
var tx []*transformers.Transformer
options := e.host.Options()

// JS files don't use reference calculations as they don't do import elision, no need to calculate it
importElisionEnabled := !options.VerbatimModuleSyntax.IsTrue() && !ast.IsInJSFile(sourceFile.AsNode())

var emitResolver printer.EmitResolver
var referenceResolver binder.ReferenceResolver
if importElisionEnabled {
emitResolver = e.host.GetEmitResolver(sourceFile, false /*skipDiagnostics*/) // !!! conditionally skip diagnostics
emitResolver.MarkLinkedReferencesRecursively(sourceFile)
referenceResolver = emitResolver
} else {
referenceResolver = binder.NewReferenceResolver(options, binder.ReferenceResolverHooks{})
}

// erase types
tx = append(tx, transformers.NewTypeEraserTransformer(emitContext, options))

// elide imports
if importElisionEnabled {
tx = append(tx, transformers.NewImportElisionTransformer(emitContext, options, emitResolver))
}

// transform `enum`, `namespace`, and parameter properties
tx = append(tx, transformers.NewRuntimeSyntaxTransformer(emitContext, options, referenceResolver))

// transform module syntax
tx = append(tx, e.getModuleTransformer(emitContext, referenceResolver, e.host))
return tx
}

func (e *emitter) emitJSFile(sourceFile *ast.SourceFile, jsFilePath string, sourceMapFilePath string) {
options := e.host.Options()

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

emitContext := printer.NewEmitContext()
for _, transformer := range e.getScriptTransformers(emitContext, sourceFile) {
for _, transformer := range transformers.GetScriptTransformers(emitContext, e.host, sourceFile) {
sourceFile = transformer.TransformSourceFile(sourceFile)
}

Expand Down Expand Up @@ -147,7 +91,7 @@ func (e *emitter) emitBuildInfo(buildInfoPath string) {
// !!!
}

func (e *emitter) printSourceFile(jsFilePath string, sourceMapFilePath string, sourceFile *ast.SourceFile, printer *printer.Printer) bool {
func (e *emitter) printSourceFile(jsFilePath string, sourceMapFilePath string, sourceFile *ast.SourceFile, printer_ *printer.Printer) bool {
// !!! sourceMapGenerator
options := e.host.Options()
var sourceMapGenerator *sourcemap.Generator
Expand All @@ -166,7 +110,7 @@ func (e *emitter) printSourceFile(jsFilePath string, sourceMapFilePath string, s
// !!! bundles not implemented, may be deprecated
sourceFiles := []*ast.SourceFile{sourceFile}

printer.Write(sourceFile.AsNode(), sourceFile, e.writer, sourceMapGenerator)
printer_.Write(sourceFile.AsNode(), sourceFile, e.writer, sourceMapGenerator)

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

// Write the output file
text := e.writer.String()
data := &WriteFileData{SourceMapUrlPos: sourceMapUrlPos} // !!! transform diagnostics
data := &printer.WriteFileData{SourceMapUrlPos: sourceMapUrlPos} // !!! transform diagnostics
err := e.host.WriteFile(jsFilePath, text, e.host.Options().EmitBOM.IsTrue(), sourceFiles, data)
if err != nil {
e.emitterDiagnostics.Add(ast.NewCompilerDiagnostic(diagnostics.Could_not_write_file_0_Colon_1, jsFilePath, err.Error()))
Expand All @@ -232,7 +176,7 @@ func getSourceFilePathInNewDir(fileName string, newDirPath string, currentDirect
return tspath.CombinePaths(newDirPath, sourceFilePath)
}

func getOwnEmitOutputFilePath(fileName string, host EmitHost, extension string) string {
func getOwnEmitOutputFilePath(fileName string, host printer.EmitHost, extension string) string {
compilerOptions := host.Options()
var emitOutputFilePathWithoutExtension string
if len(compilerOptions.OutDir) > 0 {
Expand Down Expand Up @@ -341,7 +285,7 @@ func (e *emitter) getSourceMappingURL(mapOptions *core.CompilerOptions, sourceMa
return stringutil.EncodeURI(sourceMapFile)
}

func getDeclarationEmitOutputFilePath(file string, host EmitHost) string {
func getDeclarationEmitOutputFilePath(file string, host printer.EmitHost) string {
// !!!
return ""
}
Expand All @@ -354,7 +298,7 @@ type outputPaths struct {
buildInfoPath string
}

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

func forEachEmittedFile(host EmitHost, action func(emitFileNames *outputPaths, sourceFile *ast.SourceFile) bool, sourceFiles []*ast.SourceFile, options *EmitOptions) bool {
func forEachEmittedFile(host printer.EmitHost, action func(emitFileNames *outputPaths, sourceFile *ast.SourceFile) bool, sourceFiles []*ast.SourceFile, options *EmitOptions) bool {
// !!! outFile not yet implemented, may be deprecated
for _, sourceFile := range sourceFiles {
if action(getOutputPathsFor(sourceFile, host, options.forceDtsEmit), sourceFile) {
Expand All @@ -391,7 +335,7 @@ func forEachEmittedFile(host EmitHost, action func(emitFileNames *outputPaths, s
return false
}

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

// Declaration files are not emitted
Expand Down Expand Up @@ -422,7 +366,7 @@ func sourceFileMayBeEmitted(sourceFile *ast.SourceFile, host EmitHost, forceDtsE
return false
}

func getSourceFilesToEmit(host EmitHost, targetSourceFile *ast.SourceFile, forceDtsEmit bool) []*ast.SourceFile {
func getSourceFilesToEmit(host printer.EmitHost, targetSourceFile *ast.SourceFile, forceDtsEmit bool) []*ast.SourceFile {
// !!! outFile not yet implemented, may be deprecated
var sourceFiles []*ast.SourceFile
if targetSourceFile != nil {
Expand Down
31 changes: 31 additions & 0 deletions internal/printer/emitcontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ type EmitContext struct {
textSource map[*ast.StringLiteralNode]*ast.Node
original map[*ast.Node]*ast.Node
emitNodes core.LinkStore[*ast.Node, emitNode]
assignedName map[*ast.Node]*ast.Expression
classThis map[*ast.Node]*ast.IdentifierNode
varScopeStack core.Stack[*varScope]
letScopeStack core.Stack[*varScope]
emitHelpers collections.OrderedSet[*EmitHelper]
Expand Down Expand Up @@ -575,6 +577,28 @@ func (c *EmitContext) SetTokenSourceMapRange(node *ast.Node, kind ast.Kind, loc
emitNode.tokenSourceMapRanges[kind] = loc
}

func (c *EmitContext) AssignedName(node *ast.Node) *ast.Expression {
return c.assignedName[node]
}

func (c *EmitContext) SetAssignedName(node *ast.Node, name *ast.Expression) {
if c.assignedName == nil {
c.assignedName = make(map[*ast.Node]*ast.Expression)
}
c.assignedName[node] = name
}

func (c *EmitContext) ClassThis(node *ast.Node) *ast.Expression {
return c.classThis[node]
}

func (c *EmitContext) SetClassThis(node *ast.Node, classThis *ast.IdentifierNode) {
if c.classThis == nil {
c.classThis = make(map[*ast.Node]*ast.Expression)
}
c.classThis[node] = classThis
}

func (c *EmitContext) RequestEmitHelper(helper *EmitHelper) {
if helper.Scoped {
panic("Cannot request a scoped emit helper")
Expand Down Expand Up @@ -659,6 +683,13 @@ func (c *EmitContext) HasRecordedExternalHelpers(node *ast.SourceFile) bool {
return false
}

func (c *EmitContext) IsCallToHelper(firstSegment *ast.Expression, helperName string) bool {
return ast.IsCallExpression(firstSegment) &&
ast.IsIdentifier(firstSegment.Expression()) &&
(c.EmitFlags(firstSegment.Expression())&EFHelperName) != 0 &&
firstSegment.Expression().Text() == helperName
}

//
// Visitor Hooks
//
Expand Down
29 changes: 29 additions & 0 deletions internal/printer/emithost.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package printer

import (
"github.com/microsoft/typescript-go/internal/ast"
"github.com/microsoft/typescript-go/internal/core"
"github.com/microsoft/typescript-go/internal/tspath"
)

type WriteFileData struct {
SourceMapUrlPos int
// BuildInfo BuildInfo
Diagnostics []*ast.Diagnostic
DiffersOnlyInMap bool
SkippedDtsWrite bool
}

// NOTE: EmitHost operations must be thread-safe
type EmitHost interface {
SourceFileMetaDataProvider
Options() *core.CompilerOptions
SourceFiles() []*ast.SourceFile
UseCaseSensitiveFileNames() bool
GetCurrentDirectory() string
CommonSourceDirectory() string
IsEmitBlocked(file string) bool
WriteFile(fileName string, text string, writeByteOrderMark bool, relatedSourceFiles []*ast.SourceFile, data *WriteFileData) error
GetSourceFileMetaData(path tspath.Path) *ast.SourceFileMetaData
GetEmitResolver(file *ast.SourceFile, skipDiagnostics bool) EmitResolver
}
Loading