Skip to content

Commit

Permalink
fix: repair async functions (#245)
Browse files Browse the repository at this point in the history
  • Loading branch information
morgante authored Nov 5, 2024
1 parent fe2a4e1 commit 8e97d75
Showing 1 changed file with 55 additions and 5 deletions.
60 changes: 55 additions & 5 deletions .grit/patterns/js/es6_arrow_functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,30 @@ The following pattern transforms JS traditional functions to arrow functions.
To see how it works, follow the tutorial.
*/
or {
// Rewrite traditional functions to arrow functions
or {
`async function ($args) { $body }` => `async ($args) => {
$body
}`,
`function ($args) { $body }` => `($args) => {
$body
}` where {
}`,
} where {
$body <: not contains {
or { `this`, `arguments` }
} until `function $_($_) { $_ }`
},
`($args) => { return $value }` where {
if ($value <: object()) {
$result = `($value)`
// Rewrite arrow functions to remove unnecessary return statements
or {
`async ($args) => { return $value }` where $async = `async `,
`($args) => { return $value }` where $async = .,
} where {
if ($value <: object()) {
$result = `($value)`
} else {
$result = $value
}
} => `($args) => $result`
} => `$async($args) => $result`
}
```

Expand Down Expand Up @@ -95,3 +105,43 @@ const dummyAnswer = (type) => ({
},
});
```

## Handles async functions correctly

See [this issue](https://github.com/getgrit/stdlib/issues/243).

Before:

```js
const a = {
set: async function () {
return await Promise.resolve(1);
},
};
```

After:

```js
const a = {
set: async () => {
return await Promise.resolve(1);
},
};
```

## Handles async return values

When removing an unnecessary `return` statement, we still need to consider if the function is async.

```js
const a = async () => {
return await Promise.resolve(1);
};
```

After:

```js
const a = async () => await Promise.resolve(1);
```

0 comments on commit 8e97d75

Please sign in to comment.