diff --git a/.grit/patterns/js/es6_arrow_functions.md b/.grit/patterns/js/es6_arrow_functions.md index 28f61dad..3fa0243f 100644 --- a/.grit/patterns/js/es6_arrow_functions.md +++ b/.grit/patterns/js/es6_arrow_functions.md @@ -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` } ``` @@ -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); +```