Tslint: [no-conditional-assignment]

Created on 2 Aug 2018  路  11Comments  路  Source: palantir/tslint

Feature request

I'd like to request the feature discussed in the original implementation of the no-conditional-assignment rule.

The code I'm cleaning up has the following:

let varName: SomeType | null;
while (varName = maybeNullExpensiveCalculation()) {
  // Do stuff with varName, requiring that it not be null
}

It would be great to be able to just add another set of parens around the assignment, which I think is more or less standard, to avoid having to add a tslint annotation.

Another alternative here is to make it into a while (true) loop with a check and break at the top, but that's a little ugly:

while (true) {
  const varName = maybeNullExpensiveCalculation();
  if (varName === null) break;
  // Do stuff with varName, requiring that it not be null
}
Rule Enhancement 馃尮 R.I.P. 馃尮

Most helpful comment

SGTM. Let's take in an option to allow this the way ESLint does.

All 11 comments

@felipeochoa there's always a third option: disable the rule for the line, making it clear that this assignment is intentional.

let varName: SomeType | null;
// tslint:disable-next-line:no-conditional-assignment
while (varName = maybeNullExpensiveCalculation()) {
  // Do stuff with varName, requiring that it not be null
}

Oh yes. Forgot to mention that somehow!

Is your featuer request that the rule no-conditional-assignment should not show an error when writing the following code:

let varName: SomeType | null;
while ((varName = maybeNullExpensiveCalculation())) {
  // Do stuff with varName, requiring that it not be null
}

?

I would not like such exception since it is not obvious for me how this is different from the code you posted.

The surrounding parentheses are a common convention to tell the linter that you're assigning on purpose

common convention

@felipeochoa can you provide references for that?

GCC has the -Wparentheses option which behaves in this way. (See e.g., this comment)

ESLint defaults to this style (except-parens) for their no-cond-assign rule.

Python assignment expressions are usually required to be enclosed in parens for readability

Etc.

SGTM. Let's take in an option to allow this the way ESLint does.

The same applies also fore RegExp assignment in conditionals, such as:
while ((match = someRegExp.exec("string")) { .... }
or
while ((match = someRegExp.exec("string") !== null) { .... }
I see clear condition here and don't think that it's right to mark as a potential problem.

I think both oft these examples are missing one closing paren.

And all the time I'm asking myself: the additional parents are not making a technical difference in the resulting value, right?
So if those are not a problem in your code base, why don't you just disable the rule (on the next-line, file or on the global level...)?
But I the question might be beyond the scope of this feature request.

Closing due to low priority and the deprecation roadmap #4534

馃 Beep boop! 馃憠 TSLint is deprecated 馃憟 _(#4534)_ and you should switch to typescript-eslint! 馃

馃敀 This issue is being locked to prevent further unnecessary discussions. Thank you! 馃憢

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ghost picture ghost  路  3Comments

zewa666 picture zewa666  路  3Comments

mrand01 picture mrand01  路  3Comments

Ne-Ne picture Ne-Ne  路  3Comments

ypresto picture ypresto  路  3Comments