Skip to content

Commit 01def31

Browse files
authored
feat(always-return): add ignoreLastCallback option (#365)
* feat(always-return): add `ignoreLastCallback` option * docs: format
1 parent a60d1cb commit 01def31

File tree

3 files changed

+306
-39
lines changed

3 files changed

+306
-39
lines changed

__tests__/always-return.js

Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const rule = require('../rules/always-return')
44
const RuleTester = require('eslint').RuleTester
55
const ruleTester = new RuleTester({
66
parserOptions: {
7-
ecmaVersion: 6,
7+
ecmaVersion: 11,
88
},
99
})
1010

@@ -48,6 +48,59 @@ ruleTester.run('always-return', rule, {
4848
}
4949
return x
5050
})`,
51+
{
52+
code: 'hey.then(x => { console.log(x) })',
53+
options: [{ ignoreLastCallback: true }],
54+
},
55+
{
56+
code: 'if(foo) { hey.then(x => { console.log(x) }) }',
57+
options: [{ ignoreLastCallback: true }],
58+
},
59+
{
60+
code: 'void hey.then(x => { console.log(x) })',
61+
options: [{ ignoreLastCallback: true }],
62+
},
63+
{
64+
code: `
65+
async function foo() {
66+
await hey.then(x => { console.log(x) })
67+
}`,
68+
options: [{ ignoreLastCallback: true }],
69+
},
70+
{
71+
code: `hey?.then(x => { console.log(x) })`,
72+
options: [{ ignoreLastCallback: true }],
73+
},
74+
{
75+
code: `foo = (hey.then(x => { console.log(x) }), 42)`,
76+
options: [{ ignoreLastCallback: true }],
77+
},
78+
{
79+
code: `(42, hey.then(x => { console.log(x) }))`,
80+
options: [{ ignoreLastCallback: true }],
81+
},
82+
{
83+
code: `
84+
hey
85+
.then(x => { console.log(x) })
86+
.catch(e => console.error(e))`,
87+
options: [{ ignoreLastCallback: true }],
88+
},
89+
{
90+
code: `
91+
hey
92+
.then(x => { console.log(x) })
93+
.catch(e => console.error(e))
94+
.finally(() => console.error('end'))`,
95+
options: [{ ignoreLastCallback: true }],
96+
},
97+
{
98+
code: `
99+
hey
100+
.then(x => { console.log(x) })
101+
.finally(() => console.error('end'))`,
102+
options: [{ ignoreLastCallback: true }],
103+
},
51104
],
52105

53106
invalid: [
@@ -130,5 +183,44 @@ ruleTester.run('always-return', rule, {
130183
})`,
131184
errors: [{ message }],
132185
},
186+
{
187+
code: `
188+
hey
189+
.then(function(x) { console.log(x) /* missing return here */ })
190+
.then(function(y) { console.log(y) /* no error here */ })`,
191+
options: [{ ignoreLastCallback: true }],
192+
errors: [{ message, line: 3 }],
193+
},
194+
{
195+
code: `const foo = hey.then(function(x) {});`,
196+
options: [{ ignoreLastCallback: true }],
197+
errors: [{ message }],
198+
},
199+
{
200+
code: `
201+
function foo() {
202+
return hey.then(function(x) {});
203+
}`,
204+
options: [{ ignoreLastCallback: true }],
205+
errors: [{ message }],
206+
},
207+
{
208+
code: `
209+
async function foo() {
210+
return await hey.then(x => { console.log(x) })
211+
}`,
212+
options: [{ ignoreLastCallback: true }],
213+
errors: [{ message }],
214+
},
215+
{
216+
code: `const foo = hey?.then(x => { console.log(x) })`,
217+
options: [{ ignoreLastCallback: true }],
218+
errors: [{ message }],
219+
},
220+
{
221+
code: `const foo = (42, hey.then(x => { console.log(x) }))`,
222+
options: [{ ignoreLastCallback: true }],
223+
errors: [{ message }],
224+
},
133225
],
134226
})

docs/rules/always-return.md

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,18 @@ as `return Promise.reject()`.
1010
#### Valid
1111

1212
```js
13-
myPromise.then((val) => val * 2));
14-
myPromise.then(function(val) { return val * 2; });
15-
myPromise.then(doSomething); // could be either
16-
myPromise.then((b) => { if (b) { return "yes" } else { return "no" } });
13+
myPromise.then((val) => val * 2)
14+
myPromise.then(function (val) {
15+
return val * 2
16+
})
17+
myPromise.then(doSomething) // could be either
18+
myPromise.then((b) => {
19+
if (b) {
20+
return 'yes'
21+
} else {
22+
return 'no'
23+
}
24+
})
1725
```
1826

1927
#### Invalid
@@ -31,3 +39,51 @@ myPromise.then((b) => {
3139
}
3240
})
3341
```
42+
43+
#### Options
44+
45+
##### `ignoreLastCallback`
46+
47+
You can pass an `{ ignoreLastCallback: true }` as an option to this rule to the
48+
last `then()` callback in a promise chain does not warn if it does not have a
49+
`return`. Default is `false`.
50+
51+
```js
52+
// OK
53+
promise.then((x) => {
54+
console.log(x)
55+
})
56+
// OK
57+
void promise.then((x) => {
58+
console.log(x)
59+
})
60+
// OK
61+
await promise.then((x) => {
62+
console.log(x)
63+
})
64+
65+
promise
66+
// NG
67+
.then((x) => {
68+
console.log(x)
69+
})
70+
// OK
71+
.then((x) => {
72+
console.log(x)
73+
})
74+
75+
// NG
76+
var v = promise.then((x) => {
77+
console.log(x)
78+
})
79+
// NG
80+
var v = await promise.then((x) => {
81+
console.log(x)
82+
})
83+
function foo() {
84+
// NG
85+
return promise.then((x) => {
86+
console.log(x)
87+
})
88+
}
89+
```

0 commit comments

Comments
 (0)