Skip to content

Commit

Permalink
fix some generated temporary use counts
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Apr 2, 2022
1 parent e16cde0 commit c9e2fdb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
14 changes: 9 additions & 5 deletions internal/js_parser/js_parser.go
Expand Up @@ -7303,14 +7303,16 @@ func (p *parser) visitStmtsAndPrependTempRefs(stmts []js_ast.Stmt, opts prependT
}

// Prepend the generated temporary variables to the beginning of the statement list
if len(p.tempRefsToDeclare) > 0 {
decls := []js_ast.Decl{}
for _, temp := range p.tempRefsToDeclare {
decls := []js_ast.Decl{}
for _, temp := range p.tempRefsToDeclare {
if p.symbols[temp.ref.InnerIndex].UseCountEstimate > 0 {
decls = append(decls, js_ast.Decl{Binding: js_ast.Binding{Data: &js_ast.BIdentifier{Ref: temp.ref}}, ValueOrNil: temp.valueOrNil})
p.recordDeclaredSymbol(temp.ref)
}
}

// If the first statement is a super() call, make sure it stays that way
// If the first statement is a super() call, make sure it stays that way
if len(decls) > 0 {
stmt := js_ast.Stmt{Data: &js_ast.SLocal{Kind: js_ast.LocalVar, Decls: decls}}
if len(stmts) > 0 && js_ast.IsSuperCall(stmts[0]) {
stmts = append([]js_ast.Stmt{stmts[0], stmt}, stmts[1:]...)
Expand Down Expand Up @@ -13720,7 +13722,9 @@ func (p *parser) handleIdentifier(loc logger.Loc, e *js_ast.EIdentifier, opts id
isInsideUnsupportedArrow := p.fnOrArrowDataVisit.isArrow && p.options.unsupportedJSFeatures.Has(compat.Arrow)
isInsideUnsupportedAsyncArrow := p.fnOnlyDataVisit.isInsideAsyncArrowFn && p.options.unsupportedJSFeatures.Has(compat.AsyncAwait)
if isInsideUnsupportedArrow || isInsideUnsupportedAsyncArrow {
return js_ast.Expr{Loc: loc, Data: &js_ast.EIdentifier{Ref: p.captureArguments()}}
argumentsRef := p.captureArguments()
p.recordUsage(argumentsRef)
return js_ast.Expr{Loc: loc, Data: &js_ast.EIdentifier{Ref: argumentsRef}}
}
}

Expand Down
5 changes: 5 additions & 0 deletions internal/js_parser/js_parser_lower.go
Expand Up @@ -1300,6 +1300,7 @@ func (p *parser) lowerObjectRestInForLoopInit(init js_ast.Stmt, body *js_ast.Stm
if exprHasObjectRest(s.Value) {
ref := p.generateTempRef(tempRefNeedsDeclare, "")
if expr, ok := p.lowerAssign(s.Value, js_ast.Expr{Loc: init.Loc, Data: &js_ast.EIdentifier{Ref: ref}}, objRestReturnValueIsUnused); ok {
p.recordUsage(ref)
s.Value.Data = &js_ast.EIdentifier{Ref: ref}
bodyPrefixStmt = js_ast.Stmt{Loc: expr.Loc, Data: &js_ast.SExpr{Value: expr}}
}
Expand Down Expand Up @@ -2213,6 +2214,7 @@ func (p *parser) lowerClass(stmt js_ast.Stmt, expr js_ast.Expr, shadowRef js_ast
} else if _, ok := prop.Key.Data.(*js_ast.EString); !ok {
// Store the key in a temporary so we can assign to it later
ref := p.generateTempRef(tempRefNeedsDeclare, "")
p.recordUsage(ref)
computedPropertyCache = js_ast.JoinWithComma(computedPropertyCache,
js_ast.Assign(js_ast.Expr{Loc: prop.Key.Loc, Data: &js_ast.EIdentifier{Ref: ref}}, prop.Key))
prop.Key = js_ast.Expr{Loc: prop.Key.Loc, Data: &js_ast.EIdentifier{Ref: ref}}
Expand Down Expand Up @@ -2429,6 +2431,7 @@ func (p *parser) lowerClass(stmt js_ast.Stmt, expr js_ast.Expr, shadowRef js_ast
} else {
p.symbols[methodRef.InnerIndex].Link = p.privateGetters[private.Ref]
}
p.recordUsage(methodRef)
privateMembers = append(privateMembers, js_ast.Assign(
js_ast.Expr{Loc: loc, Data: &js_ast.EIdentifier{Ref: methodRef}},
prop.ValueOrNil,
Expand Down Expand Up @@ -2815,6 +2818,8 @@ func (p *parser) lowerTemplateLiteral(loc logger.Loc, e *js_ast.ETemplate) js_as

// Cache it in a temporary object (required by the specification)
tempRef := p.generateTopLevelTempRef()
p.recordUsage(tempRef)
p.recordUsage(tempRef)
args[0] = js_ast.Expr{Loc: loc, Data: &js_ast.EBinary{
Op: js_ast.BinOpLogicalOr,
Left: js_ast.Expr{Loc: loc, Data: &js_ast.EIdentifier{Ref: tempRef}},
Expand Down

0 comments on commit c9e2fdb

Please sign in to comment.