Skip to content

Commit

Permalink
Change to no-loop-statement.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaskello committed Dec 29, 2017
1 parent 8cb5590 commit ec513f0
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 9 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Expand Up @@ -17,7 +17,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

* New rule `no-if-statement`. See [readme](https://github.com/jonaskello/tslint-immutable#no-if-statement) for more info and [#54](https://github.com/jonaskello/tslint-immutable/issues/54) for discussion.

* New rule `no-for-statement`. See [readme](https://github.com/jonaskello/tslint-immutable#no-for-statement) for more info and [#54](https://github.com/jonaskello/tslint-immutable/issues/54) for discussion.
* New rule `no-loop-statement`. See [readme](https://github.com/jonaskello/tslint-immutable#no-loop-statement) for more info and [#54](https://github.com/jonaskello/tslint-immutable/issues/54) for discussion.

### Fixed

Expand Down
6 changes: 3 additions & 3 deletions README.md
Expand Up @@ -47,7 +47,7 @@ In addition to immutable rules this project also contains a few rules for enforc
* [no-mixed-interface](#no-mixed-interface)
* [no-expression-statement](#no-expression-statement)
* [no-if-statement](#no-if-statement)
* [no-for-statement](#no-for-statement)
* [no-loop-statement](#no-loop-statement)
* [Recommended built-in rules](#recommended-built-in-rules)

## Immutability rules
Expand Down Expand Up @@ -370,9 +370,9 @@ 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).

### no-for-statement
### no-loop-statement

In functional programming we want everthing to be an expression that returns a value. The `for` statement is not an expression. This rule disallows for statements, including `for of` and `for in`.
In functional programming we want everthing to be an expression that returns a value. Loops in typescript are statements so they are not a good fit for a functional programming style. This rule disallows for loop statements, including `for`, `for...of`, `for...in`, `while`, and `do...while`.

```typescript
const numbers = [1, 2, 3];
Expand Down
6 changes: 4 additions & 2 deletions src/noForStatementRule.ts → src/noLoopStatementRule.ts
Expand Up @@ -11,7 +11,7 @@ type Options = {};
// tslint:disable-next-line:variable-name
export const Rule = createCheckNodeRule(
checkNode,
"Unexpected for, use map or reduce instead."
"Unexpected loop, use map or reduce instead."
);

function checkNode(
Expand All @@ -21,7 +21,9 @@ function checkNode(
return node &&
(node.kind === ts.SyntaxKind.ForStatement ||
node.kind === ts.SyntaxKind.ForInStatement ||
node.kind === ts.SyntaxKind.ForOfStatement)
node.kind === ts.SyntaxKind.ForOfStatement ||
node.kind === ts.SyntaxKind.WhileStatement ||
node.kind === ts.SyntaxKind.DoStatement)
? { invalidNodes: [createInvalidNode(node)] }
: {
invalidNodes: []
Expand Down
Expand Up @@ -21,4 +21,18 @@ for(const x of y) {
}
~ [failure]

[failure]: Unexpected for, use map or reduce instead.
while(1 === 1) {
~~~~~~~~~~~~~~~~~~~
console.log("a");
~~~~~~~~~~~~~~~~~~~
}
~ [failure]

do {
~~~~
console.log("a");
~~~~~~~~~~~~~~~~~~~
} while(1 === 1)
~~~~~~~~~~~~~~~~ [failure]

[failure]: Unexpected loop, use map or reduce instead.
@@ -1,6 +1,6 @@
{
"rulesDirectory": ["../../../../rules"],
"rules": {
"no-for-statement": true
"no-loop-statement": true
}
}
2 changes: 1 addition & 1 deletion tslint-immutable.json
Expand Up @@ -4,9 +4,9 @@
"no-class": false,
"no-delete": false,
"no-expression-statement": false,
"no-for-statement": false,
"no-if-statement": false,
"no-let": false,
"no-loop-statement": false,
"no-method-signature": false,
"no-mixed-interface": false,
"no-object-mutation": false,
Expand Down

0 comments on commit ec513f0

Please sign in to comment.