Skip to content

Commit

Permalink
feat(no-promise-in-callback): add exemptDeclarations option; fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
brettz9 committed Oct 16, 2024
1 parent 701279c commit 1948aed
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
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

0 comments on commit 1948aed

Please sign in to comment.