mysticatea/eslint-plugin-node

New Rule: disallow parameter destructuring in Node-style callbacks

Open

#123 geöffnet am 18. Juni 2018

Auf GitHub ansehen
 (2 Kommentare) (0 Reaktionen) (0 zugewiesene Personen)JavaScript (186 Forks)github user discovery
acceptedenhancementhelp wanted

Repository-Metriken

Stars
 (956 Stars)
PR-Merge-Metriken
 (PR-Metriken ausstehend)

Beschreibung

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