diff --git a/README.md b/README.md index 4a7df42..6d18298 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,7 @@ In addition to immutable rules this project also contains a few rules for enforc * [no-class](#no-this-no-class) * [no-mixed-interface](#no-mixed-interface) * [no-expression-statement](#no-expression-statement) + * [no-if-statement](#no-if-statement) * [Recommended built-in rules](#recommended-built-in-rules) ## Immutability rules @@ -304,6 +305,26 @@ This rule checks that the value of an expression is assigned to a variable and t "no-expression-statement": [true, {"ignore-prefix": ["console.log", "console.error"]}] ``` +### no-if-statement +If statements is not a good fit for functional style programming as they are not expresssions and do not return a value. This rule disallows if statements. + +```typescript +let x; +if(i === 1) { + x = 2; +} else { + x = 3; +} +``` + +Instead consider using the [tenary operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator) which is an expression that returns a value: + +```typescript +const x = i === 1 ? 2 : 3; +``` + +For more background see this [blog post](https://hackernoon.com/rethinking-javascript-the-if-statement-b158a61cd6cb) and discussion in [#54](https://github.com/jonaskello/tslint-immutable/issues/54). + ## Options ### Using the `ignore-local` option diff --git a/src/noIfStatementRule.ts b/src/noIfStatementRule.ts new file mode 100644 index 0000000..444619d --- /dev/null +++ b/src/noIfStatementRule.ts @@ -0,0 +1,26 @@ +import * as ts from "typescript"; +import * as Lint from "tslint"; +import { + createInvalidNode, + CheckNodeResult, + createCheckNodeRule +} from "./shared/check-node"; + +type Options = {}; + +// tslint:disable-next-line:variable-name +export const Rule = createCheckNodeRule( + checkNode, + "Unexpected if, use tenary operator instead." +); + +function checkNode( + node: ts.Node, + _ctx: Lint.WalkContext +): CheckNodeResult { + return node && node.kind === ts.SyntaxKind.IfStatement + ? { invalidNodes: [createInvalidNode(node)] } + : { + invalidNodes: [] + }; +} diff --git a/test/rules/no-if-statement/default/test.ts.lint b/test/rules/no-if-statement/default/test.ts.lint new file mode 100644 index 0000000..7404c44 --- /dev/null +++ b/test/rules/no-if-statement/default/test.ts.lint @@ -0,0 +1,10 @@ +var x = 0; + +if(i === 1) { +~~~~~~~~~~~~~ + x = 2; +~~~~~~~~~~ +} +~ [failure] + +[failure]: Unexpected if, use tenary operator instead. diff --git a/test/rules/no-if-statement/default/tslint.json b/test/rules/no-if-statement/default/tslint.json new file mode 100644 index 0000000..332aeb2 --- /dev/null +++ b/test/rules/no-if-statement/default/tslint.json @@ -0,0 +1,6 @@ +{ + "rulesDirectory": ["../../../../rules"], + "rules": { + "no-if-statement": true + } +} diff --git a/tslint-immutable.json b/tslint-immutable.json index eb3a71f..4210439 100644 --- a/tslint-immutable.json +++ b/tslint-immutable.json @@ -4,6 +4,7 @@ "no-class": false, "no-delete": false, "no-expression-statement": false, + "no-if-statement": false, "no-let": false, "no-method-signature": false, "no-mixed-interface": false,