Skip to content

Avoid holding on to NodeBuilder in checker #1289

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

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions internal/checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"github.com/microsoft/typescript-go/internal/jsnum"
"github.com/microsoft/typescript-go/internal/module"
"github.com/microsoft/typescript-go/internal/modulespecifiers"
"github.com/microsoft/typescript-go/internal/printer"
"github.com/microsoft/typescript-go/internal/scanner"
"github.com/microsoft/typescript-go/internal/stringutil"
"github.com/microsoft/typescript-go/internal/tsoptions"
Expand Down Expand Up @@ -849,8 +848,6 @@ type Checker struct {
markNodeAssignments func(*ast.Node) bool
emitResolver *emitResolver
emitResolverOnce sync.Once
diagnosticConstructionContext *printer.EmitContext
nodeBuilder *NodeBuilder
_jsxNamespace string
_jsxFactoryEntity *ast.Node
skipDirectInferenceNodes collections.Set[*ast.Node]
Expand Down Expand Up @@ -1068,8 +1065,6 @@ func NewChecker(program Program) *Checker {
c.getGlobalClassAccessorDecoratorTargetType = c.getGlobalTypeResolver("ClassAccessorDecoratorTarget", 2 /*arity*/, true /*reportErrors*/)
c.getGlobalClassAccessorDecoratorResultType = c.getGlobalTypeResolver("ClassAccessorDecoratorResult", 2 /*arity*/, true /*reportErrors*/)
c.getGlobalClassFieldDecoratorContextType = c.getGlobalTypeResolver("ClassFieldDecoratorContext", 2 /*arity*/, true /*reportErrors*/)
c.diagnosticConstructionContext = printer.NewEmitContext()
c.nodeBuilder = NewNodeBuilder(c, c.diagnosticConstructionContext)
c.initializeClosures()
c.initializeIterationResolvers()
c.initializeChecker()
Expand Down
4 changes: 2 additions & 2 deletions internal/checker/nodebuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,6 @@ func NewNodeBuilder(ch *Checker, e *printer.EmitContext) *NodeBuilder {
return &NodeBuilder{impl: impl, ctxStack: make([]*NodeBuilderContext, 0, 1), host: ch.program}
}

func (c *Checker) GetDiagnosticNodeBuilder() *NodeBuilder {
return c.nodeBuilder
func (c *Checker) getNodeBuilder() *NodeBuilder {
return NewNodeBuilder(c, printer.NewEmitContext())
}
26 changes: 15 additions & 11 deletions internal/checker/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,17 +182,18 @@ func (c *Checker) typeToStringEx(t *Type, enclosingDeclaration *ast.Node, flags
if noTruncation {
combinedFlags = combinedFlags | nodebuilder.FlagsNoTruncation
}
typeNode := c.nodeBuilder.TypeToTypeNode(t, enclosingDeclaration, combinedFlags, nodebuilder.InternalFlagsNone, nil)
nodeBuilder := c.getNodeBuilder()
typeNode := nodeBuilder.TypeToTypeNode(t, enclosingDeclaration, combinedFlags, nodebuilder.InternalFlagsNone, nil)
if typeNode == nil {
panic("should always get typenode")
}
// The unresolved type gets a synthesized comment on `any` to hint to users that it's not a plain `any`.
// Otherwise, we always strip comments out.
var printer *printer.Printer
if t == c.unresolvedType {
printer = createPrinterWithDefaults(c.diagnosticConstructionContext)
printer = createPrinterWithDefaults(nodeBuilder.EmitContext())
} else {
printer = createPrinterWithRemoveComments(c.diagnosticConstructionContext)
printer = createPrinterWithRemoveComments(nodeBuilder.EmitContext())
}
var sourceFile *ast.SourceFile
if enclosingDeclaration != nil {
Expand Down Expand Up @@ -245,22 +246,23 @@ func (c *Checker) symbolToStringEx(symbol *ast.Symbol, enclosingDeclaration *ast
internalNodeFlags |= nodebuilder.InternalFlagsWriteComputedProps
}

nodeBuilder := c.getNodeBuilder()
var sourceFile *ast.SourceFile
if enclosingDeclaration != nil {
sourceFile = ast.GetSourceFileOfNode(enclosingDeclaration)
}
var printer_ *printer.Printer
if enclosingDeclaration != nil && enclosingDeclaration.Kind == ast.KindSourceFile {
printer_ = createPrinterWithRemoveCommentsNeverAsciiEscape(c.diagnosticConstructionContext)
printer_ = createPrinterWithRemoveCommentsNeverAsciiEscape(nodeBuilder.EmitContext())
} else {
printer_ = createPrinterWithRemoveComments(c.diagnosticConstructionContext)
printer_ = createPrinterWithRemoveComments(nodeBuilder.EmitContext())
}

var builder func(symbol *ast.Symbol, meaning ast.SymbolFlags, enclosingDeclaration *ast.Node, flags nodebuilder.Flags, internalFlags nodebuilder.InternalFlags, tracker nodebuilder.SymbolTracker) *ast.Node
if flags&SymbolFormatFlagsAllowAnyNodeKind != 0 {
builder = c.nodeBuilder.SymbolToNode
builder = nodeBuilder.SymbolToNode
} else {
builder = c.nodeBuilder.SymbolToEntityName
builder = nodeBuilder.SymbolToEntityName
}
entity := builder(symbol, meaning, enclosingDeclaration, nodeFlags, internalNodeFlags, nil) // TODO: GH#18217
printer_.Write(entity /*sourceFile*/, sourceFile, getTrailingSemicolonDeferringWriter(writer), nil) // TODO: GH#18217
Expand Down Expand Up @@ -294,9 +296,10 @@ func (c *Checker) signatureToStringEx(signature *Signature, enclosingDeclaration
writer, putWriter := printer.GetSingleLineStringWriter()
defer putWriter()

nodeBuilder := c.getNodeBuilder()
combinedFlags := toNodeBuilderFlags(flags) | nodebuilder.FlagsIgnoreErrors | nodebuilder.FlagsWriteTypeParametersInQualifiedName
sig := c.nodeBuilder.SignatureToSignatureDeclaration(signature, sigOutput, enclosingDeclaration, combinedFlags, nodebuilder.InternalFlagsNone, nil)
printer_ := createPrinterWithRemoveCommentsOmitTrailingSemicolon(c.diagnosticConstructionContext)
sig := nodeBuilder.SignatureToSignatureDeclaration(signature, sigOutput, enclosingDeclaration, combinedFlags, nodebuilder.InternalFlagsNone, nil)
printer_ := createPrinterWithRemoveCommentsOmitTrailingSemicolon(nodeBuilder.EmitContext())
var sourceFile *ast.SourceFile
if enclosingDeclaration != nil {
sourceFile = ast.GetSourceFileOfNode(enclosingDeclaration)
Expand All @@ -312,9 +315,10 @@ func (c *Checker) typePredicateToString(typePredicate *TypePredicate) string {
func (c *Checker) typePredicateToStringEx(typePredicate *TypePredicate, enclosingDeclaration *ast.Node, flags TypeFormatFlags) string {
writer, putWriter := printer.GetSingleLineStringWriter()
defer putWriter()
nodeBuilder := c.getNodeBuilder()
combinedFlags := toNodeBuilderFlags(flags) | nodebuilder.FlagsIgnoreErrors | nodebuilder.FlagsWriteTypeParametersInQualifiedName
predicate := c.nodeBuilder.TypePredicateToTypePredicateNode(typePredicate, enclosingDeclaration, combinedFlags, nodebuilder.InternalFlagsNone, nil) // TODO: GH#18217
printer_ := createPrinterWithRemoveComments(c.diagnosticConstructionContext)
predicate := nodeBuilder.TypePredicateToTypePredicateNode(typePredicate, enclosingDeclaration, combinedFlags, nodebuilder.InternalFlagsNone, nil) // TODO: GH#18217
printer_ := createPrinterWithRemoveComments(nodeBuilder.EmitContext())
var sourceFile *ast.SourceFile
if enclosingDeclaration != nil {
sourceFile = ast.GetSourceFileOfNode(enclosingDeclaration)
Expand Down