Node6: Remove parentheses around the body of async arrow functions (#942)
This commit is contained in:
parent
dc4c8786e3
commit
d87480b609
@ -88,7 +88,19 @@ function transformAsyncFunctions(text) {
|
|||||||
if (node.body.type !== 'BlockStatement') {
|
if (node.body.type !== 'BlockStatement') {
|
||||||
before += `{ return `;
|
before += `{ return `;
|
||||||
after = `; }` + after;
|
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[0], node.body.range[0], before);
|
||||||
insertText(node.body.range[1], node.body.range[1], after);
|
insertText(node.body.range[1], node.body.range[1], after);
|
||||||
}
|
}
|
||||||
|
@ -70,4 +70,10 @@ describe('TransformAsyncFunctions', function() {
|
|||||||
expect(output instanceof Promise).toBe(true);
|
expect(output instanceof Promise).toBe(true);
|
||||||
output.then(result => expect(result).toBe(123)).then(done);
|
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);
|
||||||
|
});
|
||||||
});
|
});
|
Loading…
Reference in New Issue
Block a user