Skip to content

Commit

Permalink
New rule no-if-statement. Requested in #54.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaskello committed Dec 29, 2017
1 parent 09c1723 commit 8cab79c
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 0 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions src/noIfStatementRule.ts
Original file line number Diff line number Diff line change
@@ -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<Options>
): CheckNodeResult {
return node && node.kind === ts.SyntaxKind.IfStatement
? { invalidNodes: [createInvalidNode(node)] }
: {
invalidNodes: []
};
}
10 changes: 10 additions & 0 deletions test/rules/no-if-statement/default/test.ts.lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
var x = 0;

if(i === 1) {
~~~~~~~~~~~~~
x = 2;
~~~~~~~~~~
}
~ [failure]

[failure]: Unexpected if, use tenary operator instead.
6 changes: 6 additions & 0 deletions test/rules/no-if-statement/default/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"rulesDirectory": ["../../../../rules"],
"rules": {
"no-if-statement": true
}
}
1 change: 1 addition & 0 deletions tslint-immutable.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 8cab79c

Please sign in to comment.