This repository has been archived by the owner on Mar 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 887
[new-rule]prefer-while rule #3750
Merged
Merged
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
45cfcea
prefer-while rule
rwaskiewicz 5590243
renamed failure object creation method
rwaskiewicz 40b71b0
fix json test file indentatons
rwaskiewicz 0f83a6f
Address review comments
rwaskiewicz 1b09f36
remove uneccessary type assertion
rwaskiewicz 569d0b8
Add rationale field to prefer-while rule metadata
rwaskiewicz b825e27
Use 'let' instead of 'const' in test
rwaskiewicz 596f532
Update rule description & failure string
rwaskiewicz eebadbc
Coalesce test cases
rwaskiewicz 187bc3d
Add additional test cases
rwaskiewicz 61d8259
Add code examples
rwaskiewicz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/** | ||
* @license | ||
* Copyright 2018 Palantir Technologies, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { isForStatement } from "tsutils"; | ||
import * as ts from "typescript"; | ||
import * as Lint from "../index"; | ||
|
||
export class Rule extends Lint.Rules.AbstractRule { | ||
/* tslint:disable:object-literal-sort-keys */ | ||
public static metadata: Lint.IRuleMetadata = { | ||
ruleName: "prefer-while", | ||
description: "Prefer `while` loops instead of `for` loops without an initializer and incrementor.", | ||
rationale: "Simplifies the readability of the loop statement, while maintaining the same functionality.", | ||
optionsDescription: "Not configurable.", | ||
options: null, | ||
optionExamples: [true], | ||
hasFix: true, | ||
type: "style", | ||
typescriptOnly: false, | ||
}; | ||
/* tslint:enable:object-literal-sort-keys */ | ||
|
||
public static FAILURE_STRING = "Prefer `while` loops instead of `for` loops without an initializer and incrementor."; | ||
|
||
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] { | ||
const failures: Lint.RuleFailure[] = []; | ||
|
||
const cb = (node: ts.Node): void => { | ||
if (isForStatement(node) && this.doesNodeViolateRule(node)) { | ||
failures.push(this.createFailure(sourceFile, node)); | ||
} | ||
return ts.forEachChild(node, cb); | ||
}; | ||
|
||
ts.forEachChild(sourceFile, cb); | ||
return failures; | ||
} | ||
|
||
private doesNodeViolateRule(node: ts.ForStatement) { | ||
return (node.initializer === undefined && node.incrementor === undefined); | ||
} | ||
|
||
private createFailure(sourceFile: ts.SourceFile, node: ts.ForStatement): Lint.RuleFailure { | ||
const start = node.getStart(sourceFile); | ||
const end = node.statement.pos; | ||
|
||
let fix: Lint.Fix; | ||
if (node.condition === undefined) { | ||
fix = Lint.Replacement.replaceFromTo(start, end, "while (true)"); | ||
} else { | ||
fix = [ | ||
Lint.Replacement.replaceFromTo(start, node.condition.getStart(sourceFile), "while ("), | ||
Lint.Replacement.deleteFromTo(node.condition.end, end - 1), | ||
]; | ||
} | ||
return new Lint.RuleFailure(sourceFile, start, end, Rule.FAILURE_STRING, this.ruleName, fix); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// for loops without an initializer, termination condition, and incrementor should be updated | ||
while (true) { | ||
console.log(x); | ||
} | ||
|
||
// for loops without an initializer and incrementor should be updated | ||
while (true===true) { | ||
console.log(x); | ||
} | ||
|
||
// for loops with an initializer, termination condition, and incrementor should remain untouched | ||
for(let x = 1; x < 10; x++) { | ||
console.log(x); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we add a test case for when it's incremented with
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes and yes! Added test cases in 187bc3d |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// for loops without an initializer, termination condition, and incrementor should be updated | ||
for(;;) { | ||
~~~~~~~ [Prefer `while` loops instead of `for` loops without an initializer and incrementor.] | ||
console.log(x); | ||
} | ||
|
||
// for loops without an initializer and incrementor should be updated | ||
for(;true===true;) { | ||
~~~~~~~~~~~~~~~~~~ [Prefer `while` loops instead of `for` loops without an initializer and incrementor.] | ||
console.log(x); | ||
} | ||
|
||
// for loops with an initializer, termination condition, and incrementor should remain untouched | ||
for(let x = 1; x < 10; x++) { | ||
console.log(x); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"rules": { | ||
"prefer-while": true | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry to harp on this again 😊 but could you please add a
rationale
as well? It's not clear from just thedescription
why such a thing is better.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No problem 🙂 Done in 84c6d8e