tslint-immutable icon indicating copy to clipboard operation
tslint-immutable copied to clipboard

Rule Idea: no-break, no-if, no-loops

Open kevinSuttle opened this issue 8 years ago • 7 comments

https://hackernoon.com/rethinking-javascript-eliminate-the-switch-statement-for-better-code-5c81c044716d

"no-break": [true, {"ignore-switch": "true"}], // maybe continue is included here or a separate rule?
"no-if": "true", // or maybe "prefer-ternary"?
"no-loops": [
     true, 
     "while",
     "for",
      "do"
]

kevinSuttle avatar Sep 28 '17 21:09 kevinSuttle

This seems like some nice additions to the functional style rules :-).

jonaskello avatar Sep 28 '17 21:09 jonaskello

I'm think about how the options for no-loops would work.

"no-loops": [true]

That would disable all loops, including while, for, do.

"no-loops": [true, "while"]

That would only disable while leaving for and do available?

I'm thinking maybe it is more logical to have ignore options, like ignore-for, ignore-while etc.

Also not sure if do is needed as an option? If the while keyword is banned then do would not be useful anyway? And in that case maybe two separate rules named no-for and no-while would make more sense?

jonaskello avatar Sep 30 '17 16:09 jonaskello

You’re right. I like the last option. Agree it’s more logical. I like tailoring rules based on how frequently a part of code is likely to be used.

kevinSuttle avatar Sep 30 '17 20:09 kevinSuttle

FWIW: the standard lib has a no-for-in rule: https://palantir.github.io/tslint/rules/no-for-in-array/

Also, it has prefer-for-of, but that's still a loop. https://palantir.github.io/tslint/rules/prefer-for-of/

Maybe ban? https://palantir.github.io/tslint/rules/ban/

kevinSuttle avatar Oct 09 '17 20:10 kevinSuttle

So I'm thinking about implementing these rules:

  • no-loop-break (does not check in switch)
  • no-continue
  • no-if
  • no-switch (which implicitly bans break in switch)
  • no-for
  • no-while covers both while() {} and do {} while()

I think this would be a good division, but not 100% sure about no-loop-break and no-while. They could be implemented differently, for example no-break with option ignore-switch as in the original post. Also no-continue could be named no-loop-continue just for duality with no-loop-break.

jonaskello avatar Dec 28 '17 16:12 jonaskello

I published 4.5.0 with the following new rules:

  • no-loop
  • no-if-statement

In order to keep the amount of rules and documentation down I went back to the original suggestion of no-loop which includes all loop types.

These rules remains to be implemented:

  • no-loop-break
  • no-loop-continue
  • no-switch

If anyone care to implement them I'm open for a PR on any or all of the above rules :-).

jonaskello avatar Dec 29 '17 18:12 jonaskello

Awesome! Thank you!

kevinSuttle avatar Dec 30 '17 15:12 kevinSuttle