-
Notifications
You must be signed in to change notification settings - Fork 887
Conversation
src/rules/preferWhileRule.ts
Outdated
const failures: Lint.RuleFailure[] = []; | ||
|
||
const cb = (node: ts.Node): void => { | ||
if (node.kind === ts.SyntaxKind.ForStatement && this.doesNodeViolateRule(node as ts.ForStatement)) { |
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.
Use isForStatement
from tsutils
so you don't need the hardcast
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.
TIL - nice! Updated in 7eda22c
src/rules/preferWhileRule.ts
Outdated
}; | ||
/* tslint:enable:object-literal-sort-keys */ | ||
|
||
public static FAILURE_STRING = "prefer while"; |
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.
Seems rather terse compared to other error messages?
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.
Agreed, definitely something I overlooked. Updated to something a little more descriptive in 7eda22c:
Expected a 'while' loop instead of a 'for' loop without an initializer and incrementor
src/rules/preferWhileRule.ts
Outdated
optionsDescription: "Not configurable.", | ||
options: null, | ||
optionExamples: [true], | ||
hasFix: true, |
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 the description
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
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.
@rwaskiewicz cool feature! some feedback that needs to be addressed...
src/rules/preferWhileRule.ts
Outdated
/* tslint:disable:object-literal-sort-keys */ | ||
public static metadata: Lint.IRuleMetadata = { | ||
ruleName: "prefer-while", | ||
description: "Prefer while loops when instead of a for loop without an initializer and incrementor.", |
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.
remove the words "when" and "a" and add backticks around while
and for
to distinguish them from english.
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.
Done in ec8b337
src/rules/preferWhileRule.ts
Outdated
}; | ||
/* tslint:enable:object-literal-sort-keys */ | ||
|
||
public static FAILURE_STRING = "Expected a 'while' loop instead of a 'for' loop without an initializer and incrementor"; |
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.
i'd just reuse the description
above here, particularly the word "prefer" (like the rule name)
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.
Done in ec8b337. I ended up combining tests in a separate commit, so there's a little bit of churn here with files that will end up getting combined (sorry about that!)
@@ -0,0 +1,5 @@ | |||
{ | |||
"rules": { | |||
"prefer-while": true |
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.
tests that share the same config file should appear in the same .ts.lint
file. please merge all of these so there's only true
and false
tests. one .ts.lint
file can have more than one "test case" in it.
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.
Done in 8fa8062. I added some comments to the test cases. Please let me know if they look amiss or should be added in the first place.
@giladgray Thanks for the feedback! I've updated this branch as per your comments. CI seems to be failing on a non-related test, LMK if any action is needed on my part there or if you have any additional comments 🙂 |
@rwaskiewicz can you merge master into your branch or rebase off of it and see if the build still fails? |
@suchanlee I rebased off the latest
Looking at the latest CI builds for the master branch, it looks like this is occurring there as well. Would you prefer I pushed the rebase branch now, or hold off for the moment? |
Oh hmm ill look into the failing build. Please hold off for now |
@suchanlee I've tracked it down to an issue with missing backticks in the completed-docs rule. I've put up #3868 to address. Could you please take a look when you get a chance? |
@rwaskiewicz thanks for getting to that so fast! I just merged it. |
Implements prefer-while rule in place of a for loop without an initializer and incrementer
- Use isForStatement from tsutils - Update failure string to be more descriptive - Updated test messages
- assertion now uneccessary with using isForStatement from tsutils
- Updated the description and failure string of prefer-while - Updated tests accordingly
- Combine test cases for 'bad' for loops and case for 'good' for loops - Remove unneeded files - Add comments to test cases
@suchanlee Awesome! Rebased off the latest |
Hey @rwaskiewicz one thing we recently added to our docs is code examples of what passes and fails. It would be awesome if something like that can be added for this new rule. Example: https://github.com/palantir/tslint/pull/3602/files#diff-25413efe179575004d7787097d959e36R1 |
test/rules/prefer-while/test.ts.fix
Outdated
// 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 comment
The reason will be displayed to describe this comment to others. Learn more.
can we add a test case for when it's incremented with x+=1
?
does the rule handle the case when the increment happens within the block? Example:
for (let i = 0; i < 10;) {
i += 1;
}
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.
Yes and yes! Added test cases in 187bc3d
- Added test case with incrmentor in body of loop - Added test case with += incrementor
- Added code examples for prefer-while rule
@suchanlee Regarding code-examples, that looks super useful! I took a stab at them in 61d8259 - LMK what you think |
Thank you for your contribution @rwaskiewicz! |
PR checklist
Overview of change: