-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix broken edge case of async function lowering #390
Changes from all commits
2fd4b70
fe48853
197c8b2
d4fe724
ee4a24c
598d53c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -139,6 +139,7 @@ func (p *parser) lowerFunction( | |
bodyStmts *[]js_ast.Stmt, | ||
preferExpr *bool, | ||
hasRestArg *bool, | ||
isArrow bool, | ||
) { | ||
// Lower object rest binding patterns in function arguments | ||
if p.UnsupportedFeatures.Has(compat.ObjectRestSpread) { | ||
|
@@ -228,7 +229,7 @@ func (p *parser) lowerFunction( | |
// resulting promise, which needs more complex code to handle | ||
couldThrowErrors := false | ||
for _, arg := range *args { | ||
if _, ok := arg.Binding.Data.(*js_ast.BIdentifier); !ok || arg.Default != nil { | ||
if _, ok := arg.Binding.Data.(*js_ast.BIdentifier); !ok || (arg.Default != nil && couldPotentiallyThrow(arg.Default.Data)) { | ||
couldThrowErrors = true | ||
break | ||
} | ||
|
@@ -269,7 +270,7 @@ func (p *parser) lowerFunction( | |
} | ||
|
||
// Forward all arguments from the outer function to the inner function | ||
if p.fnOnlyDataVisit.argumentsRef != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The problem seems to be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the summary. |
||
if !isArrow { | ||
// Normal functions can just use "arguments" to forward everything | ||
forwardedArgs = js_ast.Expr{Loc: bodyLoc, Data: &js_ast.EIdentifier{Ref: *p.fnOnlyDataVisit.argumentsRef}} | ||
} else { | ||
|
@@ -2083,3 +2084,11 @@ func (p *parser) maybeLowerSuperPropertyAccessInsideCall(call *js_ast.ECall) { | |
thisExpr := js_ast.Expr{Loc: call.Target.Loc, Data: &js_ast.EThis{}} | ||
call.Args = append([]js_ast.Expr{thisExpr}, call.Args...) | ||
} | ||
|
||
func couldPotentiallyThrow(data js_ast.E) bool { | ||
switch data.(type) { | ||
case *js_ast.ENull, *js_ast.EUndefined, *js_ast.EBoolean, *js_ast.ENumber, *js_ast.EBigInt, *js_ast.EString, *js_ast.EFunction, *js_ast.EArrow: | ||
return false | ||
} | ||
return true | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1032,6 +1032,20 @@ in.js:24:30: warning: Writing to getter-only property "#getter" will throw | |
) | ||
`, | ||
}, { async: true }), | ||
test(['in.js', '--outfile=node.js', '--target=es6'], | ||
{ | ||
'in.js': ` | ||
function nonArrowWrapper() { | ||
return async (x, paramWithDefault = {}) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Some expressions can be statically determined to never throw. Of these, it might be worth handling the subset that are both fairly common and trivial to identify (e.g. |
||
if (x !== 123) { | ||
throw 'fail'; | ||
} | ||
console.log(paramWithDefault); | ||
}; | ||
} | ||
exports.async = () => nonArrowWrapper()(123); | ||
`, | ||
}, { async: true }), | ||
test(['in.js', '--outfile=node.js', '--target=es6'], { | ||
'in.js': ` | ||
exports.async = async () => { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suppose adding something like
&e.isArrow
might be more readable. But I figured omitting such a property onFn
was intentional?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that's correct. This change sounds good.