tslint-consistent-codestyle icon indicating copy to clipboard operation
tslint-consistent-codestyle copied to clipboard

Make early-exit consider throwing as an early exit

Open alcuadrado opened this issue 5 years ago • 0 comments
trafficstars

Hi @ajafff

Thanks for creating this package. I use multiple rules, which I find very useful.

I was considering enabling early-exit, but was surprised by tslint complaining about this pattern:

private _validateSomething(param: T) {
    if (condition(param)) {
      throw new InvalidParam(param);
    }
}

I can refactor that into this, but it feels like making things more complex instead of simpler:

private _validateSomething(param: T) {
    if (!condition(param)) {
      return;
    }

    throw new InvalidParam(param);
}

So I modified this rule to consider throwing as an early exit.

I'm not 100% sure if what I did is correct, nor if the test I added actually works, but I preferred submitting this early to get your opinion about this change.

alcuadrado avatar Feb 14 '20 23:02 alcuadrado