Skip to content

Commit

Permalink
fix: handle cases where the arrow function is an object (#239)
Browse files Browse the repository at this point in the history
  • Loading branch information
morgante authored Sep 30, 2024
1 parent b3808a4 commit c191b26
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions .grit/patterns/js/es6_arrow_functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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`
}
```

Expand Down Expand Up @@ -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;
},
});
```

0 comments on commit c191b26

Please sign in to comment.