Skip to content

Commit

Permalink
Fix case where async keyword removal breaks syntax.
Browse files Browse the repository at this point in the history
Fixes #21
  • Loading branch information
leebyron committed Sep 15, 2016
1 parent e7253aa commit bd19f6e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
9 changes: 7 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ module.exports = function asyncToGen(source, options) {

// Cheap trick for files that don't actually contain async functions
if (!(options && options.fastSkip === false) &&
source.indexOf('async ') === -1) {
source.indexOf('async') === -1) {
return editor;
}

Expand Down Expand Up @@ -293,7 +293,12 @@ function leaveArrowFunction(editor, node, ast) {

var wrapping = createAsyncWrapping(node);

editor.remove(node.start, node.start + 6);
var idx = findTokenIndex(ast.tokens, node.start);
while (ast.tokens[idx].value !== 'async') {
idx++;
}
editor.remove(ast.tokens[idx].start, ast.tokens[idx + 1].start);

if (node.body.type === 'BlockStatement') {
editor.overwrite(node.body.start, node.body.start + 1, wrapping[0]);
editor.overwrite(node.body.end - 1, node.body.end, wrapping[1]);
Expand Down
3 changes: 3 additions & 0 deletions test/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ var arrow1 = () => __async(function*(){
var arrow2 = () =>__async(function*(){
return yield 42}());

// async function with minimal whitespace
var arrow3=()=>return __async(function*(){yield(42)}())

// async obj member function
var obj = {
baz() {return __async(function*(){
Expand Down
3 changes: 3 additions & 0 deletions test/source.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ var arrow1 = async () => {
var arrow2 = async () =>
await 42;

// async function with minimal whitespace
var arrow3=async()=>await(42)

// async obj member function
var obj = {
async baz() {
Expand Down

0 comments on commit bd19f6e

Please sign in to comment.