mysticatea/eslint-plugin-node

New Rule: disallow parameter destructuring in Node-style callbacks

Open

#123 opened on Jun 18, 2018

View on GitHub
 (2 comments) (0 reactions) (0 assignees)JavaScript (186 forks)github user discovery
acceptedenhancementhelp wanted

Repository metrics

Stars
 (956 stars)
PR merge metrics
 (PR metrics pending)

Description

Redirected from a ESLint core rule proposal: https://github.com/eslint/eslint/issues/10491

Please describe what the rule should do: When creating a Node-style callback function, disallow destructuring of parameters after the 'error' parameter.

Destructuring in a function signature requires that an argument exists when the function is invoked or it will cause a TypeError: Cannot destructure property .... The Node-style callback convention is prone to this mistake because on an Error we generally don't pass additional arguments.

It is also a high-impact bug because it is only triggered by failures during runtime, which are codepaths developers don't test very often. It can sneak through manual review because the programmer/reviewer will check that the callback's 'error' is handled and not realize the latent bug.

What category of rule is this? (place an "X" next to just one item)

[X] Warns about a potential error

Provide 2-3 code examples that this rule will warn about:

const fs = require('fs')

fs.stat('invalid path', (err, { size }) => {
  if (err) {
    console.log("Looks like file didn't exist, creating it...")
    // smoothly handle error
  }
  // do regular things
})

The above code causes a TypeError: Cannot destructure property 'size' of 'undefined' or 'null'. when the file doesn't exist.

I'm not sure of y'all's usefulness bar for new rules, I think this is perhaps on the fence. Let me know if you think this is doable and useful enough!

Contributor guide