Skip to content

Commit

Permalink
feat(always-return): add ignoreLastCallback option (#365)
Browse files Browse the repository at this point in the history
* feat(always-return): add `ignoreLastCallback` option

* docs: format
  • Loading branch information
ota-meshi committed Oct 1, 2022
1 parent a60d1cb commit 01def31
Show file tree
Hide file tree
Showing 3 changed files with 306 additions and 39 deletions.
94 changes: 93 additions & 1 deletion __tests__/always-return.js
Expand Up @@ -4,7 +4,7 @@ const rule = require('../rules/always-return')
const RuleTester = require('eslint').RuleTester
const ruleTester = new RuleTester({
parserOptions: {
ecmaVersion: 6,
ecmaVersion: 11,
},
})

Expand Down Expand Up @@ -48,6 +48,59 @@ ruleTester.run('always-return', rule, {
}
return x
})`,
{
code: 'hey.then(x => { console.log(x) })',
options: [{ ignoreLastCallback: true }],
},
{
code: 'if(foo) { hey.then(x => { console.log(x) }) }',
options: [{ ignoreLastCallback: true }],
},
{
code: 'void hey.then(x => { console.log(x) })',
options: [{ ignoreLastCallback: true }],
},
{
code: `
async function foo() {
await hey.then(x => { console.log(x) })
}`,
options: [{ ignoreLastCallback: true }],
},
{
code: `hey?.then(x => { console.log(x) })`,
options: [{ ignoreLastCallback: true }],
},
{
code: `foo = (hey.then(x => { console.log(x) }), 42)`,
options: [{ ignoreLastCallback: true }],
},
{
code: `(42, hey.then(x => { console.log(x) }))`,
options: [{ ignoreLastCallback: true }],
},
{
code: `
hey
.then(x => { console.log(x) })
.catch(e => console.error(e))`,
options: [{ ignoreLastCallback: true }],
},
{
code: `
hey
.then(x => { console.log(x) })
.catch(e => console.error(e))
.finally(() => console.error('end'))`,
options: [{ ignoreLastCallback: true }],
},
{
code: `
hey
.then(x => { console.log(x) })
.finally(() => console.error('end'))`,
options: [{ ignoreLastCallback: true }],
},
],

invalid: [
Expand Down Expand Up @@ -130,5 +183,44 @@ ruleTester.run('always-return', rule, {
})`,
errors: [{ message }],
},
{
code: `
hey
.then(function(x) { console.log(x) /* missing return here */ })
.then(function(y) { console.log(y) /* no error here */ })`,
options: [{ ignoreLastCallback: true }],
errors: [{ message, line: 3 }],
},
{
code: `const foo = hey.then(function(x) {});`,
options: [{ ignoreLastCallback: true }],
errors: [{ message }],
},
{
code: `
function foo() {
return hey.then(function(x) {});
}`,
options: [{ ignoreLastCallback: true }],
errors: [{ message }],
},
{
code: `
async function foo() {
return await hey.then(x => { console.log(x) })
}`,
options: [{ ignoreLastCallback: true }],
errors: [{ message }],
},
{
code: `const foo = hey?.then(x => { console.log(x) })`,
options: [{ ignoreLastCallback: true }],
errors: [{ message }],
},
{
code: `const foo = (42, hey.then(x => { console.log(x) }))`,
options: [{ ignoreLastCallback: true }],
errors: [{ message }],
},
],
})
64 changes: 60 additions & 4 deletions docs/rules/always-return.md
Expand Up @@ -10,10 +10,18 @@ as `return Promise.reject()`.
#### Valid

```js
myPromise.then((val) => val * 2));
myPromise.then(function(val) { return val * 2; });
myPromise.then(doSomething); // could be either
myPromise.then((b) => { if (b) { return "yes" } else { return "no" } });
myPromise.then((val) => val * 2)
myPromise.then(function (val) {
return val * 2
})
myPromise.then(doSomething) // could be either
myPromise.then((b) => {
if (b) {
return 'yes'
} else {
return 'no'
}
})
```

#### Invalid
Expand All @@ -31,3 +39,51 @@ myPromise.then((b) => {
}
})
```

#### Options

##### `ignoreLastCallback`

You can pass an `{ ignoreLastCallback: true }` as an option to this rule to the
last `then()` callback in a promise chain does not warn if it does not have a
`return`. Default is `false`.

```js
// OK
promise.then((x) => {
console.log(x)
})
// OK
void promise.then((x) => {
console.log(x)
})
// OK
await promise.then((x) => {
console.log(x)
})

promise
// NG
.then((x) => {
console.log(x)
})
// OK
.then((x) => {
console.log(x)
})

// NG
var v = promise.then((x) => {
console.log(x)
})
// NG
var v = await promise.then((x) => {
console.log(x)
})
function foo() {
// NG
return promise.then((x) => {
console.log(x)
})
}
```

0 comments on commit 01def31

Please sign in to comment.