Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(always-return): add ignoreLastCallback option #365

Merged
merged 2 commits into from Oct 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,
MichaelDeBoey marked this conversation as resolved.
Show resolved Hide resolved
},
})

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`.
MichaelDeBoey marked this conversation as resolved.
Show resolved Hide resolved

```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)
})
}
```