sindresorhus/eslint-plugin-unicorn

Rule proposal: `try-maximum-statements`

Open

#651 创建于 2020年3月31日

在 GitHub 查看
 (29 评论) (7 反应) (0 负责人)JavaScript (5,022 star) (468 fork)user submission
help wantednew rule

描述

The rule name may be called unicorn/try-maximum-statements.

The idea is to avoid try on whole code blocks and encourage the developer to target the exact line statement of code that is expecting to throw.

Should we add a parameter to customize the maximum number of line statement or hardcode it to 1 line of code? 🤔

Note that I'm not a native english speaker so please fix the rule/option names.

Enforce a maximum number of lines statements to try

Applies to try clauses.

The maximum number of triable lines statements of code configurable, but defaults to 1.

This rule is not fixable.

Fail

try {
  doSomething();
  doSomethingElse();
} catch (err) {
  // …
}

Pass

try {
  doSingleThing();
} catch (err) {
  // …
}

Options

maximumNumberOfStatement

Type: number

You can set the maximum number of statements like this:

"unicorn/try-maximum-statement": [
	"error",
	{
		"maximumNumberOfStatement": 2
	}
]

贡献者指南