Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prefer-const false positive #5610

Closed
SimenB opened this issue Mar 18, 2016 · 4 comments
Closed

prefer-const false positive #5610

SimenB opened this issue Mar 18, 2016 · 4 comments
Assignees
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion bug ESLint is working incorrectly rule Relates to ESLint's core rules

Comments

@SimenB
Copy link
Contributor

SimenB commented Mar 18, 2016

What version of ESLint are you using?
2.4.0

What parser (default, Babel-ESLint, etc.) are you using?
default

Please show your full configuration:

{
  "parserOptions": {
    "ecmaVersion": 6,
    "sourceType": "module",
    "ecmaFeatures": {
      "jsx": true
    }
  },
  "rules": {
    "prefer-const": 2
  }
}

What did you do? Please include the actual source code causing the issue.

import React from 'react';
import { browser } from '../../util/browserUtil';

let DevTools;

if (!browser.ie8()) DevTools = require('../devTools/devTools');

export default ({ children }) => (
  <div>
    <div>
      {children}
    </div>
    {DevTools && <DevTools />}
  </div>
);

What did you expect to happen?
No warning, as DevTools is used outside of the if-statement

What actually happened? Please include the actual, raw output from ESLint.

/home/testyo/repos/meeeeeeep/file.js
  6:5  error  'DevTools' is never reassigned, use 'const' instead  prefer-const

I'm aware of many potentially related issues (#5251, #5283, and #5284), but I don't think this one is covered

@eslintbot eslintbot added the triage An ESLint team member will look at this issue soon label Mar 18, 2016
@btmills btmills added bug ESLint is working incorrectly rule Relates to ESLint's core rules evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion and removed triage An ESLint team member will look at this issue soon labels Mar 18, 2016
@btmills
Copy link
Member

btmills commented Mar 18, 2016

This is interesting. Tried these in the demo:

Valid:

/* eslint prefer-const: 2 */
let foo;
if (bar) {
    foo = 42;
}
baz(foo);

Invalid (valid example with braces around if body removed):

/* eslint prefer-const: 2 */
let foo;
if (bar) foo = 42;
baz(foo);
2:5 - 'foo' is never reassigned, use 'const' instead. (prefer-const)

Invalid (valid example with function call removed):

/* eslint prefer-const: 2 */
let foo;
if (bar) {
    foo = 42;
}
2:5 - 'foo' is never reassigned, use 'const' instead. (prefer-const)

Why is it that the combination of curly braces around the if with a function call is considered valid, but if I remove either it errors?

@SimenB if you add curly braces around the if body, does it stop warning for you too?

@SimenB
Copy link
Contributor Author

SimenB commented Mar 18, 2016

@btmills Ah, good catch! Adding braces makes the warning go away

@btmills
Copy link
Member

btmills commented Mar 18, 2016

Cool. So at least there's a workaround that can be used while we figure out what's going on here.

I don't see any tests that cover this case. @mysticatea you looked at scoping for prefer-const recently - any ideas as to what's going on here?

@mysticatea mysticatea added accepted There is consensus among the team that this change meets the criteria for inclusion and removed evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion labels Mar 19, 2016
@mysticatea
Copy link
Member

I has forgotten this case:

/* eslint prefer-const: 2 */
let foo;
if (bar) foo = 42;
baz(foo);

@mysticatea mysticatea self-assigned this Mar 19, 2016
nzakas added a commit that referenced this issue Mar 19, 2016
Fix: `prefer-const` false positive at non-blocked if (fixes #5610)
@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Feb 6, 2018
@eslint-deprecated eslint-deprecated bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Feb 6, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion bug ESLint is working incorrectly rule Relates to ESLint's core rules
Projects
None yet
Development

No branches or pull requests

4 participants