Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

[new-rule]prefer-while rule #3750

Merged
merged 11 commits into from
May 3, 2018
Merged

Conversation

rwaskiewicz
Copy link
Contributor

@rwaskiewicz rwaskiewicz commented Mar 1, 2018

PR checklist

Overview of change:

const failures: Lint.RuleFailure[] = [];

const cb = (node: ts.Node): void => {
if (node.kind === ts.SyntaxKind.ForStatement && this.doesNodeViolateRule(node as ts.ForStatement)) {
Copy link
Contributor

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

Copy link
Contributor Author

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

};
/* tslint:enable:object-literal-sort-keys */

public static FAILURE_STRING = "prefer while";
Copy link
Contributor

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?

Copy link
Contributor Author

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

optionsDescription: "Not configurable.",
options: null,
optionExamples: [true],
hasFix: true,
Copy link
Contributor

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.

Copy link
Contributor Author

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

Copy link

@giladgray giladgray left a 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...

/* 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.",

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in ec8b337

};
/* tslint:enable:object-literal-sort-keys */

public static FAILURE_STRING = "Expected a 'while' loop instead of a 'for' loop without an initializer and incrementor";

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)

Copy link
Contributor Author

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

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.

Copy link
Contributor Author

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.

@rwaskiewicz
Copy link
Contributor Author

@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 🙂

@suchanlee
Copy link
Contributor

@rwaskiewicz can you merge master into your branch or rebase off of it and see if the build still fails?

@rwaskiewicz
Copy link
Contributor Author

@suchanlee I rebased off the latest master locally (f47aeb3). When rerunning npm run compile (locally) I ran into a different issue with completed-docs:

src/rules/completedDocsRule.ts(186,39): error TS1005: ']' expected.
src/rules/completedDocsRule.ts(186,86): error TS1128: Declaration or statement expected.
src/rules/completedDocsRule.ts(222,16): error TS1005: ';' expected.
src/rules/completedDocsRule.ts(224,18): error TS1005: ';' expected.
src/rules/completedDocsRule.ts(256,13): error TS1109: Expression expected.
src/rules/completedDocsRule.ts(256,14): error TS1128: Declaration or statement expected.
src/rules/completedDocsRule.ts(257,10): error TS1128: Declaration or statement expected.
src/rules/completedDocsRule.ts(283,18): error TS1005: ';' expected.
src/rules/completedDocsRule.ts(289,13): error TS1005: ';' expected.
src/rules/completedDocsRule.ts(290,23): error TS1005: ';' expected.
src/rules/completedDocsRule.ts(291,25): error TS1005: ';' expected.
src/rules/completedDocsRule.ts(292,5): error TS1109: Expression expected.
src/rules/completedDocsRule.ts(295,5): error TS1128: Declaration or statement expected.
src/rules/completedDocsRule.ts(295,13): error TS1128: Declaration or statement expected.
src/rules/completedDocsRule.ts(297,5): error TS1128: Declaration or statement expected.
src/rules/completedDocsRule.ts(297,39): error TS1005: ',' expected.
src/rules/completedDocsRule.ts(297,63): error TS1005: ',' expected.
src/rules/completedDocsRule.ts(297,76): error TS1005: ';' expected.
src/rules/completedDocsRule.ts(297,97): error TS1005: ';' expected.
src/rules/completedDocsRule.ts(304,5): error TS1128: Declaration or statement expected.
src/rules/completedDocsRule.ts(304,43): error TS1005: ',' expected.
src/rules/completedDocsRule.ts(304,88): error TS1005: '(' expected.
src/rules/completedDocsRule.ts(304,89): error TS1005: ',' expected.
src/rules/completedDocsRule.ts(304,105): error TS1005: ',' expected.
src/rules/completedDocsRule.ts(305,26): error TS1005: ',' expected.
src/rules/completedDocsRule.ts(305,34): error TS1005: ',' expected.
src/rules/completedDocsRule.ts(305,41): error TS1005: ';' expected.
src/rules/completedDocsRule.ts(310,5): error TS1128: Declaration or statement expected.
src/rules/completedDocsRule.ts(311,1): error TS1128: Declaration or statement expected.

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?

@suchanlee
Copy link
Contributor

Oh hmm ill look into the failing build. Please hold off for now

@rwaskiewicz
Copy link
Contributor Author

@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?

@suchanlee
Copy link
Contributor

@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
@rwaskiewicz
Copy link
Contributor Author

@suchanlee Awesome! Rebased off the latest master and CI appears to be passing now 🙂

@suchanlee
Copy link
Contributor

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

// for loops with an initializer, termination condition, and incrementor should remain untouched
for(let x = 1; x < 10; x++) {
console.log(x);
}
Copy link
Contributor

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;
}

Copy link
Contributor Author

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
@rwaskiewicz
Copy link
Contributor Author

@suchanlee Regarding code-examples, that looks super useful! I took a stab at them in 61d8259 - LMK what you think

@suchanlee suchanlee merged commit baa413e into palantir:master May 3, 2018
@suchanlee
Copy link
Contributor

Thank you for your contribution @rwaskiewicz!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants