Skip to content

Commit 9039c46

Browse files
committed
simplify some call expression checks
1 parent 188944d commit 9039c46

File tree

1 file changed

+53
-62
lines changed

1 file changed

+53
-62
lines changed

internal/js_parser/js_parser.go

Lines changed: 53 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -14965,19 +14965,6 @@ func (p *parser) visitExprInOut(expr js_ast.Expr, in exprIn) (js_ast.Expr, exprO
1496514965
p.thenCatchChain.catchLoc = e.Args[1].Loc
1496614966
}
1496714967

14968-
// Prepare to recognize "require.resolve()" and "Object.create" calls
14969-
couldBeRequireResolve := false
14970-
couldBeObjectCreate := false
14971-
if len(e.Args) == 1 {
14972-
if dot, ok := e.Target.Data.(*js_ast.EDot); ok && dot.OptionalChain == js_ast.OptionalChainNone {
14973-
if p.options.mode != config.ModePassThrough && dot.Name == "resolve" {
14974-
couldBeRequireResolve = true
14975-
} else if dot.Name == "create" {
14976-
couldBeObjectCreate = true
14977-
}
14978-
}
14979-
}
14980-
1498114968
wasIdentifierBeforeVisit := false
1498214969
isParenthesizedOptionalChain := false
1498314970
switch e2 := e.Target.Data.(type) {
@@ -15222,57 +15209,61 @@ func (p *parser) visitExprInOut(expr js_ast.Expr, in exprIn) (js_ast.Expr, exprO
1522215209
}
1522315210

1522415211
case *js_ast.EDot:
15225-
// Recognize "require.resolve()" calls
15226-
if couldBeRequireResolve && t.Name == "resolve" {
15227-
if id, ok := t.Target.Data.(*js_ast.EIdentifier); ok && id.Ref == p.requireRef {
15228-
p.ignoreUsage(p.requireRef)
15229-
return p.maybeTransposeIfExprChain(e.Args[0], func(arg js_ast.Expr) js_ast.Expr {
15230-
if str, ok := e.Args[0].Data.(*js_ast.EString); ok {
15231-
// Ignore calls to require.resolve() if the control flow is provably
15232-
// dead here. We don't want to spend time scanning the required files
15233-
// if they will never be used.
15234-
if p.isControlFlowDead {
15235-
return js_ast.Expr{Loc: expr.Loc, Data: js_ast.ENullShared}
15236-
}
15212+
if len(e.Args) == 1 {
15213+
switch t.Name {
15214+
case "resolve":
15215+
// Recognize "require.resolve()" calls
15216+
if t.OptionalChain == js_ast.OptionalChainNone && p.options.mode != config.ModePassThrough {
15217+
if id, ok := t.Target.Data.(*js_ast.EIdentifier); ok && id.Ref == p.requireRef {
15218+
p.ignoreUsage(p.requireRef)
15219+
return p.maybeTransposeIfExprChain(e.Args[0], func(arg js_ast.Expr) js_ast.Expr {
15220+
if str, ok := e.Args[0].Data.(*js_ast.EString); ok {
15221+
// Ignore calls to require.resolve() if the control flow is provably
15222+
// dead here. We don't want to spend time scanning the required files
15223+
// if they will never be used.
15224+
if p.isControlFlowDead {
15225+
return js_ast.Expr{Loc: expr.Loc, Data: js_ast.ENullShared}
15226+
}
1523715227

15238-
importRecordIndex := p.addImportRecord(ast.ImportRequireResolve, ast.EvaluationPhase, p.source.RangeOfString(e.Args[0].Loc), helpers.UTF16ToString(str.Value), nil, 0)
15239-
if p.fnOrArrowDataVisit.tryBodyCount != 0 {
15240-
record := &p.importRecords[importRecordIndex]
15241-
record.Flags |= ast.HandlesImportErrors
15242-
record.ErrorHandlerLoc = p.fnOrArrowDataVisit.tryCatchLoc
15243-
}
15244-
p.importRecordsForCurrentPart = append(p.importRecordsForCurrentPart, importRecordIndex)
15228+
importRecordIndex := p.addImportRecord(ast.ImportRequireResolve, ast.EvaluationPhase, p.source.RangeOfString(e.Args[0].Loc), helpers.UTF16ToString(str.Value), nil, 0)
15229+
if p.fnOrArrowDataVisit.tryBodyCount != 0 {
15230+
record := &p.importRecords[importRecordIndex]
15231+
record.Flags |= ast.HandlesImportErrors
15232+
record.ErrorHandlerLoc = p.fnOrArrowDataVisit.tryCatchLoc
15233+
}
15234+
p.importRecordsForCurrentPart = append(p.importRecordsForCurrentPart, importRecordIndex)
1524515235

15246-
// Create a new expression to represent the operation
15247-
return js_ast.Expr{Loc: expr.Loc, Data: &js_ast.ERequireResolveString{
15248-
ImportRecordIndex: importRecordIndex,
15249-
CloseParenLoc: e.CloseParenLoc,
15250-
}}
15251-
}
15236+
// Create a new expression to represent the operation
15237+
return js_ast.Expr{Loc: expr.Loc, Data: &js_ast.ERequireResolveString{
15238+
ImportRecordIndex: importRecordIndex,
15239+
CloseParenLoc: e.CloseParenLoc,
15240+
}}
15241+
}
1525215242

15253-
// Otherwise just return a clone of the "require.resolve()" call
15254-
return js_ast.Expr{Loc: expr.Loc, Data: &js_ast.ECall{
15255-
Target: js_ast.Expr{Loc: e.Target.Loc, Data: &js_ast.EDot{
15256-
Target: p.valueToSubstituteForRequire(t.Target.Loc),
15257-
Name: t.Name,
15258-
NameLoc: t.NameLoc,
15259-
}},
15260-
Args: []js_ast.Expr{arg},
15261-
Kind: e.Kind,
15262-
CloseParenLoc: e.CloseParenLoc,
15263-
}}
15264-
}), exprOut{}
15265-
}
15266-
}
15243+
// Otherwise just return a clone of the "require.resolve()" call
15244+
return js_ast.Expr{Loc: expr.Loc, Data: &js_ast.ECall{
15245+
Target: js_ast.Expr{Loc: e.Target.Loc, Data: &js_ast.EDot{
15246+
Target: p.valueToSubstituteForRequire(t.Target.Loc),
15247+
Name: t.Name,
15248+
NameLoc: t.NameLoc,
15249+
}},
15250+
Args: []js_ast.Expr{arg},
15251+
Kind: e.Kind,
15252+
CloseParenLoc: e.CloseParenLoc,
15253+
}}
15254+
}), exprOut{}
15255+
}
15256+
}
1526715257

15268-
// Recognize "Object.create()" calls
15269-
if couldBeObjectCreate && t.Name == "create" {
15270-
if id, ok := t.Target.Data.(*js_ast.EIdentifier); ok {
15271-
if symbol := &p.symbols[id.Ref.InnerIndex]; symbol.Kind == ast.SymbolUnbound && symbol.OriginalName == "Object" {
15272-
switch e.Args[0].Data.(type) {
15273-
case *js_ast.ENull, *js_ast.EObject:
15274-
// Mark "Object.create(null)" and "Object.create({})" as pure
15275-
e.CanBeUnwrappedIfUnused = true
15258+
case "create":
15259+
// Recognize "Object.create()" calls
15260+
if id, ok := t.Target.Data.(*js_ast.EIdentifier); ok {
15261+
if symbol := &p.symbols[id.Ref.InnerIndex]; symbol.Kind == ast.SymbolUnbound && symbol.OriginalName == "Object" {
15262+
switch e.Args[0].Data.(type) {
15263+
case *js_ast.ENull, *js_ast.EObject:
15264+
// Mark "Object.create(null)" and "Object.create({})" as pure
15265+
e.CanBeUnwrappedIfUnused = true
15266+
}
1527615267
}
1527715268
}
1527815269
}
@@ -15281,7 +15272,7 @@ func (p *parser) visitExprInOut(expr js_ast.Expr, in exprIn) (js_ast.Expr, exprO
1528115272
if p.options.minifySyntax {
1528215273
switch t.Name {
1528315274
case "charCodeAt":
15284-
// Recognize "charCodeAt()" calls
15275+
// Recognize "'string'.charCodeAt()" calls
1528515276
if str, ok := t.Target.Data.(*js_ast.EString); ok && len(e.Args) <= 1 {
1528615277
index := 0
1528715278
hasIndex := false
@@ -15301,7 +15292,7 @@ func (p *parser) visitExprInOut(expr js_ast.Expr, in exprIn) (js_ast.Expr, exprO
1530115292
}
1530215293

1530315294
case "fromCharCode":
15304-
// Recognize "fromCharCode()" calls
15295+
// Recognize "String.fromCharCode()" calls
1530515296
if id, ok := t.Target.Data.(*js_ast.EIdentifier); ok {
1530615297
if symbol := &p.symbols[id.Ref.InnerIndex]; symbol.Kind == ast.SymbolUnbound && symbol.OriginalName == "String" {
1530715298
charCodes := make([]uint16, 0, len(e.Args))

0 commit comments

Comments
 (0)