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

ban-comma-operator: ignore for-loop incrementors #3485

Merged

Conversation

vilchik-elena
Copy link
Contributor

PR checklist

  • Addresses an existing issue: #0000
  • New feature, bugfix, or enhancement
    • Includes tests
  • Documentation update

Overview of change:

Rule ban-comma-operator raises an issue on every usage of comma operator, while using it inside for-loop incrementor IMO should be authorised.

for (let i = 0, j = 0; i < 10; i++, j++) {
}

CHANGELOG.md entry:

[enhancement] ban-comma-operator ignores comma operator inside for-loop incrementor

@palantirtech
Copy link
Member

Thanks for your interest in palantir/tslint, @vilchik-elena! Before we can accept your pull request, you need to sign our contributor license agreement - just visit https://cla.palantir.com/ and follow the instructions. Once you sign, I'll automatically update this pull request.

Copy link
Contributor

@ajafff ajafff left a comment

Choose a reason for hiding this comment

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

The proposed change seems reasonable.


function isForLoopIncrementor(node: ts.Node) {
const parent = node.parent;
return parent && parent.kind === ts.SyntaxKind.ForStatement && (parent as ts.ForStatement).incrementor === node;
Copy link
Contributor

Choose a reason for hiding this comment

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

node.parent will never be undefined here. You can simply use const parent = node.parent!

That also fixes the lint error on this line, because parent is not a boolean expression.

function isForLoopIncrementor(node: ts.Node) {
const parent = node.parent;
return parent && parent.kind === ts.SyntaxKind.ForStatement && (parent as ts.ForStatement).incrementor === node;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

linter is complaining about missing newline before return

@ajafff
Copy link
Contributor

ajafff commented Nov 14, 2017

Just to make sure: We probably don't want to whitelist the for loop initializer?

let i: number, arr: string[], len: number;
for (i = 0, arr = doStuff(), len = arr.length; i < arr.length; ++i) {
}

@vilchik-elena
Copy link
Contributor Author

Indeed, I think it's better avoid comma in initiallizer, you better put declaration instead.

@vilchik-elena
Copy link
Contributor Author

ci failed again. Is it FP? Locally it's green for me

@ajafff
Copy link
Contributor

ajafff commented Nov 15, 2017

CI failure is unrelated. Seems like typescript@next broke one of our tests.
I'll take care of that in a separate PR

@ajafff ajafff mentioned this pull request Nov 15, 2017
4 tasks
@ajafff
Copy link
Contributor

ajafff commented Nov 15, 2017

CI will be fixed by #3488
I can't merge Pull Requests with failing CI so we just need to wait until that other PR lands.

@ajafff ajafff mentioned this pull request Nov 15, 2017
@adidahiya
Copy link
Contributor

thanks @vilchik-elena!

@adidahiya adidahiya merged commit b9b26f4 into palantir:master Nov 15, 2017
HyphnKnight pushed a commit to HyphnKnight/tslint that referenced this pull request Apr 9, 2018
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