New Rule: disallow parameter destructuring in Node-style callbacks
#123 opened on 2018年6月18日
Repository metrics
- Stars
- (956 stars)
- PR merge metrics
- (PR metrics pending)
説明
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!