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.

`prefer-const` rule warns when "let" is needed for scoping

See original GitHub issue

on eslint v2.7.0.

echo 'let foo; const bar = function bar() { return foo; }; foo = 3; console.log(bar());' | ./node_modules/.bin/eslint --stdin --rule 'prefer-const: 2' --no-eslintrc --parser-options=ecmaVersion:6 gives

<text>
  1:54  error  'foo' is never reassigned, use 'const' instead  prefer-const

✖ 1 problem (1 error, 0 warnings)

I’d like it not to warn here, since the let is required to be able to refer to it without violating the no-use-before-define rule. If it does warn, then must I disable one of the rules since they are in conflict here?

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
mysticateacommented, Apr 17, 2016

@nzakas I think so.

1reaction
mysticateacommented, Apr 14, 2016

@lukeapage Thank you for the explanation!

@nzakas As I mentioned at https://github.com/eslint/eslint/issues/5692#issuecomment-203690886, I think I can add an option to ignore variables that are read before its first assignment. I can work on this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

prefer-const - ESLint - Pluggable JavaScript Linter
This rule is aimed at flagging variables that are declared using let keyword, but never reassigned after the initial assignment. Examples of incorrect...
Read more >
prefer-const - Rule
- If any variable in destructuring can be const, this rule warns for those variables. “all” - Only warns if all variables in...
Read more >
Linter rules
Since state access is preferred via the widget field, passing data to State objects using custom constructor parameters should also be avoided and...
Read more >
Const, let and var, which and when? | by Jhey Tompkins
Variables assigned with the var keyword have functional scope. This is somewhat intuitive. The variable isn't accessible outside of the function. They ...
Read more >
JavaScript let const and var | Blog
You should prefer const when declaring variables in JavaScript (ES6+). If you are assigning a value to a variable, and you're not going...
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