question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

no-cond-assign analyzes too deep into conditionals

See original GitHub issue

What version are you using?

3.2.2

What did you do?

const arr = [1, 2, 3];

if (arr.some(val => {
  let b = true;
  if (val === 2) {
    b = false;
  }
  return b;
})) console.log('returned true');

What happened?

With this eslintrc:

module.exports = {
    "env": {
        "browser": true,
        "es6": true
    },
    "extends": "eslint:recommended",
    "rules": {
      "no-console": 0,
      "no-cond-assign": [2, 'always']
    }
};

It signaled:

Unexpected assignment within an 'if' statement  no-cond-assign

What did you expect to happen?

It should have ceased to analyze once in the callback of Array.prototype.some and should not care at all about any assignments within. The purpose of the rule is to avoid mistakenly doing an assignment instead of a comparison within a conditional, not to prevent assignments at all.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:16 (12 by maintainers)

github_iconTop GitHub Comments

1reaction
Satyamcommented, Aug 15, 2016

@pmcelhaney That rule is exactly the one that the airbnb config has set: https://github.com/airbnb/javascript/blob/master/packages/eslint-config-airbnb-base/rules/errors.js#L7

The code doSomething(foo = 5) whether inside a conditional or not, should produce an error, but not under this rule. Just like a programmer might have mistakenly used an assignment instead of a double or triple equal, the guy writing the above code might have confused the new ES6 default parameter value feature, which is valid for a function declaration but not function invocation.

0reactions
platinumazurecommented, Aug 16, 2016

Apologies @nzakas, I hadn’t had time to verify until just now.

Here’s a snippet that repros in the demo:

/* eslint no-cond-assign: ["error", "always"] */
if (arr.some(function (item) { item = 5; })) {
    doSomething();
}

As discussed earlier in the issue, there is a separate issue (parentheses being too zealously considered) that prevents this from showing up as a bug with the default “except-parens” configuration. But I believe we can accept this bug, unless you disagree.

Read more comments on GitHub >

github_iconTop Results From Across the Web

In Too Deep: The Perils of Data Analysis | Lucidchart Blog
In data analytics, one question brings on another. Avoid the slippery slope of data analysis with a proven process for impact-drive research. Learn...
Read more >
The Logic of Conditionals - Stanford Encyclopedia of Philosophy
Logics of indicative conditionals, in so far as they are based on truth evaluations, all agree that a conditional is false if its...
Read more >
How Can I Create Stronger Analysis? - College of LSA
In the essay, each piece of evidence selected is paired with deep analysis that builds or elaborates on the last until the thesis...
Read more >
Content Analysis Method and Examples
A content analysis is a tool for researchers to easily determine the presence of words, themes, or concepts from qualitative data.
Read more >
How to Beat 'Analysis Paralysis' and Make All the Decisions
Explore possible causes of overthinking. It often helps to understand why you have trouble making choices. Did a previous decision not pan out ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found