sindresorhus/eslint-plugin-unicorn

Rule proposal: `try-maximum-statements`

Chiusa

#651 aperta il 31 mar 2020

 (29 commenti) (7 reazioni) (0 assegnatari)JavaScript (468 fork)user submission
help wantednew rule

Metriche repository

Star
 (5022 stelle)
Metriche merge PR
 (Merge medio 1g 16h) (399 PR mergiate in 30 g)

Descrizione

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
	}
]

Guida contributor