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

fix: no-callback-literal ignore unknown nodes #163

Merged
merged 1 commit into from
Jan 8, 2024
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
6 changes: 3 additions & 3 deletions lib/rules/no-callback-literal.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ function couldBeError(node) {
case "TaggedTemplateExpression":
case "YieldExpression":
return true // possibly an error object.

case "Literal":
return node.value == null
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

according its docs, the arg can be null/undefined, so I changed === to ==, to allow cases like cb(undefined)

case "AssignmentExpression":
return couldBeError(node.right)

Expand All @@ -76,8 +77,7 @@ function couldBeError(node) {

case "ConditionalExpression":
return couldBeError(node.consequent) || couldBeError(node.alternate)

default:
return node.value === null
return true // assuming unknown nodes can be error objects.
}
}
6 changes: 6 additions & 0 deletions tests/lib/rules/no-callback-literal.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

const RuleTester = require("#eslint-rule-tester").RuleTester
const rule = require("../../../lib/rules/no-callback-literal")
const tsParser = require("@typescript-eslint/parser")

const ruleTester = new RuleTester()
ruleTester.run("no-callback-literal", rule, {
Expand Down Expand Up @@ -40,6 +41,11 @@ ruleTester.run("no-callback-literal", rule, {
"cb(null)",
'cb(undefined, "super")',
'cb(null, "super")',
// https://github.com/eslint-community/eslint-plugin-n/issues/162
{
code: "cb(e as Error)",
languageOptions: { parser: tsParser },
},
],

invalid: [
Expand Down