Skip to content

Commit 5ff61ce

Browse files
authored
Fix transform of parenthetic expressions as body of async arrows. (#53)
Fixes #49
1 parent d2fdba1 commit 5ff61ce

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,9 +347,12 @@ function leaveArrowFunction(editor, node, ast) {
347347
while (ast.tokens[idx].type.label !== '=>') {
348348
idx--;
349349
}
350+
var nodeStart = node.body.extra && node.body.extra.parenthesized
351+
? node.body.extra.parenStart
352+
: node.body.start;
350353
editor.appendLeft(ast.tokens[idx].end, wrapping[0]);
351-
editor.prependRight(node.body.start, 'return ');
352-
editor.appendLeft(node.body.end, wrapping[1]);
354+
editor.prependRight(nodeStart, 'return ');
355+
editor.appendLeft(node.end, wrapping[1]);
353356
}
354357
}
355358
}

test/expected.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,15 @@ function reduceStream(stream, reducer, initial) {return __async(function*(){
240240
// doesn't break for holey destructuring (#22)
241241
const [,holey] = [1,2,3]
242242

243+
// support arrow functions returning parentheic expressions (#49)
244+
const arrowOfParentheic = () =>__async(function*(){ return (12345)}())
245+
const arrowOfNestedDoubleParentheic = (() =>__async(function*(){ return ((12345))}()))
246+
const arrowOfSequence = () =>__async(function*(){ return (12345, 67890)}())
247+
const arrowOfObj1 = () =>__async(function*(){ return ({})}())
248+
const arrowOfObj2 = () =>__async(function*(){ return ({
249+
key: yield x
250+
})}())
251+
243252
function __async(g){return new Promise(function(s,j){function c(a,x){try{var r=g[x?"throw":"next"](a)}catch(e){j(e);return}r.done?s(r.value):Promise.resolve(r.value).then(c,d)}function d(e){c(e,1)}c()})}
244253

245254
function __asyncGen(g){var q=[],T=["next","throw","return"],I={};for(var i=0;i<3;i++){I[T[i]]=a.bind(0,i)}I[Symbol?Symbol.asyncIterator||(Symbol.asyncIterator=Symbol()):"@@asyncIterator"]=function (){return this};function a(t,v){return new Promise(function(s,j){q.push([s,j,v,t]);q.length===1&&c(v,t)})}function c(v,t){try{var r=g[T[t|0]](v),w=r.value&&r.value.__await;w?Promise.resolve(w).then(c,d):n(r,0)}catch(e){n(e,1)}}function d(e){c(e,1)}function n(r,s){q.shift()[s](r);q.length&&c(q[0][2],q[0][3])}return I}

test/source.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,3 +239,12 @@ async function reduceStream(stream, reducer, initial) {
239239

240240
// doesn't break for holey destructuring (#22)
241241
const [,holey] = [1,2,3]
242+
243+
// support arrow functions returning parentheic expressions (#49)
244+
const arrowOfParentheic = async () => (12345)
245+
const arrowOfNestedDoubleParentheic = (async () => ((12345)))
246+
const arrowOfSequence = async () => (12345, 67890)
247+
const arrowOfObj1 = async () => ({})
248+
const arrowOfObj2 = async () => ({
249+
key: await x
250+
})

0 commit comments

Comments
 (0)