From ec513f04705b1b9a3c8f8c405328d0ec43312a26 Mon Sep 17 00:00:00 2001 From: Jonas Kello Date: Fri, 29 Dec 2017 19:20:26 +0100 Subject: [PATCH] Change to no-loop-statement. --- CHANGELOG.md | 2 +- README.md | 6 +++--- ...orStatementRule.ts => noLoopStatementRule.ts} | 6 ++++-- .../default/test.ts.lint | 16 +++++++++++++++- .../default/tslint.json | 2 +- tslint-immutable.json | 2 +- 6 files changed, 25 insertions(+), 9 deletions(-) rename src/{noForStatementRule.ts => noLoopStatementRule.ts} (74%) rename test/rules/{no-for-statement => no-loop-statement}/default/test.ts.lint (56%) rename test/rules/{no-for-statement => no-loop-statement}/default/tslint.json (68%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 840b126..0043fa8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index 3232e6a..bba008a 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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]; diff --git a/src/noForStatementRule.ts b/src/noLoopStatementRule.ts similarity index 74% rename from src/noForStatementRule.ts rename to src/noLoopStatementRule.ts index 8786445..0998e73 100644 --- a/src/noForStatementRule.ts +++ b/src/noLoopStatementRule.ts @@ -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( @@ -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: [] diff --git a/test/rules/no-for-statement/default/test.ts.lint b/test/rules/no-loop-statement/default/test.ts.lint similarity index 56% rename from test/rules/no-for-statement/default/test.ts.lint rename to test/rules/no-loop-statement/default/test.ts.lint index 56c0a4a..c51a98f 100644 --- a/test/rules/no-for-statement/default/test.ts.lint +++ b/test/rules/no-loop-statement/default/test.ts.lint @@ -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. diff --git a/test/rules/no-for-statement/default/tslint.json b/test/rules/no-loop-statement/default/tslint.json similarity index 68% rename from test/rules/no-for-statement/default/tslint.json rename to test/rules/no-loop-statement/default/tslint.json index ab79393..f2e911f 100644 --- a/test/rules/no-for-statement/default/tslint.json +++ b/test/rules/no-loop-statement/default/tslint.json @@ -1,6 +1,6 @@ { "rulesDirectory": ["../../../../rules"], "rules": { - "no-for-statement": true + "no-loop-statement": true } } diff --git a/tslint-immutable.json b/tslint-immutable.json index b2bca46..b6329c2 100644 --- a/tslint-immutable.json +++ b/tslint-immutable.json @@ -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,