Skip to content

Commit

Permalink
Node6: Remove parentheses around the body of async arrow functions (#942
Browse files Browse the repository at this point in the history
)
  • Loading branch information
JoelEinbinder committed Oct 3, 2017
1 parent dc4c878 commit d87480b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
12 changes: 12 additions & 0 deletions utils/node6-transform/TransformAsyncFunctions.js
Expand Up @@ -88,7 +88,19 @@ function transformAsyncFunctions(text) {
if (node.body.type !== 'BlockStatement') {
before += `{ return `;
after = `; }` + after;

// Remove parentheses that might wrap an arrow function
const beforeBody = text.substring(node.range[0], node.body.range[0]);
if (/\(\s*$/.test(beforeBody)) {
const afterBody = text.substring(node.body.range[1], node.range[1]);
const openParen = node.range[0] + beforeBody.lastIndexOf('(');
insertText(openParen, openParen + 1, ' ');
const closeParen = node.body.range[1] + afterBody.indexOf(')');
insertText(closeParen, closeParen + 1, ' ');
}
}


insertText(node.body.range[0], node.body.range[0], before);
insertText(node.body.range[1], node.body.range[1], after);
}
Expand Down
6 changes: 6 additions & 0 deletions utils/node6-transform/test/test.js
Expand Up @@ -70,4 +70,10 @@ describe('TransformAsyncFunctions', function() {
expect(output instanceof Promise).toBe(true);
output.then(result => expect(result).toBe(123)).then(done);
});
it('should work paren around arrow function', function(done) {
const input = `(async x => ( 123))()`;
const output = eval(transformAsyncFunctions(input));
expect(output instanceof Promise).toBe(true);
output.then(result => expect(result).toBe(123)).then(done);
});
});

0 comments on commit d87480b

Please sign in to comment.