Skip to content

Commit c191b26

Browse files
authored
fix: handle cases where the arrow function is an object (#239)
1 parent b3808a4 commit c191b26

File tree

1 file changed

+30
-3
lines changed

1 file changed

+30
-3
lines changed

.grit/patterns/js/es6_arrow_functions.md

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ title: Function expressions to arrow functions
33
tags: [js, es6, migration]
44
---
55

6-
Converts function expressions to ES6 arrow functions
7-
6+
Converts function expressions to ES6 arrow functions, including eliminating the `return` statement where possible.
87

98
```grit
109
engine marzano(0.1)
@@ -23,7 +22,13 @@ or {
2322
or { `this`, `arguments` }
2423
} until `function $_($_) { $_ }`
2524
},
26-
`($args) => { return $value }` => `($args) => $value`
25+
`($args) => { return $value }` where {
26+
if ($value <: object()) {
27+
$result = `($value)`
28+
} else {
29+
$result = $value
30+
}
31+
} => `($args) => $result`
2732
}
2833
```
2934

@@ -68,3 +73,25 @@ var sumToValue = (x, y) => {
6873

6974
var times = (x, y) => x * y;
7075
```
76+
77+
## Wraps objects correctly
78+
79+
An arrow function can return an object directly, but it must be wrapped in parentheses.
80+
81+
```js
82+
const dummyAnswer = (type) => {
83+
return {
84+
dataset: function (name, query) {
85+
return 1;
86+
},
87+
};
88+
};
89+
```
90+
91+
```js
92+
const dummyAnswer = (type) => ({
93+
dataset: (name, query) => {
94+
return 1;
95+
},
96+
});
97+
```

0 commit comments

Comments
 (0)