From 69a0c80572d9544ef4883b1c26db2fb0539f5dad Mon Sep 17 00:00:00 2001 From: Evan Wallace Date: Fri, 23 Jun 2023 14:51:33 -0400 Subject: [PATCH] abstract out binding visiting code --- internal/js_ast/js_ast_helpers.go | 28 ++++++++ internal/js_parser/js_parser.go | 112 ++++-------------------------- internal/js_parser/ts_parser.go | 44 +++--------- internal/linker/linker.go | 28 +------- 4 files changed, 52 insertions(+), 160 deletions(-) diff --git a/internal/js_ast/js_ast_helpers.go b/internal/js_ast/js_ast_helpers.go index 3798ec3575f..cb98a5dae32 100644 --- a/internal/js_ast/js_ast_helpers.go +++ b/internal/js_ast/js_ast_helpers.go @@ -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") + } +} diff --git a/internal/js_parser/js_parser.go b/internal/js_parser/js_parser.go index 82474392bc6..bab09f7aacd 100644 --- a/internal/js_parser/js_parser.go +++ b/internal/js_parser/js_parser.go @@ -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) { @@ -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{ @@ -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{ @@ -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 + }) } } } @@ -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) @@ -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 @@ -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 diff --git a/internal/js_parser/ts_parser.go b/internal/js_parser/ts_parser.go index 2cd517699c5..f076dd6db8b 100644 --- a/internal/js_parser/ts_parser.go +++ b/internal/js_parser/ts_parser.go @@ -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 + }) } } } @@ -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, diff --git a/internal/linker/linker.go b/internal/linker/linker.go index 2c407e07cef..844c5c5b68a 100644 --- a/internal/linker/linker.go +++ b/internal/linker/linker.go @@ -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. @@ -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 }