Skip to content

Commit

Permalink
abstract out binding visiting code
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Jun 23, 2023
1 parent 7114026 commit 69a0c80
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 160 deletions.
28 changes: 28 additions & 0 deletions internal/js_ast/js_ast_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2566,3 +2566,31 @@ func MangleIfExpr(loc logger.Loc, e *EIf, unsupportedFeatures compat.JSFeature,

return Expr{Loc: loc, Data: e}
}

func ForEachIdentifierBindingInDecls(decls []Decl, callback func(loc logger.Loc, b *BIdentifier)) {
for _, decl := range decls {
ForEachIdentifierBinding(decl.Binding, callback)
}
}

func ForEachIdentifierBinding(binding Binding, callback func(loc logger.Loc, b *BIdentifier)) {
switch b := binding.Data.(type) {
case *BMissing:

case *BIdentifier:
callback(binding.Loc, b)

case *BArray:
for _, item := range b.Items {
ForEachIdentifierBinding(item.Binding, callback)
}

case *BObject:
for _, property := range b.Properties {
ForEachIdentifierBinding(property.Value, callback)
}

default:
panic("Internal error")
}
}
112 changes: 12 additions & 100 deletions internal/js_parser/js_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -1562,28 +1562,11 @@ func (p *parser) hoistSymbols(scope *js_ast.Scope) {
}

func (p *parser) declareBinding(kind js_ast.SymbolKind, binding js_ast.Binding, opts parseStmtOpts) {
switch b := binding.Data.(type) {
case *js_ast.BMissing:

case *js_ast.BIdentifier:
name := p.loadNameFromRef(b.Ref)
js_ast.ForEachIdentifierBinding(binding, func(loc logger.Loc, b *js_ast.BIdentifier) {
if !opts.isTypeScriptDeclare || (opts.isNamespaceScope && opts.isExport) {
b.Ref = p.declareSymbol(kind, binding.Loc, name)
}

case *js_ast.BArray:
for _, i := range b.Items {
p.declareBinding(kind, i.Binding, opts)
}

case *js_ast.BObject:
for _, property := range b.Properties {
p.declareBinding(kind, property.Value, opts)
b.Ref = p.declareSymbol(kind, binding.Loc, p.loadNameFromRef(b.Ref))
}

default:
panic("Internal error")
}
})
}

func (p *parser) recordUsage(ref js_ast.Ref) {
Expand Down Expand Up @@ -7874,9 +7857,9 @@ func (p *parser) parseStmt(opts parseStmtOpts) js_ast.Stmt {
if opts.isNamespaceScope && opts.isExport {
var decls []js_ast.Decl
if s, ok := stmt.Data.(*js_ast.SLocal); ok {
for _, decl := range s.Decls {
decls = extractDeclsForBinding(decl.Binding, decls)
}
js_ast.ForEachIdentifierBindingInDecls(s.Decls, func(loc logger.Loc, b *js_ast.BIdentifier) {
decls = append(decls, js_ast.Decl{Binding: js_ast.Binding{Loc: loc, Data: b}})
})
}
if len(decls) > 0 {
return js_ast.Stmt{Loc: loc, Data: &js_ast.SLocal{
Expand All @@ -7899,30 +7882,6 @@ func (p *parser) parseStmt(opts parseStmtOpts) js_ast.Stmt {
}
}

func extractDeclsForBinding(binding js_ast.Binding, decls []js_ast.Decl) []js_ast.Decl {
switch b := binding.Data.(type) {
case *js_ast.BMissing:

case *js_ast.BIdentifier:
decls = append(decls, js_ast.Decl{Binding: binding})

case *js_ast.BArray:
for _, item := range b.Items {
decls = extractDeclsForBinding(item.Binding, decls)
}

case *js_ast.BObject:
for _, property := range b.Properties {
decls = extractDeclsForBinding(property.Value, decls)
}

default:
panic("Internal error")
}

return decls
}

func (p *parser) addImportRecord(kind ast.ImportKind, loc logger.Loc, text string, assertions *ast.ImportAssertions, flags ast.ImportRecordFlags) uint32 {
index := uint32(len(p.importRecords))
p.importRecords = append(p.importRecords, ast.ImportRecord{
Expand Down Expand Up @@ -10763,7 +10722,9 @@ func (p *parser) visitAndAppendStmt(stmts []js_ast.Stmt, stmt js_ast.Stmt) []js_
for _, childStmt := range s.Stmts {
if local, ok := childStmt.Data.(*js_ast.SLocal); ok {
if local.IsExport {
p.markExportedDeclsInsideNamespace(s.Arg, local.Decls)
js_ast.ForEachIdentifierBindingInDecls(local.Decls, func(loc logger.Loc, b *js_ast.BIdentifier) {
p.isExportedInsideNamespace[b.Ref] = s.Arg
})
}
}
}
Expand Down Expand Up @@ -10910,34 +10871,6 @@ func (p *parser) markExprAsParenthesized(value js_ast.Expr, openParenLoc logger.
}
}

func (p *parser) markExportedDeclsInsideNamespace(nsRef js_ast.Ref, decls []js_ast.Decl) {
for _, decl := range decls {
p.markExportedBindingInsideNamespace(nsRef, decl.Binding)
}
}

func (p *parser) markExportedBindingInsideNamespace(nsRef js_ast.Ref, binding js_ast.Binding) {
switch b := binding.Data.(type) {
case *js_ast.BMissing:

case *js_ast.BIdentifier:
p.isExportedInsideNamespace[b.Ref] = nsRef

case *js_ast.BArray:
for _, item := range b.Items {
p.markExportedBindingInsideNamespace(nsRef, item.Binding)
}

case *js_ast.BObject:
for _, property := range b.Properties {
p.markExportedBindingInsideNamespace(nsRef, property.Value)
}

default:
panic("Internal error")
}
}

func (p *parser) maybeTransposeIfExprChain(expr js_ast.Expr, visit func(js_ast.Expr) js_ast.Expr) js_ast.Expr {
if e, ok := expr.Data.(*js_ast.EIf); ok {
e.Yes = p.maybeTransposeIfExprChain(e.Yes, visit)
Expand Down Expand Up @@ -15665,27 +15598,6 @@ func (p *parser) recordExport(loc logger.Loc, alias string, ref js_ast.Ref) {
}
}

func (p *parser) recordExportedBinding(binding js_ast.Binding) {
switch b := binding.Data.(type) {
case *js_ast.BMissing:

case *js_ast.BIdentifier:
p.recordExport(binding.Loc, p.symbols[b.Ref.InnerIndex].OriginalName, b.Ref)

case *js_ast.BArray:
for _, item := range b.Items {
p.recordExportedBinding(item.Binding)
}

case *js_ast.BObject:
for _, item := range b.Properties {
p.recordExportedBinding(item.Value)
}
default:
panic("Internal error")
}
}

type importsExportsScanResult struct {
stmts []js_ast.Stmt
keptImportEquals bool
Expand Down Expand Up @@ -16006,9 +15918,9 @@ func (p *parser) scanForImportsAndExports(stmts []js_ast.Stmt) (result importsEx

case *js_ast.SLocal:
if s.IsExport {
for _, decl := range s.Decls {
p.recordExportedBinding(decl.Binding)
}
js_ast.ForEachIdentifierBindingInDecls(s.Decls, func(loc logger.Loc, b *js_ast.BIdentifier) {
p.recordExport(loc, p.symbols[b.Ref.InnerIndex].OriginalName, b.Ref)
})
}

// Remove unused import-equals statements, since those likely
Expand Down
44 changes: 9 additions & 35 deletions internal/js_parser/ts_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -1636,7 +1636,15 @@ func (p *parser) parseTypeScriptNamespaceStmt(loc logger.Loc, opts parseStmtOpts

case *js_ast.SLocal:
if s.IsExport {
p.exportDeclsInsideNamespace(exportedMembers, s.Decls)
js_ast.ForEachIdentifierBindingInDecls(s.Decls, func(loc logger.Loc, b *js_ast.BIdentifier) {
name := p.symbols[b.Ref.InnerIndex].OriginalName
member := js_ast.TSNamespaceMember{
Loc: loc,
Data: &js_ast.TSNamespaceMemberProperty{},
}
exportedMembers[name] = member
p.refToTSNamespaceMemberData[b.Ref] = member.Data
})
}
}
}
Expand Down Expand Up @@ -1712,40 +1720,6 @@ func (p *parser) parseTypeScriptNamespaceStmt(loc logger.Loc, opts parseStmtOpts
}}
}

func (p *parser) exportDeclsInsideNamespace(exportedMembers js_ast.TSNamespaceMembers, decls []js_ast.Decl) {
for _, decl := range decls {
p.exportBindingInsideNamespace(exportedMembers, decl.Binding)
}
}

func (p *parser) exportBindingInsideNamespace(exportedMembers js_ast.TSNamespaceMembers, binding js_ast.Binding) {
switch b := binding.Data.(type) {
case *js_ast.BMissing:

case *js_ast.BIdentifier:
name := p.symbols[b.Ref.InnerIndex].OriginalName
member := js_ast.TSNamespaceMember{
Loc: binding.Loc,
Data: &js_ast.TSNamespaceMemberProperty{},
}
exportedMembers[name] = member
p.refToTSNamespaceMemberData[b.Ref] = member.Data

case *js_ast.BArray:
for _, item := range b.Items {
p.exportBindingInsideNamespace(exportedMembers, item.Binding)
}

case *js_ast.BObject:
for _, property := range b.Properties {
p.exportBindingInsideNamespace(exportedMembers, property.Value)
}

default:
panic("Internal error")
}
}

func (p *parser) generateClosureForTypeScriptNamespaceOrEnum(
stmts []js_ast.Stmt, stmtLoc logger.Loc, isExport bool, nameLoc logger.Loc,
nameRef js_ast.Ref, argRef js_ast.Ref, stmtsInsideClosure []js_ast.Stmt,
Expand Down
28 changes: 3 additions & 25 deletions internal/linker/linker.go
Original file line number Diff line number Diff line change
Expand Up @@ -5871,28 +5871,6 @@ func hashWriteLengthPrefixed(hash hash.Hash, bytes []byte) {
hash.Write(bytes)
}

func preventBindingsFromBeingRenamed(binding js_ast.Binding, symbols js_ast.SymbolMap) {
switch b := binding.Data.(type) {
case *js_ast.BMissing:

case *js_ast.BIdentifier:
symbols.Get(b.Ref).Flags |= js_ast.MustNotBeRenamed

case *js_ast.BArray:
for _, i := range b.Items {
preventBindingsFromBeingRenamed(i.Binding, symbols)
}

case *js_ast.BObject:
for _, p := range b.Properties {
preventBindingsFromBeingRenamed(p.Value, symbols)
}

default:
panic(fmt.Sprintf("Unexpected binding of type %T", binding.Data))
}
}

// Marking a symbol as unbound prevents it from being renamed or minified.
// This is only used when a module is compiled independently. We use a very
// different way of handling exports and renaming/minifying when bundling.
Expand Down Expand Up @@ -5921,9 +5899,9 @@ func (c *linkerContext) preventExportsFromBeingRenamed(sourceIndex uint32) {

case *js_ast.SLocal:
if s.IsExport {
for _, decl := range s.Decls {
preventBindingsFromBeingRenamed(decl.Binding, c.graph.Symbols)
}
js_ast.ForEachIdentifierBindingInDecls(s.Decls, func(loc logger.Loc, b *js_ast.BIdentifier) {
c.graph.Symbols.Get(b.Ref).Flags |= js_ast.MustNotBeRenamed
})
hasImportOrExport = true
}

Expand Down

0 comments on commit 69a0c80

Please sign in to comment.