mysticatea/eslint-plugin-node

New Rule: disallow parameter destructuring in Node-style callbacks

Open

#123 创建于 2018年6月18日

在 GitHub 查看
 (2 评论) (0 反应) (0 负责人)JavaScript (186 fork)github user discovery
acceptedenhancementhelp wanted

仓库指标

Star
 (956 star)
PR 合并指标
 (PR 指标待抓取)

描述

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!

贡献者指南