Skip to content

Commit 56698ab

Browse files
committed
rename private symbols so generating them is safe
1 parent 109c94e commit 56698ab

File tree

3 files changed

+24
-12
lines changed

3 files changed

+24
-12
lines changed

internal/bundler_tests/snapshots/snapshots_default.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4925,9 +4925,9 @@ TestRenamePrivateIdentifiersNoBundle
49254925
class Foo {
49264926
#foo;
49274927
foo = class {
4928-
#foo;
49294928
#foo2;
4930-
#bar;
4929+
#foo22;
4930+
#bar2;
49314931
};
49324932
get #bar() {
49334933
}
@@ -4938,8 +4938,8 @@ class Bar {
49384938
#foo;
49394939
foo = class {
49404940
#foo2;
4941-
#foo;
4942-
#bar;
4941+
#foo3;
4942+
#bar2;
49434943
};
49444944
get #bar() {
49454945
}

internal/js_ast/js_ident.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,14 @@ func IsIdentifierES5AndESNext(text string) bool {
4242
return true
4343
}
4444

45-
func ForceValidIdentifier(text string) string {
46-
if IsIdentifier(text) {
47-
return text
48-
}
45+
func ForceValidIdentifier(prefix string, text string) string {
4946
sb := strings.Builder{}
5047

48+
// Private identifiers must be prefixed by "#"
49+
if prefix != "" {
50+
sb.WriteString(prefix)
51+
}
52+
5153
// Identifier start
5254
c, width := utf8.DecodeRuneInString(text)
5355
text = text[width:]

internal/renamer/renamer.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,8 @@ func (r *NumberRenamer) assignName(scope *numberScope, ref js_ast.Ref) {
439439

440440
// Don't rename unbound symbols, symbols marked as reserved names, labels, or private names
441441
symbol := r.symbols.Get(ref)
442-
if symbol.SlotNamespace() != js_ast.SlotDefault {
442+
ns := symbol.SlotNamespace()
443+
if ns != js_ast.SlotDefault && ns != js_ast.SlotPrivateName {
443444
return
444445
}
445446

@@ -452,7 +453,7 @@ func (r *NumberRenamer) assignName(scope *numberScope, ref js_ast.Ref) {
452453
}
453454

454455
// Compute a new name
455-
name := scope.findUnusedName(originalName)
456+
name := scope.findUnusedName(originalName, ns)
456457

457458
// Store the new name
458459
if inner == nil {
@@ -576,8 +577,17 @@ func (s *numberScope) findNameUse(name string) nameUse {
576577
}
577578
}
578579

579-
func (s *numberScope) findUnusedName(name string) string {
580-
name = js_ast.ForceValidIdentifier(name)
580+
func (s *numberScope) findUnusedName(name string, ns js_ast.SlotNamespace) string {
581+
// We may not have a valid identifier if this is an internally-constructed name
582+
if ns == js_ast.SlotPrivateName {
583+
if id := name[1:]; !js_ast.IsIdentifier(id) {
584+
name = js_ast.ForceValidIdentifier("#", id)
585+
}
586+
} else {
587+
if !js_ast.IsIdentifier(name) {
588+
name = js_ast.ForceValidIdentifier("", name)
589+
}
590+
}
581591

582592
if use := s.findNameUse(name); use != nameUnused {
583593
// If the name is already in use, generate a new name by appending a number

0 commit comments

Comments
 (0)