sgilroy / async-await-codemod

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

Avoid converting a getter/setter into an invalid async method #21

Open yonran opened 3 years ago

yonran commented 3 years ago

Previously, async-await.js would convert a getter/setter into an invalid async getter/setter.

Input:

class A {
  method() {return a().then(b => 'convert')}
  get prop() {return a().then(b => 'getter')}
  set prop(val) {return a(val).then(b => 'setter')}
}

Output before this PR: async get and async set methods are created, but this is invalid ECMAScript and invalid TypeScript:

class A {
  async method() {
    const b = await a();
    return 'convert';
  }
  async get prop() {
    const b = await a();
    return 'getter';
  }
  async set prop(val) {
    const b = await a(val);
    return 'setter';
  }
}

Output after this PR: get and set methods are not converted to async.

class A {
  async method() {
    const b = await a();
    return 'convert';
  }
  get prop() {return a().then(b => 'getter')}
  set prop(val) {return a(val).then(b => 'setter')}
}