Skip to content

Commit e1b136d

Browse files
committed
a few minor code adjustments to class lowering
1 parent 5e7d1fc commit e1b136d

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

internal/js_parser/js_parser_lower_class.go

+16-15
Original file line numberDiff line numberDiff line change
@@ -780,7 +780,13 @@ func (ctx *lowerClassContext) enableNameCapture(p *parser, result visitClassResu
780780
//
781781
// If this returns true, the return property should be added to the class
782782
// body. Otherwise the property should be omitted from the class body.
783-
func (ctx *lowerClassContext) lowerField(p *parser, prop js_ast.Property, private *js_ast.EPrivateIdentifier, shouldOmitFieldInitializer bool, staticFieldToBlockAssign bool) (js_ast.Property, bool) {
783+
func (ctx *lowerClassContext) lowerField(
784+
p *parser,
785+
prop js_ast.Property,
786+
private *js_ast.EPrivateIdentifier,
787+
shouldOmitFieldInitializer bool,
788+
staticFieldToBlockAssign bool,
789+
) (js_ast.Property, bool) {
784790
mustLowerPrivate := private != nil && p.privateSymbolNeedsToBeLowered(private)
785791

786792
// The TypeScript compiler doesn't follow the JavaScript spec for
@@ -825,20 +831,16 @@ func (ctx *lowerClassContext) lowerField(p *parser, prop js_ast.Property, privat
825831

826832
// Add every newly-constructed instance into this map
827833
key := js_ast.Expr{Loc: prop.Key.Loc, Data: &js_ast.EIdentifier{Ref: ref}}
828-
var args []js_ast.Expr
829-
if _, ok := init.Data.(*js_ast.EUndefined); ok {
830-
args = []js_ast.Expr{target, key}
831-
} else {
832-
args = []js_ast.Expr{target, key, init}
834+
args := []js_ast.Expr{target, key}
835+
if _, ok := init.Data.(*js_ast.EUndefined); !ok {
836+
args = append(args, init)
833837
}
834838
memberExpr = p.callRuntime(loc, "__privateAdd", args)
835839
p.recordUsage(ref)
836840
} else if private == nil && ctx.class.UseDefineForClassFields {
837-
var args []js_ast.Expr
838-
if _, ok := init.Data.(*js_ast.EUndefined); ok {
839-
args = []js_ast.Expr{target, prop.Key}
840-
} else {
841-
args = []js_ast.Expr{target, prop.Key, init}
841+
args := []js_ast.Expr{target, prop.Key}
842+
if _, ok := init.Data.(*js_ast.EUndefined); !ok {
843+
args = append(args, init)
842844
}
843845
memberExpr = js_ast.Expr{Loc: loc, Data: &js_ast.ECall{
844846
Target: p.importFromRuntime(loc, "__publicField"),
@@ -1135,8 +1137,6 @@ func (ctx *lowerClassContext) hoistComputedProperties(p *parser, classLoweringIn
11351137
// If this key is referenced elsewhere, make sure to still preserve
11361138
// its side effects in the property's original location
11371139
if analysis.isComputedPropertyCopiedOrMoved {
1138-
inlineKey := prop.Key
1139-
11401140
// If this property is being duplicated instead of moved or removed, then
11411141
// we still need the assignment to the temporary so that we can reference
11421142
// it in multiple places, but we don't have to hoist the assignment to an
@@ -1168,6 +1168,8 @@ func (ctx *lowerClassContext) hoistComputedProperties(p *parser, classLoweringIn
11681168
//
11691169
// So only do the hoist if this property is being moved or removed.
11701170
if !analysis.rewriteAutoAccessorToGetSet && (analysis.mustLowerField || analysis.staticFieldToBlockAssign) {
1171+
inlineKey := prop.Key
1172+
11711173
if !analysis.needsValueOfKey {
11721174
// In certain cases, we only need to evaluate a property key for its
11731175
// side effects but we don't actually need the value of the key itself.
@@ -1201,14 +1203,13 @@ func (ctx *lowerClassContext) hoistComputedProperties(p *parser, classLoweringIn
12011203
// Otherwise, we keep the side effects in place (as described above) but
12021204
// just store the key in a temporary so we can refer to it later.
12031205
ref := p.generateTempRef(tempRefNeedsDeclare, "")
1204-
inlineKey = js_ast.Assign(js_ast.Expr{Loc: prop.Key.Loc, Data: &js_ast.EIdentifier{Ref: ref}}, prop.Key)
1206+
prop.Key = js_ast.Assign(js_ast.Expr{Loc: prop.Key.Loc, Data: &js_ast.EIdentifier{Ref: ref}}, prop.Key)
12051207
p.recordUsage(ref)
12061208

12071209
// Use this temporary when creating duplicate references to this key
12081210
if propertyKeyTempRefs == nil {
12091211
propertyKeyTempRefs = make(map[int]ast.Ref)
12101212
}
1211-
prop.Key = inlineKey
12121213
propertyKeyTempRefs[propIndex] = ref
12131214

12141215
// Deliberately continue to fall through to the "computed" case below:

0 commit comments

Comments
 (0)