sgilroy / async-await-codemod

Codemod script for migrating promise-based functions to use async/await syntax
29 stars 4 forks source link

Identifier passed to .then() results in error #5

Closed sgilroy closed 6 years ago

sgilroy commented 6 years ago

The following examples each currently result in errors:

function doesSomethingWithAPromise() {
  promise.then(doSomething);
}

function returnsSomethingDone() {
  return promise.then(doSomething);
}

Error:

TypeError: Cannot read property 'type' of undefined
      at transformFunction (async-await.js:123:23)
      at NodePath.paths.forEach.path (async-await.js:166:13)
      at __paths.forEach (node_modules/jscodeshift/dist/Collection.js:76:36)
          at Array.forEach (<anonymous>)
      at Collection.forEach (node_modules/jscodeshift/dist/Collection.js:75:18)
      at replaceType (async-await.js:165:13)
      at transformer (async-await.js:173:3)
      at runInlineTest (node_modules/jscodeshift/dist/testUtils.js:28:18)
      at runTest (node_modules/jscodeshift/dist/testUtils.js:73:3)
      at Object.it (node_modules/jscodeshift/dist/testUtils.js:90:7)

Instead, they should be transformed as follows:

async function doesSomethingWithAPromise() {
  const promiseResult = await promise;
  doSomething(promiseResult);
}

async function returnsSomethingDone() {
  const promiseResult = await promise;
  return doSomething(promiseResult);
}