tslint-immutable
tslint-immutable copied to clipboard
Rule Idea: no-break, no-if, no-loops
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"
]
This seems like some nice additions to the functional style rules :-).
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?
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.
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/
So I'm thinking about implementing these rules:
no-loop-break(does not check inswitch)no-continueno-ifno-switch(which implicitly bansbreakinswitch)no-forno-whilecovers bothwhile() {}anddo {} 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.
I published 4.5.0 with the following new rules:
no-loopno-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-breakno-loop-continueno-switch
If anyone care to implement them I'm open for a PR on any or all of the above rules :-).
Awesome! Thank you!