@@ -42,7 +42,7 @@ type parser struct {
4242 fnOnlyDataVisit fnOnlyDataVisit
4343 allocatedNames []string
4444 currentScope *js_ast.Scope
45- scopesForCurrentPart [] *js_ast.Scope
45+ currentPart *js_ast.Part
4646 symbols []ast.Symbol
4747 astHelpers js_ast.HelperContext
4848 tsUseCounts []uint32
@@ -53,10 +53,6 @@ type parser struct {
5353 exprComments map[logger.Loc][]string
5454 mangledProps map[string]ast.Ref
5555 reservedProps map[string]bool
56- symbolUses map[ast.Ref]js_ast.SymbolUse
57- importSymbolPropertyUses map[ast.Ref]map[string]js_ast.SymbolUse
58- symbolCallUses map[ast.Ref]js_ast.SymbolCallUse
59- declaredSymbols []js_ast.DeclaredSymbol
6056 globPatternImports []globPatternImport
6157 runtimeImports map[string]ast.LocRef
6258 duplicateCaseChecker duplicateCaseChecker
@@ -115,9 +111,8 @@ type parser struct {
115111 enclosingNamespaceArgRef *ast.Ref
116112
117113 // Imports (both ES6 and CommonJS) are tracked at the top level
118- importRecords []ast.ImportRecord
119- importRecordsForCurrentPart []uint32
120- exportStarImportRecords []uint32
114+ importRecords []ast.ImportRecord
115+ exportStarImportRecords []uint32
121116
122117 // These are for handling ES6 imports and exports
123118 importItemsForNamespace map[ast.Ref]namespaceImportItems
@@ -1735,9 +1730,11 @@ func (p *parser) recordUsage(ref ast.Ref) {
17351730 // code regions since those will be culled.
17361731 if !p.isControlFlowDead {
17371732 p.symbols[ref.InnerIndex].UseCountEstimate++
1738- use := p.symbolUses[ref]
1739- use.CountEstimate++
1740- p.symbolUses[ref] = use
1733+ if part := p.currentPart; part != nil {
1734+ use := part.SymbolUses[ref]
1735+ use.CountEstimate++
1736+ part.SymbolUses[ref] = use
1737+ }
17411738 }
17421739
17431740 // The correctness of TypeScript-to-JavaScript conversion relies on accurate
@@ -1754,13 +1751,13 @@ func (p *parser) ignoreUsage(ref ast.Ref) {
17541751 p.symbols[ref.InnerIndex].UseCountEstimate--
17551752
17561753 // Only remove this usage if we're currently processing a part
1757- if p.symbolUses != nil {
1758- use := p.symbolUses [ref]
1754+ if part := p.currentPart; part != nil {
1755+ use := part.SymbolUses [ref]
17591756 use.CountEstimate--
17601757 if use.CountEstimate == 0 {
1761- delete(p.symbolUses , ref)
1758+ delete(part.SymbolUses , ref)
17621759 } else {
1763- p.symbolUses [ref] = use
1760+ part.SymbolUses [ref] = use
17641761 }
17651762 }
17661763 }
@@ -8670,7 +8667,10 @@ func (p *parser) pushScopeForVisitPass(kind js_ast.ScopeKind, loc logger.Loc) {
86708667
86718668 p.scopesInOrder = p.scopesInOrder[1:]
86728669 p.currentScope = order.scope
8673- p.scopesForCurrentPart = append(p.scopesForCurrentPart, order.scope)
8670+
8671+ if part := p.currentPart; part != nil {
8672+ part.Scopes = append(part.Scopes, order.scope)
8673+ }
86748674}
86758675
86768676type findSymbolResult struct {
@@ -10070,7 +10070,7 @@ func (p *parser) recordDeclaredSymbol(ref ast.Ref) {
1007010070 }
1007110071 }
1007210072
10073- p.declaredSymbols = append(p.declaredSymbols , js_ast.DeclaredSymbol{
10073+ p.currentPart.DeclaredSymbols = append(p.currentPart.DeclaredSymbols , js_ast.DeclaredSymbol{
1007410074 Ref: ref,
1007510075 IsTopLevel: isTopLevel,
1007610076 })
@@ -13129,22 +13129,25 @@ func (p *parser) maybeRewritePropertyAccess(
1312913129 if p.options.mode == config.ModeBundle && !p.isControlFlowDead {
1313013130 if id, ok := target.Data.(*js_ast.EImportIdentifier); ok {
1313113131 // Remove the normal symbol use
13132- use := p.symbolUses[id.Ref]
13132+ symbolUses := p.currentPart.SymbolUses
13133+ use := symbolUses[id.Ref]
1313313134 use.CountEstimate--
1313413135 if use.CountEstimate == 0 {
13135- delete(p. symbolUses, id.Ref)
13136+ delete(symbolUses, id.Ref)
1313613137 } else {
13137- p. symbolUses[id.Ref] = use
13138+ symbolUses[id.Ref] = use
1313813139 }
1313913140
1314013141 // Add a special symbol use instead
13141- if p.importSymbolPropertyUses == nil {
13142- p.importSymbolPropertyUses = make(map[ast.Ref]map[string]js_ast.SymbolUse)
13142+ importSymbolPropertyUses := p.currentPart.ImportSymbolPropertyUses
13143+ if importSymbolPropertyUses == nil {
13144+ importSymbolPropertyUses = make(map[ast.Ref]map[string]js_ast.SymbolUse)
13145+ p.currentPart.ImportSymbolPropertyUses = importSymbolPropertyUses
1314313146 }
13144- properties := p. importSymbolPropertyUses[id.Ref]
13147+ properties := importSymbolPropertyUses[id.Ref]
1314513148 if properties == nil {
1314613149 properties = make(map[string]js_ast.SymbolUse)
13147- p. importSymbolPropertyUses[id.Ref] = properties
13150+ importSymbolPropertyUses[id.Ref] = properties
1314813151 }
1314913152 use = properties[name]
1315013153 use.CountEstimate++
@@ -15167,7 +15170,7 @@ func (p *parser) visitExprInOut(expr js_ast.Expr, in exprIn) (js_ast.Expr, exprO
1516715170 record.Flags |= ast.HandlesImportErrors
1516815171 record.ErrorHandlerLoc = p.thenCatchChain.catchLoc
1516915172 }
15170- p.importRecordsForCurrentPart = append(p.importRecordsForCurrentPart , importRecordIndex)
15173+ p.currentPart.ImportRecordIndices = append(p.currentPart.ImportRecordIndices , importRecordIndex)
1517115174 return js_ast.Expr{Loc: expr.Loc, Data: &js_ast.EImportString{
1517215175 ImportRecordIndex: importRecordIndex,
1517315176 CloseParenLoc: e.CloseParenLoc,
@@ -15537,7 +15540,7 @@ func (p *parser) visitExprInOut(expr js_ast.Expr, in exprIn) (js_ast.Expr, exprO
1553715540 record.Flags |= ast.HandlesImportErrors
1553815541 record.ErrorHandlerLoc = p.fnOrArrowDataVisit.tryCatchLoc
1553915542 }
15540- p.importRecordsForCurrentPart = append(p.importRecordsForCurrentPart , importRecordIndex)
15543+ p.currentPart.ImportRecordIndices = append(p.currentPart.ImportRecordIndices , importRecordIndex)
1554115544
1554215545 // Create a new expression to represent the operation
1554315546 return js_ast.Expr{Loc: expr.Loc, Data: &js_ast.ERequireResolveString{
@@ -15755,7 +15758,7 @@ func (p *parser) visitExprInOut(expr js_ast.Expr, in exprIn) (js_ast.Expr, exprO
1575515758 record.Flags |= ast.HandlesImportErrors
1575615759 record.ErrorHandlerLoc = p.fnOrArrowDataVisit.tryCatchLoc
1575715760 }
15758- p.importRecordsForCurrentPart = append(p.importRecordsForCurrentPart , importRecordIndex)
15761+ p.currentPart.ImportRecordIndices = append(p.currentPart.ImportRecordIndices , importRecordIndex)
1575915762
1576015763 // Currently "require" is not converted into "import" for ESM
1576115764 if p.options.mode != config.ModeBundle && p.options.outputFormat == config.FormatESModule && !omitWarnings {
@@ -16830,24 +16833,27 @@ func (p *parser) globPatternFromExpr(expr js_ast.Expr) ([]globPart, logger.Range
1683016833
1683116834func (p *parser) convertSymbolUseToCall(ref ast.Ref, isSingleNonSpreadArgCall bool) {
1683216835 // Remove the normal symbol use
16833- use := p.symbolUses[ref]
16836+ symbolUses := p.currentPart.SymbolUses
16837+ use := symbolUses[ref]
1683416838 use.CountEstimate--
1683516839 if use.CountEstimate == 0 {
16836- delete(p. symbolUses, ref)
16840+ delete(symbolUses, ref)
1683716841 } else {
16838- p. symbolUses[ref] = use
16842+ symbolUses[ref] = use
1683916843 }
1684016844
1684116845 // Add a special symbol use instead
16842- if p.symbolCallUses == nil {
16843- p.symbolCallUses = make(map[ast.Ref]js_ast.SymbolCallUse)
16846+ symbolCallUses := p.currentPart.SymbolCallUses
16847+ if symbolCallUses == nil {
16848+ symbolCallUses = make(map[ast.Ref]js_ast.SymbolCallUse)
16849+ p.currentPart.SymbolCallUses = symbolCallUses
1684416850 }
16845- callUse := p. symbolCallUses[ref]
16851+ callUse := symbolCallUses[ref]
1684616852 callUse.CallCountEstimate++
1684716853 if isSingleNonSpreadArgCall {
1684816854 callUse.SingleArgNonSpreadCallCountEstimate++
1684916855 }
16850- p. symbolCallUses[ref] = callUse
16856+ symbolCallUses[ref] = callUse
1685116857}
1685216858
1685316859func (p *parser) warnAboutImportNamespaceCall(target js_ast.Expr, kind importNamespaceCallKind) {
@@ -17513,7 +17519,7 @@ func (p *parser) scanForImportsAndExports(stmts []js_ast.Stmt) (result importsEx
1751317519 }
1751417520
1751517521 // Also record these automatically-generated top-level namespace alias symbols
17516- p.declaredSymbols = append(p.declaredSymbols , js_ast.DeclaredSymbol{
17522+ p.currentPart.DeclaredSymbols = append(p.currentPart.DeclaredSymbols , js_ast.DeclaredSymbol{
1751717523 Ref: name.Ref,
1751817524 IsTopLevel: true,
1751917525 })
@@ -17551,7 +17557,7 @@ func (p *parser) scanForImportsAndExports(stmts []js_ast.Stmt) (result importsEx
1755117557 }
1755217558 }
1755317559
17554- p.importRecordsForCurrentPart = append(p.importRecordsForCurrentPart , s.ImportRecordIndex)
17560+ p.currentPart.ImportRecordIndices = append(p.currentPart.ImportRecordIndices , s.ImportRecordIndex)
1755517561
1755617562 if s.StarNameLoc != nil {
1755717563 record.Flags |= ast.ContainsImportStar
@@ -17602,7 +17608,7 @@ func (p *parser) scanForImportsAndExports(stmts []js_ast.Stmt) (result importsEx
1760217608
1760317609 case *js_ast.SExportStar:
1760417610 record := &p.importRecords[s.ImportRecordIndex]
17605- p.importRecordsForCurrentPart = append(p.importRecordsForCurrentPart , s.ImportRecordIndex)
17611+ p.currentPart.ImportRecordIndices = append(p.currentPart.ImportRecordIndices , s.ImportRecordIndex)
1760617612
1760717613 if s.Alias != nil {
1760817614 // "export * as ns from 'path'"
@@ -17623,7 +17629,7 @@ func (p *parser) scanForImportsAndExports(stmts []js_ast.Stmt) (result importsEx
1762317629
1762417630 case *js_ast.SExportFrom:
1762517631 record := &p.importRecords[s.ImportRecordIndex]
17626- p.importRecordsForCurrentPart = append(p.importRecordsForCurrentPart , s.ImportRecordIndex)
17632+ p.currentPart.ImportRecordIndices = append(p.currentPart.ImportRecordIndices , s.ImportRecordIndex)
1762717633
1762817634 for _, item := range s.Items {
1762917635 // Note that the imported alias is not item.Alias, which is the
@@ -17674,17 +17680,11 @@ func (p *parser) scanForImportsAndExports(stmts []js_ast.Stmt) (result importsEx
1767417680}
1767517681
1767617682func (p *parser) appendPart(parts []js_ast.Part, stmts []js_ast.Stmt) []js_ast.Part {
17677- p.symbolUses = make(map[ast.Ref]js_ast.SymbolUse)
17678- p.importSymbolPropertyUses = nil
17679- p.symbolCallUses = nil
17680- p.declaredSymbols = nil
17681- p.importRecordsForCurrentPart = nil
17682- p.scopesForCurrentPart = nil
17683-
1768417683 part := js_ast.Part{
17685- Stmts: p.visitStmtsAndPrependTempRefs(stmts, prependTempRefsOpts{}),
17686- SymbolUses: p.symbolUses,
17684+ SymbolUses: make(map[ast.Ref]js_ast.SymbolUse),
1768717685 }
17686+ p.currentPart = &part
17687+ part.Stmts = p.visitStmtsAndPrependTempRefs(stmts, prependTempRefsOpts{})
1768817688
1768917689 // Sanity check
1769017690 if p.currentScope != p.moduleScope {
@@ -17726,16 +17726,11 @@ func (p *parser) appendPart(parts []js_ast.Part, stmts []js_ast.Stmt) []js_ast.P
1772617726 flags |= js_ast.KeepExportClauses
1772717727 }
1772817728 part.CanBeRemovedIfUnused = p.astHelpers.StmtsCanBeRemovedIfUnused(part.Stmts, flags)
17729- part.DeclaredSymbols = p.declaredSymbols
17730- part.ImportRecordIndices = p.importRecordsForCurrentPart
17731- part.ImportSymbolPropertyUses = p.importSymbolPropertyUses
17732- part.SymbolCallUses = p.symbolCallUses
17733- part.Scopes = p.scopesForCurrentPart
1773417729 parts = append(parts, part)
1773517730 }
1773617731
1773717732 // Reset the state for this part so we don't accidentally mutate it
17738- p.symbolUses = nil
17733+ p.currentPart = nil
1773917734 return parts
1774017735}
1774117736
@@ -18146,9 +18141,14 @@ func LazyExportAST(log logger.Log, source logger.Source, options Options, expr j
1814618141 p := newParser(log, source, js_lexer.Lexer{}, &options)
1814718142 p.prepareForVisitPass()
1814818143
18144+ // Defer the actual code generation until linking
18145+ part := js_ast.Part{
18146+ SymbolUses: make(map[ast.Ref]js_ast.SymbolUse),
18147+ }
18148+
1814918149 // Optionally call a runtime API function to transform the expression
1815018150 if helperCall != nil {
18151- p.symbolUses = make(map[ast.Ref]js_ast.SymbolUse)
18151+ p.currentPart = &part
1815218152 if len(helperCall.Global) > 0 {
1815318153 ref := p.newSymbol(ast.SymbolUnbound, helperCall.Global[0])
1815418154 p.recordUsage(ref)
@@ -18162,21 +18162,16 @@ func LazyExportAST(log logger.Log, source logger.Source, options Options, expr j
1816218162 } else {
1816318163 expr = p.callRuntime(expr.Loc, helperCall.Runtime, []js_ast.Expr{expr})
1816418164 }
18165+ p.currentPart = nil
1816518166 }
18167+ part.Stmts = []js_ast.Stmt{{Loc: expr.Loc, Data: &js_ast.SLazyExport{Value: expr}}}
1816618168
1816718169 // Add an empty part for the namespace export that we can fill in later
1816818170 nsExportPart := js_ast.Part{
1816918171 SymbolUses: make(map[ast.Ref]js_ast.SymbolUse),
1817018172 CanBeRemovedIfUnused: true,
1817118173 }
1817218174
18173- // Defer the actual code generation until linking
18174- part := js_ast.Part{
18175- Stmts: []js_ast.Stmt{{Loc: expr.Loc, Data: &js_ast.SLazyExport{Value: expr}}},
18176- SymbolUses: p.symbolUses,
18177- }
18178- p.symbolUses = nil
18179-
1818018175 ast := p.toAST([]js_ast.Part{nsExportPart}, []js_ast.Part{part}, nil, "", nil)
1818118176 ast.HasLazyExport = true
1818218177 return ast
@@ -18205,22 +18200,22 @@ func GlobResolveAST(log logger.Log, source logger.Source, importRecords []ast.Im
1820518200 importRecordIndices = append(importRecordIndices, uint32(importRecordIndex))
1820618201 }
1820718202
18208- p.symbolUses = make(map[ast.Ref]js_ast.SymbolUse)
1820918203 ref := p.newSymbol(ast.SymbolOther, name)
1821018204 p.moduleScope.Generated = append(p.moduleScope.Generated, ref)
18211-
1821218205 part := js_ast.Part{
18213- Stmts: []js_ast.Stmt{{Data: &js_ast.SLocal{
18214- IsExport: true,
18215- Decls: []js_ast.Decl{{
18216- Binding: js_ast.Binding{Data: &js_ast.BIdentifier{Ref: ref}},
18217- ValueOrNil: p.callRuntime(logger.Loc{}, "__glob", []js_ast.Expr{{Data: object}}),
18218- }},
18219- }}},
1822018206 ImportRecordIndices: importRecordIndices,
18221- SymbolUses: p.symbolUses ,
18207+ SymbolUses: make(map[ast.Ref]js_ast.SymbolUse) ,
1822218208 }
18223- p.symbolUses = nil
18209+
18210+ p.currentPart = &part
18211+ part.Stmts = []js_ast.Stmt{{Data: &js_ast.SLocal{
18212+ IsExport: true,
18213+ Decls: []js_ast.Decl{{
18214+ Binding: js_ast.Binding{Data: &js_ast.BIdentifier{Ref: ref}},
18215+ ValueOrNil: p.callRuntime(logger.Loc{}, "__glob", []js_ast.Expr{{Data: object}}),
18216+ }},
18217+ }}}
18218+ p.currentPart = nil
1822418219
1822518220 p.esmExportKeyword.Len = 1
1822618221 return p.toAST([]js_ast.Part{nsExportPart}, []js_ast.Part{part}, nil, "", nil)
@@ -18676,17 +18671,13 @@ func (p *parser) toAST(before, parts, after []js_ast.Part, hashbang string, dire
1867618671 removedImportEquals := false
1867718672 partsEnd := 0
1867818673 for partIndex, part := range parts {
18679- p.importRecordsForCurrentPart = nil
18680- p.declaredSymbols = nil
18681-
18674+ p.currentPart = &part
1868218675 result := p.scanForImportsAndExports(part.Stmts)
18676+ p.currentPart = nil
1868318677 part.Stmts = result.stmts
1868418678 keptImportEquals = keptImportEquals || result.keptImportEquals
1868518679 removedImportEquals = removedImportEquals || result.removedImportEquals
1868618680
18687- part.ImportRecordIndices = append(part.ImportRecordIndices, p.importRecordsForCurrentPart...)
18688- part.DeclaredSymbols = append(part.DeclaredSymbols, p.declaredSymbols...)
18689-
1869018681 if len(part.Stmts) > 0 || uint32(partIndex) == js_ast.NSExportPartIndex {
1869118682 if p.moduleScope.ContainsDirectEval && len(part.DeclaredSymbols) > 0 {
1869218683 // If this file contains a direct call to "eval()", all parts that
0 commit comments