Source File
renamer.go
Belonging Package
github.com/evanw/esbuild/internal/renamer
for := range js_lexer.Keywords {
[] = 1
}
for := range js_lexer.StrictModeReservedWords {
[] = 1
}
for , := range {
for , := range .Members {
:= .Get(.Ref)
if .Kind == js_ast.SymbolUnbound || .MustNotBeRenamed {
[.OriginalName] = 1
}
}
for , := range .Generated {
:= .Get()
if .Kind == js_ast.SymbolUnbound || .MustNotBeRenamed {
[.OriginalName] = 1
}
}
}
return
}
type Renamer interface {
NameForSymbol(ref js_ast.Ref) string
}
type noOpRenamer struct {
symbols js_ast.SymbolMap
}
func ( js_ast.SymbolMap) Renamer {
return &noOpRenamer{
symbols: ,
}
}
func ( *noOpRenamer) ( js_ast.Ref) string {
= js_ast.FollowSymbols(.symbols, )
return .symbols.Get().OriginalName
}
type symbolSlot struct {
name string
count uint32
}
type MinifyRenamer struct {
symbols js_ast.SymbolMap
sortedBuffer StableRefArray
reservedNames map[string]uint32
slots [3][]symbolSlot
symbolToSlot map[js_ast.Ref]ast.Index32
}
func ( js_ast.SymbolMap, js_ast.SlotCounts, map[string]uint32) *MinifyRenamer {
return &MinifyRenamer{
symbols: ,
reservedNames: ,
slots: [3][]symbolSlot{
make([]symbolSlot, [0]),
make([]symbolSlot, [1]),
make([]symbolSlot, [2]),
},
symbolToSlot: make(map[js_ast.Ref]ast.Index32),
}
}
= js_ast.FollowSymbols(.symbols, )
:= .symbols.Get()
:= .SlotNamespace()
if == js_ast.SlotMustNotBeRenamed {
return .OriginalName
}
:= .NestedScopeSlot
if !.IsValid() {
var bool
, = .symbolToSlot[]
return .OriginalName
}
}
return .slots[][.GetIndex()].name
}
.sortedBuffer = .sortedBuffer[:0]
for := range {
.sortedBuffer = append(.sortedBuffer, StableRef{
StableOuterIndex: [.OuterIndex],
Ref: ,
})
}
sort.Sort(.sortedBuffer)
for , := range .sortedBuffer {
.AccumulateSymbolCount(.Ref, [.Ref].CountEstimate)
}
}
= js_ast.FollowSymbols(.symbols, )
:= .symbols.Get()
for .NamespaceAlias != nil {
= js_ast.FollowSymbols(.symbols, .NamespaceAlias.NamespaceRef)
= .symbols.Get()
}
:= .SlotNamespace()
if == js_ast.SlotMustNotBeRenamed {
return
}
:= &.slots[]
:= .NestedScopeSlot
if !.IsValid() {
var bool
, = .symbolToSlot[]
if ! {
= ast.MakeIndex32(uint32(len(*)))
* = append(*, symbolSlot{})
.symbolToSlot[] =
}
}
(*)[.GetIndex()].count +=
}
func ( *MinifyRenamer) ( *js_ast.NameMinifier) {
:= make(slotAndCountArray, len())
for , := range {
[] = slotAndCount{slot: uint32(), count: .count}
}
sort.Sort()
:= 0
for , := range {
:= .NumberToMinifiedName()
++
switch js_ast.SlotNamespace() {
case js_ast.SlotDefault:
for .reservedNames[] != 0 {
= .NumberToMinifiedName()
++
}
case js_ast.SlotLabel:
for js_lexer.Keywords[] != 0 {
= .NumberToMinifiedName()
++
}
}
if js_ast.SlotNamespace() == js_ast.SlotPrivateName {
= "#" +
}
[.slot].name =
}
}
}
:= ast.MakeIndex32(1)
for , := range .Members {
[.Ref.InnerIndex].NestedScopeSlot =
}
for , := range .Generated {
[.InnerIndex].NestedScopeSlot =
}
for , := range .Children {
.UnionMax(assignNestedScopeSlotsHelper(, , js_ast.SlotCounts{}))
}
for , := range .Members {
[.Ref.InnerIndex].NestedScopeSlot = ast.Index32{}
}
for , := range .Generated {
[.InnerIndex].NestedScopeSlot = ast.Index32{}
}
return
}
for , := range {
:= &[]
if := .SlotNamespace(); != js_ast.SlotMustNotBeRenamed && !.NestedScopeSlot.IsValid() {
.NestedScopeSlot = ast.MakeIndex32([])
[]++
}
}
for , := range .Generated {
:= &[.InnerIndex]
if := .SlotNamespace(); != js_ast.SlotMustNotBeRenamed && !.NestedScopeSlot.IsValid() {
.NestedScopeSlot = ast.MakeIndex32([])
[]++
}
}
if .LabelRef != js_ast.InvalidRef {
:= &[.LabelRef.InnerIndex]
.NestedScopeSlot = ast.MakeIndex32([js_ast.SlotLabel])
[js_ast.SlotLabel]++
}
type slotAndCountArray []slotAndCount
func ( slotAndCountArray) () int { return len() }
func ( slotAndCountArray) ( int, int) { [], [] = [], [] }
func ( slotAndCountArray) ( int, int) bool {
, := [], []
return .count > .count || (.count == .count && .slot < .slot)
}
type StableRef struct {
StableOuterIndex uint32
Ref js_ast.Ref
}
type StableRefArray []StableRef
func ( StableRefArray) () int { return len() }
func ( StableRefArray) ( int, int) { [], [] = [], [] }
func ( StableRefArray) ( int, int) bool {
, := [], []
return .StableOuterIndex > .StableOuterIndex || (.StableOuterIndex == .StableOuterIndex && .Ref.InnerIndex < .Ref.InnerIndex)
}
type NumberRenamer struct {
symbols js_ast.SymbolMap
names [][]string
root numberScope
}
func ( js_ast.SymbolMap, map[string]uint32) *NumberRenamer {
return &NumberRenamer{
symbols: ,
names: make([][]string, len(.Outer)),
root: numberScope{nameCounts: },
}
}
func ( *NumberRenamer) ( js_ast.Ref) string {
= js_ast.FollowSymbols(.symbols, )
if := .names[.OuterIndex]; != nil {
if := [.InnerIndex]; != "" {
return
}
}
return .symbols.Get().OriginalName
}
func ( *NumberRenamer) ( js_ast.Ref) {
.assignName(&.root, )
}
func ( *NumberRenamer) ( *numberScope, js_ast.Ref) {
= js_ast.FollowSymbols(.symbols, )
:= .names[.OuterIndex]
if != nil && [.InnerIndex] != "" {
return
}
:= .symbols.Get()
if .SlotNamespace() != js_ast.SlotDefault {
return
}
:= .findUnusedName(.OriginalName)
= make([]string, len(.symbols.Outer[.OuterIndex]))
.names[.OuterIndex] =
}
[.InnerIndex] =
}
func ( *NumberRenamer) ( *js_ast.Scope, uint32, *numberScope, *[]int) {
:= &numberScope{parent: , nameCounts: make(map[string]uint32)}
for , := range * {
.assignName(, js_ast.Ref{OuterIndex: , InnerIndex: uint32()})
}
for , := range .Generated {
.assignName(, )
}
for , := range {
go func( uint32, []*js_ast.Scope) {
var []int
for , := range {
.assignNamesRecursive(, , &.root, &)
}
.Done()
}(, )
}
.Wait()
}
type numberScope struct {
parent *numberScope
nameCounts map[string]uint32
}
type nameUse uint8
const (
nameUnused nameUse = iota
nameUsed
nameUsedInSameScope
)
func ( *numberScope) ( string) nameUse {
:=
for {
if , := .nameCounts[]; {
if == {
return nameUsedInSameScope
}
return nameUsed
}
= .parent
if == nil {
return nameUnused
}
}
}
func ( *numberScope) ( string) string {
= js_lexer.ForceValidIdentifier()
:= uint32(1)
= .nameCounts[]
}
:=
if == nameUsedInSameScope {
.nameCounts[] =
}
break
}
}
}
.nameCounts[] = 1
return
}
type ExportRenamer struct {
count int
used map[string]uint32
}
func ( *ExportRenamer) ( string) string {
if .used == nil {
.used = make(map[string]uint32)
}
if , := .used[]; {
:=
for {
++
= + strconv.Itoa(int())
if , := .used[]; ! {
break
}
}
.used[] =
} else {
.used[] = 1
}
return
}
func ( *ExportRenamer) () string {
:= js_ast.DefaultNameMinifier.NumberToMinifiedName(.count)
.count++
return
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |