diff --git a/.grit/patterns/js/es6_arrow_functions.md b/.grit/patterns/js/es6_arrow_functions.md index eeedb843..28f61dad 100644 --- a/.grit/patterns/js/es6_arrow_functions.md +++ b/.grit/patterns/js/es6_arrow_functions.md @@ -3,8 +3,7 @@ title: Function expressions to arrow functions tags: [js, es6, migration] --- -Converts function expressions to ES6 arrow functions - +Converts function expressions to ES6 arrow functions, including eliminating the `return` statement where possible. ```grit engine marzano(0.1) @@ -23,7 +22,13 @@ or { or { `this`, `arguments` } } until `function $_($_) { $_ }` }, - `($args) => { return $value }` => `($args) => $value` + `($args) => { return $value }` where { + if ($value <: object()) { + $result = `($value)` + } else { + $result = $value + } + } => `($args) => $result` } ``` @@ -68,3 +73,25 @@ var sumToValue = (x, y) => { var times = (x, y) => x * y; ``` + +## Wraps objects correctly + +An arrow function can return an object directly, but it must be wrapped in parentheses. + +```js +const dummyAnswer = (type) => { + return { + dataset: function (name, query) { + return 1; + }, + }; +}; +``` + +```js +const dummyAnswer = (type) => ({ + dataset: (name, query) => { + return 1; + }, +}); +```