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(no-promise-in-callback): add exemptDeclarations option #513

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions __tests__/no-promise-in-callback.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,19 @@ ruleTester.run('no-promise-in-callback', rule, {

// weird case, we assume it's not a big deal if you return (even though you may be cheating)
'a(function(err) { return doThing().then(a) })',

{
code: `
function fn(err) {
return { promise: Promise.resolve(err) };
}
`,
options: [
{
exemptDeclarations: true,
},
],
},
],

invalid: [
Expand Down
6 changes: 6 additions & 0 deletions docs/rules/no-promise-in-callback.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ promisify(doSomething)()
.catch(console.error)
```

## Options

### `exemptDeclarations`

Whether or not to exempt function declarations. Defaults to `false`.

## When Not To Use It

If you do not want to be notified when using promises inside of callbacks, you
Expand Down
8 changes: 6 additions & 2 deletions rules/lib/is-inside-callback.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

const isInsidePromise = require('./is-inside-promise')

function isInsideCallback(node) {
/**
* @param {import('eslint').Rule.Node} node
* @param {boolean} [exemptDeclarations]
*/
function isInsideCallback(node, exemptDeclarations) {
const isFunction =
node.type === 'FunctionExpression' ||
node.type === 'ArrowFunctionExpression' ||
node.type === 'FunctionDeclaration' // this may be controversial
(!exemptDeclarations && node.type === 'FunctionDeclaration') // this may be controversial

// it's totally fine to use promises inside promises
if (isInsidePromise(node)) return
Expand Down
19 changes: 17 additions & 2 deletions rules/no-promise-in-callback.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,23 @@ module.exports = {
description: 'Disallow using promises inside of callbacks.',
url: getDocsUrl('no-promise-in-callback'),
},
schema: [],
schema: [
{
type: 'object',
properties: {
exemptDeclarations: {
type: 'boolean',
},
},
additionalProperties: false,
},
],
messages: {
avoidPromiseInCallback: 'Avoid using promises inside of callbacks.',
},
},
create(context) {
const { exemptDeclarations = false } = context.options[0] || {}
return {
CallExpression(node) {
if (!isPromise(node)) return
Expand All @@ -34,7 +45,11 @@ module.exports = {
// what about if the parent is an ArrowFunctionExpression
// would that imply an implicit return?

if (getAncestors(context, node).some(isInsideCallback)) {
if (
getAncestors(context, node).some((ancestor) => {
return isInsideCallback(ancestor, exemptDeclarations)
})
) {
context.report({
node: node.callee,
messageId: 'avoidPromiseInCallback',
Expand Down