Skip to content

Commit 71b3d9f

Browse files
committed
split off lowerPrivateMethod from lowerMethod
1 parent 0861625 commit 71b3d9f

File tree

1 file changed

+64
-60
lines changed

1 file changed

+64
-60
lines changed

internal/js_parser/js_parser_lower_class.go

Lines changed: 64 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -897,74 +897,78 @@ func (ctx *lowerClassContext) lowerField(
897897
return prop, true
898898
}
899899

900-
// If this returns true, the method property should be dropped as it has
901-
// already been accounted for elsewhere (e.g. a lowered private method).
902-
func (ctx *lowerClassContext) lowerMethod(p *parser, prop js_ast.Property, private *js_ast.EPrivateIdentifier) bool {
903-
if private != nil && p.privateSymbolNeedsToBeLowered(private) {
904-
// All private methods can share the same WeakSet
905-
var ref *ast.Ref
900+
func (ctx *lowerClassContext) lowerPrivateMethod(p *parser, prop js_ast.Property, private *js_ast.EPrivateIdentifier) {
901+
// All private methods can share the same WeakSet
902+
var ref *ast.Ref
903+
if prop.Flags.Has(js_ast.PropertyIsStatic) {
904+
ref = &ctx.privateStaticMethodRef
905+
} else {
906+
ref = &ctx.privateInstanceMethodRef
907+
}
908+
if *ref == ast.InvalidRef {
909+
// Generate a new symbol to store the WeakSet
910+
var name string
906911
if prop.Flags.Has(js_ast.PropertyIsStatic) {
907-
ref = &ctx.privateStaticMethodRef
912+
name = "_static"
908913
} else {
909-
ref = &ctx.privateInstanceMethodRef
914+
name = "_instances"
910915
}
911-
if *ref == ast.InvalidRef {
912-
// Generate a new symbol to store the WeakSet
913-
var name string
914-
if prop.Flags.Has(js_ast.PropertyIsStatic) {
915-
name = "_static"
916-
} else {
917-
name = "_instances"
918-
}
919-
if ctx.optionalNameHint != "" {
920-
name = fmt.Sprintf("_%s%s", ctx.optionalNameHint, name)
921-
}
922-
*ref = p.generateTempRef(tempRefNeedsDeclare, name)
916+
if ctx.optionalNameHint != "" {
917+
name = fmt.Sprintf("_%s%s", ctx.optionalNameHint, name)
918+
}
919+
*ref = p.generateTempRef(tempRefNeedsDeclare, name)
923920

924-
// Generate the initializer
925-
if p.weakSetRef == ast.InvalidRef {
926-
p.weakSetRef = p.newSymbol(ast.SymbolUnbound, "WeakSet")
927-
p.moduleScope.Generated = append(p.moduleScope.Generated, p.weakSetRef)
928-
}
929-
ctx.privateMembers = append(ctx.privateMembers, js_ast.Assign(
930-
js_ast.Expr{Loc: ctx.classLoc, Data: &js_ast.EIdentifier{Ref: *ref}},
931-
js_ast.Expr{Loc: ctx.classLoc, Data: &js_ast.ENew{Target: js_ast.Expr{Loc: ctx.classLoc, Data: &js_ast.EIdentifier{Ref: p.weakSetRef}}}},
932-
))
933-
p.recordUsage(*ref)
934-
p.recordUsage(p.weakSetRef)
921+
// Generate the initializer
922+
if p.weakSetRef == ast.InvalidRef {
923+
p.weakSetRef = p.newSymbol(ast.SymbolUnbound, "WeakSet")
924+
p.moduleScope.Generated = append(p.moduleScope.Generated, p.weakSetRef)
925+
}
926+
ctx.privateMembers = append(ctx.privateMembers, js_ast.Assign(
927+
js_ast.Expr{Loc: ctx.classLoc, Data: &js_ast.EIdentifier{Ref: *ref}},
928+
js_ast.Expr{Loc: ctx.classLoc, Data: &js_ast.ENew{Target: js_ast.Expr{Loc: ctx.classLoc, Data: &js_ast.EIdentifier{Ref: p.weakSetRef}}}},
929+
))
930+
p.recordUsage(*ref)
931+
p.recordUsage(p.weakSetRef)
935932

936-
// Determine what to store in the WeakSet
937-
var target js_ast.Expr
938-
if prop.Flags.Has(js_ast.PropertyIsStatic) {
939-
target = ctx.nameFunc()
940-
} else {
941-
target = js_ast.Expr{Loc: ctx.classLoc, Data: js_ast.EThisShared}
942-
}
933+
// Determine what to store in the WeakSet
934+
var target js_ast.Expr
935+
if prop.Flags.Has(js_ast.PropertyIsStatic) {
936+
target = ctx.nameFunc()
937+
} else {
938+
target = js_ast.Expr{Loc: ctx.classLoc, Data: js_ast.EThisShared}
939+
}
943940

944-
// Add every newly-constructed instance into this set
945-
methodExpr := p.callRuntime(ctx.classLoc, "__privateAdd", []js_ast.Expr{
946-
target,
947-
{Loc: ctx.classLoc, Data: &js_ast.EIdentifier{Ref: *ref}},
948-
})
949-
p.recordUsage(*ref)
941+
// Add every newly-constructed instance into this set
942+
methodExpr := p.callRuntime(ctx.classLoc, "__privateAdd", []js_ast.Expr{
943+
target,
944+
{Loc: ctx.classLoc, Data: &js_ast.EIdentifier{Ref: *ref}},
945+
})
946+
p.recordUsage(*ref)
950947

951-
// Make sure that adding to the map happens before any field
952-
// initializers to handle cases like this:
953-
//
954-
// class A {
955-
// pub = this.#priv;
956-
// #priv() {}
957-
// }
958-
//
959-
if prop.Flags.Has(js_ast.PropertyIsStatic) {
960-
// Move this property to an assignment after the class ends
961-
ctx.staticPrivateMethods = append(ctx.staticPrivateMethods, methodExpr)
962-
} else {
963-
// Move this property to an assignment inside the class constructor
964-
ctx.instancePrivateMethods = append(ctx.instancePrivateMethods, js_ast.Stmt{Loc: ctx.classLoc, Data: &js_ast.SExpr{Value: methodExpr}})
965-
}
948+
// Make sure that adding to the map happens before any field
949+
// initializers to handle cases like this:
950+
//
951+
// class A {
952+
// pub = this.#priv;
953+
// #priv() {}
954+
// }
955+
//
956+
if prop.Flags.Has(js_ast.PropertyIsStatic) {
957+
// Move this property to an assignment after the class ends
958+
ctx.staticPrivateMethods = append(ctx.staticPrivateMethods, methodExpr)
959+
} else {
960+
// Move this property to an assignment inside the class constructor
961+
ctx.instancePrivateMethods = append(ctx.instancePrivateMethods, js_ast.Stmt{Loc: ctx.classLoc, Data: &js_ast.SExpr{Value: methodExpr}})
966962
}
967-
p.symbols[private.Ref.InnerIndex].Link = *ref
963+
}
964+
p.symbols[private.Ref.InnerIndex].Link = *ref
965+
}
966+
967+
// If this returns true, the method property should be dropped as it has
968+
// already been accounted for elsewhere (e.g. a lowered private method).
969+
func (ctx *lowerClassContext) lowerMethod(p *parser, prop js_ast.Property, private *js_ast.EPrivateIdentifier) bool {
970+
if private != nil && p.privateSymbolNeedsToBeLowered(private) {
971+
ctx.lowerPrivateMethod(p, prop, private)
968972

969973
// Move the method definition outside the class body
970974
methodRef := p.generateTempRef(tempRefNeedsDeclare, "_")

0 commit comments

Comments
 (0)