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 generates false positive (same scope) #5350

Closed
mdebruijne opened this Issue Feb 20, 2016 · 7 comments

Comments

Projects
None yet
5 participants
@mdebruijne
Contributor

mdebruijne commented Feb 20, 2016

There already is an issue report that prefer-const generates false positives when variables are declared in a higher scope (issue #5251), but also in the same scope prefer-const generates false positives.

Code (simplified);

'use strict'

/* eslint prefer-const: 2 */
/* eslint-env es6 */

const myFunction = () => {
  let myVar

  myVar = 'myValue'
  console.log(myVar)
}

myFunction()

ESLint output;

7:7  error  'myVar' is never modified, use 'const' instead  prefer-const

ESLint config;

none, only inline config

Versions;

eslint --version
v2.2.0

node --version
v5.6.0

If you need more information then please let me know.

@eslintbot eslintbot added the triage label Feb 20, 2016

@michaelficarra

This comment has been minimized.

Show comment
Hide comment
@michaelficarra

michaelficarra Feb 20, 2016

Member

What makes you think that you can't replace that with a const?

Member

michaelficarra commented Feb 20, 2016

What makes you think that you can't replace that with a const?

@ilyavolodin ilyavolodin added question and removed triage labels Feb 20, 2016

@ilyavolodin

This comment has been minimized.

Show comment
Hide comment
@ilyavolodin

ilyavolodin Feb 20, 2016

Member

@mdebruijne You example can be safely re-written with

'use strict'

/* eslint prefer-const: 2 */
/* eslint-env es6 */

const myFunction = () => {
  const myVar = 'myValue';

  console.log(myVar);
};

myFunction();
Member

ilyavolodin commented Feb 20, 2016

@mdebruijne You example can be safely re-written with

'use strict'

/* eslint prefer-const: 2 */
/* eslint-env es6 */

const myFunction = () => {
  const myVar = 'myValue';

  console.log(myVar);
};

myFunction();
@mdebruijne

This comment has been minimized.

Show comment
Hide comment
@mdebruijne

mdebruijne Feb 20, 2016

Contributor

@michaelficarra, @ilyavolodin

I am aware that this can be rewritten to const myVar = 'myValue'

This simplified example demonstrates the incorrect error message error 'myVar' is never modified. The error message is incorrect (and therefore confusing) as the myVar variable is modified in the example provided. The error should probably be changed to something like error 'let myVar' can be refactored to 'const myVar'.

If you agree to this change then I can send a pull request if desired.

Contributor

mdebruijne commented Feb 20, 2016

@michaelficarra, @ilyavolodin

I am aware that this can be rewritten to const myVar = 'myValue'

This simplified example demonstrates the incorrect error message error 'myVar' is never modified. The error message is incorrect (and therefore confusing) as the myVar variable is modified in the example provided. The error should probably be changed to something like error 'let myVar' can be refactored to 'const myVar'.

If you agree to this change then I can send a pull request if desired.

@michaelficarra

This comment has been minimized.

Show comment
Hide comment
@michaelficarra

michaelficarra Feb 20, 2016

Member

👎 This rule is talking about reassignment. You've simply separated declaration from initialisation, but you've still not used any reassignment. I'd be in favour of changing the wording from "modified" to "reassigned".

Member

michaelficarra commented Feb 20, 2016

👎 This rule is talking about reassignment. You've simply separated declaration from initialisation, but you've still not used any reassignment. I'd be in favour of changing the wording from "modified" to "reassigned".

@mdebruijne

This comment has been minimized.

Show comment
Hide comment
@mdebruijne

mdebruijne Feb 20, 2016

Contributor

Agree. Changing "modified" to "reassigned" better reflects the reasoning behind this rule and makes the error message less confusing.

Pull request required?

Contributor

mdebruijne commented Feb 20, 2016

Agree. Changing "modified" to "reassigned" better reflects the reasoning behind this rule and makes the error message less confusing.

Pull request required?

@platinumazure

This comment has been minimized.

Show comment
Hide comment
@platinumazure

platinumazure Feb 20, 2016

Member

@mdebruijne I would advise waiting for the issue to get an "accepted" label, but then yes, you may submit a pull request if you like. I highly doubt this is something the core team would jump on since there are higher priorities.

Member

platinumazure commented Feb 20, 2016

@mdebruijne I would advise waiting for the issue to get an "accepted" label, but then yes, you may submit a pull request if you like. I highly doubt this is something the core team would jump on since there are higher priorities.

@ilyavolodin

This comment has been minimized.

Show comment
Hide comment
@ilyavolodin

ilyavolodin Feb 21, 2016

Member

modified -> reassigned sounds good to me. PRs welcome.

Member

ilyavolodin commented Feb 21, 2016

modified -> reassigned sounds good to me. PRs welcome.

ilyavolodin added a commit that referenced this issue Feb 21, 2016

Merge pull request #5356 from mdebruijne/issue5350
Update: prefer-const; change modified to reassigned (fixes #5350)

@eslint eslint bot locked and limited conversation to collaborators Feb 6, 2018

@eslint eslint bot added the archived due to age label Feb 6, 2018

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.