@@ -232,13 +232,15 @@ const constantStaticMethods: { readonly [name: string]: { readonly [name: string
232
232
"$" : constantFunctionMethods ,
233
233
} as const ;
234
234
235
+ type HelperName = "_async" | "_await" | "_empty" | "_call" | "_yield" | "_continue" | "_continueIgnored" | "_catch" | "_catchInGenerator" | "_finally" | "_finallyRethrows" | "_invoke" | "_invokeIgnored" | "_switch" | "_for" | "_do" | "_forTo" | "_forOf" | "_forOwn" | "_forIn" | "_forAwaitOf" | "_rethrow" | "_promiseResolve" | "_promiseThen" | "_AsyncGenerator" ;
236
+
235
237
// Weakly stored information on nodes
236
238
237
239
const originalNodeMap = new WeakMap < Node , Node > ( ) ;
238
240
const skipNodeSet = new WeakSet < Node > ( ) ;
239
241
const breakIdentifierMap = new WeakMap < Node , Identifier > ( ) ;
240
242
const isHelperDefinitionSet = new WeakSet < Node > ( ) ;
241
- const helperNameMap = new WeakMap < Identifier | MemberExpression , string > ( ) ;
243
+ const helperNameMap = new WeakMap < Identifier | MemberExpression , HelperName > ( ) ;
242
244
const nodeIsAsyncSet = new WeakSet < Node > ( ) ;
243
245
244
246
interface ForAwaitStatement {
@@ -307,7 +309,7 @@ interface ExtractedDeclarations {
307
309
308
310
interface Helper {
309
311
readonly value : Node ;
310
- readonly dependencies : readonly string [ ] ;
312
+ readonly dependencies : readonly HelperName [ ] ;
311
313
}
312
314
let helpers : { [ name : string ] : Helper } | undefined ;
313
315
@@ -1117,10 +1119,8 @@ export default function ({
1117
1119
}
1118
1120
args . push ( directExpression ) ;
1119
1121
}
1120
- let helperName = directExpression ? ( callTarget ? "_call" : "_await" ) : callTarget ? "_invoke" : "_continue" ;
1121
- if ( ignoreResult ) {
1122
- helperName += "Ignored" ;
1123
- }
1122
+ const baseHelper : "_call" | "_await" | "_invoke" | "_continue" = directExpression ? ( callTarget ? "_call" : "_await" ) : callTarget ? "_invoke" : "_continue" ;
1123
+ const helperName : HelperName = ignoreResult ? ( baseHelper + "Ignored" ) as HelperName : baseHelper ;
1124
1124
if ( args . length === 1 ) {
1125
1125
// Handle a few cases where a helper isn't actually necessary
1126
1126
switch ( helperName ) {
@@ -1995,12 +1995,14 @@ export default function ({
1995
1995
callTarget = onlyArgument ;
1996
1996
}
1997
1997
// Match function() { return _await(...()); } or function() { return Promise.resolve(...()); }
1998
- if (
1999
- ( types . isIdentifier ( callee ) || types . isMemberExpression ( callee ) ) &&
2000
- helperNameMap . get ( callee ) === "_await"
2001
- ) {
2002
- if ( types . isCallExpression ( onlyArgument ) && onlyArgument . arguments . length === 0 ) {
2003
- callTarget = onlyArgument . callee ;
1998
+ if ( types . isIdentifier ( callee ) || types . isMemberExpression ( callee ) ) {
1999
+ switch ( helperNameMap . get ( callee ) ) {
2000
+ case "_await" :
2001
+ case "_promiseResolve" :
2002
+ if ( types . isCallExpression ( onlyArgument ) && onlyArgument . arguments . length === 0 ) {
2003
+ callTarget = onlyArgument . callee ;
2004
+ }
2005
+ break ;
2004
2006
}
2005
2007
}
2006
2008
break ;
@@ -3417,7 +3419,7 @@ export default function ({
3417
3419
) ;
3418
3420
}
3419
3421
if ( parent . node . finalizer ) {
3420
- let finallyName : string ;
3422
+ let finallyName : HelperName ;
3421
3423
let finallyArgs : Identifier [ ] ;
3422
3424
let finallyBody = parent . node . finalizer . body ;
3423
3425
if ( ! pathsReturnOrThrow ( parent . get ( "finalizer" ) ) . all ) {
@@ -3848,12 +3850,13 @@ export default function ({
3848
3850
reusingExisting . remove ( ) ;
3849
3851
}
3850
3852
}
3853
+ const parentNode = parent . node ;
3851
3854
relocateTail (
3852
3855
state . generatorState ,
3853
3856
awaitPath . isYieldExpression ( )
3854
3857
? yieldOnExpression ( state . generatorState , awaitExpression )
3855
3858
: awaitExpression ,
3856
- parent . isStatement ( ) ? parent . node : types . returnStatement ( parent . node ) ,
3859
+ types . isStatement ( parentNode ) ? parentNode : types . returnStatement ( parentNode ) ,
3857
3860
parent ,
3858
3861
additionalConstantNames ,
3859
3862
resultIdentifier ,
@@ -3921,6 +3924,11 @@ export default function ({
3921
3924
) {
3922
3925
switch ( helperNameMap . get ( path . node . callee ) ) {
3923
3926
case "_await" :
3927
+ const args = path . get ( "arguments" ) ;
3928
+ if ( args . length > 0 && args [ 0 ] . isExpression ( ) ) {
3929
+ unpromisify ( args [ 0 ] , pluginState ) ;
3930
+ }
3931
+ // fallthrough
3924
3932
case "_call" : {
3925
3933
const args = path . get ( "arguments" ) ;
3926
3934
if ( args . length > 2 ) {
@@ -3936,6 +3944,21 @@ export default function ({
3936
3944
}
3937
3945
break ;
3938
3946
}
3947
+ case "_promiseThen" : {
3948
+ const args = path . get ( "arguments" ) ;
3949
+ if ( args . length > 2 ) {
3950
+ const firstArg = args [ 1 ] ;
3951
+ if ( types . isExpression ( firstArg . node ) && isContinuation ( firstArg . node ) ) {
3952
+ firstArg . traverse ( unpromisifyVisitor , pluginState ) ;
3953
+ } else if ( firstArg . isIdentifier ( ) ) {
3954
+ const binding = firstArg . scope . getBinding ( firstArg . node . name ) ;
3955
+ if ( binding && binding . path . isVariableDeclarator ( ) ) {
3956
+ binding . path . get ( "init" ) . traverse ( unpromisifyVisitor , pluginState ) ;
3957
+ }
3958
+ }
3959
+ }
3960
+ break ;
3961
+ }
3939
3962
}
3940
3963
return ;
3941
3964
}
@@ -4099,7 +4122,7 @@ export default function ({
4099
4122
}
4100
4123
4101
4124
// Emits a reference to a helper, inlining or importing it as necessary
4102
- function helperReference ( state : PluginState , path : NodePath , name : string ) : Identifier {
4125
+ function helperReference ( state : PluginState , path : NodePath , name : HelperName ) : Identifier {
4103
4126
const file = getFile ( path ) ;
4104
4127
let result = file . declarations [ name ] ;
4105
4128
if ( result ) {
@@ -4216,13 +4239,15 @@ export default function ({
4216
4239
// Emits a reference to Promise.resolve and tags it as an _await reference
4217
4240
function promiseResolve ( ) {
4218
4241
const result = types . memberExpression ( types . identifier ( "Promise" ) , types . identifier ( "resolve" ) ) ;
4219
- helperNameMap . set ( result , "_await " ) ;
4242
+ helperNameMap . set ( result , "_promiseResolve " ) ;
4220
4243
return result ;
4221
4244
}
4222
4245
4223
4246
// Emits a call to a target's then method
4224
4247
function callThenMethod ( value : Expression , continuation : Expression ) {
4225
- return types . callExpression ( types . memberExpression ( value , types . identifier ( "then" ) ) , [ continuation ] ) ;
4248
+ const thenExpression = types . memberExpression ( value , types . identifier ( "then" ) ) ;
4249
+ helperNameMap . set ( thenExpression , "_promiseThen" ) ;
4250
+ return types . callExpression ( thenExpression , [ continuation ] ) ;
4226
4251
}
4227
4252
4228
4253
// Checks if an expression is an async call expression
@@ -4231,6 +4256,8 @@ export default function ({
4231
4256
switch ( helperNameMap . get ( path . node . callee ) ) {
4232
4257
case "_await" :
4233
4258
case "_call" :
4259
+ case "_promiseResolve" :
4260
+ case "_promiseThen" :
4234
4261
return path . node . arguments . length < 3 ;
4235
4262
}
4236
4263
}
@@ -4581,6 +4608,7 @@ export default function ({
4581
4608
const firstArgument = callArgs [ 0 ] ;
4582
4609
if ( types . isExpression ( firstArgument ) ) {
4583
4610
switch ( helperNameMap . get ( argument . node . callee ) ) {
4611
+ case "_promiseResolve" :
4584
4612
case "_await" :
4585
4613
argument . replaceWith ( firstArgument ) ;
4586
4614
break ;
0 commit comments