Node6: Remove parentheses around the body of async arrow functions (#942)

This commit is contained in:
JoelEinbinder 2017-10-02 18:25:11 -07:00 committed by GitHub
parent dc4c8786e3
commit d87480b609
2 changed files with 18 additions and 0 deletions

View File

@ -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);
}

View File

@ -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);
});
});